Graph Traversal Generator
Two ways to walk a graph, on the same graph, so the difference is visible rather than described. Breadth-first fans out one step at a time, which is why its order also gives the shortest hop count to every node. Depth-first commits to one branch until it runs out, so it reaches distant nodes early and near ones late. Neighbours are taken in alphabetical order, so both orders are reproducible and can be worked out by hand.
What this generator does
Runs both traversals from the first node, taking neighbours in alphabetical order so the result is deterministic and hand-checkable. It also reports each node's distance from the start in edges, which is what breadth-first order produces as a by-product.
How to use this tool
- Choose the graph size and density.
- Write out the breadth-first order yourself, then the depth-first one.
- Compare against both orders shown.
- Check each node's distance from the start against your own count.
Understanding the controls
- How many nodes
- Between 4 and 12. Around eight is where the two orders start to differ interestingly.
- Extra edge density (%)
- Sparse graphs make depth-first wander further before backtracking; dense ones flatten the level structure.
- Seed
- Reproduces the same graph, so the same traversal exercise can be set again.
Common use cases
- Practice at both traversal orders with an answer to check
- Showing that breadth-first order gives shortest hop counts for free
- Teaching why the two orders differ on the same graph
- Test instances for a traversal implementation
- Setting the same graph for a group from a seed
How this generator works
Breadth-first uses a queue and records the level each node was reached at; depth-first uses recursion. The check requires both orders to cover every node exactly once, breadth-first order never to visit a nearer node after a further one, and every node's level to be exactly one more than its closest neighbour's.
Randomness and fairness
The graph is random; both traversal orders are deterministic given the graph, because neighbours are always taken alphabetically. Seeded graphs reproduce exactly and are therefore explicitly not cryptographically secure, and unseeded ones use the browser's cryptographically secure generator.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Traversals always start at the first node rather than one you pick.
- Only connected graphs, so neither traversal ever has to restart from an unvisited component.
- Edge weights are generated but ignored — traversal counts hops, not cost.
- No animation of the walk; both finished orders are shown.
- Graphs are not kept between visits; seed one you want again.
Privacy and your data
The graph and both traversal orders are computed in your browser. Nothing is transmitted or stored.
Related generators
- Shortest Path GeneratorWeighted graphs with Dijkstra's shortest route, verified by the relaxation condition rather than by trusting the code.
- Euler Path GeneratorTrails that use every edge exactly once, with the odd-degree count that decides whether one can exist at all.
- Binary Search Tree GeneratorAn insertion order and the tree it builds, with all three traversals, the height, and whether it came out balanced.
- Random Graph GeneratorRandom graphs at a density you choose, drawn and exported as an edge list or DOT, with connectivity measured rather than assumed.