Data Structures & AlgorithmsVery hardGATE CS

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

  1. A. ∞ and ∞
  2. B. 12 and 6
  3. C. ∞ and 6
  4. D. 12 and ∞

Correct answer: D. 12 and ∞

After phase k, D[i][j] is the shortest path using only vertices 1..k as intermediates. With 1 and 2 allowed, D[3][4] = 12 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