Skip to content

Bellman-Ford shortest paths

networks

Level 3 of 3

Also called Bellman-Ford algorithm and Single-source shortest paths with negative lengths.

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

Bellman-Ford runs on this map from A. What distance does place C end with?

511-32A0BCEF
the answer, and why

the answer is A

Through E and F, B is 1 + 1 - 3, which is -1. So place C is -1 + 2, which is 1.

this tests how it works, on the learn page

if you chose another

  • That is 5 + 2, by the road from A to B, before F to B pays back.
  • That is B's distance. Place C is one road further.
  • That is place C's own road alone, with nothing added for the way to B.

question 2

On this line from A, how many roads does the run look along?

111A0BCD
the answer, and why

the answer is A

Round 1 looks along all 3 roads and round 2 along all 3 again, finding nothing shorter.

if you chose another

  • That is round 1 alone. A round must make nothing shorter before the run stops.
  • That is the same line backwards.
  • That is n×mn \times m, the ceiling on any map of 4 places and 3 roads.

question 3

Which is true of a line of nn places in letter order and of the same line backwards, from 4 places on?

the answer, and why

the answer is C

Backwards needs n−1n - 1 rounds, and letter order 2.

this tests best, worst and average, on the learn page

if you chose another

  • Only the line in letter order. Backwards looks along (n−1)(n+2)2\frac{(n - 1)(n + 2)}{2}.
  • It needs 2. Round 1 writes every place.
  • A last pass writes no distance. It only looks for a road that can still make a way shorter.

question 4

On which map does the run look along the most roads? A>B 1 means a road from A to B, 1 long, and the letter after the semicolon is the start.

the answer, and why

the answer is A

Its loop B, C, G adds up to 3 below 0, so every round changes something. That is 3 rounds and the last pass, 4 roads each, so 16 looks. The others cost 6, 9 and 2.

if you chose another

  • Two rounds of 3 roads, so 6.
  • One road, then 2, then 3, then the last pass's 3, so 9.
  • Round 1 and the last pass, 1 road each, so 2.

pen and paper

question 5about 8 minutes

The map has one-way roads A to B 3, A to E 1, E to F 2, B to C 2 and F to G 4. F to B is 3 below 0. Start at A.

  1. Look along the roads in letter order, and write every distance after each round.
  2. How many rounds does the run take, and how many roads does it look along?
  3. Which road gives B its shortest way, and by how much does it beat A to B?
a hint

Keep a row of distances for each round, and change a number only when a way beats it.

a worked answer

Round 1: A to B writes 3 at B, A to E writes 1, B to C writes 5 and E to F writes 3. F to B makes B 0, and F to G writes 7. So after round 1: A 0, B 0, C 5, E 1, F 3, G 7. Round 2 carries B's 0 on to C, which becomes 2. Round 3 makes nothing shorter, so the run stops after 3 rounds, with 6 roads looked along in each, 18 in all. F to B gives B its shortest way, 0 against the 3 of A to B, so 3 shorter.

312-324ABCEFG
The whole map, each road with its length.

this tests how it works, on the learn page

what a full answer includes
  • Round 1: A 0, B 0, C 5, E 1, F 3, G 7. Round 2: C 2. Round 3: no change.
  • 3 rounds, 18 roads.
  • F to B, 3 shorter.

question 6about 8 minutes

The map has one-way roads A to B 3, A to E 2, B to C 1, G to F 1, F to B 1 and G to H 1. C to G is 6 below 0. Start at A.

  1. Add up the loop B, C, G, F. What does each lap do?
  2. After round 6 the distances are A 0, B -6, C -2, E 2, F -7, G -8 and H -7. Which road on the loop still makes a way shorter on the last pass?
  3. Which places get −∞-\infty, and which keep their distance?
a hint

Draw the map first, and mark the loop.

a worked answer

The loop adds up to 1 - 6 + 1 + 1, which is -3, so each lap is 3 shorter. On the last pass B to C gives -6 + 1, which is -5, beating C's -2. C to G gives -8, G to F gives -7 and F to B gives -6, none shorter. So B, C, F and G get −∞-\infty, and so does H, past the loop. A keeps 0 and E keeps 2.

321-6111ABCEFGH
The whole map. The loop, drawn heavy, holds B, C, G and F.
Question 6: what does such a loop do, and how does the last pass find it? Let LL be the rounds before that pass.
  1. the move: no shortest way

    the line:
    past such a loop,no way is shortest\begin{gathered} \text{past such a loop,} \\ \text{no way is shortest} \end{gathered}
    42-513ABCFG
    The loop B, C, F, drawn heavy, adds up to 2 below 0, so each lap is 2 shorter. G is past it.
    afterABCFG
    round 102614
    round 2004-12
    round 30-22-30
    round 40-40-5-2
    Each round, each place from B on is 2 lower, one lap's worth.

    why it is allowed: Such a loop is one the start can reach, adding up below 0. Go round it once more and a way through it is shorter, as often as you like. So a place it can reach has no shortest way.

  2. the move: no such loop

    the line:
    no such loop: the lastpass changes nothing\begin{gathered} \text{no such loop: the last} \\ \text{pass changes nothing} \end{gathered}

    why it is allowed: By question 2 each place the start reaches is right after round n−1n - 1, and by its line 1 no distance goes lower. So no road can make a way shorter.

  3. the move: such a loop

    the line:
    such a loop: a road on it canalways make a way shorter\begin{gathered} \text{such a loop: a road on it can} \\ \text{always make a way shorter} \end{gathered}
    roadway along itwritten at its far endshorter?
    B to C-4 + 2 is -20yes
    C to F0 - 5 is -5-5no
    F to B-5 + 1 is -4-4no
    The last pass on the loop, after round 4. B to C still makes a way shorter. A last pass writes no distance, so C keeps its 0 for the next row.

    why it is allowed: Say each place on it has a distance, and no road on it makes a way shorter. Then each far end is at most its near end plus the road's length. With each letter for its place's distance, on this map C is at most B + 2, F at most C - 5, and B at most F + 1. Add the three, and B + C + F is at most itself take away 2, so 0 is at most -2. That is false. On any such loop the distances cancel the same way, leaving 0 at most its total, which is below 0. So some road on it makes a way shorter.

  4. the move: all the rounds

    the line:
    L=n−1L = n - 1

    why it is allowed: Line 1's table shows it, as all 4 rounds change something. Now any such loop. Say a round changed nothing. Then every road from a place with a distance already has a distance at its far end, so each place on the loop has one. By line 3 a road on the loop then makes a way shorter, so that round did change something. So the run uses all n−1n - 1 rounds. With repeats cut out, a way to a place on the loop has at most n−1n - 1 roads, and each round writes a distance at least one road further. So by round n−1n - 1 each place on it has one. For 5 places, 4.

  5. the move: every place past it

    the line:
    the far end, and all itleads on to: −∞\begin{gathered} \text{the far end, and all it} \\ \text{leads on to: } -\infty \end{gathered}

    why it is allowed: Take a place no such loop reaches. A way to it never passes such a loop, or that loop would reach it. So question 2's steps hold along that way, and the place is right. The near end of a road into it is also a place no such loop reaches, or the loop would reach it along that road. So that end is right too, and the road cannot beat what is written. So a road that still makes a way shorter ends at a place such a loop reaches, and by line 3 one ends on each such loop. The run writes −∞-\infty there and at every place it leads on to, and nowhere else. Here that is B, C, F and G, and A keeps 0.

try it with a small nn: at n=5n = 5, line 4 gives L=4L = 4, which you can work out by hand

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

what a full answer includes
  • -3: each lap is 3 shorter.
  • B to C.
  • B, C, F, G and H get −∞-\infty. A keeps 0 and E keeps 2.