Skip to main content

Breadth-First Search (BFS)

Definition​

Breadth-First Search (BFS) is a graph traversal algorithm that explores all the neighbor nodes at the present depth before moving on to the nodes at the next depth level. It operates on a data structure called a queue, enabling it to visit nodes in a level-by-level manner

Practice​

BFS(graph, start_node):
initialize an empty queue
enqueue start_node onto the queue
mark start_node as visited

while queue is not empty:
current_node = dequeue from queue
process current_node

for each neighbor_node of current_node:
if neighbor_node is not visited:
mark neighbor_node as visited
enqueue neighbor_node onto queue