Graph Theory Intermediate

Minimum Spanning Tree Visualizer

Compare Kruskal's and Prim's algorithms step by step — see how Union-Find detects cycles and how the cut property drives greedy edge selection.

Kruskal's & Prim's algorithms
Canvas graph visualization
Live
Algorithm
Graph Edges (undirected)
Node 1Node 2Weight
450ms
MST edge Candidate Rejected In MST Checking
Edges Sorted by Weight
Step Log
Log appears as you step...
Current Step: READY
What is a Minimum Spanning Tree?
A Minimum Spanning Tree (MST) connects every node in a graph using exactly |V|−1 edges, with the smallest possible total weight — and no cycles.

Choose an algorithm above, then press ▶ Run All or ⏭ Step to trace through each decision.
Two classic approaches
Kruskal's: Sort all edges by weight; greedily add the cheapest edge that doesn't create a cycle. Uses a Union-Find structure to track components.

Prim's: Grow the MST outward from a start node; at each step pick the cheapest edge crossing the frontier between the MST and the rest of the graph.
Graph Visualization
Step Table
Minimum Spanning Trees
|V| − 1 edges, minimum total weight, no cycles

An MST connects all vertices using exactly |V|−1 edges with minimum total weight — no cycles. Kruskal's sorts edges by weight and greedily adds the cheapest edge that doesn't form a cycle, using Union-Find in O(α(n)) per operation.

Prim's grows the MST from a start node by always picking the cheapest edge crossing the cut between the MST and the remaining graph — the Cut Property guarantees correctness.

Both algorithms are greedy and always produce an optimal MST. Kruskal's is easier to implement; Prim's is faster on dense graphs with an adjacency matrix.
Where MSTs Appear
  • Network cable / road layout minimization
  • Cluster analysis in machine learning
  • Approximation algorithms for TSP
  • Circuit board trace routing
  • Image segmentation

Stuck on graph algorithms?

MSTs, shortest paths, dynamic programming — we explain the intuition behind the proof, not just the steps.

Book a Free Consultation →