Data Structure Applications Which data structure is used for BFS and DFS of a graph?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Data Structure Applications
Which data structure is used for BFS and DFS of a graph?
Pick ONE option
Show answer & explanation
Answer: A. Stacks and Queues respectively
BFS (Breadth-First Search) uses a Queue to explore nodes level-by-level in FIFO order, while DFS (Depth-First Search) uses a Stack to explore nodes depth-first in LIFO order. Option A correctly identifies this fundamental distinction between the two traversal algorithms.
Step-by-step Derivation:
BFS traversal: Start at root → add to queue → dequeue → visit children → add children to queue (FIFO). DFS traversal: Start at root → push to stack → pop → visit → push children to stack (LIFO). Therefore, BFS uses Queue and DFS uses Stack respectively.