What data structure is used for Depth-First Search (DFS) in a graph?

What data structure is used for Depth-First Search (DFS) in a graph?

a) Stack
b) Queue
c) Priority Queue
d) Heap

Answer:

a) Stack

Explanation:

Depth-First Search (DFS) is implemented using a stack data structure. DFS explores a graph by going as deep as possible along a branch before backtracking, and this is naturally supported by the Last-In-First-Out (LIFO) nature of stacks.

In recursive implementations, DFS uses the call stack implicitly. In iterative implementations, an explicit stack is used to track the vertices that need to be processed.

DFS is useful for applications like topological sorting, detecting cycles in graphs, and solving mazes.

Reference:

DSA (Data Structures and Algorithms) MCQ Questions and Answers

Scroll to Top