diff --git a/CPP/graphseries.cpp b/CPP/graphseries.cpp new file mode 100644 index 00000000..52ad5d58 --- /dev/null +++ b/CPP/graphseries.cpp @@ -0,0 +1,591 @@ +#include +#define ll long long int +using namespace std; + +bool cycleBFS(int node, vector &vis, vector arr[]) +{ + queue> q; + q.push({node, -1}); + vis[node] = 1; + while (!q.empty()) + { + int n = q.front().first; + int p = q.front().second; + q.pop(); + for (auto i : arr[n]) + { + if (vis[i] == 0) + { + vis[i] = 1; + q.push({i, n}); + } + else + { + if (i != p) + return true; + } + } + } + return false; +} + +bool cycleDFS(int node, int parent, vector &vis, vector arr[]) +{ + vis[node] = 1; + for (auto i : arr[node]) + { + if (!vis[i]) + { + if (cycleDFS(i, node, vis, arr)) + return true; + } + else + { + if (i != parent) + return true; + } + } + + return false; +} + +void bfs(int node, vector &vis, vector arr[], vector &ans) +{ + queue q; + q.push(node); + vis[node] = 1; + while (!q.empty()) + { + int f = q.front(); + q.pop(); + ans.push_back(f); + for (auto i : arr[f]) + { + if (!vis[i]) + { + vis[i] = 1; + q.push(i); + } + } + } +} + +void dfs(int node, vector &vis, vector arr[], vector &ans) +{ + vis[node] = 1; + ans.push_back(node); + for (auto i : arr[node]) + { + if (!vis[i]) + { + vis[i] = 1; + dfs(i, vis, arr, ans); + } + } +} + +bool bipertiteBFS(int n, vector &color, vector arr[]) +{ + queue q; + q.push(n); + color[n] = 1; + while (!q.empty()) + { + int front = q.front(); + q.pop(); + for (auto i : arr[n]) + { + if (color[i] == -1) + { + color[i] = 1 - color[n]; + q.push(i); + } + else + { + if (color[i] == color[n]) + return false; + } + } + } + + return true; +} + +bool bipertiteDFS(int n, vector &color, vector arr[]) +{ + if (color[n] == -1) + color[n] = 1; + for (auto i : arr[n]) + { + if (color[i] == -1) + { + color[i] = 1 - color[n]; + if (!bipertiteDFS(i, color, arr)) + return false; + } + else + { + if (color[i] == color[n]) + return false; + } + } + return true; +} + +bool cycleDirectedDFS(int n, vector &vis, vector &dfsvis, vector arr[]) +{ + vis[n] = 1; + dfsvis[n] = 1; + + for (auto i : arr[n]) + { + if (vis[i] == 0) + { + if (cycleDirectedDFS(i, vis, dfsvis, arr)) + return true; + } + else + { + if (dfsvis[i]) + return true; + } + } + + dfsvis[n] = 0; + return false; +} + +void findTopoSortDFS(int node, vector arr[], stack &s, vector &vis) +{ + vis[node] = 1; + + for (auto i : arr[node]) + { + if (!vis[i]) + { + findTopoSortDFS(i, arr, s, vis); + } + } + + s.push(node); +} + +vector topoSortDFS(vector arr[], int n) +{ + stack s; + vector vis(n, 0); + for (int i = 0; i < n; i++) + { + if (!vis[i]) + { + findTopoSortDFS(i, arr, s, vis); + } + } + + vector ans; + while (!s.empty()) + { + ans.push_back(s.top()); + s.pop(); + } + + return ans; +} + +// BFS (Kahn's Algorithm) +vector topoSortBFS(vector arr[], int n) +{ + queue q; + vector inorder(n, 0); + + for (int i = 0; i < n; i++) + { + for (auto it : arr[i]) + { + inorder[it]++; + } + } + + for (int i = 0; i < n; i++) + { + if (inorder[i] == 0) + q.push(i); + } + int count = 0; // this is used to find cycle in graph + vector ans; + while (!q.empty()) + { + int front = q.front(); + q.pop(); + count++; + ans.push_back(front); + for (auto i : arr[front]) + { + inorder[i]--; + if (inorder[i] == 0) + q.push(i); + } + } + + if (count == n) + { + // this means that our graph does not contain a cycle + cout << "Graph does not contain a cycle"; + } + else + { + // this means that our graph contains a cycle + cout << "Graph contains a cycle"; + } + + return ans; +} + +int shortestPathUndirectedBFS(vector arr[], int n, int source, int destination) +{ + queue q; + vector distance(n, INT_MAX); + distance[source] = 0; + q.push(source); + while (!q.empty()) + { + int front = q.front(); + q.pop(); + for (auto i : arr[front]) + { + distance[i] = min(distance[front] + 1, distance[i]); + q.push(i); + } + } + + // now my distance array has shortest distance from source to every node. + return distance[destination]; +} + +// This function is customized for the "shortestPathDAG" fuction +void findTopoSortDFS2(int node, vector> arr[], stack &s, vector &vis) +{ + vis[node] = 1; + for (auto i : arr[node]) + { + if (!vis[i]) + { + findTopoSortDFS2(i.first, arr, s, vis); + } + } + s.push(node); +} + +// shortest path in weighted DAG(Directed Acyclic Graph) +void shortestPathDAG(vector> arr[], int n, int source) +{ + stack s; + vector vis(n, 0); + for (int i = 0; i < n; i++) + { + if (!vis[i]) + { + findTopoSortDFS2(i, arr, s, vis); + } + } + + vector distance(n, INT_MAX); + distance[source] = 0; + while (!s.empty()) + { + int temp = s.top(); + s.pop(); + + if (distance[temp] != INT_MAX) + { + for (auto i : arr[temp]) + { + distance[i.first] = min(distance[i.first], distance[temp] + i.second); + } + } + } + + for (auto i : distance) + { + if (i == INT_MAX) + { + cout << "This node is not reachable"; + } + else + cout << i << " "; + } +} + +//shortest path in weighted undirected graph +void dijkstrasAlgorithm(vectorarr[],int n,int source){ + vectordis(n,INT_MAX); + vectorpath(n); + //min priority queue + priority_queue,vector>,greater>q; + dis[source]=0; + q.push({0,source}); + while(!q.empty()){ + pairtop=q.top(); + int prev=top.second; + int weight=top.first; + q.pop(); + for(auto i:arr[prev]){ + if(dis[i.first] > dis[prev]+i.second){ + dis[i]=dis[prev]+i.second; + path[i.first]=prev;// this line is optional and tells you the shortest path elements + q.push({dis[i.first],i.first}); + } + } + } + + for(int i:dis)cout< BRUTE FORCE O(n^2); +void primsAlogrithm(vectorarr[],int n){ + int key[n],parent[n]; + bool mst[n]; + for(int i=0;i Efficent approch O((N+E)logn); +void primsAlgo(vectorarr[],int n){ + int key[n],parent[n]; + bool mst[n]; + for(int i=0;i,vector>,greater>q; + + key[0]=0; + parent[0]=-1; + //in pair first value is weight and second value is node/index. + q.push({0,0}); + + for(int count=0;count&parent,vector&rank){ + u=findpair(u,parent); + v=findpair(v,parent); + + if(rank[u]&parent){ + if(parent[n]==n)return n; + + return parent[n]= findpair(parent[n],parent); +} + +void kruskalsAlgo(){ + int n,m; + cin>>n>>m; + vectoredges; + for(int i=0;i>u>>v>>wt; + edges.push_back(node(u,v,wt)); + } + + sort(edges.begin(),edges.end(),comp); + vectorparent(n); + vectorrank(n,0); + + for(int i=0;i>mst;//minimum spanning tree + for(auto it : edges){ + if(findpair(it.u,parent) !=findpair(it.v,parent)){ + cost+=it.wt; + mst.push_back({it.u,it.v}); + unionn(it.u,it.v,parent,rank); + } + } + + cout<&vis,vector&low,vector&in,vectorarr[]){ + vis[node]=1; + low[node]=in[node]=timer; + timer++; + + for(auto it : arr[node]){ + if(it==parent)continue; + if(vis[it]==1){ + low[node]=min(low[node],in[it]); + }else{ + bridgeInGraph(it,node,timer,vis,low,in,arr); + //after dfs call we backtrack + //if in[node] value is smaller than low[it] then it is an bridge edge. + //how? because if in[node] is smaller than it means that "it" is not + //connect to any ansistor and does not have any other path to reach. + //if if[node] is greater that means "it" is connect to the ansistor so it has more paths to reach it. + if(in[node]>n>>m; + vectoredges; + for(int i=0;i>x>>y>>w; + edges.push_back(node(x,y,w)); + } + + vectordistance(n,INT_MAX); + distance[source]=0; + + //relaxing N-1 times every edge + for(int i=0;i<=n-1;i++){ + for(auto it:edges){ + if(distance[it.v]>distance[it.u]+it.w){ + distance[it.v]=distance[it.u]+it.w; + } + } + } + + int flag=0; + + //deceting negative cycle by relaxing one more time i.e Nth time + for(auto it:edges){ + if(distance[it.v]>distance[it.u]+it.w){ + cout<<"Negative cycle"; + flag=1; + break; + } + } + + if(flag==0){ + for(int i=0;i> n >> m; + + vector arr[n + 1]; + for (int i = 0; i < m; i++) + { + int x, y; + cin >> x >> y; + arr[x].push_back(y); + arr[y].push_back(x); + } + vector ans; + int color[n + 1]; // for bipartite graph + memset(color, -1, sizeof(color)); // for bipartite graph + vector vis(n + 1, 0); + for (int i = 1; i <= n; i++) + { + if (!vis[i]) + { + if (cycleDFS(i, -1, vis, arr)) + cout << true; + } + } + + for (auto i : ans) + cout << i << " "; + return 0; +} \ No newline at end of file