Match the following algorithms with the data structure it uses. Algorithm Data…
2023
Match the following algorithms with the data structure it uses.
Algorithm | Data structure |
I. Breadth first search | 1. Stacks |
II. Heap sort | 2. Queue |
III. Depth first search | 3. Arrays |
Answer: C. I - 2; II - 3; III - 1 — Concept: which support structure an algorithm needs is decided by its processing order — FIFO discovery order needs a Queue, LIFO backtracking order needs a…
- A.
I - 1; II - 2; III - 3
- B.
I - 3; II - 1; III - 2
- C.
I - 2; II - 3; III - 1
- D.
I - 3; II - 2; III - 1
Attempted by 1136 students.
Show answer & explanation
Correct answer: C
Concept: which support structure an algorithm needs is decided by its processing order — FIFO discovery order needs a Queue, LIFO backtracking order needs a Stack, and direct index-based access needs an Array.
Breadth-first search (BFS) visits nodes in the exact order they were discovered, level by level — that FIFO behaviour requires a Queue.
Heap sort maintains a binary heap where each node's parent/children are found by index arithmetic (2i+1, 2i+2) — that needs O(1) random access, so the heap is stored in an Array.
Depth-first search (DFS) dives as far as possible along one branch, then backtracks to the most recently visited unfinished node — that LIFO behaviour requires a Stack (explicit, or the call stack via recursion).
Cross-check: this also matches known behaviour — BFS's queue-driven order is exactly what guarantees the shortest path in an unweighted graph, while DFS's stack-driven order lets it dive arbitrarily deep before backtracking, confirming the queue/stack split; heap sort's O(log n) sift-up/sift-down operations only work with direct array indexing, confirming the array.
So I (Breadth first search) - 2 (Queue), II (Heap sort) - 3 (Arrays), III (Depth first search) - 1 (Stacks).