Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Prim's algorithm starts at vertex 0 on the undirected weighted graph with edges 0-1 (6), 0-3 (2), 1-2 (3), 1-3 (8), 1-4 (5), 2-4 (7), 3-4 (9), 4-5 (4), 3-5 (6). What is the total weight of the minimum spanning tree it builds?
- A. 20
- B. 36
- C. 50
- D. 22
Correct answer: A. 20
Prim from 0 adds 0-3 (2), then 3-5 (6, tied with 0-1), 5-4 (4), 4-1 (5) and 1-2 (3), total 20. Always taking the heaviest edge gives the maximum spanning tree (36), 50 sums every edge, and Dijkstra's shortest-path tree from 0 (0-3, 0-1, 3-5, 1-2, 1-4) weighs 22, not minimal.
More Coding Interview Problems questions
- Problem: A directed graph on vertices 0–5 has arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, 4→5. Its strongly connected components are computed. How…
- Problem: The number of spanning trees of the complete graph K4 (4 labelled vertices, every pair joined by an edge) is what value?…
- Problem: A graph on vertices 0–6 has edges 0-1, 0-2, 1-3, 2-3, 3-4, 4-5, 5-6, 4-6. Count the bridges (edges whose removal increases the…
- Problem: Maximum bipartite matching is computed for left side {A, B, C, D} and right side {1, 2, 3, 4} with allowed pairs: A–1, A–2, B–2…
- Problem: A tree has 8 nodes numbered 1–8 with edges 1-2, 1-3, 2-4, 2-5, 3-6, 3-7, 7-8. What is the size of a minimum vertex cover (a…
- Problem: In the directed graph on vertices 1–6 with arcs 1→2, 1→3, 2→4, 3→4, 4→5, 3→6, 6→5, what is the length of the longest directed…