GATE Computer ScienceHardGATE CS (PYQ pattern)

Floyd–Warshall is run on a 5-vertex directed graph with weight matrix (rows are sources, ∞ = no edge): row1: [0, 6, ∞, 3, ∞]; row2: [∞, 0, 2, ∞, 8]; row3: [4, ∞, 0, ∞, 1]; row4: [∞, 5, ∞, 0, ∞]; row5: [7, ∞, ∞, 2, 0]. After the iterations for intermediate vertices 1 and 2 only (k = 1, 2) have completed, what is the matrix entry D[4][5]?

  1. A. 8
  2. B. 13
  3. C. 15
  4. D. ∞

Correct answer: B. 13

Vertex 4 has the direct edge 4 → 2 of weight 5, and vertex 2 has the direct edge 2 → 5 of weight 8, so after k = 2, D[4][5] = D[4][2] + D[2][5] = 5 + 8 = 13. The value 8 is the final answer reached only after vertex 3 is used (4 → 2 → 3 → 5 = 5 + 2 + 1).

More Algorithms questions

Exams that ask this topic