How to use the Minimum Spanning Tree Visualizer
- 1. Enter weighted edgesOne per line, like A-B 4.
- 2. Pick an algorithmKruskal, or Prim with a start node.
- 3. Step throughSee why each edge is taken or skipped.
Two greedy algorithms
A minimum spanning tree connects every node of a weighted, undirected graph using the least total edge weight, with no cycles. It always has V − 1 edges. It's used to design networks such as cables, pipelines and roads, and in clustering.
- Kruskal's algorithm sorts all edges by weight and takes each one unless it would create a cycle. A union-find structure tracks which nodes are already connected. Time: O(E log E).
- Prim's algorithm grows a single tree from a start node, always adding the cheapest edge that reaches a new node. With a binary heap: O(E log V).
Both are greedy and both give the same total weight. If several edges have equal weights, the exact edges chosen can differ. If the graph isn't connected, Kruskal gives a minimum spanning forest, and the tool tells you.
Worked example
The sample is the 9-node graph from the standard algorithms textbook (CLRS), with 14 edges. Running Kruskal:
- Take G-H (1), C-I (2) and F-G (2), then A-B (4) and C-F (4).
- Skip G-I (6): G and I are already connected through F and C.
- Take C-D (7); skip H-I (7); take A-H (8); skip B-C (8); take D-E (9).
That's 8 edges with a total weight of 37. Run Prim from A and the edges are added in a different order, A-B first, but the total is again 37. Taken edges turn green and skipped ones are dashed red, so you can see exactly which edges would have made a cycle.
Frequently asked questions
Do negative weights work?
Yes. Both algorithms only compare weights, so negative values are fine for an MST.
Does it work on directed graphs?
No. MSTs are defined for undirected graphs. The directed version is called a minimum arborescence.
When is Prim better than Kruskal?
On dense graphs with many edges, Prim with an adjacency matrix or heap is usually faster; Kruskal suits sparse graphs.
Is the minimum spanning tree visualizer free to use?
Yes, the minimum spanning tree visualizer is free and costs nothing. Your first MST shows a short form for your name and WhatsApp number one time; once that's done, this tool and every other Uncodemy tool open with no form.