﻿ Strongly Connected Components - Lecture by Rashid Bin Muhammad, PhD. # Strongly Connected Components Decomposing a directed graph into its strongly connected components is a classic application of depth-first search. The problem of finding connected components is at the heart of many graph application. Generally speaking, the connected components of the graph correspond to different classes of objects. The first linear-time algorithm for strongly connected components is due to Tarjan (1972). Perhaps, the algorithm in the CLRS is easiest to code (program) to find strongly connected components and is due to Sharir and Kosaraju.

Given digraph or directed graph G = (V, E), a strongly connected component (SCC) of G is a maximal set of vertices C subset of V, such that for all u, v in C, both u Þ v and v Þ u; that is, both u and v are reachable from each other. In other words, two vertices of directed graph are in the same component if and only if they are reachable from each other. C1                      C2             C3            C4

The above directed graph has 4 strongly connected components: C1, C2, C3 and C4. If G has an edge from some vertex in Ci to some vertex in Cj where ij, then one can reach any vertex in Cj from any vertex in Ci but not return. In the example, one can reach any vertex in C2 from any vertex in C1 but cannot return to C1 from C2.

The algorithm in CLRS for finding strongly connected components of G = (V, E) uses the transpose of G, which define as:

• GT = (V, ET), where ET = {(u, v): (v, u) in E}.

• GT is G with all edges reversed.

From the given graph G, one can create GT in linear time (i.e., Θ(V + E)) if using adjacency lists.

Observation:

The graphs G and GT have the same SCC's. This means that vertices u and v are reachable from each other in G if and only if reachable from each other in GT.

Component Graph

The idea behind the computation of SCC comes from a key property of the component graph, which is defined as follows:

GSCC = (VSCC, ESCC), where VSCC has one vertex for each SCC in G and ESCC has an edge if there's an edge between the corresponding SCC's in G.

For our example (above) the GSCC is: The key property of GSCC is that the component graph is a dag, which the following lemma implies.

Lemma    GSCC is a dag. More formally, let C and C' be distinct SCC's in G, let u, v in C, u', v' in C', and suppose there is a path u Þ u' in G. Then there cannot also be a path v' Þ v in G.

Proof    Suppose there is a path v' Þ v in G. Then there are paths u Þ u' Þ v' and v' Þ v Þ u in G. Therefore, u and v' are reachable from each other, so they are not in separate SCC's.

This completes the proof.

ALGORITHM

A DFS(G) produces a forest of DFS-trees. Let C be any strongly connected component of G, let v be the first vertex on C discovered by the DFS and let T be the DFS-tree containing v when DFS-visit(v) is called all vertices in C are reachable from v along paths containing visible vertices; DFS-visit(v) will visit every vertex in C, add it to T as a descendant of v.

STRONGLY-CONNECTED-COMPONENTS (G)

1. Call DFS(G) to compute finishing times f[u] for all u.
2. Compute GT
3. Call DFS(GT), but in the main loop, consider vertices in order of decreasing f[u] (as computed in first DFS)
4. Output the vertices in each tree of the depth-first forest formed in second DFS as a separate SCC.

Time: The algorithm takes linear time i.e., θ(V + E), to compute SCC of a digraph G.

From our Example (above):

1. Do DFS
2. GT
3. DFS (roots blackened) Another Example (CLRS)    Consider a graph G = (V, E).

1. Call DFS(G) 2. Compute GT 3. Call DFS(GT) but this time consider the vertices in order to decreasing finish time. 4. Output the vertices of each tree in the DFS-forest as a separate strongly connected components.

{a, b, e}, {c, d}, {f, g}, and {h}

Now the question is how can this possibly work?

Idea    By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the component graph in topological sort order.

To prove that it really works, first we deal with two notational issues:

• We will be discussing d[u] and f[u]. These always refer to the first DFS in the above algorithm.

• We extend notation for d and f to sets of vertices U subset V:

• d(U) = minu in U {d[u]}                 (earliest discovery time of any vertex in U)

• f(U) = minu in U {f[u]}                  (latest finishing time of any vertex in U)

Lemma    Let C and C' be distinct SCC's in G = (V, E). Suppose there is an edge (u, v) in E such that u in C and v in C'. Then f(C) > f(C'). Proof    There are two cases, depending on which SCC had the first discovered vertex during the first DFS.

Case i. If d(C) > d(C'), let x be the first vertex discovered in C. At time d[x], all vertices in C and C' are white. Thus, there exist paths of white vertices from x to all vertices in C and C'.

By the white-path theorem, all vertices in C and C' are descendants of x in depth-first tree.

By the parenthesis theorem, we have f[x] = f(C) > f(C').

Case ii. If d(C) > d(C'), let y be the first vertex discovered in C'. At time d[y], all vertices in C' are white and there is a white path from y to each vertex in C. This implies that all vertices in C' become descendants of y. Again, f[y] = f(C').

At time d[y], all vertices in C are white.

By earlier lemma, since there is an edge (u, v), we cannot have a path from C' to C. So, no vertex in C is reachable from y. Therefore, at time f[y], all vertices in C are still white. Therefore, for all w in C, f[w] > f[y], which implies that f(C) > f(C').

This completes the proof.

Corollary    Let C and C' be distinct SCC's in G = (V, E). Suppose there is an edge (u, v) in ET where u in C and v in C'. Then f(C) < f(C').

Proof    Edge (u, v) in ET implies (v, u) in E. Since SCC's of G and GT are the same, f(C') > f(C). This completes the proof.

Corollary    Let C and C' be distinct SCC's in G = (V, E), and suppose that f(C) > f(C'). Then there cannot be an edge from C to C' in GT.

Proof Idea    It's the contrapositive of the previous corollary.

Now, we have the intuition to understand why the SCC procedure works.

When we do the second DFS, on GT, start with SCC C such that f(C) is maximum. The second DFS starts from some x in C, and it visits all vertices in C. Corollary says that since f(C) > f(C') for all C' C, there are no edges from C to C' in GT. Therefore, DFS will visit only vertices in C.

Which means that the depth-first tree rooted at x contains exactly the vertices of C.

The next root chosen in the second DFS is in SCC C' such that f(C') is maximum over all SCC's other than C. DFS visits all vertices in C', but the only edges out of C' go to C, which we've already visited.

Therefore, the only tree edges will be to vertices in C'.

We can continue the process.

Each time we choose a root for the second DFS, it can reach only

• vertices in its SCC  ‾  get tree edges to these,

• vertices in SCC's already visited in second DFS  ‾  get no tree edges to these.

We are visiting vertices of (GT)SCC in reverse of topologically sorted order. [CLRS has a formal proof.]

Before leaving strongly connected components, lets prove that the component graph of G = (V, E) is a directed acyclic graph.

Proof (by contradiction)    Suppose component graph of G = (V, E) was not a DAG and G comprised of a cycle consisting of vertices v1, v2 , . . . , vn . Each vi corresponds to a strongly connected component (SCC) of component graph G. If v1, v2 , . . . , vn themselves form a cycle then each vi ( i runs from 1 to n) should have been included in the SCC corresponding to vj ( j runs from 1 to n and i j). But each of the vertices is a vertex from a difference SCC of G. Hence, we have a contradiction! Therefore, SCC of G is a directed acyclic graph.

Related Problems

1. Edge-vertex connectivity problem.
2. Shortest path problem. Updated: March 13, 2010.