ABCDEFGH
AlgoPlus//graph / bfs
Read the theory

Breadth-First Search

Explore the graph layer by layer from the source.

Stability
In-Place
Space Complexity
Avg Time
Source
Target
Legend
Current
Frontier
Visited
Path / MST
AI Tutor Workspace
In a nutshell
Breadth-first search explores a graph in rings spreading out from a start point. It uses a queue — a first-in, first-out line: visit a node, add its unvisited neighbours to the back, then take the next node from the front. Because it finishes everything one step away before anything two steps away, in an unweighted graph it reaches every node by a path with the fewest edges.
Ready
Press play to begin the cinematic walkthrough.
Energy ripples outward layer by layer — every node at distance k is visited before any at distance k+1.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Breadth-First Search.
Code Simulator
1
def bfs(graph, start):
2
    visited = []
3
    queue = [start]
4
    while queue:
5
        node = queue.pop(0)  # Dequeue
6
        visited.append(node)  # Visit
7
        for nb in graph[node]:
8
            if nb not in visited:
9
                queue.append(nb)  # Enqueue
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.