Prim's Algorithm on a Distance Matrix and Spanning Tree Problems | AQA A Level Further Maths notes

Free preview: Prim's Algorithm on a Distance Matrix and Spanning Tree Problems

Networks as Tables: The Distance Matrix

Year 1 (AS) for OCR B (MEI). For the other boards, check your own specification for which parts are AS and which are A level.

Let's start with why a matrix version is useful. Computers store networks as tables, not drawings, and big networks are too messy to draw: imagine 50 towns with every pair joined. Prim's algorithm (from the previous note) works neatly on a table, so let's learn to run it there.

  • A distance matrix lists the weight of the edge between each pair of vertices, with a dash where there is no edge.
  • It is symmetric: the entry in row B, column C equals the entry in row C, column B, because the cable from B to C is the same cable as from C to B.
  • Here is the cable network from the previous note as a matrix.

Network with vertices A to G. Edges: AB 13, AC 10, BC 7, BD 16, BE 12, CE 15, CF 19, DE 8, DF 9, EF 6, DG 14, FG 11

ABCDEFG
A–1310––––
B13–71612––
C107––1519–
D–16––8914
E–12158–6–
F––1996–11
G–––14–11–

How to read it

  • Row B lists every edge at B: BA 13, BC 7, BD 16, BE 12.
  • Column B lists the same numbers again, because the matrix is symmetric.
  • The dashes on the diagonal mean a vertex is not joined to itself.

Prim's algorithm in matrix form: the recipe

  1. Choose a starting vertex. Write 1 above its column and cross out its row. Why: the column holds the edges leaving this vertex; the row is crossed out so nothing can come back into it.
  2. Look down every numbered column and find the smallest entry that has not been crossed out. Circle it. Why: these are exactly the edges from the tree to a vertex not yet in the tree.
  3. The circled entry's row tells you the new vertex. Number its column with the next number, and cross out its row. Why: the new vertex is now in the tree.
  4. Repeat until every column is numbered. The circled entries are the edges of the minimum spanning tree.

💡 In plain words

  • Numbered columns are the vertices already in the tree. Crossed-out rows are the vertices already in the tree too.
  • So an entry that is in a numbered column but not a crossed-out row joins a tree vertex to a new vertex: exactly the edges Prim looks at.

✏️ YOUR TURN 1

Try this first:

Use the matrix above. (a) Which entries would you search after numbering only column A? (b) Which vertex joins first, and by which edge?

Answer:

  • (a) Column A, with row A crossed out: entries 13 (row B) and 10 (row C). The dashes are not edges.
  • (b) The smallest is 10 in row C, so C joins by edge AC.

Prim's Matrix Method Step by Step

Start at A.

StepColumns numbered (in the tree)Smallest entry in those columns, in a row not crossed outEdge added
1A10 (column A, row C)AC
2A, C7 (column C, row B)CB
3A, C, B12 (column B, row E)BE
4A, C, B, E6 (column E, row F)EF
5A, C, B, E, F8 (column E, row D)ED
6A, C, B, E, F, D11 (column F, row G)FG
  • Order of numbering: A 1, C 2, B 3, E 4, F 5, D 6, G 7.
  • Edges: AC, CB, BE, EF, ED, FG. Total 10+7+12+6+8+11=54, the same as on the network.
  • Look at step 5: column F has 9 (row D) and 11 (row G), column E has 8 (row D). Row C's 19 in column F is crossed out because C is already in the tree.

Diagram: Columns A, C, B, E and F are numbered (blue): those vertices are in the tree.

What the picture shows

  • Columns A, C, B, E and F are numbered (blue): those vertices are in the tree.
  • Rows of tree vertices are crossed out, so only rows D and G are searched.
  • The entries left in the numbered columns are 16, 8, 9 and 11 (orange). The smallest is 8, in column E, row D, so add ED.

Unlock the full note: start 7 days free.

Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.

Log in · Subscribe · Start free trial

All notes in Minimum Spanning Trees

  1. Kruskal's and Prim's Algorithms on a Network
  2. Prim's Algorithm on a Distance Matrix and Spanning Tree Problems

What's inside for this topic

2 teacher notes, 60 practice questions, 41 flashcards, 2 quizzes.

Get the full notes, every practice question, flashcards, quizzes and mock papers.

Start your free 7-day trial

Questions? Chat with us on WhatsApp · Try one live lesson

Previous note: Kruskal's and Prim's Algorithms on a Network

Prim's Algorithm on a Distance Matrix and Spanning Tree Problems on other exam boards

All AQA A Level Further Maths topics · A Level Further Maths