Breadth-first search
BFS uses a first-in, first-out queue. It visits the start, then all vertices one edge away, then all vertices two edges away, and so on.
queue: remove front, add neighbors at back
Private by design: your graph is parsed and traversed on this device. It is not uploaded, stored, or included in the page URL.
| Vertex | Visit # | Parent | Depth | Status |
|---|---|---|---|---|
| No traversal yet. | ||||
Both algorithms systematically visit vertices reachable from a chosen start, but they choose the next vertex differently. The visualizer uses adjacency lists and always considers neighbors in vertex declaration or first-appearance order.
BFS uses a first-in, first-out queue. It visits the start, then all vertices one edge away, then all vertices two edges away, and so on.
queue: remove front, add neighbors at back
DFS uses a last-in, first-out stack or recursion. It follows the first unvisited branch and backtracks when that branch has no unvisited neighbor.
stack: follow child, then backtrack
With adjacency lists, each reachable vertex and edge is examined a constant number of times.
time: O(V + E)
active frontier: O(V)
Shortest-path distinction: in an unweighted graph, BFS assigns the minimum number of edges from the start to each reachable vertex. DFS produces a valid traversal tree, but its tree depths are not necessarily shortest-path distances.
For the sample graph and start A, neighbors are considered in the order B then C. BFS spreads by level, while DFS finishes the B branch before returning to C. Cross-edge E–F does not revisit F after it has already been discovered.
| Algorithm | Traversal order | What controls the next visit? |
|---|---|---|
| BFS | A → B → C → D → E → F → G | The earliest discovered vertex still in the queue |
| DFS | A → B → D → E → F → C → G | The first unvisited neighbor of the active stack vertex |
Enter one edge per line as start,end. In directed mode, A -> B is also accepted. Edge endpoints are added automatically, while the optional vertex field preserves isolated vertices.
Vertex declaration order comes first; labels first seen in edges are appended. Each adjacency list follows that order. Reordering vertices can therefore change a valid BFS or DFS traversal without changing reachability.
Undirected edges can be crossed both ways. Directed edges only go from their first endpoint to their second. The tool traverses the reachable component from the start and leaves all other vertices unvisited.
Repeated edges are merged. Self-loops are allowed but never cause a second visit. The visualizer accepts up to 50 vertices and 2,000 unique edges; labels can contain up to 80 characters.
The queue and recursive-style stack behavior follows the standard graph-search presentation in Princeton University’s Algorithms, 4th Edition graph materials. The displayed DFS includes backtracking steps, so it can have more playback steps than BFS even when both reach the same vertices.
Traversal note: directed, undirected, cyclic, disconnected, duplicate-edge, self-loop, isolated-vertex, single-vertex, and invalid-input cases checked by the Starlight Tools editorial team. Last reviewed: .
Breadth-first search explores all reachable vertices at one edge-distance before moving to the next level, using a queue. Depth-first search follows one branch as far as possible before backtracking, using a stack or recursion.
BFS finds a path with the fewest edges from the start vertex in an unweighted graph. It does not minimize arbitrary edge weights; use a weighted shortest-path algorithm for that problem.
A graph traversal order depends on how neighbors are ordered. This visualizer uses vertex declaration and first-appearance order, making results deterministic for the same input.
A traversal started at one vertex only visits vertices reachable from that start. Disconnected or directionally unreachable vertices remain unvisited and are reported in the results.
Choose Directed graph, then enter A,B or A -> B. Both mean an edge from A to B. Add B,A separately if the edge should also be traversable in reverse.
Yes. Duplicate edges are merged, and self-loops are displayed. Neither causes a vertex to be visited twice.
With adjacency lists, both run in O(V + E) time and use O(V + E) total graph and traversal storage. The active queue or stack can contain O(V) vertices.
No. Parsing, traversal, visualization, copying, and CSV preparation run locally in your browser. The tool does not transmit or store your input.