Breadth-first search (BFS) and depth-first search (DFS) are the two basic ways to visit every node of a graph. BFS spreads out level by level from the start node using a queue; DFS follows one path as deep as it can before backtracking, using a stack. This visualizer runs both on the same graph and shows the queue or stack contents, the visit order and the edges used to reach each node.
The graph is yours to change: add nodes, connect or disconnect them, delete nodes, or generate a random graph. Then pick a start node and press Run. Step back and forth to see exactly why BFS finds the shortest path in an unweighted graph and DFS does not.
How to use the BFS and DFS Graph Visualizer tool
- Choose BFS or DFS and a start node (or click a node in Pick start mode).
- Optionally edit the graph: Add node (click an empty spot), Add or remove edge (click two nodes) or Delete node.
- Press Run and watch: the orange node is being visited, purple nodes wait in the queue or stack, green nodes are done.
- Read the queue or stack and the visit order below the graph, and the highlighted pseudocode line.
Why use ToolOrca BFS and DFS Graph Visualizer
- BFS with a queue and DFS with a stack
- Editable graph: add and delete nodes and edges
- Random and sample graphs
- Queue or stack contents and visit order at every step
- Edges of the BFS or DFS tree highlighted
- Code in C++, Java, Python and JavaScript
How breadth-first search works
BFS puts the start node in a queue and marks it as seen. Then it repeats: take the node at the front of the queue, visit it, and add each neighbour that has not been seen yet to the back of the queue. Because a queue is first in, first out, all nodes at distance 1 are visited before any node at distance 2, and so on.
That level-by-level order means the BFS tree gives the shortest path (fewest edges) from the start to every reachable node in an unweighted graph. BFS is used for shortest routes in grids and mazes, finding people within k connections in a social network, and web crawlers.
How depth-first search works
DFS uses a stack (or recursion, which uses the call stack). Pop a node, skip it if it was already visited, otherwise visit it and push its unvisited neighbours. The most recently found node is explored first, so DFS dives down one branch until it hits a dead end and then backtracks.
DFS is the basis of cycle detection, topological sorting, finding connected components, solving puzzles and mazes by backtracking, and finding bridges and articulation points. It uses less memory than BFS on wide graphs but does not find shortest paths.
Complexity of BFS and DFS
With an adjacency list, both visit every vertex once and look at every edge once (twice in an undirected graph), so both run in O(V + E) time with O(V) extra memory for the visited set and the queue or stack. With an adjacency matrix, finding neighbours costs O(V) per node, so both become O(V²).
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is the difference between BFS and DFS?
BFS explores neighbours level by level using a queue; DFS goes as deep as possible along one path using a stack, then backtracks. BFS finds shortest paths in unweighted graphs; DFS is better for cycle detection, topological sort and backtracking.
Why does BFS find the shortest path?
Its queue visits all nodes at distance d before any node at distance d + 1, so the first time a node is reached is always by a path with the fewest edges.
What is the time complexity of BFS and DFS?
O(V + E) with an adjacency list, where V is the number of vertices and E the number of edges.
Can DFS be written without recursion?
Yes. Replace the call stack with an explicit stack, as this visualizer does. Pushing neighbours in reverse order makes it visit them in the same order as the recursive version.
What happens with nodes that cannot be reached?
They are never visited. Run BFS or DFS again from an unvisited node to find each connected component.