Skip to content

Floyd-Warshall, every pair

networks

Level 3 of 3

Also called Floyd-Warshall algorithm and All-pairs 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

Floyd-Warshall runs on this map. What does the table hold for A to C at the end?

4123ABCFABCFABCF04103020
the answer, and why

the answer is B

Through F and B, A to C is 1 + 2 + 3, which is 6. The way through B alone is 4 + 3, which is 7, and stop F beats it.

this tests how it works, on the learn page

if you chose another

  • That is 4 + 3, by the road from A to B. It is written at stop B, and stop F then beats it.
  • That is B to C's own road alone, with nothing added for the way to B.
  • A cell holds the shortest way, of any number of roads. Roads lead from A to C by way of B.

question 2

How many pairs does the run try on this line of 5 places?

1111ABCDGABCDGABCDG010101010
the answer, and why

the answer is A

5 stops, 4 places to start from and 3 to end at each, so 5 times 4 times 3, which is 60.

if you chose another

  • That is a map of 4 places.
  • That is 5 times 5 times 5, the ceiling, which also counts the choices that share a place.
  • That is the ways made shorter, not the pairs tried.

question 3

Which is true of a star and a line of 5 places?

the answer, and why

the answer is A

Any map of 5 places tries 5 times 4 times 3 pairs, which is 60. Only the ways made shorter differ, none on the star and 6 on the line.

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

if you chose another

  • A try that fails at once still counts.
  • 60 is the pairs it tries. It makes 6 ways shorter.
  • 6 is the line's. A star has no way through a stop.

question 4

On which map does the run make the most ways shorter? A>B 1 means a road from A to B, 1 long.

the answer, and why

the answer is D

Its stops write A to C, E to C, A to B, and A to C again, so 4. The others make 3, none and none.

if you chose another

  • A line of 4 writes A to C, A to D and B to D, so 3.
  • A star has no way through a stop, so it writes none.
  • One road has no stop to try, so it writes none.

pen and paper

question 5about 8 minutes

The map has one-way roads A to B 2, B to C 5, A to F 1, F to C 2 and C to D 1.

  1. Write the table before any stop.
  2. Allow the places as stops in letter order. Which cells are written, at which stop, and to what?
  3. How many pairs does the run try, and how many ways does it make shorter?
a hint

Keep the table on paper, and change a cell only when a way through the stop beats it.

a worked answer

Before any stop, A to B is 2, A to F is 1, B to C is 5, C to D is 1 and F to C is 2. Each place to itself is 0, and the rest are empty. Stops A and D write nothing, as no road leads into A and none leaves D. The table below gives the other writes. The run tries 5 times 4 times 3 pairs, which is 60, and makes 6 ways shorter.

stoppairto the stop + from itin the cell beforewritten now
BA to C2 + 5 is 7empty7
CA to D7 + 1 is 8empty8
CB to D5 + 1 is 6empty6
CF to D2 + 1 is 3empty3
FA to C1 + 2 is 373
FA to D1 + 3 is 484
Every write in the run, stop by stop.

this tests how it works, on the learn page

what a full answer includes
  • A to B 2, A to F 1, B to C 5, C to D 1, F to C 2, each place to itself 0.
  • The 6 writes in the table.
  • 60 pairs, 6 ways.

question 6about 6 minutes

A friend says a line of 6 places must try fewer pairs than a map of 6 places with many roads. Their reason is that most of its cells stay empty for most of the run.

  1. How many pairs does one stop try?
  2. How many does the whole run try?
  3. Where does your friend go wrong?
a hint

Count the tries before asking which of them can write anything.

a worked answer

One stop leaves 5 places to start from and 4 to end at, so 20 pairs. Six stops make 120. Your friend counts only the tries that can write. A try with no way to the stop or none from it fails at once, but it is still made, on any map of 6 places.

Question 1: how many pairs, PP, does a run try on nn places, whatever the roads?
  1. the move: each stop's pairs

    the line:
    each stop tries(n−1)(n−2) pairs\begin{gathered} \text{each stop tries} \\ (n - 1)(n - 2) \text{ pairs} \end{gathered}
    fromtrieswritten
    Ato C, to Eto C
    Cto A, to Enone
    Eto A, to Cto C
    Stop B on the small map, with roads A to B 3, A to E 1, E to B 1 and B to C 4. It has 3 places to start from and 2 pairs each. C has no way to B, and B has no way to A or E, so the other tries fail at once.

    why it is allowed: Set the stop aside, and n−1n - 1 places are left to start from. Each has n−2n - 2 left to end at, as it cannot end at itself. For 4 places, 3 times 2, which is 6.

  2. the move: every stop

    the line:
    P=n(n−1)(n−2)P = n(n - 1)(n - 2)

    why it is allowed: Each of the nn places is a stop once, and no run ends early. A try that fails at once still counts, so no road, length or letter changes this. For 4 places, 4 times 6, which is 24.

  3. the move: a ceiling

    the line:
    P≤n3P \le n^3

    why it is allowed: n−1n - 1 and n−2n - 2 are each below nn, so the product is below nn times nn times nn. For 4 places, PP is 24, at most 64.

  4. the move: read the bound

    the line:
    P∈O(n3)P \in O(n^3)

    why it is allowed: By line 3, with the fixed multiple 1. ∈\in reads is in, and O(n3)O(n^3) means at most a fixed multiple of n3n^3, from some nn on.

  5. the move: a floor

    the line:
    P≥n34P \ge \frac{n^3}{4}

    why it is allowed: From 4 places on, n−1n - 1 and n−2n - 2 are each at least half of nn. So the product is at least nn times half of nn times half of nn. For 4 places, PP is 24, at least 16.

  6. the move: read the bound

    the line:
    P∈Ω(n3)P \in \Omega(n^3)

    why it is allowed: By line 5, with the fixed multiple a quarter. Ω(n3)\Omega(n^3) means at least a fixed multiple of n3n^3, above 0, from some nn on.

  7. the move: both together

    the line:
    P∈Θ(n3)P \in \Theta(n^3)

    why it is allowed: Lines 4 and 6 bound PP by the same growth, only a fixed multiple apart. Θ(n3)\Theta(n^3) means both.

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

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

what a full answer includes
  • 20 pairs.
  • 120 pairs.
  • A try that fails at once is still a try, so the roads change nothing.