Leaving Certificate Applied Mathematics2.8 Shortest paths and Dijkstra's algorithm

All books

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.

  1. A network has edges AB=4AB = 4, AC=2AC = 2, BC=1BC = 1, BD=5BD = 5, CD=8CD = 8, CE=10CE = 10, DE=2DE = 2, DF=6DF = 6, EF=3EF = 3. Use Dijkstra's algorithm to find the shortest distance from AA to FF.
  2. For the same network, find the shortest distance from AA to BB.
  3. For the same network, find the shortest distance from AA to DD.
  4. For the same network, write down the shortest distance from AA to each of AA, BB, CC, DD, EE and FF. Give the sum of the six distances.
  5. Explain why Dijkstra's algorithm can safely settle the vertex with the smallest tentative distance, and never revise it afterwards.
  6. On the same network, the shortest path from AA to FF has total weight 1313, and the minimum spanning tree also has total weight 1313. Explain why these being equal is a coincidence, and give one way the two answers differ.
  7. 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 1313, along A→C→B→D→E→FA \to C \to B \to D \to E \to F, whose weights add to 2+1+5+2+3=132 + 1 + 5 + 2 + 3 = 13. The direct alternatives are longer: A→C→D→FA \to C \to D \to F is 2+8+6=162 + 8 + 6 = 16 and A→B→D→FA \to B \to D \to F is 4+5+6=154 + 5 + 6 = 15.

2. Show the answer

2. Shortest distance from A to B

Answer: The shortest distance from AA to BB is 33, along A→C→BA \to C \to B: 2+1=32 + 1 = 3. The direct edge ABAB has weight 44, 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 88, along A→C→B→DA \to C \to B \to D: 2+1+5=82 + 1 + 5 = 8. Going straight through CC and DD costs 2+8=102 + 8 = 10, so the longer-looking detour is cheaper.

4. Show the answer

4. The six distances, then their sum

Answer: A=0A = 0, C=2C = 2, B=3B = 3, D=8D = 8, E=10E = 10, F=13F = 13. The sum is 0+2+3+8+10+13=360 + 2 + 3 + 8 + 10 + 13 = 36. The distance from AA to itself is 00 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 n−1n-1 edges, while a path can have any number from 1 to n−1n-1. They also need not share a single edge. On this network the shortest path is A→C→B→D→E→FA \to C \to B \to D \to E \to F and the tree is {AC,BC,BD,DE,EF}\{AC, BC, BD, DE, EF\}, 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.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.