Which data structure is most suitable for implementing Breadth-First Search (BFS) in a graph?
a) Stack
b) Queue
c) Linked List
d) Heap
Answer:
b) Queue
Explanation:
Breadth-First Search (BFS) is implemented using a queue data structure. BFS explores a graph level by level, visiting all nodes at the current level before moving to the next level.
The queue ensures that nodes are processed in the order they are discovered, maintaining the breadth-first nature of the traversal. Nodes are enqueued when they are first encountered and dequeued for processing in a First-In-First-Out (FIFO) manner.
BFS is commonly used to find the shortest path in an unweighted graph or to explore all connected components of a graph.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers