How to bfs

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: Breadth-First Search (BFS) is a graph traversal algorithm that explores neighbor nodes first before moving to the next level. It's commonly used to find the shortest path in an unweighted graph and is implemented using a queue data structure.

Key Facts

What is Breadth-First Search (BFS)?

Breadth-First Search (BFS) is a fundamental graph traversal algorithm used in computer science. It starts at a chosen node (the 'source' or 'root') and explores all of its immediate neighbors. Once all neighbors at the current depth have been visited, it moves on to the neighbors of those neighbors, and so on. This systematic exploration ensures that BFS discovers nodes in increasing order of their distance from the source node.

How Does BFS Work?

The core mechanism of BFS relies on a data structure called a queue. A queue operates on a First-In, First-Out (FIFO) principle, meaning the first element added to the queue is the first one to be removed. The BFS algorithm proceeds as follows:

  1. Initialization: Choose a starting node. Mark it as visited and add it to the queue.
  2. Exploration: While the queue is not empty:
    • Dequeue a node (let's call it 'current').
    • Process 'current' (e.g., print its value, check if it's the target node).
    • For each unvisited neighbor of 'current':
      • Mark the neighbor as visited.
      • Enqueue the neighbor.

This process continues until the queue is empty, meaning all reachable nodes from the source have been visited. If a specific target node is being searched for, the algorithm can terminate as soon as that node is dequeued and processed.

Key Concepts and Data Structures

Applications of BFS

BFS has numerous practical applications due to its systematic exploration strategy:

BFS vs. DFS (Depth-First Search)

It's common to compare BFS with its counterpart, Depth-First Search (DFS). While both are graph traversal algorithms, they differ significantly in their approach and use cases:

Time and Space Complexity

The efficiency of BFS is analyzed in terms of its time and space complexity:

Implementation Example (Conceptual)

Imagine a simple social network graph where people are nodes and friendships are edges. If you start a BFS from 'Alice' to find who is two degrees away:

  1. Start with Alice. Add Alice to the queue and mark her visited.
  2. Dequeue Alice. Find her friends (e.g., Bob, Carol). Enqueue Bob and Carol, mark them visited. (These are 1 degree away).
  3. Dequeue Bob. Find his friends who haven't been visited (e.g., David, Eve). Enqueue David and Eve, mark them visited. (These are 2 degrees away).
  4. Dequeue Carol. Find her friends who haven't been visited (e.g., Frank). Enqueue Frank, mark him visited. (Frank is also 2 degrees away).
  5. Continue until the queue is empty or you've found your target.

BFS systematically expands outwards, ensuring you find direct friends first, then friends of friends, and so on.

Sources

  1. Breadth-first search - WikipediaCC-BY-SA-4.0
  2. Breadth-First Search (BFS) - GeeksforGeeksfair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.