Breadth-First and Depth-First Search Graph Visualizer
Compare BFS and DFS on the same directed or undirected graph. See traversal order, queue or stack state, parent edges, and vertex depths at every step—all locally in your browser.
Enter an unweighted graph
Each edge can be traversed in both directions.
Separate labels with commas, semicolons, or lines. List isolated vertices here.
Format: A,B7 lines
Labels are case-sensitive. Both searches use this start.
Private by design: your graph is parsed and traversed on this device. It is not uploaded, stored, or included in the page URL.
BFS and DFS comparison
The sample graph is calculated below.
Show:
UnvisitedFrontierCurrentVisitedTraversal tree edge
Step 0 of 0
ReadyCalculate a graph to inspect both algorithms.
Vertex states at the selected traversal step
Vertex
Visit #
Parent
Depth
Status
No traversal yet.
How breadth-first and depth-first search differ
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.
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
Depth-first search
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
Complexity
Let V be the number of vertices and E the number of edges in the input graph. Standard adjacency-list BFS and DFS examine each reachable vertex and edge a constant number of times, taking O(V + E) time and O(V + E) total graph and traversal storage, with an O(V) active queue or stack. These bounds exclude parsing, neighbor ordering, recorded playback states, and rendering, which may add costs not quantified here.
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.
Worked BFS and DFS example
For the stated sample and start A, A's neighbors are considered in the order B then C. BFS spreads by level. DFS can reach C through F while exploring the B branch, before backtracking to A. The E–F edge can discover F from E in DFS, whereas BFS can have discovered F before examining that edge from E; neither search discovers a vertex twice. The complete sample input is not shown here, so the listed traversal orders cannot be reproduced from this example alone. Reproducing a traversal requires the vertex declaration order, complete edge list, graph mode, and start vertex. A traversal parent is the vertex from which a vertex is first discovered. The start has no parent and has depth zero; every other discovered vertex's depth is the number of parent-tree edges from the start. BFS depth gives the minimum edge distance in an unweighted graph, while DFS depth need not. An unvisited vertex at an intermediate playback step may still be reached later; a vertex left unvisited after the single-start traversal finishes is unreachable from that start under the selected edge directions.
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
Input rules, assumptions, and limits
Edge format
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.
Deterministic neighbor order
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.
Directed and disconnected graphs
Undirected edges can be crossed both ways. Directed edges only go from their first endpoint to their second. The tool traverses the vertices reachable from the start by following the selected edge directions and leaves all other vertices unvisited. In directed mode, a vertex can belong to the same underlying undirected component as the start yet remain unreachable. Traversal uses only the selected start; it does not restart at unvisited vertices.
Duplicates and limits
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.
For background on standard BFS and DFS graph-search methods, see Princeton University’s Algorithms, 4th Edition graph materials. This algorithm reference does not verify this visualizer's implementation or privacy claims. The stated limits, neighbor ordering, duplicate handling, and privacy behavior are not independently verified here. The displayed DFS is described as including backtracking steps, which can produce more playback steps than BFS even when both reach the same vertices.
Verification limits: Checks of directed, undirected, cyclic, disconnected, duplicate-edge, self-loop, isolated-vertex, single-vertex, and invalid-input cases are not documented here. The unverified review date is .
BFS and DFS graph visualizer FAQ
What is the difference between BFS and DFS?
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.
Does BFS always find the shortest path?
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.
Why can my BFS or DFS order differ from another answer?
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.
What happens to disconnected vertices?
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.
How are directed edges entered?
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.
Are self-loops and duplicate edges allowed?
Yes. Duplicate edges are merged, and self-loops are displayed. Neither causes a vertex to be visited twice.
What are the time and space complexities of BFS and DFS?
Let V be the number of vertices and E the number of edges in the input graph. Standard adjacency-list BFS and DFS 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. These bounds cover standard traversal and graph storage. Parsing, neighbor ordering, recorded playback states, and rendering may introduce additional costs not quantified here.
Does the visualizer save my graph?
No. Parsing, traversal, visualization, copying, and CSV preparation run locally in your browser. The tool does not transmit or store your input.