Borůvka minimum spanning forest: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Each component chooses its cheapest outgoing undirected edge, then components merge.

Each component chooses a cheap outgoing edge in a round; safe choices merge components and reduce their number.

Open the 3D visualization

Borůvka minimum spanning forest example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Borůvka minimum spanning forest

Comparing component-parallel spanning-forest growth.

Choosing an alternative

Kruskal sorts globally; Prim grows a single current frontier.

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: Each vertex starts in a separate tree.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4
      ],
      "active": [],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  2. Step 1: Merge components with edge 0—2 (2); total 2.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        0,
        3,
        4
      ],
      "active": [
        0,
        2
      ],
      "kind": "graph",
      "visited": [
        0,
        2
      ],
      "chosen": [
        1
      ]
    }
  3. Step 3: Merge components with edge 3—4 (2); total 5.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        3,
        3
      ],
      "active": [
        3,
        4
      ],
      "kind": "graph",
      "visited": [
        0,
        2,
        1,
        3,
        4
      ],
      "chosen": [
        1,
        2,
        5
      ]
    }
  4. Step 4: Merge components with edge 1—3 (5); total 10.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        1,
        3
      ],
      "kind": "graph",
      "visited": [
        0,
        2,
        1,
        3,
        4
      ],
      "chosen": [
        1,
        2,
        5,
        3
      ]
    }
  5. Step 5: Minimum spanning tree weight: 10.

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

Expected final state

Minimum spanning tree weight: 10.

Complexity and input bounds

Family: Graphs. Time complexity: O(E log V).

Implementation detail: The usual O(E log V) round bound treats component lookups as efficient operations; the reference also spends time on union-find lookups and per-round vertex initialization.

Space complexity: O(V + E) component and candidate storage.

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

Disconnected components can have no outgoing edge; terminate when a round makes no merge.

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
29
30
31
// 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. Writes selected edge indices and returns minimum spanning forest weight. 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 parent = Array(256).fill(0); let cheapest = Array(256).fill(0); let rank = Array(256).fill(0);
    for (let v = 0; v < vertices; v++) { parent[v] = v; }
    let total = 0; let count = 0;
    while (1 == 1) {
        for (let v = 0; v < vertices; v++) { cheapest[v] = -1; }
        for (let i = 0; i < edges; i++) {
            let u = from[i]; let v = to[i];
            while (parent[u] != u) { parent[u] = parent[parent[u]]; u = parent[u]; }
            while (parent[v] != v) { parent[v] = parent[parent[v]]; v = parent[v]; }
            if (u == v) { continue; }
            if (cheapest[u] < 0 || weight[i] < weight[cheapest[u]]) { cheapest[u] = i; }
            if (cheapest[v] < 0 || weight[i] < weight[cheapest[v]]) { cheapest[v] = i; }
        }
        let changed = 0;
        for (let component = 0; component < vertices; component++) {
            let index = cheapest[component]; if (index < 0) { continue; }
            let u = from[index]; let v = to[index];
            while (parent[u] != u) { parent[u] = parent[parent[u]]; u = parent[u]; }
            while (parent[v] != v) { parent[v] = parent[parent[v]]; v = parent[v]; }
            if (u != v) {
                if (rank[u] < rank[v]) { parent[u] = v; }
                else { parent[v] = u; if (rank[u] == rank[v]) { rank[u]++; } }
                total += weight[index]; output[count++] = index; changed = 1;
            }
        }
        if (changed == 0) { break; }
    }
    return total;
}

Source

Digitized original Czech publication from 1926. The playground extends the presentation to disconnected spanning forests.