Data Structures & AlgorithmsVery hardProduct-company placement

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

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

Correct answer: D. 5 and ∞

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic