Data Structures & AlgorithmsVery hardGATE CS
Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (1), 1→3 (5), 2→1 (1), 2→4 (8), 3→1 (3), 3→2 (9), 4→3 (2). After the outer-loop iterations k = 1 and k = 2 only (paths may use only 1 and 2 as intermediates), what are D[3][4] and D[4][2]?
- A. ∞ and ∞
- B. 12 and 6
- C. ∞ and 6
- D. 12 and ∞
Correct answer: D. 12 and ∞
After phase k, D[i][j] is the shortest path using only vertices 1..k as intermediates. With 1 and 2 allowed, D[3][4] = 12 and D[4][2] = ∞; the final all-pairs values can be smaller still.
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
- A tree on vertices 1–9 has edges 2–1, 3–1, 4–3, 5–2, 6–4, 7–6, 8–6, 9–8. What are its diameter and radius, and which vertex/vertices form…
- Five matrices have dimensions 12×10 , 10×4 , 4×6 , 6×6 , 6×8. What is the minimum number of scalar multiplications needed to compute their…
- An undirected graph has edges A–D, A–F, A–G, B–C, B–F, C–D, C–E, E–G, F–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips…
- What is the length of the longest common subsequence of ONTOLOGY and TOPOLOGY, and how many DISTINCT longest common subsequences (as…
- Edit distance between MARTIAN and COMPILER where insert and delete cost 1 each but a substitution costs 2: what is the minimum total cost?
- Dijkstra's algorithm runs from A on the directed graph A→C (4), B→A (1), B→F (6), C→B (7), C→E (12), C→F (6), D→C (3), E→A (15), E→B (3)…