2.8 Shortest paths and Dijkstra's algorithm — Practise
Work independently. The questions and answers are shown together at the end.
Show the settled distances in a table, and trace the route back from the destination. Check the route's weights add to the distance you found.
- A network has edges , , , , , , , , . Use Dijkstra's algorithm to find the shortest distance from to .
- For the same network, find the shortest distance from to .
- For the same network, find the shortest distance from to .
- For the same network, write down the shortest distance from to each of , , , , and . Give the sum of the six distances.
- Explain why Dijkstra's algorithm can safely settle the vertex with the smallest tentative distance, and never revise it afterwards.
- On the same network, the shortest path from to has total weight , and the minimum spanning tree also has total weight . Explain why these being equal is a coincidence, and give one way the two answers differ.
- H Both Dijkstra's algorithm and Kruskal's algorithm repeatedly take a cheapest something. State what each takes, and why taking the cheapest is right for one and wrong for the other.
Answers and concise solutions
1. Show the answer
1. Shortest distance from A to F
Answer: The shortest distance is , along , whose weights add to . The direct alternatives are longer: is and is .
2. Show the answer
2. Shortest distance from A to B
Answer: The shortest distance from to is , along : . The direct edge has weight , which is longer. This is worth noticing: the shortest route between two places is not always the direct one, and a shortest-path algorithm is needed precisely because you cannot tell by looking.
3. Show the answer
3. Shortest distance from A to D
Answer: The shortest distance is , along : . Going straight through and costs , so the longer-looking detour is cheaper.
4. Show the answer
4. The six distances, then their sum
Answer: , , , , , . The sum is . The distance from to itself is by definition.
5. Show the answer
5. Why the smallest can be settled
Answer:
The unsettled vertex with the smallest tentative distance has the least distance of any unsettled vertex. Any other route to it would have to leave the settled set first, and that step already costs at least as much as the smallest distance, before even reaching the vertex. Since every weight is positive, the rest of that route only adds more. So no cheaper route can exist, and the distance is final. This is why the algorithm needs every weight to be non-negative. A negative weight would let a longer-looking route become cheaper, and the argument would break.
6. Show the answer
6. Why it is a coincidence
Answer:
They answer different questions. The minimum spanning tree is the cheapest set of edges that connects every vertex; the shortest path is the cheapest route between two vertices. One uses 5 edges and the other 5 here, but on another network the counts differ — a spanning tree always has edges, while a path can have any number from 1 to . They also need not share a single edge. On this network the shortest path is and the tree is , which happen to agree; add one cheaper edge elsewhere and the two answers separate. The equality of the totals here is a coincidence of this network, not a rule.
7. Show the answer
7. What each takes
Answer:
Dijkstra takes the unsettled vertex with the smallest known distance from the start, and settles it. Kruskal takes the cheapest edge anywhere that does not close a cycle. Each rule is right for its own question and wrong for the other. Kruskal's cheapest edge anywhere would not find a shortest path from one vertex, because the cheapest edge may point away from the destination. Dijkstra's settling of the nearest unsettled vertex would not build a spanning tree, because it never considers the cheapest edge between two vertices that are both already settled — and those edges are exactly what a tree is made of.