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

All books

2.7 Minimum spanning trees: Prim's algorithm — Learn

Prerequisites: Section 2.6 — Kruskal's algorithm.

Objectives: find a minimum spanning tree with Prim's algorithm; compare it with Kruskal's; say why the starting vertex does not change the total weight.

By the end of this section you have a second way to find the same answer, and a check on the first.

Growing one tree

Kruskal's algorithm can build several separate pieces before it joins them: it takes the cheapest edge anywhere. Robert Prim's method is different. It keeps one connected tree and grows it outwards, one vertex at a time.

Prim's algorithm.

  1. Choose any vertex to start. It is the tree.
  2. Find the cheapest edge that leaves the tree — one end in the tree, one end outside.
  3. Add it, bringing in a new vertex.
  4. Repeat until every vertex is in the tree.

The tree is connected at every step, which is the visible difference from Kruskal.

Example 1. Find a minimum spanning tree by Prim's algorithm, starting from AA.

  • AB=4AB = 4, AC=2AC = 2, BC=1BC = 1, BD=5BD = 5, CD=8CD = 8, CE=10CE = 10, DE=2DE = 2, DF=6DF = 6, EF=3EF = 3
Tree so far Cheapest edge leaving it Weight
{A}\{A\} AC=2AC = 2 2
{A,C}\{A, C\} CB=1CB = 1 1
{A,C,B}\{A, C, B\} BD=5BD = 5 5
{A,C,B,D}\{A, C, B, D\} DE=2DE = 2 2
{A,C,B,D,E}\{A, C, B, D, E\} EF=3EF = 3 3

All 6 vertices are in the tree, so the algorithm stops. The edges are

{AC,  CB,  BD,  DE,  EF}\{AC,\; CB,\; BD,\; DE,\; EF\}

with total weight

2+1+5+2+3=132 + 1 + 5 + 2 + 3 = 13

the same total Kruskal found.

Why ABAB is not considered

Look at the step where the tree is {A,C,B}\{A, C, B\}. The edge AB=4AB = 4 is cheap, but it is not a candidate: both of its ends are already in the tree. Prim's algorithm only ever takes an edge that leaves the tree, so an edge between two tree vertices is never looked at.

That is not an omission. Adding ABAB would close the cycle A−B−C−AA-B-C-A, which a tree does not have. So the rule "take the cheapest edge leaving the tree" carries the no-cycle condition inside it — there is no separate check to make.

Figure 1

Predict, then test. Run Prim from AA and write down the edge chosen at each step. Then start again from DD and compare. The totals should agree — write down why the starting vertex cannot change the total. Finally switch to Kruskal and run it on the same network. Notice where the two algorithms make different choices and still agree at the end.

Which algorithm, and when

Both always find a minimum spanning tree, and on a network with distinct weights they find the same one. So the choice is about the work, not the answer.

For an exam question, either is accepted. Write down which you are using, and be consistent.

Progress check

Back to the chapter problem. Run Prim's algorithm on your seven-room network, starting from whichever room you like, and find the total length of cable.

Then start again from a different room. If the two totals agree, your arithmetic is almost certainly right, and you have found the cheapest cabling for the building. If they disagree, one run has a mistake in it — find it before you trust either.

You should now be able to

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.