Strongly Connected Components in a Directed Graph C++ implementation

Here, I describe the Kosaraju-Sharir algorithm for finding the strongly connected components in a directed graph and provide its C++ implementation.

A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex. The strongly connected components of a directed graph G are its maximal strongly connected subgraphs.

Kosaraju-Sharir algorithm requires two passes of DFS to find the SCC (strongly connected components) i.e., it finds the SCC in O(V + E).

These are the steps of the algorithm:

  1. Compute the reverse graph GR, such that there is an edge from u to v in GR if and only if there is an edge from v to u in the original graph G.
  2. Run DFS over GR and compute the order in which vertices finish expansion.
  3. Run DFS over G in the reverse of the order computed in step 2. All vertices that are discovered during the expansion of a vertex v belong in the same SCC as v.

Take a look at the C++ implementation.

For an application of SCC, view the 2-SAT problem.

Reference: Algorithms 4th Edition by Robert Sedgewick and Kevin Wayne

Advertisements

One thought on “Strongly Connected Components in a Directed Graph C++ implementation

  1. Pingback: Solving 2-SAT in linear time | Everything Under The Sun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s