...

/

The Kosaraju-Sharir Algorithm

The Kosaraju-Sharir Algorithm

Learn about the Kosaraju-Sharir algorithm for identifying strongly connected components.

The Kosaraju-Sharir algorithm finds the strongly connected components of a digraph by requiring two passes of depth-first search. While this algorithm performs more work than Tarjan’s algorithm, it’s elegant in its simple presentation and is easier to understand. Interestingly, despite the extra work, its running time remains asymptotically the same as that of depth-first search, i.e., O(n+m)O(n+m), where nn and mm are the numbers of vertices and edges, respectively.

Press + to interact

The algorithm was published in 1981 by Micha Sharir but is also attributed independently to S. Rao Kosaraju (unpublished, 1978).

Before we describe the algorithm, let’s first talk about the transpose of a digraph, as it’s directly relevant to the algorithm’s implementation.

Transpose of a digraph

Given a digraph GG, if the directions on all the edges are reversed, the resulting digraph GG' is called the transpose of GG.

That’s a neat little name for referring to the resultant graph, seeing that the adjacency matrix of GG' really is the transpose of the adjacency matrix of GG.

Here’s an example of a digraph and its transpose:

Press + to interact
A digraph and its transpose
A digraph and its transpose

Clearly, the transpose of the transpose of GG is the digraph GG itself.

Computing the transpose graph

It’s not hard to see how, given the adjacency list of a digraph GG, we can create an adjacency list of the transpose digraph GG'.

  • O(n)O(n) time is needed to create an empty list against each vertex.
  • As each edge (u,v)(u,v) on the adjacency list of uu in GG is read, it is inserted against vv in time O(1)O(1) at the head of its adjacency list. So, over all the edges, O(n+m)O(n+m) time is taken.

Think, think, think

Let’s explore some facts that follow directly from the definition of transpose digraphs.

Fact: Suppose ...