Data Structures & AlgorithmsVery hardGATE CS

Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (10), 1→4 (1), 2→1 (12), 2→3 (10), 3→1 (12), 4→1 (1), 4→3 (4). 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. 5 and 11
  3. C. 20 and 11
  4. D. ∞ and 11

Correct answer: C. 20 and 11

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] = 20 and D[4][2] = 11; the final all-pairs values can be smaller still.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic