Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Interactive depth-first search visualizer
Explores as far as possible along each branch before backtracking
Select an algorithm and generate steps to begin visualization
Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. Starting from a source node it follows one path until it reaches a dead end, then backs up to the most recent branching point and tries the next unexplored edge, typically using recursion or an explicit stack.
DFS marks the start node visited, then recursively visits the first unvisited neighbor, going deeper at each step. When a node has no unvisited neighbors, the recursion unwinds and the search resumes from the previous node. Each vertex and edge is handled exactly once, giving O(V + E) time and O(V) space for the visited set and recursion stack. The order in which nodes enter and leave the recursion yields discovery and finish times used by many derived algorithms.
DFS is the foundation for topological sorting, cycle detection, strongly connected components, articulation points, bridges, and maze generation. In practice it underlies dependency resolution in build tools, deadlock detection, and puzzle solvers. Interviewers use DFS constantly in problems involving backtracking, islands in grids, and path enumeration.
DFS is usually written recursively, but the iterative form makes the stack explicit and avoids blowing the call stack on deep graphs. Both produce the same discovery order.
DFS(graph, source):
time = 0
visit(source)
visit(u):
visited.add(u)
disc[u] = ++time // discovery time
for each neighbor v of u:
if v not in visited:
parent[v] = u
visit(v)
fin[u] = ++time // finish timeThe discovery and finish times are the real product of DFS. The interval [disc[u], fin[u]] of a descendant nests strictly inside its ancestor, and that nesting property is what topological sort, cycle detection, Tarjan strongly connected components, articulation points and bridges are all built on.
Run DFS from A on the graph the visualizer loads by default, always taking neighbors in alphabetical order.
Example graph: Undirected edges A-B (2), A-C (3), B-C (1) and C-D (4). DFS ignores the weights.
The traversal order is A, B, C, D. BFS happens to visit these four nodes in the same order, but the trees differ: BFS builds a shallow tree with A to B, A to C and C to D, while DFS builds the single chain A to B to C to D. The back edge C to A identifies the cycle A-B-C-A, and the nested intervals A[1,8], B[2,7], C[3,6], D[4,5] show the recursion depth directly.
Time: O(V + E) · Space: O(V)
Each vertex is visited exactly once because the visited check guards the recursive call, and each edge is examined once from each endpoint, giving 2E inspections in an undirected graph and E in a directed one. Space is the visited set plus the recursion stack, both O(V). The recursion depth is the length of the longest simple path, so on a path graph of a million nodes a recursive implementation will overflow the call stack in most languages and you need the explicit-stack form.
Choose DFS when the question is about structure. Choose BFS when the question is about distance.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS | You need the fewest hops, level order, or the graph is very deep and shallow answers are likely near the source. | O(V + E) |
| Iterative deepening | The graph is effectively infinite or extremely deep and you still want the shallowest solution without the memory cost of BFS. | O(b^d) |
| Tarjan strongly connected components | You specifically want SCCs of a directed graph. It is DFS plus low-link bookkeeping in one pass. | O(V + E) |
| Union-Find | You only need connected components in an undirected graph and edges arrive incrementally. | near O(E) |
Read the full article: BFS vs DFS: When to Use Each Traversal
Related algorithms: Breadth-First Search, Topological Sort, Cycle Detection