Graphs · Exercise 45 of 100
Prim’s minimum spanning tree
Prim’s algorithm builds a minimum spanning tree in a connected, undirected, weighted graph by starting with one vertex. At each step, choose the cheapest edge with one endpoint in the tree and the other outside it, so the tree grows by one vertex without creating a cycle.
Try it yourself
Two-way links have costs Mill to Lake 4, Mill to Farm 7, Lake to Hill 2 and Farm to Bay 1. Starting with Mill as the first vertex, which vertex does Prim’s algorithm add third?
One-step hint
Circle Mill. At each step compare only links with exactly one circled endpoint, then circle the new vertex on the cheapest eligible link.
Worked solution
- From Mill the cheaper link costs 4 and reaches Lake, so Lake is the second vertex.
- The links leaving the tree now cost 2 to Hill and 7 to Farm. The cheaper one adds Hill third.