Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
Undirected weighted graph on letters A to G with edges: D–F (23), A–C (16), C–G (36), C–F (12), A–G (10), B–D (8), E–F (34), A–E (18), A–D (4), F–G (15). 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?
- A. A D B G F C E
- B. A E F D C G B
- C. A C G E D F B
- D. G A D B F C E
Correct answer: A. A D B G F C E
Choosing the lightest crossing edge each time gives A D B G F C E. Choosing the heaviest crossing edge gives A E F D C G B, and a BFS sweep gives A C G E D F B; both are common slips.
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
- Undirected weighted graph on letters A to F with edges: E–F (11), C–E (13), B–D (26), A–F (23), A–D (5), B–F (30), D–E (10), C–D (7)…
- Undirected weighted graph on letters A to E with edges: B–C (1), A–E (20), A–C (26), A–B (39), D–E (31), A–D (8), B–E (7), C–E (24)…
- Undirected weighted graph on letters A to E with edges: C–D (29), D–E (38), A–D (34), A–C (14), C–E (37), B–E (5), B–C (23), A–E (19), B–D…
- Undirected weighted graph on letters A to F with edges: B–D (31), A–B (18), B–E (23), B–F (14), C–F (15), E–F (30), A–D (6), B–C (33), C–D…
- Undirected weighted graph on letters A to E with edges: A–E (4), C–E (28), C–D (35), A–D (21), B–D (15), B–C (20), A–C (2), A–B (7)…
- 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…