Data Structures & AlgorithmsModerateGATE CS
An undirected graph has edges A–D, B–D, B–F, C–D, C–E, D–G, E–F, E–H, F–G, G–H. BFS starts at A and always scans neighbours in alphabetical order. In what order are the vertices visited?
- A. A D B F E C H G
- B. A D G H E F B C
- C. A D B C G F E H
- D. A D B C G E F H
Correct answer: C. A D B C G F E H
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 D B C G F E H.
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
- An undirected graph has edges A–C, A–E, B–D, B–E, C–D, C–F, D–G, E–F, E–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips…
- An undirected graph has edges A–B, A–E, A–F, B–D, B–E, B–F, C–D, C–G, E–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips…
- 0/1 knapsack with capacity 16: items (weight, value) = (9, 40), (3, 34), (2, 19), (7, 38), (7, 32). What is the maximum total value?
- Using parts 1, 5, 6 (each any number of times), in how many ways can 9 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- In the DAG with edges A→B, A→G, B→D, D→G, E→A, E→B, E→C, E→D, E→F, E→G, G→C, how many distinct directed paths are there from E to C, and…
- A 5 × 5 grid is given row by row as .##.. / ..... / ....# / ...#. / .#... ('#' = wall). Moving one step up, down, left or right through…