QUESTION 21
Which data structure distinguishes ordinary BFS from iterative DFS?
2 marksAnswer
BFS uses a FIFO queue; iterative DFS uses a stack. Recursive DFS uses pending function calls with stack-like order.
QUESTION 21
BFS uses a FIFO queue; iterative DFS uses a stack. Recursive DFS uses pending function calls with stack-like order.
QUESTION 22
DFS and BFS both explore reachable vertices, but their pending-work order differs.
Both need a graph representation, a starting rule and visited tracking. Both must restart at unvisited vertices if the task requires every component. State neighbour order when giving one exact printed sequence.
It follows a branch before backtracking. On the common five-vertex example with ascending neighbours, it gives 0, 1, 3, 2, 4. An explicit stack or recursion keeps unfinished work. The maximum active depth can reach V.
It explores level by level using a queue. The same example gives 0, 1, 2, 3, 4. Mark on enqueue to avoid duplicate pending entries. It naturally supports unweighted edge-distance calculations when distances are recorded.
Both have O(V + E) time with standard adjacency lists and O(V²) with a matrix full traversal. Both can need O(V) auxiliary storage, although their practical pending-work patterns differ. The choice does not automatically reduce the representation’s own storage.
Use BFS for level-order reachability or shortest numbers of unweighted edges. DFS is useful for deep exploration and as a base for analyses that require entering and finishing vertices. Neither basic traversal performs all possible graph applications without additional state or rules.
| Feature | DFS | BFS |
|---|---|---|
| Pending work | Stack/calls | Queue |
| Exploration | Depth first | Level first |
| List time | O(V + E) | O(V + E) |
| Matrix time | O(V²) | O(V²) |
| Auxiliary space | O(V) worst case | O(V) worst case |
The following are topic matches from the supplied papers. Marks are their printed allocations, separate from the practice answer badge.
Compare BFS and DFS for a graph.
Paper DS01 · page 2Compare BFS and DFS with examples.
Paper DS02 · page 1Differentiate BFS and DFS.
Paper DS06 · page 1