2.7 Minimum spanning trees: Prim's algorithm — Practise
Work independently. The questions and answers are shown together at the end.
State the starting vertex first, then at each step state the tree so far and the cheapest edge leaving it.
- Prim's algorithm starts from one vertex. A network has edges , , , , , , , , . Starting from , which edge is chosen first, and why?
- Continue Prim's algorithm from on that network and find the total weight of the minimum spanning tree.
- Prim's and Kruskal's algorithms gave the same total weight. State the one thing they have in common and the one thing that differs.
- Prim's algorithm, part-way through, has a tree on . The edges leaving the tree are , and . Which edge does it take next?
- State the rule Prim's algorithm follows at each step, and say why the chosen edge can never close a cycle.
Answers and concise solutions
1. Show the answer
1. The first edge
Answer: The edges at are and . Prim's algorithm takes the cheapest edge leaving the chosen set, so it takes first. (Kruskal would also start at , so the two algorithms begin differently — and still end the same.)
2. Show the answer
2. Total weight
Answer: From , take . From the cheapest edge out is ; take it. From the cheapest is ( would close the cycle and is not considered). From the cheapest is . From the cheapest is . All 6 vertices are now in the tree, with edges and total weight — the same total Kruskal found.
3. Show the answer
3. The common thing, then the difference
Answer:
Both algorithms always produce a minimum spanning tree, so on a network with distinct weights they give the same total weight, and even the same tree. They differ in how they grow it. Kruskal considers all edges in order of weight and adds the cheapest that does not close a cycle, so it can build several separate pieces before joining them. Prim starts from one vertex and always extends that single connected tree by its cheapest edge out, so the tree is connected at every step.
4. Show the answer
4. The next edge
Answer: The cheapest edge leaving the tree has weight , and there are two of them: and . Either may be taken. If both are taken in turn, both are added, since neither closes a cycle — they reach different new vertices. So the answer is an edge of weight , and the tie may be broken either way.
5. Show the answer
5. The rule, and why no cycle
Answer:
At each step Prim's algorithm takes the cheapest edge joining a vertex already in the tree to a vertex not yet in it. The chosen edge must reach a vertex outside the tree, because any edge between two vertices already in the tree would not be a candidate — it is not an edge leaving the tree. Adding an edge that brings in a new vertex cannot close a cycle, because a cycle needs every one of its vertices already in the tree. So the tree grows by one vertex and one edge each step, and stays a tree.