diff --git a/Graph/kosaraju.cpp b/Graph/kosaraju.cpp new file mode 100644 index 000000000..2e66f131f --- /dev/null +++ b/Graph/kosaraju.cpp @@ -0,0 +1,134 @@ +/* Implementation of Kosaraju's Algorithm to find out the strongly connected components (SCCs) in a graph. + Author:Anirban166 +*/ + +#include +#include + +using namespace std; + +/** +* Iterative function/method to print graph: +* @param a[] : array of vectors (2D) +* @param V : vertices +* @return void +**/ +void print(vector a[],int V) +{ + for(int i=0;i"; + for(int j=0;j &st,bool vis[],vector adj[]) +{ + vis[v]=true; + for(auto i=adj[v].begin();i!=adj[v].end();i++) + { + if(vis[*i]==false) + push_vertex(*i,st,vis,adj); + } + st.push(v); +} + + +/** +* //Recursive function/method to implement depth first traversal(dfs): +* @param v : vertices +* @param vis[] : array to keep track of visited nodes (boolean type) +* @param grev[] : graph with reversed edges +* @return void +**/ +void dfs(int v,bool vis[],vector grev[]) +{ + vis[v]=true; + // cout<0)) + i.e. it returns the count of (number of) strongly connected components (SCCs) in the graph. (variable 'count_scc' within function) +**/ +int kosaraju(int V, vector adj[]) +{ + bool vis[V]={}; + stack st; + for(int v=0;v grev[V]; + for(int i=0;i