Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Interactive breadth-first search visualizer
Explores graph level by level, visiting all neighbors before moving deeper
Select an algorithm and generate steps to begin visualization
Breadth-First Search (BFS) is a fundamental graph traversal algorithm that explores a graph level by level. Starting from a source node, it visits every neighbor at distance one, then every node at distance two, and so on, using a queue to keep track of the frontier. Because it always expands the closest unvisited nodes first, BFS finds the shortest path in any unweighted graph.
BFS begins by placing the start node in a queue and marking it visited. It then repeatedly removes the node at the front of the queue, inspects each of its neighbors, and appends any neighbor that has not been visited yet. This first-in, first-out discipline guarantees that nodes are processed in increasing order of distance from the source. The algorithm runs in O(V + E) time and O(V) space, where V is the number of vertices and E the number of edges.
BFS powers shortest-path queries in unweighted networks, web crawlers, social network friend suggestions, GPS broadcast search, and level-order traversal of trees. It is also the backbone of more advanced algorithms such as Edmonds-Karp for maximum flow. BFS is one of the most frequently asked topics in coding interviews, appearing in grid, maze, and word-ladder style problems.
The whole algorithm is a queue and a visited set. Everything else BFS is famous for follows from the order in which the queue hands nodes back.
BFS(graph, source):
visited = {source}
dist[source] = 0
queue = [source]
while queue is not empty:
u = queue.removeFirst()
for each neighbor v of u:
if v not in visited:
visited.add(v)
dist[v] = dist[u] + 1
parent[v] = u
queue.addLast(v)The invariant is that the queue always holds nodes from at most two consecutive distance levels, in nondecreasing order of distance. That single property is what makes dist correct: a node is assigned its distance the first time it is seen, and it can never be reached more cheaply later.
Run BFS from A on the graph the visualizer loads by default, so you can follow along step for step in the panel above.
Example graph: Undirected edges A-B (2), A-C (3), B-C (1) and C-D (4). BFS ignores the weights entirely and counts hops, so every edge is worth one.
Final distances are A 0, B 1, C 1, D 2, and the parent pointers give the shortest-path tree A to B, A to C, and C to D. Note that BFS routes to D through C even though the weighted cost that way is 7 against the 3 of A to B to C: hop count is all it optimises, which is exactly why weighted graphs need Dijkstra instead.
Time: O(V + E) · Space: O(V)
Each vertex enters the queue at most once, because it is marked visited at the moment it is enqueued rather than when it is dequeued. That bounds the outer loop at V iterations. Inside the loop, the work is proportional to the degree of the current vertex, and the sum of all degrees is 2E in an undirected graph, so the neighbor scanning totals O(E). Space is dominated by the visited set, the distance array and the queue, each O(V). On an adjacency matrix the neighbor scan becomes O(V) per vertex and the whole run degrades to O(V squared).
BFS is the right default whenever edges are unweighted. The moment weights appear, or the goal changes from distance to structure, something else wins.
| Alternative | Prefer it when | Cost |
|---|---|---|
| DFS | You need structural facts rather than distance: cycles, topological order, components, bridges. DFS also uses less memory on wide graphs. | O(V + E) |
| Dijkstra's algorithm | Edges carry nonnegative weights, so hop count no longer equals distance. | O((V + E) log V) |
| 0-1 BFS | Every weight is 0 or 1. A deque replaces the queue and beats a full priority queue. | O(V + E) |
| Bidirectional BFS | You want the distance between one specific pair in a large graph, and you can search backward from the target. | O(b^(d/2)) |
Read the full article: BFS vs DFS: When to Use Each Traversal
Related algorithms: Depth-First Search, Dijkstra's Algorithm, Bipartite Check