Breadth-First Search (BFS) is an algorithm used for traversing or searching tree or graph data structures. It starts at the tree root and explores all nodes at the present depth prior to moving on to nodes at the next depth level.

BFS Algorithm Characteristics

  • It uses a queue data structure.
  • It explores all the vertices of a graph in breadth-first order.
  • It is used for traversing or searching tree or graph data structures.
  • It is not suitable for weighted graphs.

BFS Algorithm Steps

  1. Create a queue and enqueue the root vertex.
  2. While the queue is not empty:
    • Dequeue a vertex from the queue and print it.
    • Enqueue all its adjacent vertices that are not visited.

Example

Consider the following graph:

    A
   / \
  B   C
 / \   \
D   E   F

BFS traversal will be: A -> B -> C -> D -> E -> F

Visualization

Here is a visual representation of the BFS algorithm applied to the above graph:

    A
   / \
  B   C
 / \   \
D   E   F

[

BFS Visualization
]

Learn More

For a deeper understanding of BFS and other algorithms, visit our Algorithms page.


If you're interested in exploring more about algorithms, check out our Data Structures section.