Data Structures & AlgorithmsVery hardProduct-company placement
Floyd–Warshall runs on vertices 1–4 with directed edges 1→3 (2), 2→1 (7), 2→3 (4), 3→1 (3), 3→4 (8), 4→1 (7), 4→2 (1). After the outer-loop iterations k = 1 and k = 2 only (paths may use only 1 and 2 as intermediates), what are D[4][3] and D[2][4]?
- A. ∞ and ∞
- B. 5 and 12
- C. 9 and 12
- D. 5 and ∞
Correct answer: D. 5 and ∞
After phase k, D[i][j] is the shortest path using only vertices 1..k as intermediates. With 1 and 2 allowed, D[4][3] = 5 and D[2][4] = ∞; 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–8 has edges 2–1, 3–1, 4–2, 5–2, 6–4, 7–5, 8–3. What is the sum of the distances (in edges) over all unordered pairs…
- Keys 64, 95, 22, 60, 55, 18, 69, 31, 78, 65, 26 are inserted in that order into an empty BST. Then 64 is deleted and after that 22 is…
- A 4 × 4 grid has rows 1 5 2 1 / 1 1 5 4 / 7 4 6 2 / 1 6 3 8. Moving only right or down from the top-left to the bottom-right cell, what is…
- How many calls of g (including the first) are made when g(7) is evaluated, and what value is returned? c int g(int n) { if (n <= 2) return…
- The words trie, tried, trip, try, tree are inserted into an empty trie (one node per character, shared prefixes share nodes, plus a root)…
- In the grid 21112 / 11110 / 10111 / 11111 (rows top to bottom; 2 = rotten, 1 = fresh, 0 = empty), every minute each rotten orange rots its…