A Level Further Mathematics · Chapter 65 · Decision Mathematics · Decision 1
Connecting every town as cheaply as possible and finding the quickest route between two of them are different problems, each with its own efficient algorithm.
A council laying cables wants to reach every village for the least cost, while a driver wants the shortest route from home to work. This chapter teaches Kruskal's and Prim's algorithms for the first problem and Dijkstra's and Floyd's algorithms for the second.
In this chapter · jump to a section
Start here
Your route through this chapter
By the end of this chapter you will be able to:
- explain what a spanning tree is and why a minimum spanning tree is useful
- apply Kruskal's algorithm, rejecting edges that would form a cycle
- apply Prim's algorithm to a drawn network and to a distance matrix
- use Dijkstra's algorithm to find the shortest route between two vertices
- trace a shortest route back through the labels and state its length
- use Floyd's algorithm to find all shortest distances and routes
Time: three sessions, about 4 hours, plus the practice ladder and the exam workshop.
You need first: graphs, networks and planarity, especially trees, cycles and distance matrices.
The big idea
The big idea
Two questions come up again and again in networks. How can every vertex be connected as cheaply as possible? How can one vertex be reached from another by the shortest route? The first asks for a tree, the second for a path.
This network is used throughout the chapter. Its minimum spanning tree has weight 15, and the shortest route from A to F has length 14. Notice that these are different answers to different questions.
Spanning trees
A spanning tree of a connected graph is a tree that includes every vertex. It connects everything using the fewest possible edges, one fewer than the number of vertices. A graph usually has many different spanning trees, with different total weights.
Minimum spanning trees
A minimum spanning tree, or minimum connector, is a spanning tree of least total weight. It models the cheapest way to link every vertex, such as laying pipes, cables or roads. It does not give the shortest route between any particular pair.
Shortest paths
A shortest path joins two chosen vertices with the least total weight. It might pass through many vertices or very few. Different pairs of vertices have different shortest paths, and these paths need not lie in the minimum spanning tree.
Greedy algorithms
Kruskal's, Prim's and Dijkstra's algorithms are all greedy: at each step they take the best immediate choice. Greedy methods are not always optimal, but for these three problems they provably are, provided the rules are followed exactly.
Efficiency
Checking every spanning tree or every route is hopeless for large networks, because the numbers grow far too quickly. The algorithms in this chapter have polynomial order, so they remain practical even for networks with thousands of vertices.
Reading a network carefully
Before applying any algorithm, list the vertices and edges and check the weights. Many errors come from misreading a weight or missing an edge. A neat list of edges in order of weight is the starting point for Kruskal's algorithm.
Choosing which algorithm to use
Read the question for the kind of answer it wants. Connecting every vertex cheaply calls for Kruskal or Prim. Getting from one vertex to another calls for Dijkstra. A table of every shortest distance calls for Floyd. Naming the right algorithm is often worth a mark.
Section 1
Spanning trees and Kruskal's algorithm
The idea behind Kruskal's algorithm
Kruskal's algorithm builds a minimum spanning tree by choosing edges in order of weight, lightest first. It accepts each edge unless that edge would create a cycle with the edges already chosen. The chosen edges may form separate pieces until the very end.
Common mistake: accepting the next lightest edge even when it makes a cycle with edges already chosen. A tree has no cycles. If both ends of an edge are already connected by chosen edges, adding it closes a cycle, so the edge must be rejected, however light it is.
Kruskal's algorithm
- Accepted so far
- BC, DE, AC, EF, BD
- Weight so far
- 15
- Minimum spanning tree
- 15
Figure 65.1 steps through Kruskal's algorithm on the chapter's network. Watch for the edge that is rejected because it would close a cycle.
The steps of Kruskal's algorithm
First, list all the edges in ascending order of weight. Then take each edge in turn. If it joins two vertices not yet connected by chosen edges, accept it; otherwise reject it. Stop when the number of accepted edges is one less than the number of vertices.
Detecting a cycle
An edge makes a cycle exactly when its two ends are already joined by a chain of chosen edges. Keep track of which vertices are connected, for example by listing the groups of joined vertices. An edge inside one group is rejected; an edge joining two groups is accepted.
Ties between edges
When two edges have equal weight, either may be considered first. Different choices can lead to different minimum spanning trees, but they all have the same total weight. Exam answers usually accept any valid order, as long as it is consistent.
When to stop
A spanning tree on vertices has exactly edges. Once that many edges have been accepted, every vertex is connected, and any further edge would make a cycle. There is no need to look at the remaining edges.
Showing your working
Write the sorted list of edges, then mark each one as accepted or rejected, with a brief reason for each rejection. Finish with the list of accepted edges and the total weight. Examiners award method marks for a clear record of every decision.
Why Kruskal's algorithm is optimal
Suppose some lighter spanning tree existed. It would have to omit an accepted edge and include a heavier one in its place. Swapping them back would reduce its weight, which is impossible. This exchange argument shows the greedy choice never goes wrong.
Proof: a tree on n vertices has n − 1 edges
Build the tree by adding vertices one at a time, each joined by one edge. Starting from one vertex with no edges,
and each new vertex adds exactly one edge, since a second edge would make a cycle, so
Hence every spanning tree on vertices has edges.
Kruskal's algorithm on a list
Kruskal's algorithm works directly from a list of edges, so it is natural when the network is given as a table of connections. It is less convenient with a distance matrix, because the edges must first be extracted and sorted.
The order of Kruskal's algorithm
Most of the work is sorting the edges. With a good sorting algorithm, this takes about operations for edges. Checking for cycles is quick with careful bookkeeping, so the algorithm is efficient even for very large networks.
Spanning forests
Partway through Kruskal's algorithm, the accepted edges usually form several separate trees, called a spanning forest. Each new accepted edge joins two of these trees into one. When only a single tree remains, the algorithm has finished.
Kruskal's algorithm by hand
On paper, write the sorted list down the page with two columns beside it, one for the decision and one for the groups of connected vertices. Updating the groups after each acceptance makes cycle checks instant and keeps the working easy to mark.
Worked example 1: Kruskal's algorithm
Use Kruskal's algorithm on the chapter's network to find a minimum spanning tree and its weight.
| Working | Reason |
|---|---|
| Sorted: BC 2, DE 2, AC 3, EF 3, AB 4, BD 5, DF 6, CD 8, CE 10 | List the edges by increasing weight |
| Accept BC, DE, AC, EF; reject AB | AB would close the cycle ABC |
| Accept BD, giving weight | Five edges now join all six vertices |
Maximum spanning trees
Occasionally a question asks for a spanning tree of greatest total weight, for example to maximise the capacity of a set of links. Kruskal's algorithm works with the edges taken in descending order instead. The cycle rule is unchanged.
Worked example 2: a second network
A network has edges AB 1, AC 2, AD 6, BD 9, BE 8, CD 3, CE 4 and DE 5. Use Kruskal's algorithm to find a minimum spanning tree.
| Working | Reason |
|---|---|
| Sorted: AB 1, AC 2, CD 3, CE 4, DE 5, AD 6, BE 8, BD 9 | Arrange the edges by weight first |
| Accept AB, AC, CD, CE | None of these makes a cycle |
| Four edges join five vertices; weight | The tree is complete after four edges |
Check yourself. How many edges does a spanning tree of a network with 10 vertices have?
Show the answer
A spanning tree on vertices has edges, so a network with 10 vertices has spanning trees with 9 edges.
Section 2
Prim's algorithm
The idea behind Prim's algorithm
Prim's algorithm grows one tree from a chosen starting vertex. At each step it adds the lightest edge that joins a vertex already in the tree to a vertex not yet in it. The tree stays connected throughout, unlike the pieces in Kruskal's algorithm.
Common mistake: extending the tree only from the most recently added vertex instead of from every vertex already in the tree. Prim's algorithm considers edges from all the vertices in the tree. Looking only at the newest vertex builds a path, which can be much heavier than the minimum spanning tree.
Prim's algorithm
- Order of joining
- A, B, C, D, E
- Tree weight
- 10
Figure 65.2 runs Prim's algorithm from a start vertex of your choice. The order of joining changes with the start, but the total weight stays the same.
The steps of Prim's algorithm
Choose any vertex to start. Look at every edge joining the tree to a vertex outside it, and add the lightest. Repeat until every vertex is in the tree. Record the order in which vertices and edges are added.
No cycle checking needed
Prim's algorithm never creates a cycle, because each new edge always joins the tree to a new vertex. This makes it simpler to apply than Kruskal's algorithm, which must check every edge for cycles.
The starting vertex
Any vertex may be used to start. Different starts give different orders of joining, and sometimes different trees when weights tie, but the total weight is always the minimum. Exam questions usually name the start vertex, so follow it exactly.
Comparing Prim and Kruskal
Both algorithms give a minimum spanning tree of the same total weight. Kruskal works with a sorted list and builds pieces that merge; Prim grows one connected tree. Prim is easier to apply to a distance matrix, as the next section shows.
Recording the order
Examiners usually ask for the order in which edges are added, not just the final tree. Write each edge as it is chosen, such as AB, AC, CD, CE. This shows that the algorithm, and not some other method, was used.
Ties in Prim's algorithm
If two edges from the tree have equal least weight, either may be chosen. The resulting tree may differ, but its weight will not. State which edge you chose so that your working can be followed.
Disconnected networks
If the network is not connected, Prim's algorithm gets stuck: eventually no edge joins the tree to the remaining vertices. Kruskal's algorithm would then produce a spanning forest instead. Check the network is connected before starting.
Prim's algorithm by hand
On a drawing, mark each vertex as it joins the tree and highlight each chosen edge. Before every step, scan all edges leaving the highlighted set, not just those near the latest vertex. A quick tick beside each considered edge prevents edges being missed.
Why Prim's algorithm is optimal
The lightest edge leaving any set of vertices always belongs to some minimum spanning tree. Prim's algorithm only ever adds such edges, so the tree it builds is always a minimum. This is the same exchange idea that justifies Kruskal's algorithm.
Worked example 3: Prim's algorithm
Use Prim's algorithm, starting at A, on the network with edges AB 1, AC 2, AD 6, BD 9, BE 8, CD 3, CE 4 and DE 5.
| Working | Reason |
|---|---|
| Add AB 1, then AC 2 | The lightest edges leaving the tree |
| Add CD 3, then CE 4 | C offers the lightest edges onwards |
| Order A, B, C, D, E; weight 10 | Every vertex has now joined the tree |
Prim's algorithm for large networks
On a computer, Prim's algorithm keeps, for each vertex outside the tree, the lightest edge joining it to the tree. After each addition only these values need updating. This gives an algorithm of quadratic order, ideal for dense networks stored as matrices.
Worked example 4: starting elsewhere
Use Prim's algorithm on the chapter's network, starting at F.
| Working | Reason |
|---|---|
| Add EF 3, then DE 2 | The lightest edges from the growing tree |
| Add BD 5, then BC 2, then AC 3 | BD is lighter than DF, CD and CE |
| Order F, E, D, B, C, A; weight 15 | The same weight as Kruskal's tree |
Check yourself. What is the weight of the tree found by Prim's algorithm on the chapter's network, starting at A?
Show the answer
Every start gives a minimum spanning tree, so the weight is 15. Starting at A, the vertices join in the order A, C, B, D, E, F.
Section 3
Prim's algorithm on a distance matrix
Why use a matrix
Large networks are often stored as distance matrices rather than drawings. Prim's algorithm adapts neatly to a matrix, because each step only needs the distances from the vertices already chosen. Computers use exactly this method.
Common mistake: forgetting to cross out the row of each newly added vertex, so an edge back into the tree is chosen and makes a cycle. Once a vertex is in the tree, no edge should lead to it again. Crossing out its row removes those entries from later choices.
The matrix method
Choose a starting vertex. Cross out its row and number its column 1. Look down every numbered column for the smallest entry not crossed out. That entry's row gives the next vertex: cross out its row and number its column with the next number.
Repeating the step
Continue looking down all the numbered columns, never just the most recent one. Each time, the smallest available entry gives the next edge and the next vertex. Stop when every column is numbered.
Reading off the tree
The circled entries are the edges of the tree, and the column numbers give the order in which vertices joined. Add the circled entries for the total weight. Record the edges in order, naming both ends of each.
Rows and columns
The matrix for an undirected network is symmetric, so rows and columns carry the same information. Working down columns and crossing out rows is a convention that keeps the bookkeeping tidy. Follow it exactly, so that examiners can check every step.
Blank entries
A blank or a dash in the matrix means there is no direct edge. Treat it as unavailable, never as zero. Choosing a blank entry as the smallest would add an edge that does not exist.
Checking against the drawing
When both a drawing and a matrix are given, check the tree on the drawing at the end. It should connect every vertex without any cycle. This quick check catches errors in reading the matrix.
Why the matrix method works
Numbered columns are the vertices already in the tree, and uncrossed rows are the vertices still outside. Looking down numbered columns in uncrossed rows finds exactly the edges from the tree to new vertices, which is Prim's rule.
Laying out the matrix in an exam
Copy the matrix neatly, leaving space above each column for its label. Cross out rows with a single line so the entries stay readable, and circle each chosen entry. The examiner then sees the order of selection at a glance.
Prim's algorithm and Kruskal's on a matrix
Kruskal's algorithm can be used on a matrix, but every entry above the diagonal must first be listed and sorted. For large matrices that is slow and error-prone, which is why Prim's algorithm is the standard choice when a network arrives as a table.
Worked example 5: Prim from a matrix
The network in the diagram has a distance matrix with rows A: –, 1, 2, 6, –; B: 1, –, –, 9, 8; C: 2, –, –, 3, 4. The remaining rows are D: 6, 9, 3, –, 5 and E: –, 8, 4, 5, –. Use Prim's algorithm from A.
| Working | Reason |
|---|---|
| Column A: smallest is 1 in row B; add AB | Start at A and cross out row A |
| Columns A, B: smallest is 2 in row C; add AC | Look down every numbered column |
| Then CD 3 and CE 4; total weight 10 | Every column is now numbered |
Matrices with missing edges
When many entries are blank, the network is sparse, and some vertices have very few connections. A vertex with only one non-blank entry must join the tree along that edge, whatever its weight. Spotting such forced edges is a useful check on the final tree.
Worked example 6: a different start
Use the same matrix, starting from E.
| Working | Reason |
|---|---|
| Column E: smallest is 4 in row C; add CE | Cross out row E first |
| Columns E, C: smallest is 2 in row A; add AC | A is the nearest vertex to the tree |
| Then AB 1 and CD 3; total weight 10 | The order is E, C, A, B, D |
Check yourself. Starting from C instead, what total weight does the matrix method give?
Show the answer
Every starting vertex gives a minimum spanning tree, so the total weight is again 10. The edges chosen are the same four, AB, AC, CD and CE.
Section 4
Dijkstra's algorithm
The shortest path problem
Dijkstra's algorithm finds the shortest route from a starting vertex to every other vertex. It works by giving vertices permanent labels in order of their distance from the start. Once a vertex has a permanent label, its shortest distance is known.
Common mistake: building a route by always taking the shortest edge from the current vertex, instead of tracing back from the destination's final label. A short first step can lead into long later steps. Only Dijkstra's labels, followed backwards, guarantee the shortest route.
Dijkstra's algorithm
- Order made permanent
- A, C, B, D, E, F
- Shortest A to E
- 11
- Route
- A, B, D, E
Figure 65.3 shows the permanent labels for the chapter's network. Choose a destination and see the shortest route traced back through them.
The labelling boxes
In exam answers, each vertex gets a box with three parts: the order in which it was made permanent, its permanent label, and its working values. Working values are tentative distances that may be improved before the label becomes permanent.
The steps
Give the start a permanent label of 0. From the most recently made permanent vertex, update the working value of each neighbour not yet permanent: if the new distance is smaller, write it down. Then make permanent the smallest working value anywhere.
Choosing the next permanent vertex
The next vertex to be made permanent has the smallest working value among all vertices not yet permanent, not just among the neighbours of the latest vertex. This rule is what guarantees that each permanent label is truly the shortest distance.
Why permanent labels are final
When a vertex has the smallest working value, any other route to it would pass through a vertex with a larger working value, and weights are never negative. So no other route can be shorter, and the label can safely be made permanent.
Proof: a permanent label is the shortest distance
Let have the smallest working value among vertices not yet permanent. Any other route to leaves the permanent set at some vertex with working value , then adds further non-negative weights, so its length satisfies
and therefore
So no route to is shorter than , and the label can be made permanent.
Negative weights
Dijkstra's algorithm assumes that every weight is zero or positive. With negative weights, a longer-looking route could later become shorter, and permanent labels could be wrong. In this course every weight is non-negative, so the algorithm always works.
When to stop
If only one destination is needed, the algorithm can stop as soon as that vertex receives a permanent label. To find shortest distances to every vertex, continue until every vertex is permanent.
Directed networks
For a directed network, only follow edges in their given direction when updating working values. Everything else is unchanged. One-way streets and flows are modelled this way.
A full labelling box
Each vertex's box has the order of labelling in the top left, the permanent label in the top right, and the working values below. Write working values in the order they arise, without crossing any out, so the examiner can see every comparison.
Common checks during the algorithm
After each new permanent label, update only the neighbours of that vertex. Before choosing the next permanent vertex, scan every box, not just the neighbours. These two habits prevent the two most common errors in Dijkstra's algorithm.
Worked example 7: Dijkstra's algorithm
Use Dijkstra's algorithm to find the shortest distance from A to F in the chapter's network.
| Working | Reason |
|---|---|
| A 0; working values C 3, B 4 | Edges leaving the start vertex |
| C 3, B 4, then D 9 and E 11 | Make the smallest working value permanent |
| F: | The smaller of the two routes in |
Worked example 8: another destination
Find the shortest distance from A to E, and the route.
| Working | Reason |
|---|---|
| E has working values 13 via C and 11 via D | Both routes into E are compared |
| Permanent label for E is 11 | The smaller working value wins |
| Route A, B, D, E: | Trace back through the labels |
Check yourself. Using the chapter's network, what is the shortest distance from A to D?
Show the answer
D receives working values 9 through B and 11 through C. The smaller, 9, becomes its permanent label, so the shortest distance is 9, along A, B, D.
Section 5
Tracing routes and checking labels
Tracing the route back
To find the route, start at the destination. Find an edge into it whose weight equals the difference between the two permanent labels. Move to that vertex and repeat until you reach the start. Read the route forwards to finish.
Common mistake: making a working value permanent before checking that it is the smallest of all the current working values. Choosing the vertex next to the latest one, rather than the smallest working value anywhere, can fix a label too early and give a distance that is too long.
The difference test
An edge lies on a shortest route exactly when the label of minus the label of equals its weight. This test makes tracing back quick and reliable.
Several shortest routes
Sometimes two different edges pass the difference test. Then there are two shortest routes of the same length. Either may be given, unless the question asks for all of them.
Checking the order of labelling
The order of permanent labels must have non-decreasing distances. If a later permanent label is smaller than an earlier one, a mistake has been made. This check catches the most common error before it spreads.
Working values that never change
Not every working value is used. A vertex may receive several working values, but only the smallest becomes permanent. Show every working value in the box, crossing out none, so examiners can see each update.
Shortest paths versus spanning trees
The routes found by Dijkstra's algorithm form a tree, called a shortest-path tree, rooted at the start. It usually differs from the minimum spanning tree. The edge AB of weight 4 is in this chapter's shortest-path tree but not in its minimum spanning tree.
Reusing labels
Once Dijkstra's algorithm has run from a start vertex, the labels give the shortest distance to every vertex. To find shortest distances from a different start, the algorithm must be run again. Floyd's algorithm, in the next section, handles every start at once.
The order of Dijkstra's algorithm
Each vertex is made permanent once, and each time every other vertex may be checked. So the work grows roughly like for vertices. This quadratic order makes Dijkstra's algorithm fast enough for road maps with very many junctions.
A worked check on a tricky step
When D is first reached, it has working value 11 through C. Later, B is made permanent and gives D a smaller value, 9. Had D been made permanent at 11, the label would have been wrong. Waiting for the smallest working value avoids this.
Worked example 9: the order of labelling
State the order in which the vertices of the chapter's network are made permanent, starting from A.
| Working | Reason |
|---|---|
| A 0, C 3, B 4 | Smallest working values come first |
| D 9, E 11, F 14 | Labels increase through the process |
| Order A, C, B, D, E, F | Matches the non-decreasing check |
Worked example 10: tracing back
Trace the shortest route from A to D using the permanent labels A 0, B 4, C 3 and D 9.
| Working | Reason |
|---|---|
| D 9 − B 4 = 5, the weight of BD | BD passes the difference test |
| D 9 − C 3 = 6, but CD weighs 8 | CD fails the difference test |
| B 4 − A 0 = 4, the weight of AB; route A, B, D | Continue back to the start |
Check yourself. Starting from C, what is the shortest distance to F in the chapter's network?
Show the answer
From C, the labels are B 2, A 3, D 7, E 9 and then F 12. So the shortest distance is 12, along the route C, B, D, E, F.
Section 6
Floyd's algorithm
All shortest distances at once
Floyd's algorithm finds the shortest distance between every pair of vertices. It works on a distance table and a route table, updating both as it allows each vertex in turn to be used as an intermediate stop.
Common mistake: reading the route table as giving the last vertex before the destination, when it gives the first vertex to travel to next. The entry in row , column says where to go first from on the way to . Follow it repeatedly to build the whole route.
The initial tables
The distance table starts as the distance matrix, with infinity where there is no direct edge. The route table starts with every entry in column equal to : to reach , go directly to .
Each iteration
In iteration , compare each distance with . If going through is shorter, replace the distance and copy the route entry into . Row and column do not change.
The number of iterations
There is one iteration for each vertex, so a network with vertices needs iterations. Each iteration checks every entry of the table, so the work grows like . Floyd's algorithm has cubic order.
Reading the final tables
After the last iteration, the distance table gives every shortest distance. To find a route, read the route table: go to , then from there to , and so on until reaching .
Floyd versus Dijkstra
Dijkstra's algorithm gives shortest distances from one start. Floyd's algorithm gives them from every start, at the cost of more work. For a single journey, Dijkstra is quicker; for a complete table of distances, Floyd is the natural choice.
Showing your working
Write out both tables after every iteration, circling or highlighting the entries that changed. Examiners award marks for each correct iteration, so neat tables matter. Leave the diagonal as zero or a dash throughout.
Symmetry as a check
For an undirected network, the final distance table should be symmetric. If and differ, an update has been missed. Route tables need not be symmetric, since the first step from each end differs.
Infinity in the tables
Use the symbol for vertices with no direct edge. Any sum involving is itself , so it can never replace a finite distance. As soon as a route is found, the infinity is replaced by a finite value in that iteration.
Why Floyd's algorithm works
After iteration , each distance is the shortest using only the first vertices as intermediate stops. The next iteration allows one more stop. After all iterations, every vertex is allowed, so every distance is the true shortest.
Worked example 11: Floyd's distances
For the network in the diagram, use Floyd's algorithm to find the shortest distance from A to D.
| Working | Reason |
|---|---|
| Iteration B: | Going through B is shorter |
| Iteration B: | A first route from A to D |
| Iteration C: | Going through C is shorter still |
Floyd on directed networks
For a directed network, the distance table need not be symmetric, because going from to may differ from going back. Floyd's algorithm works unchanged, treating each entry separately. Only the symmetry check is lost.
Using the final table
The completed distance table is often the starting point for later problems. The travelling salesman problem, for example, replaces a network by its table of shortest distances. Every pair of vertices is then joined directly by the length of its shortest route, which makes later reasoning much simpler.
Worked example 12: reading the route table
The final route table has , and . Find the route from A to D.
| Working | Reason |
|---|---|
| From A, go first to B | Read row A, column D |
| From B, go to C; from C, go to D | Follow the table step by step |
| Route A, B, C, D of length | Agrees with the distance table |
Check yourself. In the same network, what is the shortest distance from B to D?
Show the answer
Going directly costs 7, but going through C costs . So the shortest distance from B to D is 3.
Reference
Results in this chapter
| Result | What it says | When to use it | In the formula booklet? |
|---|---|---|---|
| Spanning tree has edges | Size of a tree | Stopping Kruskal or Prim | No, derive it |
| Kruskal: lightest edge without a cycle | Minimum spanning tree | Networks given as lists | No, learn it |
| Prim: lightest edge from the tree | Minimum spanning tree | Drawings and matrices | No, learn it |
| Dijkstra: smallest working value next | Shortest path from one start | Single journeys | No, learn it |
| Difference test for routes | Label difference equals weight | Tracing routes back | No, learn it |
| Floyd: compare with | All shortest distances | Complete distance tables | No, learn it |
Reasoning
Modelling task: connecting villages to broadband
The situation
A broadband company must connect five villages, A to E, to a fibre network. The possible cable routes and their costs, in thousands of pounds, are AB 1, AC 2, AD 6, BD 9, BE 8, CD 3, CE 4 and DE 5. The company wants every village connected at the least cost.
Assumptions
Each village is a vertex and each possible cable route is an edge, weighted by its cost. Signals can pass through any village, so each village only needs a connection to the network, not a direct cable to every other village.
The costs are assumed fixed and independent, so the total cost is the sum of the chosen edges. Capacity and reliability are ignored in this first model.
The model
The cheapest way to connect every village is a minimum spanning tree. Prim's algorithm from A adds AB, AC, CD and CE in turn. Kruskal's algorithm accepts the same four edges in order of cost.
Results
The minimum spanning tree costs thousand pounds. Every village is connected, with four cables linking five villages, and no cheaper set of cables can connect them all.
Evaluating the model
The model gives the cheapest network, but a tree has a weakness: if any single cable fails, some villages lose their connection. A company might pay for an extra cable to give a backup route.
The model also ignores distances travelled by the signal. Village E is reached through C, which is acceptable for broadband but would matter for a network where delay or pressure loss depends on the route length.
Your turn
The company decides to add one extra cable so that no single failure disconnects village E. Suggest which cable to add and find the new total cost.
Show a model answer
E is joined only by CE, so add the cheapest other edge at E, which is DE of cost 5. Then E has two routes into the network, and the total cost becomes thousand pounds.
Practice
Practice ladder
Rung 1: Recall and fluency
Q1. State the number of edges in a spanning tree of a network with 12 vertices.
Worked solution to Q1
A spanning tree on vertices always has edges. So a network with 12 vertices has spanning trees with 11 edges.
Q2. For the edges AB 1, AC 2, AD 6, BD 9, BE 8, CD 3, CE 4 and DE 5, list the edges Kruskal's algorithm accepts.
Worked solution to Q2
Sorting gives AB 1, AC 2, CD 3, CE 4, DE 5 and so on. The first four make no cycle, so Kruskal accepts AB, AC, CD and CE, with total weight 10.
Q3. On the same network, give the order in which Prim's algorithm adds vertices when it starts at A.
Worked solution to Q3
From A the lightest edge is AB, then AC from A, then CD and CE from C. So the vertices join in the order A, B, C, D, E.
Rung 2: Application
Q4. Apply Prim's algorithm from A to the chapter's network. Give the order in which vertices join and the total weight.
Worked solution to Q4
From A the lightest edge is AC, then BC from C, then BD from B, then DE and EF. The order is A, C, B, D, E, F, and the total weight is 15.
Q5. Use the matrix method for Prim's algorithm on the matrix of section 3, starting from E. Give the order of joining.
Worked solution to Q5
Column E gives CE 4, then AC 2 is the smallest in columns E and C, then AB 1, then CD 3. The vertices join in the order E, C, A, B, D, with total weight 10.
Q6. Determine the shortest distance from A to E in the chapter's network, and the route.
Worked solution to Q6
Dijkstra's algorithm gives permanent labels A 0, C 3, B 4, D 9 and E 11. Tracing back, is the weight of DE, and so on, giving the route A, B, D, E of length 11.
Rung 3: Analysis
Q7. Calculate the shortest distance from C to F in the chapter's network using Dijkstra's algorithm.
Worked solution to Q7
Starting from C, the permanent labels are C 0, B 2, A 3, D 7, E 9 and F 12. Tracing back from F gives the route C, B, D, E, F, of length 12.
Q8. For the network of section 6, find the shortest distance from B to D using Floyd's algorithm.
Worked solution to Q8
The direct edge BD has weight 7. In the iteration for C, is smaller, so the shortest distance from B to D is 3.
Q9. Using the route table of worked example 12, write down the full route from A to D and check its length.
Worked solution to Q9
The route table sends A to B, then B to C, then C to D. The route A, B, C, D has length , matching the distance table.
Rung 4: Stretch
Q10. Explain why always taking the shortest edge from A fails to find the shortest route to F in the chapter's network.
Worked solution to Q10
The greedy route goes A, C, B, D, E, F with length . Dijkstra's algorithm finds A, B, D, E, F with length 14, so the greedy choice of C first was a mistake.
Q11. Someone takes the five lightest edges of the chapter's network as its minimum spanning tree. Explain the error and its effect on the total.
Worked solution to Q11
The five lightest edges BC, DE, AC, EF and AB weigh 14, but AB closes the cycle ABC. They fail to connect A, B and C to D, E and F, so they do not form a spanning tree at all.
Q12. Deduce, for a start at A in the chapter's network, the order in which Dijkstra's algorithm makes vertices permanent.
Worked solution to Q12
The permanent labels in order of size are A 0, C 3, B 4, D 9, E 11 and F 14. So the order is A, C, B, D, E, F, with the labels never decreasing.
Exam practice
Exam technique and worked answer workshop
How these questions are marked
M1 marks go to applying each algorithm correctly. That means sorting and checking cycles in Kruskal, choosing from the whole tree in Prim, updating working values in Dijkstra, and comparing through each vertex in Floyd. A1 marks go to correct orders, weights and routes. B1 marks go to clear statements and explanations.
Always state the order in which edges or vertices are selected, since a correct tree without the order may lose method marks. For Dijkstra, show every working value in the boxes.
Exam question 1 (6 marks · Paper 3 · about 8 minutes)
Use Kruskal's algorithm to find a minimum spanning tree for the network in the big idea. List the edges in the order you consider them, state which are rejected and why, and give the total weight. (6)
Mark scheme for exam question 1
Edges sorted by weight M1
BC, DE, AC, EF accepted A1
AB rejected M1
Because it makes the cycle ABC B1
BD accepted A1
Total weight 15 A1
Exam question 2 (6 marks · Paper 3 · about 8 minutes)
Apply Prim's algorithm, starting at F, to the network in the big idea. State the order in which the edges are added and the weight of the tree, and comment on how this compares with Kruskal's algorithm. (6)
Mark scheme for exam question 2
Starts at F with EF M1
Then DE A1
Then BD A1
Then BC and AC A1
Total weight 15 A1
Same total as Kruskal's algorithm B1
Exam question 3 (6 marks · Paper 3 · about 8 minutes)
The distance matrix in section 3 has rows A: –, 1, 2, 6, –; B: 1, –, –, 9, 8; C: 2, –, –, 3, 4; D: 6, 9, 3, –, 5; E: –, 8, 4, 5, –. Starting from A, use the matrix form of Prim's algorithm to find a minimum spanning tree, stating the order of selection. (6)
Mark scheme for exam question 3
Crosses out row A and labels column A M1
AB first A1
Looks down all labelled columns M1
AC then CD A1
CE last A1
Total weight 10 A1
Exam question 4 (7 marks · Paper 3 · about 10 minutes)
Use Dijkstra's algorithm to find the shortest route from A to F in the network in the big idea. Show the working values at each vertex, state the order of labelling, and give the route and its length. (7)
Mark scheme for exam question 4
Labels A 0 and working values C 3, B 4 M1
C, then B made permanent A1
D 9 A1
E 11 A1
F 14 A1
Traces back using the difference test M1
Route A, B, D, E, F A1
Exam question 5 (5 marks · Paper 3 · about 7 minutes)
A student finds a route from A to F in the network in the big idea by always taking the shortest edge to an unvisited vertex. Find the length of the student's route, and explain why Dijkstra's algorithm gives a better answer. (5)
Mark scheme for exam question 5
Student's route A, C, B, D, E, F M1
Length 15 A1
Dijkstra's length 14 A1
Greedy steps ignore later costs B1
Dijkstra compares all working values B1
Exam question 6 (7 marks · Paper 3 · about 10 minutes)
The network in section 6 has edges AB 3, AC 8, BC 2, BD 7 and CD 1. Apply Floyd's algorithm to find the shortest distances from A to every other vertex, and use the final route table to find the route from A to D. (7)
Mark scheme for exam question 6
Initial tables with infinity for AD B1
Iteration B updates AC to 5 M1 A1
Iteration B gives AD 10 A1
Iteration C updates AD to 6 A1
Route table entry for A to D is B M1
Route A, B, C, D A1
Exam question 7 (5 marks · Paper 3 · about 7 minutes)
Explain why every spanning tree of the network in the big idea has exactly five edges, and why Prim's algorithm never needs to check for cycles. (5)
Mark scheme for exam question 7
Six vertices B1
A tree on vertices has edges M1
So 5 edges A1
Each Prim edge joins the tree to a new vertex B1
So no cycle can form B1
Exam question 8 (6 marks · Paper 3 · about 8 minutes)
Find the shortest distance from C to F in the network in the big idea using Dijkstra's algorithm, giving the order in which vertices become permanent and the route. (6)
Mark scheme for exam question 8
Starts with C 0 M1
B 2 then A 3 A1
D 7 A1
E 9 A1
F 12 A1
Route C, B, D, E, F A1
Annotated model answer: exam question 4
Give A the permanent label 0. Its neighbours get working values C 3 and B 4. M1. The smallest working value, C 3, becomes permanent, then B 4. A1. From B, D gets 9; from C, E gets 13. D 9 becomes permanent, A1, and gives E 11 and F 15.
E 11 becomes permanent, A1, and gives F , which becomes permanent. A1. Tracing back, is EF, is DE, is BD and is AB. M1. The route is A, B, D, E, F, of length 14. A1.
Annotated model answer: exam question 6
The initial distance table has , since there is no direct edge. B1. In iteration B, becomes , M1 A1, and becomes . A1. In iteration C, becomes . A1.
The route table has , M1, then and . The route is A, B, C, D. A1.
Connections
Synoptic link
Graphs, networks and planarity introduced trees, cycles and distance matrices, which every algorithm here relies on. Algorithms, sorting and bin packing introduced the order of an algorithm, used here to compare Kruskal, Dijkstra and Floyd.
Route inspection will use Dijkstra's shortest paths to pair up odd vertices. The travelling salesman problem will use minimum spanning trees to find lower bounds for the length of a tour.
Synoptic question 1. Explain why the shortest route between two vertices need not lie within the minimum spanning tree, using the chapter's network.
Answer to synoptic question 1
The minimum spanning tree joins A to B through C, a route of length . The direct edge AB of length 4 is shorter but is not in the tree, so shortest routes and minimum spanning trees answer different questions.
Recap
Recap and next step
- A spanning tree on vertices has edges; a minimum spanning tree has least total weight.
- Kruskal's algorithm takes edges in order of weight, rejecting any that make a cycle.
- Prim's algorithm grows one tree, adding the lightest edge from any vertex already in it.
- On a matrix, Prim's algorithm crosses out rows and looks down every labelled column.
- Dijkstra's algorithm makes the smallest working value permanent each time, then traces routes back.
- Floyd's algorithm finds all shortest distances by allowing each vertex in turn as an intermediate stop.
Next chapter: Route inspection, which finds the shortest closed route travelling along every edge of a network.
For your exam board
This chapter covers Pearson Edexcel A Level Further Mathematics (9FM0) Decision Mathematics 1 section 2: Kruskal's and Prim's algorithms, including the matrix form of Prim's algorithm, Dijkstra's algorithm and Floyd's algorithm. It is assessed on Paper 3D, Decision Mathematics 1. Similar content appears in AQA (7367) and OCR (H245) discrete options.