- Kosaraju's algorithm
In
computer science , Kosaraju's algorithm is analgorithm to find thestrongly connected component s of adirected graph . Aho, Hopcroft and Ullman credit it to an unpublished paper from1978 byS. Rao Kosaraju . It makes use of the fact that thetranspose graph (the same graph with the direction of every edge reversed) has exactly the same strongly connected components as the original graph.Kosaraju's algorithm is simple and works as follows:
* Let G be a directed graph and S be an empty stack.
* While S does not contain all vertices:
** Choose an arbitrary vertex "v" not in S. Perform adepth-first search starting at "v". Each time that depth-first search finishes expanding a vertex "u", push "u" onto S.
* Reverse the directions of all arcs to obtain the transpose graph.
* While S is nonempty:
** Pop the top vertex "v" from S. Perform a depth-first search starting at "v". The set of visited vertices will give the strongly connected component containing "v"; record this and remove all these vertices from the graph G and the stack S.Equivalently,
breadth-first search (BFS) can be used instead of depth-first search.Complexity
Provided the graph is described using an
adjacency list , Kosaraju's algorithm performs two complete traversals of the graph and so runs in Θ(V+E) (linear) time, which isasymptotically optimal because there is a matching lower bound (any algorithm must examine all vertices and edges). It is the conceptually simplest efficient algorithm, but is not as efficient in practice asTarjan's strongly connected components algorithm andGabow's algorithm , which perform only one traversal of the graph.If the graph is represented as an
adjacency matrix , the algorithm requires "Ο(V2)" time.References
* Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman. "
The Design and Analysis of Computer Algorithms ". Addison-Wesley, 1974.
* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein. "Introduction to Algorithms ", 2nd edition. The MIT Press, 2001. ISBN 0-262-03293-7.External links
* [http://lcm.csa.iisc.ernet.in/dsa/node171.html A description and proof of Kosaraju's Algorithm]
* [http://scienceblogs.com/goodmath/2007/10/computing_strongly_connected_c.php Good Math, Bad Math: Computing Strongly Connected Components]
Wikimedia Foundation. 2010.