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?

  1. A. 20
  2. B. 36
  3. C. 50
  4. 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

Exams that ask this topic