Data Structures & AlgorithmsVery hardGATE CS

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

  1. A. ∞ and ∞
  2. B. 15 and 17
  3. C. ∞ and 17
  4. D. 15 and ∞

Correct answer: D. 15 and ∞

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic