It would would be great if someone could share the intuition behind Kosaraju-Sharir’s algorithm for finding strongly connected components in a directed graph.
so ja bhai
Brother, a directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex. The strongly connected components of a directed graph G are its maximal strongly connected subgraphs.
Kosaraju-Sharir algorithm requires two passes of DFS to find the SCC (strongly connected components) i.e., it finds the SCC in O(V + E).
These are the steps of the algorithm:
Compute the reverse graph GR, such that there is an edge from u to v in GR if and only if there is an edge from v to u in the original graph G.
Run DFS over GR and compute the order in which vertices finish expansion.
Run DFS over G in the reverse of the order computed in step 2. All vertices that are discovered during the expansion of a vertex v belong in the same SCC as v.
For Java implementation refer : http://ideone.com/nTZ0nV
For C++ implementation refer : http://ideone.com/oPnraZ
Well I already knew the algorithm, I just wanted to know about the intuition behind the working of scc algorithm(why it works). However I have realized it now, It is based on the idea that both transpose directed graph (G’) and graph © have same strongly connected components.