Reputation: 59
Let's say we run sharir kosaraju algorithm on a Directed graph. And we have an arc (u,v) on this graph. In this algorithm we have two DFS passes. Now suppose we insert vertex u into the first depth tree T. Where can v appear? Is it in another tree created earlier or maybe later? Thanks in advance !
I'm learning for a test... So this is a kind of homework I guess but I really have no clue!
Upvotes: 0
Views: 1584
Reputation: 3913
The wiki: http://en.wikipedia.org/wiki/Kosaraju%27s_algorithm is pretty instructive. I have implemented the algorithm and it is available here.
http://khanna111.com/wordPressBlog/2012/04/11/strongly-connected-components-of-a-graph/
The primary thing to understand is that the top elements in the stack in the first step after a pass would be the parents and that in the second step they would be popped out earlier and operate on the transpose where the nodes that were strongly connected in the original graph will remain strongly connected in the transpose.
The whole reason for the first pass is to get the parents to the top of the stack.
Upvotes: 0
Reputation: 450
Kosaraju's Algorithm is based on the fact that, Transpose of a Graph has the same number of Strongly Connected Components (SCCs) as the original Graph.
1) You have a graph G and an empty Stack S.
2) While S does not contain all the nodes in G, choose a random vertex u and do DFS on u. When you are done exploring a node v during this DFS, push the node v in S.
Back to your question, if there is an directed edge (u,v), v will be inserted in the stack S surely before u. But, there can be more nodes between insertion of v and insertion of u.
3) You do DFS of Transpose of G, by popping vertices from stack S, till S is empty. This will get you all the SCC's in the Graph G.
Upvotes: 2