Skip to content

Dijkstra's shortest paths

networks

Level 2 of 3

Also called Dijkstra's algorithm and Single-source shortest paths.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

The search runs from A to G on this map. What distance does it settle G at?

5222451A0BCEFG
the answer, and why

the answer is D

The shortest way is A, E, F, G, which is 2 + 2 + 5, so 9.

if you chose another

  • That is A, B, C, G, 5 + 4 + 1, longer than 9.
  • That adds only A-E and F-G, leaving out E-F.
  • That is A, E, F, B, C, G, a longer way round.

question 2

On this map, from A to H, which place is settled fourth?

61113162A0BCEFGH
the answer, and why

the answer is B

A at 0, E at 1 and F at 2 come first. Through F, place B is 3, beating its 6.

if you chose another

  • Place C is 6 away, after place B.
  • Place F, at 2, is settled third.
  • Place G is 7 away, after place C.

question 3

Which is true of Dijkstra's method on a map of nn places with every pair joined?

the answer, and why

the answer is B

Round 1 weighs the start alone. Then at most all the others wait, one fewer each round. Section 4's question 3 adds them up.

this tests where the formula comes from, on the learn page

if you chose another

  • With every pair joined, mm grows like n2n^2, so the heap's ceiling grows faster.
  • The end settled second skips the road, so each road is looked along at most once.
  • That is a line's count. Settling every place here weighs up more, from 3 places on.

question 4

The search runs from the first place after the semicolon to the second. On which map does it weigh up the most places?

the answer, and why

the answer is C

Settling A writes a distance at all 3 others, so 1, then 3, 2 and 1, which is 7.

if you chose another

  • A line of 4 weighs up one place each round, 4.
  • H is on another part of the map, so the run ends after A and B, 2.
  • B is nearest and is the place to reach, so the run stops after A, then B and E, 3.

pen and paper

question 5about 8 minutes

The map has roads A-B 6, A-E 1, E-F 2, B-F 1, B-C 3, F-G 6 and C-G 1. The search runs from A to G.

  1. Write each place and its distance, in the order settled.
  2. Which distances are written and later beaten?
  3. What is the shortest way?
a hint

Write every distance down, and cross it out when a shorter one comes.

a worked answer

In the order settled: A 0, E 1, F 3, B 4, C 7, G 8. B's 6 becomes 4, and G's 9 becomes 8. The shortest way is A, E, F, B, C, G, which is 1 + 2 + 1 + 3 + 1, so 8.

6121361ABCEFG
The shortest way, drawn heavy, 8.

this tests how it works, on the learn page

what a full answer includes
  • A 0, E 1, F 3, B 4, C 7, G 8.
  • B from 6 to 4, and G from 9 to 8.
  • A, E, F, B, C, G, length 8.

question 6about 8 minutes

Four roads run one way, and the search starts at A. A to E is 7, A to B is 8, B to C is 2, and C to E is 4 below 0.

  1. Run on until every place is settled. What does the search say E is, and what is the shortest?
  2. When C is settled, why does E not change?
  3. Make C to E 0 instead. What does the search say E is now, and is it right?
a hint

Look along a road only to a place not settled yet.

a worked answer

Settling A writes E 7 and B 8, so E is settled at 7. B is settled at 8 and writes C 10. C is settled at 10. Through it, E would be 6, but E is settled, so it says 7, and the shortest is 6. With C to E at 0, through C is 10, so it says 7, and now 7 is the shortest.

ABCE
The four one-way roads. Their lengths are in the question.
Question 7: why does one length below 0 break it?
  1. the move: a length below 0

    the line:
    one way:E to B is −5\begin{gathered} \text{one way:} \\ E \text{ to } B \text{ is } -5 \end{gathered}
    ABCE
    The shortest way to B, drawn heavy, A, C, E, B.
    one-way roadlength
    A to B6
    A to C7
    C to E3
    E to B−5-5
    A to B alone is 6. A, C, E, B is 7 plus 3 plus −5-5, which is 5.

    why it is allowed: These roads run one way. On a two-way road below 0 you could go back and forth, shorter each time, so no way would be shortest.

  2. the move: the nearer place settled first

    the line:
    B is settled at 6\begin{gathered} B \text{ is settled at } 6 \end{gathered}

    why it is allowed: Search from A to B. Settling A writes B 6 and C 7. B is nearer, so round 2 settles it at 6, and the run stops.

  3. the move: the way round

    the line:
    Z=7−rZ = 7 - r

    why it is allowed: ZZ is the way A, C, E, B. It is 7 up to C, then rr less, what the rest of the way takes off. Here rr is 2, as C to E adds 3 and E to B takes off 5.

  4. the move: wrong, with no warning

    the line:
    it says 6;the shortest is 5\begin{gathered} \text{it says } 6; \\ \text{the shortest is } 5 \end{gathered}

    why it is allowed: By lines 2 and 3. Running on past B would not help, as a settled place gets no new distance. The answer looks like any other, and nothing says it is wrong.

  5. the move: which line failed

    the line:
    a way got shorterby going on\begin{gathered} \text{a way got shorter} \\ \text{by going on} \end{gathered}

    why it is allowed: Line 6 of question 6 says the whole way is at least the way up to xx. Here xx is C. The way up to C is 7, and the whole way is 5.

try it with a small rr: at r=2r = 2, line 3 gives Z=5Z = 5, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • It says 7; the shortest is 6.
  • E is settled already, so the search looks along no road to it.
  • It says 7, and it is right: through C is 10.