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
VertexVisit #ParentDepthStatus
No traversal yet.

Advertisement

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

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.

Worked BFS and DFS example

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.

AlgorithmTraversal orderWhat controls the next visit?
BFSA → B → C → D → E → F → GThe earliest discovered vertex still in the queue
DFSA → B → D → E → F → C → GThe 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 reachable component from the start and leaves all other vertices unvisited.

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.

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: .

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?

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.

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.

Explore more tools