Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B

Undirected weighted graph on letters A to E with edges: D–E (17), A–B (4), B–C (38), A–D (34), C–E (18), B–E (16), A–E (21), A–C (2), B–D (32), C–D (22). Starting from A, Prim's algorithm repeatedly adds the cheapest edge that leaves the tree together with its new vertex. In what order are the vertices added, starting with A?

  1. A. A D B C E
  2. B. A B D E C
  3. C. A C B E D
  4. D. E B A C D

Correct answer: C. A C B E D

Choosing the lightest crossing edge each time gives A C B E D. Choosing the heaviest crossing edge gives A D B C E, and a BFS sweep gives A B D E C; both are common slips.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic