Published by Dimitris Kougioumtzis · Updated
Treat edges as undirected and accept lightest edges that do not form a cycle.
Accept light edges only when they join different components; union-find identifies cycle-forming proposals.
When to use Kruskal minimum spanning tree
Minimum-cost undirected connectivity.
Choosing an alternative
Prim grows a frontier; Boruvka lets multiple components choose edges per round.
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.
Step 0: Initial graph.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞" ], "active": [], "kind": "graph", "visited": [], "chosen": [] }Step 3: Consider edge 0—2, weight 2.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞" ], "active": [ 0, 2 ], "kind": "graph", "visited": [ 2, 1 ], "chosen": [ 2 ] }Step 6: Accept edge; total weight 5.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞" ], "active": [ 3, 4 ], "kind": "graph", "visited": [ 2, 1, 0, 3, 4 ], "chosen": [ 2, 1, 5 ] }Step 8: Consider edge 1—3, weight 5.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞" ], "active": [ 1, 3 ], "kind": "graph", "visited": [ 2, 1, 0, 3, 4 ], "chosen": [ 2, 1, 5 ] }Step 11: Minimum spanning tree weight: 10.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞" ], "active": [], "kind": "graph", "visited": [ 2, 1, 0, 3, 4 ], "chosen": [ 2, 1, 5, 3 ] }
Expected final state
Minimum spanning tree weight: 10.
Complexity and input bounds
Family: Graphs. Time complexity: O(E log E).
Implementation detail: O(E log E) describes efficient edge sorting with suitable disjoint sets. The JavaScript/C-family reference uses insertion sorting and simple parent walks: O(E squared + EV + V). The Python version uses its built-in sort but retains simple parent walks.
Space complexity: O(V + E) component and edge-order 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 input produces a forest, and a spanning tree is not a shortest-path tree.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 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. 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 order = Array(256).fill(0); for (let v = 0; v < vertices; v++) { parent[v] = v; } for (let i = 0; i < edges; i++) { order[i] = i; } for (let i = 1; i < edges; i++) { let j = i; while (j > 0 && weight[order[j - 1]] > weight[order[j]]) { let t = order[j]; order[j] = order[j - 1]; order[j - 1] = t; j--; } } let total = 0; let count = 0; for (let i = 0; i < edges; i++) { let k = order[i]; let u = from[k]; let v = to[k]; while (parent[u] != u) { u = parent[u]; } while (parent[v] != v) { v = parent[v]; } if (u != v) { parent[u] = v; output[count++] = k; total += weight[k]; } } return total; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- On the shortest spanning subtree of a graph and the traveling salesman problem
Joseph B. Kruskal (1956). Research paper.