Union–find (rank + path compression): Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Treat edges as undirected unions. Edge weights are ignored; node values show parents.

Compress root-search paths and merge by rank so repeated connectivity queries use increasingly shallow representative trees.

Open the 3D visualization

Union–find (rank + path compression) example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Union–find (rank + path compression)

Incremental undirected connectivity and cycle checks.

Choosing an alternative

DFS computes static connectivity; union-find efficiently supports subsequent merges.

Worked example: input and state changes

Example input

0,1,4; 0,2,2; 2,1,1; 1,3,5; 2,3,8; 3,4,2

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: Every vertex begins as its own component.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [],
      "kind": "graph",
      "visited": [],
      "chosen": [],
      "rank": [
        0,
        0,
        0,
        0,
        0
      ]
    }
  2. Step 3: Compress the path from 2 to root 0.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        3,
        4
      ],
      "active": [
        2,
        0
      ],
      "kind": "graph",
      "visited": [
        0,
        1,
        2
      ],
      "chosen": [
        0,
        1
      ],
      "rank": [
        1,
        0,
        0,
        0,
        0
      ]
    }
  3. Step 7: Join roots 0 and 3; components = 2.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        4
      ],
      "active": [
        1,
        3
      ],
      "kind": "graph",
      "visited": [
        0,
        1,
        2,
        3
      ],
      "chosen": [
        0,
        1,
        3
      ],
      "rank": [
        1,
        0,
        0,
        0,
        0
      ]
    }
  4. Step 10: Nodes 2 and 3 are already connected.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        4
      ],
      "active": [
        2,
        3
      ],
      "kind": "graph",
      "visited": [
        0,
        1,
        2,
        3
      ],
      "chosen": [
        0,
        1,
        3
      ],
      "rank": [
        1,
        0,
        0,
        0,
        0
      ]
    }
  5. Step 13: Connected components = 1.

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

Expected final state

Connected components = 1.

Complexity and input bounds

Family: Graphs. Time complexity: O((V + E) α(V)).

Space complexity: O(V) parents and ranks.

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.

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

It does not support arbitrary deletions, and root IDs have no inherent member ordering.

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
// Directed edge arrays from, to, weight; nodes 0..vertices-1; source 0. Up to 8 vertices, 16 edges. Allocate output with at least 256 integers. Treats edges as undirected; ignores weights. Union by rank with path halving; output receives component representatives. Returns component count. 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 rank = Array(256).fill(0);
    for (let v = 0; v < vertices; v++) { output[v] = v; }
    let components = vertices;
    for (let i = 0; i < edges; i++) {
        let x = from[i]; let y = to[i];
        while (output[x] != x) { output[x] = output[output[x]]; x = output[x]; }
        while (output[y] != y) { output[y] = output[output[y]]; y = output[y]; }
        if (x == y) { continue; }
        if (rank[x] < rank[y]) { let t = x; x = y; y = t; }
        output[y] = x;
        if (rank[x] == rank[y]) { rank[x]++; }
        components--;
    }
    for (let v = 0; v < vertices; v++) {
        let x = v;
        while (output[x] != x) { output[x] = output[output[x]]; x = output[x]; }
        output[v] = x;
    }
    return components;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.