In a directed graph, being able to go from A to B does not mean you can come back. A strongly connected component (SCC) is a largest group of vertices where every vertex can reach every other one. On a one-way street map, it is an area where a driver can get anywhere and back; on a social network, it is a group where everyone is connected through follows in both directions. This lesson finds SCCs in C with two classic linear-time algorithms: Kosaraju’s, which uses two DFS passes, and Tarjan’s, which needs just one.
In this lesson you will learn
- What a strongly connected component is
- Kosaraju’s algorithm: finish order, reversed graph, second DFS
- Tarjan’s algorithm with discovery times and low-link values
- Why SCCs form a graph with no cycles
- Where SCCs are used in practice
You should know DFS and cycle detection.
Kosaraju’s Algorithm
#include <stdio.h>
#define N 8
static int adj[N][N], radj[N][N];
static int visited[N], order[N], order_len, comp[N];
/* Pass 1: DFS on the graph, recording vertices in order of finishing */
static void dfs1(int u) {
visited[u] = 1;
for (int v = 0; v < N; v++)
if (adj[u][v] && !visited[v]) dfs1(v);
order[order_len++] = u;
}
/* Pass 2: DFS on the reversed graph labels one component */
static void dfs2(int u, int c) {
comp[u] = c;
for (int v = 0; v < N; v++)
if (radj[u][v] && comp[v] == -1) dfs2(v, c);
}
int main(void) {
/* Who follows whom on a small social network */
int edges[][2] = {
{0, 1}, {1, 2}, {2, 0}, /* 0, 1 and 2 all reach each other */
{2, 3}, {3, 4}, {4, 5}, {5, 3}, /* 3, 4 and 5 form a second loop */
{5, 6}, {6, 7}, /* 6 and 7 are one-way followers */
};
int m = sizeof edges / sizeof edges[0];
for (int i = 0; i < m; i++) {
adj[edges[i][0]][edges[i][1]] = 1;
radj[edges[i][1]][edges[i][0]] = 1; /* reversed edge */
}
for (int u = 0; u < N; u++) if (!visited[u]) dfs1(u);
for (int i = 0; i < N; i++) comp[i] = -1;
int count = 0;
for (int i = order_len - 1; i >= 0; i--) /* latest finisher first */
if (comp[order[i]] == -1) dfs2(order[i], count++);
printf("finish order:");
for (int i = 0; i < order_len; i++) printf(" %d", order[i]);
printf("\n%d strongly connected components:\n", count);
for (int c = 0; c < count; c++) {
printf(" component %d:", c);
for (int u = 0; u < N; u++) if (comp[u] == c) printf(" %d", u);
printf("\n");
}
return 0;
}
Output
finish order: 7 6 5 4 3 2 1 0
4 strongly connected components:
component 0: 0 1 2
component 1: 3 4 5
component 2: 6
component 3: 7
- Pass 1 runs DFS on the graph and records each vertex when it finishes, after everything reachable from it has been explored.
- The vertex that finishes last belongs to a “source” component: one with no edges coming in from other components. Here that is vertex 0.
- Pass 2 runs DFS on the reversed graph, starting from vertices in reverse finish order. In the reversed graph, a DFS from a source component cannot escape into other components, so each DFS collects exactly one SCC.
- The result has four components: {0, 1, 2} and {3, 4, 5} are loops of mutual follows, while 6 and 7 are only followers, each forming a component on its own.
- Both passes visit every vertex and edge once: O(V + E) with adjacency lists.
Tarjan’s Algorithm
#include <stdio.h>
#define N 6
static int adj[N][N];
static int disc[N], low[N], on_stack[N], stack[N], top = -1, timer = 0, comps = 0;
static int min2(int a, int b) { return a < b ? a : b; }
/* Tarjan's algorithm: one DFS, using discovery times and "low-link" values */
static void tarjan(int u) {
disc[u] = low[u] = ++timer;
stack[++top] = u;
on_stack[u] = 1;
for (int v = 0; v < N; v++) {
if (!adj[u][v]) continue;
if (!disc[v]) { /* tree edge */
tarjan(v);
low[u] = min2(low[u], low[v]);
} else if (on_stack[v]) { /* edge back into the current component */
low[u] = min2(low[u], disc[v]);
}
}
if (low[u] == disc[u]) { /* u is the root of a component: pop it */
printf("component %d:", comps++);
int v;
do {
v = stack[top--];
on_stack[v] = 0;
printf(" %d", v);
} while (v != u);
printf("\n");
}
}
int main(void) {
/* One-way streets between six junctions */
int edges[][2] = { {0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 4}, {4, 3}, {4, 5} };
for (int i = 0; i < 7; i++) adj[edges[i][0]][edges[i][1]] = 1;
for (int u = 0; u < N; u++) if (!disc[u]) tarjan(u);
printf("%d components: a driver can travel freely only inside each one\n", comps);
return 0;
}
Output
component 0: 5
component 1: 4 3
component 2: 2 1 0
3 components: a driver can travel freely only inside each one
- Each vertex gets a discovery time
discwhen DFS first reaches it, and a low-link valuelow: the earliest discovery time it can reach while staying within the vertices still on the stack. - Vertices are pushed onto a stack as they are discovered. An edge to a vertex that is still on the stack means a way back into the current component, so
lowis lowered. - When a vertex finishes with
low[u] == disc[u], nothing below it can reach anything earlier, so it is the root of a component. Popping the stack down to it outputs the whole component. - Components appear in reverse topological order: {5} first, because nothing in it leads anywhere else, and the source component {2, 1, 0} last.
- Tarjan’s algorithm needs only one DFS and no reversed graph, which saves memory, but it is a little harder to understand than Kosaraju’s.
Kosaraju vs Tarjan
| Kosaraju | Tarjan | |
|---|---|---|
| DFS passes | Two | One |
| Needs reversed graph | Yes | No |
| Extra data | Finish order | disc, low, stack |
| Time | O(V + E) | O(V + E) |
| Component order | Topological (sources first) | Reverse topological |
If each SCC is shrunk into a single vertex, the result, called the condensation, never contains a cycle. That means it can be topologically sorted, which many algorithms rely on.
Common Mistakes
- Using discovery order instead of finish order in Kosaraju’s first pass.
- Forgetting to reverse the edges for the second pass.
- Updating
lowfrom vertices that are already in a finished component in Tarjan’s algorithm; only vertices still on the stack count. - Confusing SCCs with connected components of an undirected graph, which ignore direction.
Practice Exercises
Exercise 1. A directed graph has edges 0 → 1, 1 → 2, 2 → 0 and 2 → 3. What are its SCCs?
Show answer
{0, 1, 2} and {3}. Vertex 3 can be reached from the loop, but it cannot get back.
Exercise 2. A city wants every junction to be reachable from every other one. How can SCCs check its one-way street plan?
Show answer
Run either algorithm. The plan works exactly when there is a single SCC containing all junctions. If there are several, the components show which areas are cut off in one direction.
Frequently Asked Questions
Where are strongly connected components used?
In checking road networks for one-way traps, finding groups of web pages that all link to each other, detecting circular dependencies between software modules, and solving 2-SAT logic problems, where a variable and its negation must not share a component.
Does every directed graph have at least one SCC?
Yes. Every vertex belongs to exactly one SCC, even if that component contains only the vertex itself. A graph with no cycles has one single-vertex component per vertex.
Summary
- An SCC is a largest set of vertices that can all reach each other.
- Kosaraju: DFS for finish order, then DFS on the reversed graph.
- Tarjan: one DFS with discovery times, low-links and a stack.
- Both run in O(V + E); shrinking SCCs gives a graph with no cycles.
Next: Two Pointer Technique in C.
