2.8 Shortest paths and Dijkstra's algorithm — Practise with help
Warm-up: remember
- A network has 7 vertices. How many edges has a minimum spanning tree of it?
- On the same network, how many edges could a shortest path have?
Answers and feedback
- edges.
- Anywhere from 1 (if the two vertices are joined) to 6 (if the route visits every vertex). A shortest path has no fixed length — that is the difference from a spanning tree.
Practise with help
1. A network has edges , , . Find the shortest distance from to using Dijkstra's algorithm.
Hint
There are two routes: direct, and through .
Next step
Direct: . Through : .
Full working
| Settled | Distance | Updates |
|---|---|---|
| 0 | ; | |
| 3 | ||
| 6 | done |
The shortest distance is 6, and here the direct edge is as good as the detour. Both routes cost 6, so there are two shortest paths: and .
2. A network has edges , , , , . Find the shortest distance from to .
Hint
Settle , then the nearest unsettled vertex.
Next step
From : , . Settle next.
Full working
| Settled | Distance | Updates |
|---|---|---|
| 0 | ; | |
| 1 | ; | |
| 3 | ||
| 6 | done |
The shortest distance is 6, along : . The direct route costs , and going through is cheaper.
3. In that question, at the moment is settled, 's tentative distance is 5. Why does it change to 3?
Hint
A tentative distance is a guess, and a guess can improve.
Next step
Settling at distance 1 opens a new route to : through .
Full working
When is settled, the only known route to is the direct edge , so 's tentative distance is 5. But 5 is a guess, not a fact — is not settled yet.
When is settled at distance 1, the algorithm looks at 's neighbours. The route costs , which is less than 5, so the tentative distance is replaced by 3.
That is the point of keeping tentative distances: the algorithm improves its guesses as it learns more, and only fixes a distance when it settles the vertex.
4. A student runs Dijkstra's algorithm and settles a vertex at distance 7, then later finds a route to it of total 6. What has gone wrong?
Hint
Can a settled distance ever be improved?
Next step
No — that is the guarantee the algorithm rests on.
Full working
A settled distance can never be improved, so a route of total 6 found after settling a vertex at 7 means the algorithm was not run correctly. Something has gone wrong in one of two ways:
- a tentative distance was not updated when a neighbour was settled, so the vertex was settled at 7 when its true distance was already 6; or
- the wrong vertex was settled — the smallest tentative distance was not chosen, so a vertex was settled before its distance was final.
Both are the same mistake in different clothes: settling something before it was the smallest unsettled distance.
5. On the network , , , , , , , , , find the shortest distance from to .
Hint
Run the same table as Example 1 and stop when is settled.
Next step
is updated twice: to 12 via , then to 10 via .
Full working
From Example 1 the settled distances are , , , , , .
The shortest distance from to is 10, along : .
The direct-looking route costs , so the longer way round is cheaper. This is why the algorithm is needed — you cannot tell by looking.
6. Explain why Dijkstra's algorithm cannot be used on a network with a negative edge weight.
Hint
The algorithm settles the nearest vertex because going further always costs more.
Next step
With a negative weight, is going further still more expensive?
Full working
The algorithm settles the nearest unsettled vertex because any other route to it would have to pass through an unsettled vertex whose distance is already at least as large, and every extra edge adds more.
A negative edge breaks that. A route could go to a vertex that looks far away, take a negative edge, and arrive back cheaper than the vertex the algorithm already settled. The settled distance would then be wrong, and the whole guarantee fails.
Every weight in this course is a distance, cost or time, so every weight is positive, and the algorithm is safe to use.
A question from Section 2.7
7. The minimum spanning tree of this network weighs 13, and the shortest path from to also weighs 13. A student concludes they are the same thing. Explain the mistake.
Hint
What does each answer consist of?
Next step
One is a set of edges reaching every vertex; the other is a route between two.
Full working
The two answers happen to share the same total here, but they are different objects. The minimum spanning tree is a set of 5 edges reaching every vertex. The shortest path is a single route from to .
They also answer different questions: "connect everything as cheaply as possible" and "get from here to there as cheaply as possible". A route that is cheapest between two vertices need not be part of the cheapest way to connect everything, and the cheapest way to connect everything is not a route at all.
The equality of the two totals is a coincidence of this network. Change from 6 to 4 and the tree changes while the shortest path does not, or vice versa. Never use one answer for the other question.
8. A network has 6 vertices. Dijkstra's algorithm from settles them in the order with distances . Write down the shortest path from to , and explain how you found it.
Hint
Trace back from : which settled vertex updated it last?
Next step
, and .
Full working
Trace back from :
- , and the edge has weight 3, so came from ;
- , and , so came from ;
- , and , so came from ;
- , and , so came from ;
- , and , so came from .
The shortest path is . To check it, add the weights along the route: , which matches the settled distance of .