Kosaraju
Kosaraju
Kosaraju
Contents
1 The algorithm
2 Complexity
3 References
4 External links
The algorithm
The primitive graph operations that the algorithm uses are to enumerate the vertices of the graph, to
store data per vertex (if not in the graph data structure itself, then in some table that can use vertices
as indices), to enumerate the out-neighbours of a vertex (traverse edges in the forward direction),
and to enumerate the in-neighbours of a vertex (traverse edges in the backward direction); however
the last can be done without, at the price of constructing a representation of the transpose graph
during the forward traversal phase. The only additional data structure needed by the algorithm is an
ordered list L of graph vertices, that will grow to contain each vertex once.
If strong components are to be represented by appointing a separate root vertex for each component,
and assigning to each vertex the root vertex of its component, then Kosaraju's algorithm can be
stated as follows.
The key point of the algorithm is that during the first (forward) traversal of the graph edges, vertices
are prepended to the list L in post-order relative to the search tree being explored. This means it
does not matter whether a vertex v was first Visited because it appeared in the enumeration of all
vertices or because it was the out-neighbour of another vertex u that got Visited; either way v will
be prepended to L before u is, so if there is a forward path from u to v then u will appear before v
on the final list L (unless u and v both belong to the same strong component, in which case their
relative order in L is arbitrary). As given above, the algorithm for simplicity employs depth-first
search, but it could just as well use breadth-first search as long as the post-order property is
preserved.
The algorithm can be understood as identifying the strong component of a vertex u as the set of
vertices which are reachable from u both by backwards and forwards traversal. Writing
{\displaystyle F(u)}F(u) for the set of vertices reachable from {\displaystyle u}u by forward
traversal, {\displaystyle B(u)}B(u) for the set of vertices reachable from {\displaystyle u}u by
backwards traversal, and {\displaystyle P(u)}{\displaystyle P(u)} for the set of vertices which
appear strictly before {\displaystyle u}u on the list L after phase 2 of the algorithm, the strong
component containing a vertex {\displaystyle u}u appointed as root is