Data Structures & AlgorithmsVery hardGATE CS · ICPC regional · Product Company SDE
An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [G]; B: [C]; C: [B, D]; D: [C, F, G]; E: [F]; F: [D, E]; G: [A, D]. What is the BFS traversal order starting from A?
- A. E, B, F, C, D, G, A
- B. A, G, D, F, C, E, B
- C. A, G, D, C, F, B, E
- D. A, G, D, C, B, F, E
Correct answer: C. A, G, D, C, F, B, E
BFS visits A, enqueues its unvisited neighbours in sorted order, then repeatedly dequeues the front of the queue and enqueues its unvisited neighbours, giving A, G, D, C, F, B, E. A DFS from the same start and adjacency lists would instead give A, G, D, C, B, F, E, since DFS dives down one branch before backtracking.
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
- Keys 35, 45, 55 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- Keys 60, 70, 80 are inserted in this order into an empty AVL tree. After AVL rebalancing (a single rotation resolves the imbalance here)…
- A binary tree has preorder traversal [90, 31, 36, 77, 58, 43, 83] and inorder traversal [31, 36, 43, 58, 77, 83, 90]. Reconstructing the…
- Undirected graph edges: A–D, D–C, B–C, C–A. Using a BFS 2-colouring, is this graph bipartite?
- Keys 57, 11, 34, 33, 80, 9, 72, 24, 56, 66 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain)…
- Undirected graph edges: B–C, C–A, E–C, E–D, E–B. Using a BFS 2-colouring, is this graph bipartite?