Data Structures & AlgorithmsVery hardProduct-company placement

Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (2), 1→3 (8), 2→1 (8), 2→4 (3), 3→2 (7), 3→4 (6), 4→1 (2). After the outer-loop iterations k = 1 and k = 2 only (paths may use only 1 and 2 as intermediates), what are D[1][4] and D[2][3]?

  1. A. 5 and 16
  2. B. ∞ and ∞
  3. C. 5 and 13
  4. D. ∞ and 16

Correct answer: A. 5 and 16

After phase k, D[i][j] is the shortest path using only vertices 1..k as intermediates. With 1 and 2 allowed, D[1][4] = 5 and D[2][3] = 16; the final all-pairs values can be smaller still.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic