Skip to content

Commit

Permalink
DSA algorithms #6
Browse files Browse the repository at this point in the history
contains subfolder which contains algorithms for mentioned subtopics.
  • Loading branch information
Lord-Morpheus authored Oct 4, 2023
1 parent b696d86 commit 4d002c6
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Binary file not shown.
44 changes: 44 additions & 0 deletions DSA algorithms/Two Pointer/Kadane's_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*KADANE'S ALGORITHM for finding maximum possible contiguous subarray sum*/

/*Algorithm
1)initialize an integer(let's say int sum) with minimum value(INT_MIN)
2)inititalize an integer(let's say int current) with value 0
3)iterate over an array(int a[])
4)update current=max(a[i],a[i]+current)
4)if (current>=sum) update sum, make it equal to current
5)if (current<sum) do nothing
*/

#include<bits/stdc++.h>
using namespace std;


int maxSubArray(vector<int> nums) {

int ans=INT_MIN;
int cur=0;
for(int num:nums){
cur=max(num,cur+num);
if(cur>=ans){
ans=cur;
}
else{
continue;
}
}
return ans;
}

/*TWO POINTER APPROACH comes in play when we want to find the length of max subarray*/
/* just initialize 3 more integers to save start & end indexes as 0
update end whenever sum increases
update start whenever sum decreases
}
*/
int main(){
vector<int>a={1,-2,-3,4,-1,2,1,-5,4};
int ans=maxSubArray(a);
cout<<ans;
}


77 changes: 77 additions & 0 deletions DSA algorithms/graphs/cyclicity_in_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*DISJOINT SET UNION*/
/*Algorithm for finding if an undirected graph is cyclic or acyclic*/
/*prerequisites
-basic knowledge about graphs
-should know traversing in graph(BFS, DFS)
*/

/*SCENARIO
There are n cities. They are connected by roads. If city 1 is connected directly with city 2,
and city 2 is connected directly with city 3,...., city k is connected with city 1. then we say that
roads form a cycle. i.e.
1---2---4 1---2---4---5
| /\ | | \
| / \ | | \
3 5---6 6---3 7---8
|
|
7
in first case we see that roads connecting city 1,2,3 form a cycle.
in second case we see that roads connecting ctiy 1,2,3,6 form a cycle.
*/

/*ALGORITHM
1) first label each node as its parent node. i.e. each node is parent node of itself.
2) then traverse across the graph and find parent of each node.
3) if they have different parent do union of those 2, i.e. make thier parents same.
4) if two node have same parent then it must be a cycle.
*/

#include <bits/stdc++.h>
using namespace std;

int find(int node,vector<int>&parent){
//finding the parent node
while(parent[node]!=node){
node=parent[node];
}
return node;
}

void Union(int i,int j,vector<int>&parent){
// union of node, i.e, setting parent as same.
int iroot=find(i,parent);
int jroot=find(j,parent);
if(iroot!=jroot){
parent[jroot]=iroot;
}
}

bool dsu(vector<vector<int>>& edges) {
//setting parent of each node
vector<int>parent(edges.size()+1);
for(int i=0;i<edges.size()+1;i++)parent[i]=i;

//appyling dsu
for(auto edge:edges){
if(find(edge[0],parent)==find(edge[1],parent))return true;
Union(edge[0],edge[1],parent);
}
return false;
}

int main(){
vector<vector<int>>graph={{1,2},{2,3},{3,4},{1,5},{1,4}};
/*
graph is:
2--1--5
| |
3--4
*/
bool ans= dsu(graph);

if(ans)cout<<"graph is cyclic!"<<endl;
else cout<<"graph is acyclic!"<<endl;
}

0 comments on commit 4d002c6

Please sign in to comment.