Minimum Spanning Trees (Prim's Algorithm) (DP IB Applications & Interpretation (AI)): Revision Note

Prim's algorithm

How can I use Prim’s algorithm to find a minimum spanning tree?

  • STEP 1
    Start at any vertex and choose the nearest adjacent vertex

  • STEP 2
    Choose the nearest adjacent vertex to any of the currently selected vertices

    • You do not need to only look at the last selected vertex

    • You can look at all the selected vertices

  • STEP 3
    Repeat step 2 until all the vertices are added to the tree

Examiner Tips and Tricks

Make sure you state the order of your selected vertices. The question might ask you to state the edges.

Worked Example

Consider the weighted graph below.

3-10-3-ib-ai-hl-minimum-spanning-trees-we-2

a) Using Prim’s algorithm, find the minimum spanning tree.

3-10-3-ib-ai-hl-minimum-spanning-trees-w2a-solution

b) State the total weight of the minimum spanning tree.

3-10-3-ib-ai-hl-minimum-spanning-trees-w2b-solution

Prim's algorithm using a matrix

How do I apply Prim’s algorithm to a weighted adjacency table?

  • STEP 1
    Select any vertex to start from

    • Cross out the values in the column associated with that vertex

    • Label the row associated with the vertex 1

  • STEP 2
    Circle the lowest value in any cell along that row and add the corresponding edge to your tree

    • Cross out the remaining values in the column of the cell that you have circled

    • Label the row associated with the last selected vertex with the next number

  • STEP 3
    Circle the lowest value in any cell along any of the rows that have been labelled and add the edge to your tree

    • Cross out the remaining values in the column of the cell that you have circled

    • Label the row associated with the last selected vertex with the next number

  • STEP 4
    Repeat step 3 until all rows have been labelled and all vertices have been added to the tree

    • The labels tell you the order in which you selected the vertices

Which algorithm is better for finding minimum spanning trees?

  • Kruskal’s algorithm can be used when the information is in graph form whereas Prim’s algorithm can be used in either graph or matrix form.

  • Prim’s algorithm is sometimes considered to be more efficient than Kruskal’s algorithm because

    • the edges do not need to be ordered at the start and

    • it does not rely on checking for cycles at each step

  • An exam question will usually specify which method should be used, otherwise you have the choice

  • If you are asked to find the minimum spanning tree and the information given in the question is in the form of a table, you should use Prim’s algorithm

Examiner Tips and Tricks

  • Look out for questions that ask you to minimise the cost or length etc. from a weighted graph – they are implying that they want you to find the minimum spanning tree!

Worked Example

Celeste is building a model city incorporating 6 main buildings that need to be connected to an electrical supply.

Each vertex listed in the table below represents a building and the weighting of each edge is the cost in USD of creating a link to the electrical supply between the given vertices.

 

A

B

C

D

E

F

A

-

4

9

8

11

3

B

4

-

13

2

5

12

C

9

13

-

7

1

4

D

8

2

7

-

10

3

E

11

5

1

10

-

15

F

3

12

4

3

15

-

Celeste wants to find the lowest cost solution that links all 6 buildings up to the electrical supply.

a) Starting from vertex A, use Prim’s algorithm on the table to find and draw the minimum spanning tree. Show each step of the process clearly.

3-10-3-ib-ai-hl-minimum-spanning-trees-w3ai-solution
3-10-3-ib-ai-hl-minimum-spanning-trees-w3aii-solution

b) State the lowest cost of connecting all of the buildings to the electricity supply.

3-10-3-ib-ai-hl-minimum-spanning-trees-w3b-solution

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Naomi C

Author: Naomi C

Expertise: Maths Content Creator

Naomi graduated from Durham University in 2007 with a Masters degree in Civil Engineering. She has taught Mathematics in the UK, Malaysia and Switzerland covering GCSE, IGCSE, A-Level and IB. She particularly enjoys applying Mathematics to real life and endeavours to bring creativity to the content she creates.

Dan Finlay

Reviewer: Dan Finlay

Expertise: Maths Subject Lead

Dan graduated from the University of Oxford with a First class degree in mathematics. As well as teaching maths for over 8 years, Dan has marked a range of exams for Edexcel, tutored students and taught A Level Accounting. Dan has a keen interest in statistics and probability and their real-life applications.