Site icon DocMCQs

The data structure required for Breadth First Traversal on a graph is?
a) Array
b) Stack
c) Tree
d) Queue

A queue is the essential data structure for Breadth-First Search (BFS) traversal on a graph.

Why a Queue?

How it works:

  1. Enqueue the starting node: Add the initial node to the queue.
  2. Dequeue a node: Remove the front node from the queue and visit it.
  3. Enqueue neighbors: Add all unvisited neighbors of the dequeued node to the queue.  
  4. Repeat steps 2 and 3: Continue until the queue is empty.

By using a queue, BFS ensures that all nodes at a given depth are visited before moving to the next level, resulting in a breadth-wise exploration of the graph.

Exit mobile version