BFS vs DFS Explorer Screensaver
The BFS vs DFS Explorer screensaver runs real breadth-first and depth-first search on the same random graph side by side, so you can watch their traversal orders diverge live. It is free, runs in your browser, and ESC exits.
How the BFS vs DFS Explorer screensaver works
The saver builds one real random graph and explores two identical copies of it at once: one with real breadth-first search, one with real depth-first search.
Breadth-first search visits every neighbor of the current layer before moving deeper, so it spreads outward as a widening ring.
Depth-first search commits to one branch and follows it as far as possible before backtracking, so it snakes deep before it spreads.
Once both traversals finish visiting every reachable node, the graph fades and a fresh random graph begins.
Two ways to walk a graph
Breadth-first search and depth-first search are the two basic strategies for visiting every node in a graph that can be reached from a starting point. They differ in the order they take the next node from the waiting list. Breadth-first uses a queue, so it handles all nodes at distance one before any at distance two. Depth-first uses a stack or recursion, so it follows a path as deep as it can and backtracks at a dead end.
That small difference gives them distinct strengths. On a graph where every edge counts the same, breadth-first finds a path with the fewest edges. Depth-first is the natural tool for finding connected and strongly connected components, topological sorting, finding bridges and solving mazes. On a full traversal both take time proportional to the number of nodes plus edges, but on a huge graph searched to a limited depth, depth-first needs memory proportional to that depth, far less than breadth-first.
The ideas are old. Konrad Zuse devised breadth-first search in 1945 in a thesis that went unpublished until 1972 and Edward Moore reinvented it in 1959 to find the shortest path out of a maze. Depth-first search was studied in the nineteenth century by Charles Pierre Trémaux as a maze-solving strategy. Neither finds the cheapest route once edges carry different weights; Dijkstra’s algorithm handles that.
A worked example
On the same graph, breadth-first search lights up an even, expanding ring of nodes, while depth-first search draws a single long, winding thread that only branches once it hits a dead end.
Settings & tips
- Accent recolours the visited-node trail.
- Both traversals run on the exact same real random graph each time.
- A fresh graph generates automatically once both traversals finish.
Frequently asked questions
- Are these real BFS and DFS implementations?
- Yes — real, correct breadth-first and depth-first search, run on identical copies of the same graph.
- Why do they look so different?
- BFS explores layer by layer with a queue; DFS commits to one path with a stack, so their real visit orders diverge sharply.
- Is a key needed?
- No — it is a pure local computation.
- Is it free?
- Yes — free, no download, in your browser.
- Which one finds the shortest path?
- Breadth-first search does, when path length is counted in edges, since it reaches nodes in order of how many steps they are from the start. With different edge weights you need Dijkstra’s algorithm or something similar.
- When is depth-first search better?
- It suits problems like finding connected components, topological sorting and backtracking through mazes. On very large or infinite graphs searched to a limited depth it needs far less memory than breadth-first.
- Do both visit every node?
- Both visit every node reachable from the start. If the graph has disconnected parts, the search has to be restarted from a node in each one.