Kosaraju strongly connected components: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Finish a DFS forest, then explore reversed edges in decreasing finish order.

Finish a DFS forest, then search reversed edges in decreasing finish order; each second-pass search isolates one SCC.

Open the 3D visualization

Kosaraju strongly connected components example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Kosaraju strongly connected components

Analyzing cyclic dependencies in directed graphs.

Choosing an alternative

Tarjan identifies SCCs during one DFS forest with low links.

Worked example: input and state changes

Example input

0,1,1;1,2,1;2,0,1;2,3,1;3,4,1;4,3,1

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initialize graph state.

    State at this checkpoint
    {
      "values": [
        "-",
        "-",
        "-",
        "-",
        "-"
      ],
      "active": [],
      "kind": "graph",
      "chosen": [],
      "visited": []
    }
  2. Step 5: Discover 3; push onto the active stack.

    State at this checkpoint
    {
      "values": [
        "d:0 l:0",
        "d:1 l:1",
        "d:2 l:0",
        "d:3 l:3",
        "d:-1 l:-1"
      ],
      "active": [
        3
      ],
      "kind": "graph",
      "chosen": [],
      "visited": [],
      "discovery": [
        0,
        1,
        2,
        3,
        -1
      ],
      "low": [
        0,
        1,
        0,
        3,
        -1
      ],
      "components": [
        -1,
        -1,
        -1,
        -1,
        -1
      ],
      "stack": [
        0,
        1,
        2,
        3
      ],
      "finish": []
    }
  3. Step 11: Inspect 0 > 1; low[0]=0.

    State at this checkpoint
    {
      "values": [
        "d:0 l:0",
        "d:1 l:0",
        "d:2 l:0",
        "d:3 l:3",
        "d:4 l:3"
      ],
      "active": [
        0,
        1
      ],
      "kind": "graph",
      "chosen": [],
      "visited": [],
      "discovery": [
        0,
        1,
        2,
        3,
        4
      ],
      "low": [
        0,
        0,
        0,
        3,
        3
      ],
      "components": [
        -1,
        -1,
        -1,
        -1,
        -1
      ],
      "stack": [
        0,
        1,
        2,
        3,
        4
      ],
      "finish": [
        4,
        3,
        2,
        1
      ]
    }
  4. Step 16: Complete component {0, 2, 1}; representative 0.

    State at this checkpoint
    {
      "values": [
        "C:0",
        "C:0",
        "C:0",
        "d:3 l:3",
        "d:4 l:3"
      ],
      "active": [
        0,
        2,
        1
      ],
      "kind": "graph",
      "chosen": [],
      "visited": [],
      "discovery": [
        0,
        1,
        2,
        3,
        4
      ],
      "low": [
        0,
        0,
        0,
        3,
        3
      ],
      "components": [
        0,
        0,
        0,
        -1,
        -1
      ],
      "stack": [],
      "finish": [
        4,
        3,
        2,
        1,
        0
      ]
    }
  5. Step 21: Strongly connected components complete.

    State at this checkpoint
    {
      "values": [
        "C:0",
        "C:0",
        "C:0",
        "C:3",
        "C:3"
      ],
      "active": [],
      "kind": "graph",
      "chosen": [],
      "visited": [
        0,
        1,
        2,
        3,
        4
      ],
      "result": 2,
      "components": [
        0,
        0,
        0,
        3,
        3
      ]
    }

Expected final state

Strongly connected components complete.

Complexity and input bounds

Family: Graphs. Time complexity: O(V + VE) with edge-array scans.

Space complexity: O(V) state with edge scans; an explicit transpose adds O(V + E).

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0. At most 16 edges. Weights are ignored. Vertices include every index from 0 to the highest endpoint.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Use finish order rather than discovery order, and reverse all edges for the second pass.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Edge arrays from/to/weight; 1..8 vertices indexed 0..vertices-1, 0..16 edges, endpoints in bounds. Weights ignored. Output buffer at least 256 integers. Directed SCCs: returns component count; output[v] is the smallest vertex in v's component. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(from, to, weight, edges, vertices, output) {
    let seen = Array(256).fill(0); let cursor = Array(256).fill(0); let dfs = Array(256).fill(0); let finish = Array(256).fill(0); let group = Array(256).fill(0);
    let root = 0; let u = 0; let v = 0; let index = 0; let top = 0; let length = 0; let size = 0; let position = 0; let label = 0; let count = 0;
    for (u = 0; u < vertices; u++) { output[u] = -1; }
    for (root = 0; root < vertices; root++) {
        if (seen[root] != 0) { continue; }
        seen[root] = 1; dfs[top++] = root;
        while (top > 0) {
            u = dfs[top - 1]; index = cursor[u];
            while (index < edges && from[index] != u) { index++; }
            cursor[u] = index + 1;
            if (index < edges) { v = to[index]; if (seen[v] == 0) { seen[v] = 1; dfs[top++] = v; } }
            else { top--; finish[length++] = u; }
        }
    }
    for (position = length - 1; position >= 0; position--) {
        root = finish[position]; if (output[root] >= 0) { continue; }
        output[root] = root; dfs[top++] = root; size = 0; label = root;
        while (top > 0) {
            u = dfs[--top]; group[size++] = u; if (u < label) { label = u; }
            for (index = 0; index < edges; index++) { v = from[index]; if (to[index] == u && output[v] < 0) { output[v] = root; dfs[top++] = v; } }
        }
        for (index = 0; index < size; index++) { output[group[index]] = label; }
        count++;
    }
    return count;
}

Source

Sharir is a published source for the two-pass strong-component method, often called Kosaraju-Sharir; this is not a claim of sole or first discovery.