Advanced maths

Minimum spanning tree calculator from weighted edges

Find a minimum-weight spanning tree or disconnected spanning forest, with Kruskal decisions and a reproducible edge selection.

Your numbers

Vertices are numbered 1 through this count. Include vertices without edges.
One undirected edge per row. Integer costs may be negative or zero. Leave empty for isolated vertices.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A minimum spanning tree connects every vertex of a connected undirected graph with the least possible total edge cost and without a cycle. This calculator uses Kruskal’s algorithm: inspect edges in ascending weight order, accepting an edge exactly when it joins two previously separate components. If the entered network is disconnected, the answer is a minimum spanning forest with one tree per original component. The decision table keeps input row identities, so the chosen cost can be checked against the supplied edges.

THE FORMULAChoose an acyclic spanning edge set minimizing Σw(e); each component contributes |V_component|−1 edges.

A worked example

For edges 1–2 of cost 1, 2–3 of cost 2, 3–4 of cost 3, 1–4 of cost 10 and 1–3 of cost 4, the selected rows are the first three. They connect all four vertices at cost 1+2+3=6. Edge 1–3 is skipped because vertices one and three are already connected; adding it would create a cycle. The cost-ten edge is skipped for the same reason. A route from one to four along the tree costs six, but shortest routes are not the optimization objective.

Good to know

Undirected simple graphs with 1–40 vertices and at most 200 edges; integer weights within ±1,000,000. Negative and zero weights are supported. Self-loops and repeated unordered endpoint pairs are rejected. Equal-cost edges use input-row order to choose one reproducible optimum; uniqueness is not claimed. A disconnected input returns its component-wise forest, never an invented connection. No directed arborescence, constrained degree, reliability, geographic routing or capacity model is inferred.

A safe edge joins components; a cheap edge alone is not enough

Kruskal’s test concerns connectivity in the edges already accepted, not just whether an endpoint has appeared before. A new edge can join two large partial trees even when both endpoint labels have been seen. Conversely, a very cheap edge is skipped when its endpoints are already connected, because it would close a cycle. The component structure is updated only after an accepted edge; rejected rows remain visible in the table.

The cut property explains why this local choice can produce a global minimum: a least-weight edge crossing a suitable component cut can be included in some minimum spanning forest. The algorithm does not enumerate all possible trees. After completion, a graph with n vertices and c connected components has exactly n−c selected edges. That count checks the forest structure, while the cost-order and component decisions establish the optimization rule.

The three cheapest cycle-free edges connect four vertices at total cost six1234123410Selected cost: 1+2+3Minimum total: 64 − 1 = 3 tree edges
Solid edges 1–2,2–3,3–4 have weights 1,2,3. Dashed alternatives 1–3 and 1–4 would add cycles after those choices. Drawing lengths do not supply the edge weights.

The Matrix determinant calculator evaluates determinants, which appear in spanning-tree counting. A minimum spanning forest instead selects a least-cost network without counting every possible tree.

Ties and disconnected vertices change interpretation, not the problem definition

Equal weights can allow several optimal trees. Changing the order of tied input rows may change the returned edge set while leaving the minimum total cost unchanged. A drawing is only a picture of connectivity; a long drawn edge can still have a small entered cost. Enter the objective weight you intend to minimize, rather than assuming the visual geometry supplies a missing distance or travel time.

An isolated vertex still belongs to the graph and contributes one component with no selected edge. A negative edge is also legitimate: in a cycle-free selection, it lowers cost without allowing an indefinitely repeated walk. This differs from shortest-walk problems, where a reachable negative cycle may make a minimum undefined. Use the shortest-path calculator for an endpoint journey, and the spanning-tree count calculator when the question is how many different trees exist.

A few questions, answered

Does a minimum spanning tree give every pair its shortest route?

No. It minimizes the sum of selected edge weights over the whole spanning network. A route between two vertices can be longer than their shortest path in the original graph.

Can a disconnected input have a minimum spanning tree?

It has no single spanning tree. This page returns a minimum spanning forest and reports the number of components explicitly.

Find your calculator

Start typing to explore the library.