Minimum spanning tree calculator
Use Kruskal’s algorithm to connect an undirected graph at minimum total weight. Your inputs stay on your device.
Set up your task
Starting local processing
Ready. Processing stays in this browser.
2 Your result
Your result updates in this browser. Check your input if the result looks unexpected.
How to use Minimum spanning tree calculator
Use Kruskal’s algorithm to connect an undirected graph at minimum total weight. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.
- Check the example inputs: Vertices (space-separated), Edges (from, to, weight, one per line).
- Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
- Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.
Supported inputs and limits
Use up to 20 vertices and 80 weighted, undirected edges. Negative edge weights are allowed.
How it works
A spanning tree connects every vertex of a connected undirected graph using the fewest edges needed for connectivity. A minimum spanning tree chooses those edges to minimise their total weight. Each candidate edge can be viewed as a connection cost, such as the cost of joining two network sites.
Kruskal’s algorithm processes edges in ascending weight. It adds an edge only when that edge joins different components. This condition avoids closing a cycle. The disjoint-set data structure records the current component of each vertex. With disconnected input, the same process builds a tree within each component.
Example
Run the pre-filled example. Its first result is “Minimum spanning tree weight: 6”.
- Vertices (space-separated)
- A B C D
- Edges (from, to, weight, one per line)
- A B 1
A C 4
B C 2
B D 5
C D 3
Minimum spanning tree weight: 6 Components: 1 A — B · 1 B — C · 2 C — D · 3
Questions about Minimum spanning tree calculator
How many edges should a connected result have?
A spanning tree for n vertices has n−1 edges. In a forest with c components there are n−c selected edges.
Is a minimum spanning tree a collection of shortest routes?
Its objective is the sum of selected edge weights. Paths within the tree can be longer than shortest paths in the original graph.
Remove both edges incident to D in the example.
D is a component by itself. The tool returns a forest of two components with total weight 3, from the edges A–B and B–C.
Related tools
Last updated