Data Structures & AlgorithmsModerateGATE CS
An undirected graph has edges A–E, A–H, B–F, C–E, C–G, D–G, D–H, E–F, E–G, F–H. BFS starts at A and always scans neighbours in alphabetical order. In what order are the vertices visited?
- A. A E C G D H F B
- B. A H F E G D C B
- C. A E H C D F G B
- D. A E H C F G D B
Correct answer: D. A E H C F G D B
BFS dequeues level by level, and within a level in the order vertices were enqueued (which depends on their parents, not on alphabetical order): A E H C F G D B.
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
- A tree on vertices 1–10 has edges 2–1, 3–2, 4–1, 5–4, 6–1, 7–3, 8–7, 9–5, 10–9. What is the size of a minimum DOMINATING set (every vertex…
- What is the length of the longest common subsequence of CONTEST and RECESSION, and how many DISTINCT longest common subsequences (as…
- A tree on vertices 1–8 has edges 2–1, 3–2, 4–1, 5–2, 6–5, 7–2, 8–1. What is the sum of the distances (in edges) over all unordered pairs…
- How many structurally different binary trees with 7 nodes have height EXACTLY 4 (height = edges on the longest root-to-leaf path)?
- Keys 54, 17, 39, 66, 11, 36, 83, 51, 94, 10, 76 are inserted in that order into an empty BST. Then 39 is deleted and after that 83 is…
- Prim's algorithm starts at E on the weighted undirected graph A–C (13), A–D (1), A–G (11), B–D (16), B–G (6), C–D (14), C–E (18), C–F…