AQA 7517, section 4.3.6.1
Dijkstra's algorithm finds the shortest path from one starting vertex to every other vertex in a weighted graph, where the weights (distances, times or costs) are not negative.
You are expected to understand it and to be able to trace it. You are not expected to recall the steps from memory, since the question will give them to you.
Applications
- Satnav and map apps: the shortest or fastest route between places.
- Network routing: choosing the best path for data to travel between routers.
- Flight and travel planning.
- Game AI: finding a route for a character across a map.
The idea
Keep a distance for each vertex, meaning the shortest distance found so far from the start. At the beginning, the start is 0 and every other vertex is infinity (unknown). Then repeatedly:
- Pick the unvisited vertex with the smallest distance and mark it as visited. Its distance is now final.
- Look at each of its neighbours. If going through this vertex gives a shorter distance than the neighbour currently has, update it (and remember the vertex you came from).
Stop when every vertex has been visited.
The example graph
The graph is undirected, with these weighted edges:
As an adjacency list with weights:
graph = {
"A": [("B", 4), ("C", 2)],
"B": [("A", 4), ("C", 1), ("D", 5)],
"C": [("A", 2), ("B", 1), ("D", 8), ("E", 10)],
"D": [("B", 5), ("C", 8), ("E", 2)],
"E": [("C", 10), ("D", 2)],
}
Trace from A
The table shows the distance to each vertex after each step. The vertex chosen in that step has its distance finalised.
| Step | Visit | A | B | C | D | E |
|---|---|---|---|---|---|---|
| Start | 0 | inf | inf | inf | inf | |
| 1 | A (0) | 0 | 4 | 2 | inf | inf |
| 2 | C (2) | 0 | 3 | 2 | 10 | 12 |
| 3 | B (3) | 0 | 3 | 2 | 8 | 12 |
| 4 | D (8) | 0 | 3 | 2 | 8 | 10 |
| 5 | E (10) | 0 | 3 | 2 | 8 | 10 |
How each step works:
- Visit A. Its neighbours are B (distance ) and C (), both better than infinity.
- Visit C, the smallest unvisited (2). Its neighbours: B via C is , which beats 4, so B becomes 3. D via C is , and E via C is .
- Visit B (3). D via B is , which beats 10, so D becomes 8.
- Visit D (8). E via D is , which beats 12, so E becomes 10.
- Visit E. Nothing left to improve.
Shortest distances from A: B = 3, C = 2, D = 8, E = 10.
Finding the route
Each time a distance is updated, record where it came from. Working back from E: E came from D, D came from B, B came from C, and C came from A. So the shortest route to E is A, C, B, D, E, with a total of .
Notice that the shortest route to B isn't the direct edge A to B (4). It is the longer-looking route via C (3). This is why you can't simply choose the shortest edge each time.
In Python
def dijkstra(graph, start):
dist = {v: float("inf") for v in graph}
dist[start] = 0
unvisited = set(graph)
while unvisited:
v = min(unvisited, key=lambda x: dist[x])
unvisited.remove(v)
for nbr, w in graph[v]:
if dist[v] + w < dist[nbr]:
dist[nbr] = dist[v] + w
return dist
print(dijkstra(graph, "A"))
# {'A': 0, 'B': 3, 'C': 2, 'D': 8, 'E': 10}
A real implementation usually uses a priority queue to find the closest unvisited vertex more quickly, but the logic is the same.
Worked example
Task: use Dijkstra's algorithm to find the shortest distance from S to T in a graph with edges S-A 3, S-B 6, A-B 2, A-T 8, B-T 3.
| Step | Visit | S | A | B | T |
|---|---|---|---|---|---|
| Start | 0 | inf | inf | inf | |
| 1 | S (0) | 0 | 3 | 6 | inf |
| 2 | A (3) | 0 | 3 | 5 | 11 |
| 3 | B (5) | 0 | 3 | 5 | 8 |
| 4 | T (8) | 0 | 3 | 5 | 8 |
At step 2, B improves from 6 to and T becomes . At step 3, T improves to . The shortest distance from S to T is 8, along S, A, B, T.
Exam tip: in a trace, always choose the unvisited vertex with the smallest current distance next, and only update a neighbour if the new distance is smaller. Show the working for each update, such as ", so update B".
Check yourself
What does Dijkstra's algorithm find?
The shortest path from one starting vertex to all the other vertices in a weighted graph with non-negative weights.
At each step, which vertex does the algorithm visit next?
The unvisited vertex with the smallest distance found so far.
Give two applications of shortest path algorithms
Any two of: satnav and route planning; routing data across a network; travel planning; finding routes for characters in games.
Why is the shortest route to B in the example not the direct edge A to B?
The direct edge costs 4, but going via C costs , which is shorter.