# Breadth-First Search (BFS)

> Walking a graph one layer at a time, nearest nodes first.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** BFS, Breadth First Search, Level-Order Traversal

## Think of it like

Think of it like ripples in a pond. They spread out one ring at a time.

## Definition

Breadth-first search walks a graph or tree one layer at a time. It visits every neighbour of the start, then every neighbour of those, and so on. It keeps the nodes it has yet to visit in a queue. Because it goes layer by layer, it finds the path with the fewest steps first. It marks each node it has seen, so loops cannot trap it.

## Example

Emma's app finds how many steps away Jack is in her friend graph. BFS checks her friends, then theirs, and finds him at step 2.

## Why it matters

It is the standard way to find the shortest path when every step costs the same.

## Common mistake

Forgetting to mark visited nodes. On a graph with loops, the search never ends.

## Three principles

- **Layer by layer.** Nearest first.
- **Uses a queue.** First in, first out.
- **Fewest steps.** Found first.

## Related terms

- [Depth-First Search (DFS)](https://learn.benibix.com/term/development/depth-first-search)
- [Graph (Data Structure)](https://learn.benibix.com/term/development/graph)
- [Queue (Data Structure)](https://learn.benibix.com/term/development/queue)
- [Dijkstra's Algorithm](https://learn.benibix.com/term/development/dijkstras-algorithm)

## Told apart from

- [BFS vs DFS](https://learn.benibix.com/compare/bfs-vs-dfs): Breadth-first search walks a graph one layer at a time and finds the path with the fewest steps. Depth-first search follows one path to its end, then backs up.

---

Source: https://learn.benibix.com/term/development/breadth-first-search
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
