How to run Prim or Kruskal 01 Build a weighted graph. Try the Grid preset (rows 4, cols 5 — 20 nodes, 31 unit edges), a Random connected graph, or Complete K₆. 02 Pick Prim and set Source , or switch to Kruskal, which needs no source. 03 Read the table. Total weight first, then edges chosen, components and is-forest; below sit one row per chosen edge with its weight. 04 Cross-check the other algorithm — the two must agree on total weight for the same graph. 05 Step the run with Play / Prev / Next; Prim highlights the growing frontier, Kruskal the accepted and rejected edges.
Worked readouts Values as displayed by the panel:
Graph · algorithm
Readout
Grid 4 × 5, Prim from 1-1
total weight 19 ; edges chosen 19 / 31 ; components 1 ; is forest No
Grid 4 × 5, Kruskal
total weight 19 ; edges chosen 19 / 31
Random connected (n 6, p 0.3, seed 20260730, max weight 9), Prim from A
total weight 16 ; chosen A–B(3), A–C(4), C–E(2), A–D(6), D–F(1) ; edges chosen 5 / 7
The same graph, Kruskal
total weight 16 ; the same five edges in weight order: D–F(1), C–E(2), A–B(3), A–C(4), A–D(6)
Complete K₆, Prim
total weight 5 ; edges chosen 5 / 15 ; edges A–B(1), A–F(1), A–E(1), A–D(1), A–C(1)
The grid MST is 19 unit edges for 20 nodes — the theoretical minimum — and both algorithms reach it. On the random graph one edge is enough to check the answer by hand: D–F weighs 1 , the lightest in the graph, so every MST must contain it, and both runs do.
Why the two lists differ Prim and Kruskal pick edges in different orders, and on ties they may pick different valid trees. Kruskal sorts all edges globally, so its table starts with the lightest edge anywhere; Prim only looks at the frontier of one growing tree, so its table starts with whatever leaves the root first. On the random graph the two produce the identical set — the minimum spanning tree there is unique — but the K₆ list (all weights 1) is just one of many perfect trees, so compare total weight and edge count, not edge lists, when weights repeat.
Limits
A disconnected graph gives a forest. The is-forest row turns Yes and the component count stays above 1; the answer is then a minimum spanning forest, one tree per component, not a single tree.
Undirected edges only. Prim treats a directed graph as undirected and says so in a warning; Kruskal's component merging is also an undirected idea. Neither algorithm solves the directed minimum arborescence.
The tie-break trap On Complete K₆ every edge weighs 1 , so every spanning tree weighs 5 ; Prim happens to answer with a star around A: A–B, A–F, A–E, A–D, A–C . Read that list as a minimum tree, not the minimum tree. The same caution applies to equal-weight grids: the grid answer 19 is stable, but which 19 unit edges appear can differ between Prim and Kruskal while both remain correct.
Where it is useful Checking a cheapest-wiring claim On the random graph the two heaviest edges, E–F(8) and B–D(7) , are both excluded while A–D(6) is accepted. Check a proposed network the same way: if it pays for E–F(8) while leaving the cheaper D–F(1) out, it is not minimal.
Teaching the cut property The grid makes the cut reasoning visible: every unit edge crosses some cut at weight 1, and both algorithms accept 19 of the 31 without a single cycle. Switch to the Random connected graph and run both; the step playback shows Kruskal skipping E–F(8) because its endpoints are already joined.
Privacy Graph editing and both MST runs execute locally in your browser; nothing is uploaded.
References
Wikipedia, Prim's algorithm , en.wikipedia.org (访问日期:2026-10-07)— frontier growth from a root.
Wikipedia, Kruskal's algorithm , en.wikipedia.org (访问日期:2026-10-07)— weight order with union-find.
Wikipedia, Minimum spanning tree , en.wikipedia.org (访问日期:2026-10-07)— cut and cycle properties, uniqueness on distinct weights.
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein, Introduction to Algorithms , 4th ed., MIT Press, 2022, mitpress.mit.edu (访问日期:2026-10-07)— MST algorithms and the cut property.
Calculators in this hub
Hand-picked tools, one click away. The mini versions compute live and carry your values into the full calculator.
Sources & review
Reviewed by CalcX Editorial Team
Updated 2026-10-07