Graph Academy
Kosaraju's



What does it do ?

Kosaraju's algorithm is used to find strongly connected components of a directed graph.

What are strongly connected components ?


A graph is said to be strongly connected if every vertex of the graph is reachable from every other vertex.


For instance, the above graph is not strongly connected as there is no way to reach vertex 4 from vertex 5, or reach vertex 3 from vertex 4. However, 1, 2 and 3 together would be a strongly connected component as there exists a path between any pair of those vertices.

What's the idea ?


1) Initialize an empty stack S, and choosing any starting point, begin recursive depth first search for every node that can be reached from that vertex.

2) If a vertex X is being considered, check if all of its adjacent vertices have been visited. If they have, then trace back to the vertex from where you came to X and do the same. If not, then go the next adjacent vertex and continue. DO so until all vertices are visited and pushed into the stack

3) Take the transpose of the graph by switching the directions of the edges.

4) Now pop the nodes from the stack S one by one. Use dfs again to find all the vertices which can be visited from that particular node. The group of the node along with the vertices which it can visit will form a strongly connected component.
If a newly popped vertex is already part of a previously formed strongly connected component, its has already been placed, so move on to the next vertex in the stack.

Test yourself

Consider the following graph..

Which four vertices will be pushed onto the stack first ? (in order of first pushed to last pushed)

Which of the following pairs will belong to the same strongly connected component ?