How to run a traversal 01 Build the graph. Pick a preset and press Build — a Grid with rows 4 and cols 5 gives 20 nodes and 31 unit edges — or draw edges by clicking two nodes in turn in Move / connect. 02 Choose BFS or DFS in the Traversal tab and pick a Source; after Build the source is the first preset node. 03 Read the result table. BFS prints visit order, max layer, unreachable and tree edges; DFS prints preorder, postorder and back edges; the canvas colours visited nodes and tree edges. 04 Step through. Play, Prev / Next and the slider walk the same snapshots, so a class can see the queue or the stack move.
Worked readouts Every number below is read from the panel.
Run
Readout
Grid 4 × 5, BFS from 1-1
visit order 1-1 → 1-2 → 2-1 → 1-3 → 2-2 → 3-1 → … → 4-5 ; max layer 7 ; unreachable — ; tree edges 19
Grid 4 × 5, DFS from 1-1
preorder 1-1 → 1-2 → 1-3 → 1-4 → 1-5 → 2-5 → … → 4-1 ; postorder starts 4-1 → 4-2 → 4-3 → 4-4 → 4-5 … ; back edges 31
Cycle C₅, BFS from A
visit order A → B → E → C → D ; max layer 2 ; tree edges 4
Cycle C₅, DFS from A
preorder A → B → C → D → E ; postorder E → D → C → B → A
Default graph, BFS from A
visit order A → B → C → D → E → F ; max layer 3 ; tree edges 5
Default graph, DFS from A
preorder A → B → C → D → E → F ; postorder F → E → D → C → B → A ; back edges 9
On the grid, BFS reaches the far corner in 7 hops — the Manhattan distance between 1-1 and 4-5 — and its 19 tree edges form one spanning tree of the 20 nodes.
The identical-order trap On the default graph the BFS visit order and the DFS preorder print the same string, A → B → C → D → E → F , so the order row alone does not identify the algorithm. The other rows do: BFS puts F in layer 3 , while DFS finishes in F → E → D → C → B → A and counts 9 back edges. On an undirected graph the back-edge row counts arcs, not edges — the grid reports 31 because its 31 edges are seen from both ends (19 tree arcs and 12 forward arcs make up the other half of 62). Read the whole table, not the first row.
Where traversal is useful Checking reachability first BFS lists every node the source cannot reach. Build Random G(n,p) with n 6, p 0.25, seed 20260731: the canvas holds components A, B, D, E, F and the isolated C , and BFS from A names C as unreachable — the same split the components algorithm reports as two components, largest 5.
Teaching the queue and the stack Run both algorithms on C₅ from A. BFS fans out to both neighbours before going deeper (A → B → E → C → D ); DFS commits to one direction and turns back only at the end (A → B → C → D → E with postorder E → D → C → B → A ). The step slider shows the frontier shrinking in one case and growing in the other.
Limits Hops, not weights. Both traversals count edges; the weights you type in the editor do not change anything here. Weighted routes belong to the shortest path panel. One source at a time. There is no multi-source BFS: run the sources in turn. The panel also does not summarise the whole graph (diameter, girth, centrality) or order it topologically — those live in metrics & centrality and order & connectivity .
Direction follows the toggle. On a directed graph both searches follow arrows only; nodes reached by an incoming edge stay unreachable until you flip the toggle back.
Privacy Graph editing and both searches run inside your browser; nothing is uploaded, and the last graph is kept in local storage until site data is cleared.
References
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein, Introduction to Algorithms , 4th ed., MIT Press, 2022, mitpress.mit.edu (访问日期:2026-10-07)— BFS and DFS, queue and stack discipline.
Wikipedia, Breadth-first search , en.wikipedia.org (访问日期:2026-10-07)— layers, distance and tree edges.
Wikipedia, Depth-first search , en.wikipedia.org (访问日期:2026-10-07)— preorder, postorder and back edges.
Calculators in this hub
Hand-picked tools, one click away. The mini versions compute live and carry your values into the full calculator.
Sources & review
Reviewed by CalcX Editorial Team
Updated 2026-10-07