Why a Queue?
FIFO (First In, First Out) principle: This is crucial for exploring nodes level by level. The first node added to the queue is the first one to be visited.
Efficient enqueue and dequeue operations: The queue allows for quick insertion and removal of nodes, which is necessary for BFS.
How it works:
Enqueue the starting node: Add the initial node to the queue.
Dequeue a node: Remove the front node from the queue and visit it.
Enqueue neighbors: Add all unvisited neighbors of the dequeued node to the queue.
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.
If you think there is any confusion in the given MCQs, you can comment below.
docmcqs.com is Pakistan's No.1 online platform for preparing for all types of exams including PPSC, FPSC, KPSC, SPSC, Ministry of Defence, and for one-paper MCQs. This includes Pedagogy MCQs, general knowledge MCQs, current affairs MCQs, and much more.