Edit the tree or graph, pick a start node, then press Play. BFS visits every node one level at a time, using a queue to remember who is next.
Not found yetIn queueVisiting nowVisitedColor shows the level, the number of steps from the start.
Step 0 of 0
Traversal order
Each node appears here as BFS visits it. The levels below show which nodes sit the same number of steps from the start.
Adjacency table
Every node and the nodes it connects to. It updates as you edit the graph. Click a row to select that node.
Node
Connected to
Level
Status
The algorithm
The highlighted lines are the ones running in the current step.
Breadth-First Search (BFS) is a graph and tree traversal algorithm that explores a structure one level at a time — visiting every neighbour of a node before moving on to their neighbours. It uses a queue (first in, first out) to keep track of which node to visit next, which is what forces the level-by-level order.
O(V + E)
Time complexity — every vertex and edge is visited once
O(V)
Space complexity — for the queue and the visited/discovered set
Queue
Core data structure — FIFO ordering drives the traversal
Shortest path
Guaranteed on unweighted graphs, counted in number of edges
How it works
Pick a start node, mark it as discovered, and push it into the queue.
Remove the node at the front of the queue — this is the node currently being visited.
Look at each of its neighbours. Any neighbour not yet discovered gets marked as discovered and pushed to the back of the queue.
Repeat steps 2–3 until the queue is empty.
Nodes come out of the queue in the same order they went in, which is why BFS finishes an entire level before starting the next one.
Key properties
Level order: all nodes at distance k from the start are visited before any node at distance k+1.
Completeness: BFS will find a node if it exists anywhere in a finite, connected structure.
Shortest path: on an unweighted graph, the first time BFS reaches a node is guaranteed to be via the shortest possible path.
Queue vs. stack: swapping the queue for a stack turns the same basic algorithm into Depth-First Search (DFS), which dives deep before it goes wide.
Worked example
Same graph as the default example in the Visualizer. Start at A; neighbours are taken in alphabetical order.