Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
Undirected weighted graph on letters A to G with edges: C–D (32), D–E (10), A–E (12), F–G (9), B–G (1), D–G (6), E–G (14), B–F (21), B–D (7), D–F (37), A–C (38). 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 C D F B E G
- B. A E D G B F C
- C. A E C D G B F
- D. G B D F E A C
Correct answer: B. A E D G B F C
Choosing the lightest crossing edge each time gives A E D G B F C. Choosing the heaviest crossing edge gives A C D F B E G, and a BFS sweep gives A E C D G B F; 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: C–D (8), A–E (13), B–C (26), A–F (11), B–E (22), D–E (27), A–C (32), B–D (5), D–F…
- Undirected weighted graph on letters A to F with edges: C–D (24), B–C (31), D–F (26), B–F (11), B–D (9), A–B (6), C–F (16), D–E (8)…
- 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…
- 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…