Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (3), 1→4 (9), 2→3 (6), 2→4 (10), 3→1 (12), 3→2 (6), 4→1 (12), 4→2 (7), 4→3 (3). dist[i][i] = 0, dist[i][j] is the arc weight, or ∞ when there is no arc. The outer loop runs over k = 1, 2, … in that order. After the iterations for k = 1 through 3 have finished, what is dist[1][3]?

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

Correct answer: D. 9

After rounds k = 1..3, only vertices 1 to 3 may sit inside a path. 1→2 (3) + 2→3 (6) = 9 appears in round k = 2 and nothing through vertex 3 improves it. Before round 2 the entry is still ∞, 12 is the route 1→4→3 through a vertex not yet allowed, and 6 is just the arc 2→3.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic