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
- A. ∞
- B. 8
- C. 6
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- How many distinct spanning trees does the graph with vertices A, B, C, D and edges A–B, B–C, C–D, D–A, A–C (a 4-cycle plus one diagonal)…
- A recursive segment tree is built over an array of n = 6 elements (each internal node splits its range into two halves; leaves are single…
- In a Fenwick (binary indexed) tree of size 16 (1-based), the update at index 5 adds the value to i = 5 and then repeatedly does i += i &…
- Union–find with NO union by rank/size and NO path compression: union(a, b) sets parent[find(a)] = find(b). Starting with singletons 0–5…
- A connected undirected graph has vertex degrees A: 3, B: 4, C: 2, D: 3, E: 2. Does it have an Euler path (using every edge exactly once)…
- Houses in a row hold [3, 2, 7, 10, 1, 9]. No two ADJACENT houses may both be chosen. What is the maximum total? (dp[i] = max(dp[i − 1]…