Decision Mathematics 1 (Paper 3D or 4D), topic 2 (Algorithms on graphs), spec point 2.2
Dijkstra's algorithm
Finds the shortest route from one start vertex to every other vertex. Each vertex box shows its order of labelling, its final value and its working values.
- Give the start vertex final value 0 and order label 1.
- For each vertex joined to the vertex just labelled, work out (final value of that vertex + edge weight). Write it as a working value if it is smaller than any working value already there.
- Choose the vertex with the smallest working value that has no final value. Give it that final value and the next order label.
- Repeat until the destination has a final value.
- To find the route, trace back from the destination: an edge is on the route if the difference between the final values at its ends equals its weight.
Worked example 1
Using the network in the table (same as for minimum spanning trees), from A:
| Edge | AB | AC | AD | BC | BE | CD | CE | DE | DF | EF |
|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 7 | 5 | 9 | 3 | 6 | 4 | 8 | 5 | 11 | 6 |
| Vertex | A | C | B | D | E | F |
|---|---|---|---|---|---|---|
| Order labelled | 1 | 2 | 3 | 4 | 5 | 6 |
| Final value | 0 | 5 | 7 | 9 | 13 | 19 |
Tracing back from F: (EF), (BE), (AB). Shortest route A-B-E-F, length 19. At E, the edge CE also works (), so A-C-E-F is an equally short route.
Floyd's algorithm
Finds the shortest distance between every pair of vertices. Keep a distance table and a route table.
- Start with the direct distances ( where there is no edge). In the route table, every entry in a column is that column's letter.
- For iteration , highlight row and column . For every other entry, if (row entry in column ) + (column entry in row ) is smaller than the current entry, replace it, and put the letter of vertex in the same place in the route table.
- Do one iteration for each vertex in turn: the first iteration uses the first row and column, the second uses the second, and so on.
Worked example 2
Edges: AB 4, BC 3, CD 2, AD 9. After all four iterations:
| Distance | A | B | C | D |
|---|---|---|---|---|
| A | – | 4 | 7 | 9 |
| B | 4 | – | 3 | 5 |
| C | 7 | 3 | – | 2 |
| D | 9 | 5 | 2 | – |
| Route | A | B | C | D |
|---|---|---|---|---|
| A | – | B | B | D |
| B | A | – | C | C |
| C | B | B | – | D |
| D | A | C | C | – |
For example, B to D is 5: the route table says go via C, and C to D is direct (the entry is D), so the route is B-C-D.
Reading a route
Look up the entry for the start row and finish column. If it is the finish letter, the edge is direct. Otherwise the route passes through that vertex: find each half of the route in the same way.
Common mistake: in Dijkstra's algorithm, giving a final value to a vertex that is not the smallest working value, or changing a final value later. Final values never change.
Exam tip: show every working value, crossed out (not rubbed out) when replaced. In Floyd's algorithm, show the tables after each iteration if asked.
Check yourself
What does Dijkstra’s algorithm find?
The shortest route from one vertex to every other vertex.
How many iterations does Floyd’s algorithm use for 5 vertices?
.
In Floyd’s algorithm, what goes in the route table when an entry improves at iteration ?
The letter of vertex .