Minimum Spanning Tree: Kruskal and Prim

Join every point to every other with as little road as possible. Kruskal takes roads shortest first and refuses any that would close a loop; Prim grows one network from A and always takes the cheapest road out of it. Step through either and the page gives the reason for every road taken or refused. Drag the points and the tree re-forms.

Loading interactive simulation...

Lesson

The theory — Minimum Spanning Tree: Kruskal and Prim

A spanning tree of a set of points is a set of roads that connects every point to every other and contains no loop; on n points it always has n − 1 roads. The minimum spanning tree is the spanning tree with the smallest total length. A cut splits the points into two groups, and a road crosses the cut when its two ends are in different groups. A Steiner tree may also use junctions that are not among the points.

What each symbol means

n
the number of points
w(u, v)
the length of the straight road from u to v, in km
S
the points on one side of a cut; in Prim's algorithm, the network built so far
Assumes
Roads are straight and cost their length, and any two points may be joined. When no two distances are equal the minimum spanning tree is unique. With ties, as in the triangle and the square, several trees can share the shortest length and the two algorithms may pick different ones.

Cheapest across the cut, not nearest from here 🖖

Press Twelve villages and step through Kruskal. It goes through the 66 possible roads shortest first, so K–L, 13.89 km, goes in at once, and C–D and B–C are refused because the path drawn in red already joins their ends. Switch to Prim, which grows one network out from A. It takes the roads in another order, K–L last of all, refuses the same two, and ends on the same 11 roads and the same 199.04 km. Both are obeying one rule, the cut property: split the points into two groups any way you like, and the shortest road between the groups belongs to the minimum spanning tree, as long as no two roads are the same length. Leave it out, and the tree's own path between that road's two ends crosses the gap somewhere on a longer road; swap the two and the tree gets shorter. Prim's shaded network is one side of such a cut at every step. Now press Nearest neighbour. It is Prim's loop with one change, looking out from the last village reached instead of from the whole network, and at its third step it takes D–F, 17.96 km, while A–C, 17.67 km, crosses the cut. It walks past A–C seven more times and finishes 10.76% longer. Otakar Borůvka, who solved the problem first, in 1926, with the electricity network of southern Moravia in mind, stated it for points whose “mutual distances are different”: the same condition, and the one under which Kruskal and Prim cannot disagree.

The shortest network is not a network of short trips 🖖

Press A ring of towns. Ten towns stand round a lake, and the tree is the shore road with one stretch left out: the widest gap, between J and A. Those two are 25.89 km apart in a straight line and 128.30 km apart along the tree, 4.96 times as far, because the only route from one to the other is the entire network. The dashed purple line marks the pair; drag any town and the page finds the new worst one. A minimum spanning tree makes the total as small as it can be and promises nothing about the distance between any two towns. Building the 25.89 km road as well would make the trip from A to J a straight one. Whether that is worth the extra road is a question about journeys, which is the shortest-path problem, and the page on Dijkstra's algorithm takes it from there.

A junction can beat the tree, and nobody has proved by how much 🖖

Press Three towns. They stand 40 km apart, so the tree is 80.00 km. A junction in the middle, where three roads meet at 120°, joins them with 69.28 km, 13.40% less, which is 1 − √3/2. Press Four towns on a square and two junctions save 8.93%. Back on the triangle, drag C down towards A and B: once the angle at C passes 120° the junction slides into C, and from there no junction helps at all. Finding the best junctions is the Steiner tree problem. In 1968 Edgar Gilbert and Henry Pollak conjectured that the equilateral triangle is the worst case, that no set of points in the plane has a Steiner tree shorter than √3/2 of its minimum spanning tree. Du and Hwang published a proof in 1990. In 2010 Innami and three colleagues published evidence that its key step fails, and in 2012 Ivanov and Tuzhilin, who had been pointing at gaps in it since 2002, wrote that the conjecture is still open. A 2025 collection of open problems on Steiner trees still lists it as open. Nobody knows whether some arrangement of points can save more than the triangle's 13.40%.

Problem solved in full

  1. Two junctions for a square 4 steps

    Where do the junctions go, and how long is the road? Press Four towns on a square: towns at the corners of a 40 km square, a 120.00 km spanning tree, and 109.28 km with junctions. Find that length exactly, and test the guess most people make first, an X through the centre.

    Open this problem in the tool

    1. Where three roads meet at a junction of a shortest network, they meet at 120°. If they did not, the three unit vectors pointing along them would not cancel, and moving the junction in the direction of their sum would shorten the total.

    2. Put both junctions on the line through the middle of the square, each serving the two corners nearest it, with a middle road between them. Each spoke meets the middle road at 120°, so it runs at 60° to that road's line. It has to cover 20 km sideways, half the side, so its length is 20/sin 60° and it reaches 20/tan 60° along the road's direction.

    3. Four spokes, and a middle road made of whatever the two spokes leave of the 40 km.

    4. The X: four roads from the corners to the centre, each half a diagonal, 20√2 km.

    Answer

    40(1 + √3) = 109.28 km, with each junction 11.55 km in from the side it serves; the X is 113.14 km. The X beats the spanning tree but loses to the junctions, because its roads meet at 90° rather than 120°. There are two networks of exactly this length, the one on the page and the same one turned through 90°. The saving is 1 − (1 + √3)/3 = 8.93%, less than the triangle's 13.40%.

Learning path

Why an algorithm is correct

References (3)