MCS-208 · Unit 8

Graphs

Section 8 of 8

Block 3 · Unit 8Graphs11 min · Section 8 of 8

8.8Strongly Connected Components

Covers IGNOU MCS-208 §8.7, 8.8

In a directed graph some groups of vertices can all reach each other: in a one-way street network, a district where you can drive from any junction to any other. These groups are the strongly connected components. They can be found with two depth-first searches.

Definition

“Maximal” means no vertex can be added without breaking the property. Every vertex belongs to exactly one SCC (a vertex on no cycle forms an SCC by itself). If each SCC is shrunk to a single vertex, the resulting graph is always a DAG.

The transpose GT=(V,ET)G^T = (V, E^T) has the same vertices and every edge reversed: ET={(u,v):(v,u)∈E}E^T = \{(u, v) : (v, u) \in E\}. Its adjacency matrix is the transpose of GG‘s. GG and GTG^T have exactly the same SCCs, because reversing every edge keeps “u and v reach each other” true.

The algorithm (Kosaraju)

  1. Run DFS on GG and record the finishing time f[u]f[u] of every vertex.
  2. Compute GTG^T.
  3. Run DFS on GTG^T, taking the start vertices in the main loop in decreasing order of f[u]f[u].
  4. Each tree of the DFS forest from step 3 is one strongly connected component.

Two DFS runs and one transpose: O(n+e)O(n + e) with adjacency lists.

Uses

  • Simplifying a digraph into its “component DAG” before other algorithms.
  • Finding groups of web pages or accounts that all link to each other.
  • Detecting circular dependencies (each SCC with more than one vertex is a cycle of mutual dependencies).
  • Solving 2-SAT problems in logic.

Key takeaways

  • SCC: maximal set of vertices that can all reach each other.
  • GTG^T reverses every edge and has the same SCCs as GG.
  • Kosaraju: DFS on GG (finishing times), then DFS on GTG^T in decreasing finishing order; each tree is an SCC.
  • Runs in O(n+e)O(n + e); shrinking SCCs to single vertices gives a DAG.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In the digraph with edges 1→2, 2→3, 3→1, 3→4, how many SCCs are there?

Show answer

Answer: B. 2

{1, 2, 3} form a cycle; 4 cannot reach the others, so it is a component by itself.

Question 2 · multiple choice

What is the transpose GTG^T of a digraph?

Show answer

Answer: C. The graph with every edge reversed

(u,v)∈ET(u, v) \in E^T exactly when (v,u)∈E(v, u) \in E; its adjacency matrix is the transpose of GG‘s.

Question 3 · multiple choice

In Kosaraju’s algorithm, in which order are vertices chosen as roots in the second DFS?

Show answer

Answer: C. Decreasing finishing time from the first DFS

Starting from the vertex that finished last ensures each second-pass tree stays inside one component.

Question 4 · multiple choice

Which statement is always true?

Show answer

Answer: B. Shrinking every SCC to one vertex produces a DAG.

If the component graph had a cycle, those components would reach each other and would be one SCC. A DAG has nn single-vertex SCCs.

Question 5 · multiple choice

What does this program print?

C · q8-transpose.c
#include <stdio.h>

int main(void)
{
    int g[3][3] = {{0, 1, 0},
                   {0, 0, 1},
                   {1, 0, 0}}, gt[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            gt[j][i] = g[i][j];          /* reverse every edge */
    printf("%d %d %d\n", gt[1][0], gt[0][2], gt[0][1]);
    return 0;
}
Show answer

Answer: A. 1 1 0

GG has 0→1, 1→2, 2→0, so GTG^T has 1→0, 2→1, 0→2: gt[1][0] = 1, gt[0][2] = 1, gt[0][1] = 0 (there is no edge 0→1 in the transpose).

Question 6 · short answer

Write the algorithm for finding the strongly connected components of a digraph and state its complexity.

Show worked answer

Strongly-Connected-Components(G): (1) call DFS(G) to compute the finishing time f[u] of every vertex; (2) compute the transpose GTG^T by reversing every edge; (3) call DFS(GTG^T), but in its main loop consider the vertices in order of decreasing f[u]; (4) output the vertices of each tree in the depth-first forest of step 3 as a separate strongly connected component. With adjacency lists, each DFS and the transpose take O(n+e)O(n + e), so the whole algorithm is O(n+e)O(n + e).

↑ ↓ move↵ openTab switch type