Minimum Spanning Tree Calculator

Kruskal's Algorithm · Prim's Algorithm · Graph Visualization

Enter a weighted undirected graph as an edge list and compute the MST using both Kruskal's and Prim's algorithms. See step-by-step tables and a canvas visualization.

Quick Examples

What Is a Spanning Tree?

A spanning tree of a connected undirected graph G = (V, E) is a subgraph that includes all vertices V and a subset of edges forming a tree — a connected, acyclic graph. For a graph with n vertices, every spanning tree has exactly n−1 edges. The concept is fundamental to network design, where you want every node reachable from every other node with no redundant connections.

What Is a Minimum Spanning Tree?

A minimum spanning tree (MST) is a spanning tree whose total edge weight is minimised over all possible spanning trees of the graph. Given a weighted undirected graph, the MST answers the question: "What is the cheapest way to connect all vertices?" For a graph with n vertices and m edges, the MST contains exactly n−1 edges and its total weight is the smallest achievable.

Two classical greedy algorithms solve this problem efficiently: Kruskal's algorithm and Prim's algorithm. Both are guaranteed to find an MST, and when all edge weights are distinct they both find the same unique tree.

Kruskal's Algorithm

Kruskal's algorithm processes edges globally, sorted by weight. It starts with each vertex in its own component and merges components by greedily adding the cheapest edge that does not create a cycle.

Steps

  1. Sort all edges by weight in ascending order.
  2. Initialize a Union-Find (disjoint set) structure with each vertex as its own set.
  3. For each edge (u, v, w) in sorted order: if u and v are in different components, add the edge to the MST and union the two components.
  4. Stop when n−1 edges have been added.

The Union-Find data structure with path compression and union by rank makes the cycle detection nearly O(1) per edge, giving Kruskal's algorithm an overall time complexity of O(E log E) — dominated by the sort.

Prim's Algorithm

Prim's algorithm grows the MST from a single starting vertex, always selecting the cheapest edge that connects the current MST to a vertex not yet in the tree.

Steps

  1. Start with any vertex; mark it visited.
  2. Add all edges from the current MST to a min-priority queue.
  3. Extract the minimum-weight edge (u, v) where v is unvisited; add it to the MST.
  4. Mark v visited and enqueue its edges. Repeat until n−1 edges are added.

With a binary heap, Prim's runs in O(E log V). With a Fibonacci heap it reaches O(E + V log V), which is superior for dense graphs.

Kruskal's vs Prim's — Comparison

PropertyKruskal'sPrim's
ApproachGlobal edge sortVertex-by-vertex growth
Data StructureUnion-FindMin-Heap / Priority Queue
Time ComplexityO(E log E)O(E log V)
Best ForSparse graphsDense graphs
Starting PointNone (all edges)Chosen start vertex
Produces same MST?Yes (for unique weights)Yes (for unique weights)

MST Uniqueness

An MST is unique if all edge weights are distinct. This follows from the cycle property: the maximum-weight edge in any cycle is never in any MST. When two edges share the same weight, multiple MSTs with equal total weight may exist. However, the total MST weight is always unique regardless of tie-breaking.

Disconnected Graphs — Spanning Forests

If the graph is disconnected, no spanning tree exists. Instead, each connected component has its own MST, and together they form a minimum spanning forest. For a graph with n vertices and k components, the spanning forest contains exactly n−k edges.

Real-World Applications

  • Network Design: Designing telephone, power, or internet networks with minimum total cable length.
  • Cable/Pipeline Laying: Finding the cheapest way to connect cities or infrastructure with physical links.
  • Cluster Analysis: Single-linkage hierarchical clustering uses MSTs to group similar data points.
  • TSP Approximation: The MST gives a 2-approximation for the metric Travelling Salesman Problem.
  • Image Segmentation: Graph-based segmentation algorithms build MSTs over pixel similarity graphs.
  • VLSI Circuit Design: Minimising wire length connecting circuit components on a chip.

Frequently Asked Questions

What is a minimum spanning tree?
A minimum spanning tree is a spanning tree of a connected weighted undirected graph with the smallest possible total edge weight. It contains all n vertices of the graph connected by exactly n−1 edges with no cycles, and no other spanning tree of the same graph has a smaller sum of edge weights.
What is the difference between Kruskal's and Prim's algorithm?
Kruskal's algorithm works globally — it sorts all edges and greedily adds each one if it does not form a cycle (detected via Union-Find). It is efficient for sparse graphs with O(E log E) time. Prim's algorithm works locally — starting from one vertex, it always picks the cheapest edge reaching an unvisited node, growing the MST outward. It runs in O(E log V) with a binary heap and suits dense graphs. Both algorithms always find a valid MST.
Does every connected graph have a minimum spanning tree?
Yes. Every finite connected weighted undirected graph has at least one minimum spanning tree. The proof follows from the fact that the set of all spanning trees is finite and non-empty (for a connected graph), so a spanning tree of minimum weight always exists. For disconnected graphs, each connected component separately has an MST, forming a minimum spanning forest overall.
Is the minimum spanning tree always unique?
The MST is unique if and only if all edge weights are distinct. When distinct weights are used, both Kruskal's and Prim's always select the same edges. When some edges share the same weight, multiple MSTs may exist, all having the same total weight. The total MST weight is always uniquely determined regardless of how ties are broken.
What is the time complexity of Kruskal's algorithm?
Kruskal's algorithm runs in O(E log E) time, where E is the number of edges. Sorting the edges dominates. The Union-Find operations, with path compression and union by rank, run in O(E α(V)) where α is the inverse Ackermann function — effectively a constant less than 5 for any practical input size. For dense graphs where E ≈ V², this becomes O(V² log V).
What are the real-world applications of minimum spanning trees?
MSTs appear throughout engineering and computer science: designing minimum-cost telecommunications and electrical grid layouts; laying cables, water pipes, or roads between cities at minimum total cost; single-linkage hierarchical clustering in machine learning; constructing 2-approximation solutions for the Travelling Salesman Problem; image segmentation in computer vision; VLSI circuit layout minimising wire length; and finding bottleneck spanning trees for network reliability analysis.
What is a spanning forest?
A spanning forest is the generalization of a spanning tree to disconnected graphs. It is a maximal acyclic subgraph — equivalently, a collection of spanning trees, one for each connected component. If a graph has n vertices and k connected components, its minimum spanning forest contains exactly n−k edges and minimises total weight across all components.