2.7 Minimum spanning trees: Prim's algorithm — Practise with help
Warm-up: remember
- How many edges has a spanning tree on 6 vertices?
- In Kruskal's algorithm, why is an edge rejected?
Answers and feedback
- edges.
- Because adding it would close a cycle with the edges already chosen.
Practise with help
1. A network has edges , , . Run Prim's algorithm from .
Hint
The cheapest edge leaving is the cheapest edge at .
Next step
At : and . Take the cheaper.
Full working
From the cheapest edge leaving is . Tree .
From the edges leaving are and . Take .
Tree: , total . Same as Kruskal gave.
2. For the same network, run Prim's algorithm from . Does the total change?
Hint
At the edges are and .
Next step
Take first.
Full working
From the cheapest edge is . Tree .
From the edges leaving are and . Take .
Tree: , total . The total is the same, and in fact it is the same tree — the order the edges were added in differs, but the set does not.
3. A network has edges , , , , , . Run Prim's algorithm from .
Hint
At : , , .
Next step
Take . Then look at edges leaving .
Full working
- : cheapest leaving is . Tree .
- : leaving edges are , , . Take .
- : leaving edges are , , (not a candidate, both ends inside). Take .
- : leaving edges are , . Take .
Tree: , total . The same tree Kruskal found, with the edges added in a different order.
4. In that run, at the step where the tree is , why is not a candidate?
Hint
Where are the two ends of ?
Next step
Both and are in the tree already.
Full working
Prim's algorithm only considers edges with one end in the tree and one end outside it. Both ends of are already in the tree, so it is not a candidate at all — it is not that it was considered and rejected, it is that it is never looked at.
If it were added, it would close the cycle , since and are already chosen. The rule excludes it automatically.
5. A network has edges , , , , . Run Prim's algorithm from , then from . Compare the two trees.
Hint
From , the cheapest edge leaving is . From , it is also .
Next step
After , from the leaving edges are , , , .
Full working
From : take . Then from take any of the four weight-5 edges, say . Then from take or ; both reach the last vertex. Total .
From : take . Then the same four weight-5 edges are available. Take , then . Total .
The totals agree at 11, but the trees differ: the first used and , the second used and — the same set here — but a different tie-breaking could give from , which is a different tree of the same weight 11. When weights are tied, Prim can return different minimum spanning trees from different starts, and the weight is the only thing guaranteed.
6. Explain why Prim's algorithm is guaranteed to produce a tree, and never a network with a cycle.
Hint
Every edge added brings in a new vertex.
Next step
Could an edge that brings in a new vertex close a cycle?
Full working
Every edge Prim's algorithm adds has one end inside the tree and one end outside. Adding it brings exactly one new vertex into the tree and increases the edge count by one.
A cycle needs all of its vertices already in the tree: to close a loop you must return to a vertex you have already left. Since each added edge reaches a vertex that was not in the tree, there is nothing to return to. So no cycle can form.
The algorithm starts with one vertex, zero edges, and adds edges to bring in the other vertices. The result is connected and has no cycle: a tree.
A question from Section 2.6
7. Kruskal's algorithm sorted the edges and considered first. Prim's algorithm from considered first. Both are correct. Explain why they can start with different edges.
Hint
What is each algorithm allowed to consider at the first step?
Next step
Kruskal may take any edge. Prim may only take one leaving the starting vertex.
Full working
Kruskal's algorithm may take any edge in the network, so its first choice is the globally cheapest edge, .
Prim's algorithm may only take an edge leaving the starting vertex. From the only edges are and , so its first choice is .
Both are correct because both rules are safe: each one takes an edge that is guaranteed to belong to some minimum spanning tree. They are different safe choices, and the algorithms converge to the same total because the minimum total is a property of the network.
8. A network is stored as an adjacency matrix. Explain why Prim's algorithm is convenient to run on it.
Hint
Where are the edges at the newest vertex?
Next step
In the row (and column) for that vertex.
Full working
When a new vertex joins the tree, the edges leaving the tree through that vertex are exactly the non-zero entries in its row of the adjacency matrix — the weights of the edges at that vertex. To find the cheapest edge leaving the tree, you scan that one row and compare against the edges already known.
So Prim's algorithm on a matrix is: add a vertex, scan its row, keep the cheapest edge found. No sorting of the whole edge list is needed, which is why it suits a network stored as a table and why it is usually the faster method on a dense network.