Prim's Algorithm (Edexcel A Level Further Maths: Decision Maths 1)

Revision Note

Test yourself
Naomi C

Author

Naomi C

Last updated

Introduction to Prim's Algorithm

What is Prim's algorithm?

  • Prim's algorithm is another method of finding the minimum spanning tree in a network
  • It can be used when the information for a network is given as a graph or in a matrix 

Prim's Algorithm using Edges & Vertices

What are the steps in Prim’s Algorithm when using a graph?

  • Prim’s algorithm involves adding edges from vertices that are already connected to the tree.
  • Cycles are avoided by only adding edges that are not already connected at one end.
     
  • STEP 1
    Start at any vertex and choose the edge of least weight that is connected to it
     
  • STEP 2
    Choose the edge of least weight that is incident (connected) to any of the vertices already connected and does not connect to another vertex that is already in the tree
    • If there is a choice of the edges of equal weight, either can be selected
       
  • STEP 3
    Repeat STEP 2 until all of the vertices are added to the tree

Worked example

Consider the network, G, below.

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

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

  • STEP 1
    Select a starting vertex (A) and choose the edge of least weight that is connected to it
  

prim-1

AB (15)

 

  • STEP 2
    Select the next edge of least weight that is incident (connected) to either of the vertices already in the tree
 
tYwxOSlL_picture-2
AB (15), BC (13)

 

  • STEP 3
    Continue to select the edge of least weight that is incident to any of the other vertices that are already connected to the tree
prim-2
AB (15), BC (13), CE (12)
 
Remember to record the order in which the edges were added
 
prim-3
Edges added in the order: AB (15), BC (13), CE (12), BD (17)

 

b)
State the total weight of the minimum spanning tree.
 
Add up the weight of the edges in the minimum spanning tree.
 
15 + 13 + 12 + 17 = 57
 
Total weight = 57

Prim's Algorithm using a Matrix

How do you apply Prim’s algorithm to a matrix?

  • A minimum spanning tree is built up from the least weight edges that are incident to vertices already in the tree by looking at the relevant rows in the distance matrix
     
  • STEP 1
    Select any vertex to start from, cross out the values in the column associated with that vertex and label the row associated with the vertex 1
     
  • STEP 2
    Circle the lowest value in any cell along that row and add the edge to your tree, cross out the remaining values in the column of the cell that you have circled
     
  • STEP 3
    Label the row associated with the same vertex as the column in the previous STEP with the next number
     
  • STEP 4
    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
     
  • STEP 5
    Repeat STEPS 3 and 4 until all vertices have been added to the tree

  • Some versions of Prim's algorithm using a matrix will cross out rows and label columns

Examiner Tip

  • Look out for questions that ask you to minimise the cost or length etc. from a network – they are implying that they want you to find the minimum spanning tree!
  • Be careful not to confuse Prim's algorithm with the Nearest Neighbour algorithm!

Worked example

The adjacency matrix of a network, is shown below.

  A B C D E
A - 15 22 18 25
B 15 - 13 17 24
C 22 13 - 30 12
D 18 17 30 - 21
E 25 24 12 21 -

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.
 
  • STEP 1
    Start at the row for vertex A and label it 1

    Cross out the values in column A

 
prims-1  

  • STEP 2
    Circle the edge of least weight in row 1 and record which edge it is
    Delete the remaining values in column B

 
 prims-2AB (15) 
  

  • STEP 3
    Label the row for vertex B, 2
     
  • STEP 4
    Circle the edge of least weight in row 1 and row 2, remembering to record the edge
    Cross out the remaining values in the column of that circled edge

 
 prims-3AB (15), BC (13)

  

  • STEP 5
    Repeat Steps 3 and 4 until all vertices are added

  • STEP 3
    Label the row for vertex C, 3
     
  • STEP 4
    Circle the edge of least weight in rows 1 to 3, remembering to record the edge
    Delete the remaining values in column E

  
 
prims-4AB (15), BC (13), CE (12)

  

  • STEP 3
    Label the row for vertex E, 4
     
  • STEP 4
    Circle the edge of least weight in rows 1 to 4, remembering to record the edge
    Delete the remaining values in column D

 
prims-5

AB (15), BC (13), CE (12), DE (17)

 
   All vertices have been selected
   You should have a list of the order in which the edges were selected

 

prim-3
Edges added in the order: AB (15), BC (13), CE (12), BD (17)

 

b)
State the lowest cost of connecting all of the buildings to the electricity supply.
  
Add up the weights of the edges in the minimum spanning tree.
 
15 + 13 + 12 + 17 = 57
 
Total weight = 57

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

the (exam) results speak for themselves:

Did this page help you?

Naomi C

Author: Naomi C

Expertise: Maths

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.