BFS Visualizer
Visualize Breadth-First Search as it explores a graph level by level. Build your own graph, pick a starting node, and watch BFS visit every reachable node in order of distance.
How It Works
Breadth-First Search (BFS) is a graph traversal algorithm that explores all neighbors of a node before moving on to the next level of neighbors. It uses a queue to process nodes in the order they are discovered.
BFS is guaranteed to visit nodes in order of their distance from the source, making it ideal for finding the shortest path in unweighted graphs. It was first described by Konrad Zuse in 1945 and later reinvented by Edward F. Moore in 1959.
- 1
Start at the source node and mark it as visited
- 2
Add the source node to a queue
- 3
Dequeue the front node from the queue
- 4
Visit all unvisited neighbors of the current node and add them to the queue
- 5
Mark each newly discovered neighbor as visited
- 6
Repeat until the queue is empty
Key Properties
Time Complexity
O(V + E) where V is the number of vertices and E is the number of edges in the graph.
Level-Order
Visits nodes layer by layer — all nodes at distance 1 before distance 2, and so on.
Unweighted Shortest Path
Finds the shortest path in unweighted graphs since it explores in order of distance from the source.
Queue-Based
Uses a FIFO queue to ensure nodes are processed in the order they were discovered.
Common Use Cases
- Web crawlers — discovering pages level by level from a seed URL
- Social networks — finding degrees of separation between users
- Network broadcasting — reaching all nodes in minimum hops
Frequently Asked Questions
- What is Breadth-First Search?
- Breadth-First Search (BFS) is a graph traversal algorithm that explores all neighbors of a node before moving to the next level. It uses a FIFO queue to visit nodes in order of their distance from the source, guaranteeing level-by-level exploration.
- What is the time complexity of BFS?
- BFS runs in O(V + E) time, where V is the number of vertices and E is the number of edges. Each vertex and edge is processed exactly once.
- What is the difference between BFS and DFS?
- BFS explores all neighbors at the current depth before going deeper (level by level), while DFS dives as deep as possible along each branch before backtracking. BFS uses a queue; DFS uses a stack.
- Does BFS find the shortest path?
- Yes, in unweighted graphs. Since BFS visits nodes in order of distance from the source, the first time it reaches any node is via the shortest path. For weighted graphs, use Dijkstra's algorithm instead.
- Which algorithm was reinvented by Edward F. Moore in 1959?
- Breadth-First Search (BFS). Edward F. Moore independently developed BFS in 1959 while working on maze-solving and shortest-path problems. BFS had been described earlier by Konrad Zuse in 1945. Moore's formulation is the version commonly taught today — exploring all neighbors before advancing to the next level.
Ready to see it in action?
Open the Visualizer