Data Structures & AlgorithmsHardInfosys · Wipro pattern

Floyd–Warshall on vertices 0–3 starts from the weight matrix below (∞ = no edge). After the iteration with k = 0 (paths allowed to go through vertex 0 only), what is dist[2][1]?

Floyd–Warshall on vertices 0–3 starts from the weight matrix below (∞ = no edge). After the iteration with k = 0 (paths allowed to go through vertex 0 only), what is dist[2][1]?
     0   1   2   3
0    0   3   ∞   7
1    8   0   2   ∞
2    5   ∞   0   1
3    2   ∞   ∞   0
  1. A. ∞
  2. B. 8
  3. C. 6
  4. D. 4

Correct answer: B. 8

With only vertex 0 as an intermediate, dist[2][1] = min(∞, dist[2][0] + dist[0][1]) = 5 + 3 = 8. The final answer after all k would be smaller (2 → 3 → 0 → 1 = 1 + 2 + 3 = 6), but that needs vertex 3, which is only allowed from k = 3 onward.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic