Leaving Certificate Applied Mathematics2.7 Minimum spanning trees: Prim's algorithm

All books

2.7 Minimum spanning trees: Prim's algorithm — Practise with help

Warm-up: remember

  1. How many edges has a spanning tree on 6 vertices?
  2. In Kruskal's algorithm, why is an edge rejected?
Answers and feedback
  1. 6−1=56 - 1 = 5 edges.
  2. Because adding it would close a cycle with the edges already chosen.

Practise with help

1. A network has edges PQ=3PQ = 3, PR=6PR = 6, QR=4QR = 4. Run Prim's algorithm from PP.

Hint

The cheapest edge leaving {P}\{P\} is the cheapest edge at PP.

Next step

At PP: PQ=3PQ = 3 and PR=6PR = 6. Take the cheaper.

Full working

From {P}\{P\} the cheapest edge leaving is PQ=3PQ = 3. Tree {P,Q}\{P, Q\}.

From {P,Q}\{P, Q\} the edges leaving are PR=6PR = 6 and QR=4QR = 4. Take QR=4QR = 4.

Tree: {PQ,QR}\{PQ, QR\}, total 3+4=73 + 4 = 7. Same as Kruskal gave.

2. For the same network, run Prim's algorithm from RR. Does the total change?

Hint

At RR the edges are PR=6PR = 6 and QR=4QR = 4.

Next step

Take QR=4QR = 4 first.

Full working

From {R}\{R\} the cheapest edge is QR=4QR = 4. Tree {R,Q}\{R, Q\}.

From {R,Q}\{R, Q\} the edges leaving are PQ=3PQ = 3 and PR=6PR = 6. Take PQ=3PQ = 3.

Tree: {QR,PQ}\{QR, PQ\}, total 4+3=74 + 3 = 7. 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 AB=1AB = 1, BC=2BC = 2, CD=3CD = 3, DE=4DE = 4, EA=5EA = 5, AC=6AC = 6. Run Prim's algorithm from CC.

Hint

At CC: BC=2BC = 2, CD=3CD = 3, AC=6AC = 6.

Next step

Take BC=2BC = 2. Then look at edges leaving {C,B}\{C, B\}.

Full working
  • {C}\{C\}: cheapest leaving is BC=2BC = 2. Tree {C,B}\{C, B\}.
  • {C,B}\{C, B\}: leaving edges are AB=1AB = 1, CD=3CD = 3, AC=6AC = 6. Take AB=1AB = 1.
  • {C,B,A}\{C, B, A\}: leaving edges are CD=3CD = 3, EA=5EA = 5, AC=6AC = 6 (not a candidate, both ends inside). Take CD=3CD = 3.
  • {C,B,A,D}\{C, B, A, D\}: leaving edges are DE=4DE = 4, EA=5EA = 5. Take DE=4DE = 4.

Tree: {BC,AB,CD,DE}\{BC, AB, CD, DE\}, total 2+1+3+4=102 + 1 + 3 + 4 = 10. The same tree Kruskal found, with the edges added in a different order.

4. In that run, at the step where the tree is {C,B,A}\{C, B, A\}, why is AC=6AC = 6 not a candidate?

Hint

Where are the two ends of ACAC?

Next step

Both AA and CC 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 ACAC 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 A−C−B−AA-C-B-A, since ABAB and BCBC are already chosen. The rule excludes it automatically.

5. A network has edges AB=5AB = 5, BC=5BC = 5, CD=5CD = 5, DA=5DA = 5, AC=1AC = 1. Run Prim's algorithm from AA, then from CC. Compare the two trees.

Hint

From AA, the cheapest edge leaving is AC=1AC = 1. From CC, it is also AC=1AC = 1.

Next step

After ACAC, from {A,C}\{A, C\} the leaving edges are AB=5AB = 5, AD=5AD = 5, CB=5CB = 5, CD=5CD = 5.

Full working

From AA: take AC=1AC = 1. Then from {A,C}\{A, C\} take any of the four weight-5 edges, say AB=5AB = 5. Then from {A,C,B}\{A, C, B\} take CD=5CD = 5 or AD=5AD = 5; both reach the last vertex. Total =1+5+5=11= 1 + 5 + 5 = 11.

From CC: take AC=1AC = 1. Then the same four weight-5 edges are available. Take CD=5CD = 5, then AB=5AB = 5. Total =1+5+5=11= 1 + 5 + 5 = 11.

The totals agree at 11, but the trees differ: the first used ABAB and CDCD, the second used CDCD and ABAB — the same set here — but a different tie-breaking could give {AC,AB,AD}\{AC, AB, AD\} from AA, 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 n−1n - 1 edges to bring in the other n−1n - 1 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 BC=1BC = 1 first. Prim's algorithm from AA considered AC=2AC = 2 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, BC=1BC = 1.

Prim's algorithm may only take an edge leaving the starting vertex. From AA the only edges are AB=4AB = 4 and AC=2AC = 2, so its first choice is AC=2AC = 2.

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.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.