Time/Space Complexity of Depth First Search
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Depth-first search, or DFS, is one of the standard graph traversal algorithms. The textbook answer for its complexity is usually O(V + E) time and O(V) space, but those numbers depend on how the graph is stored and whether the implementation is recursive or iterative. Once you break the algorithm into visited vertices, scanned edges, and stack usage, the result becomes straightforward.
Why DFS Runs in O(V + E) Time
Assume the graph uses an adjacency list. DFS visits each reachable vertex once and scans each adjacency list once. Across the full traversal, that means it touches every vertex and every edge a bounded number of times.
Here is a simple iterative implementation in Python:
The loop does not multiply into O(V * E). Each vertex is marked once, and each edge in the adjacency lists is considered while iterating over neighbors. That gives:
- '
O(V)for vertex visits' - '
O(E)for neighbor scans' - total
O(V + E)
In an undirected graph, each edge appears in two adjacency lists, but that is still linear in the size of the input representation.
Why the Space Complexity Is O(V)
DFS needs memory for two main structures:
- a
visitedset - a traversal stack, either explicit or from recursion
Both can grow to the number of vertices in the worst case. A path-shaped graph is the easiest way to see this. If the graph is just a long chain, DFS may hold nearly every vertex on the current path before it finishes.
A recursive version has the same asymptotic behavior:
The difference is practical rather than asymptotic. Recursive DFS uses the language call stack. Iterative DFS uses an explicit stack you control.
Representation Changes the Result
The standard O(V + E) analysis assumes adjacency lists. If the graph is stored as an adjacency matrix, finding neighbors requires scanning an entire row of length V for each vertex. That changes the time complexity to O(V^2).
So the correct statement is:
- adjacency list DFS:
O(V + E)time - adjacency matrix DFS:
O(V^2)time
This is why sparse graphs are usually represented with adjacency lists.
Trees Are a Special Case
For a tree with N nodes, the edge count is N - 1, so DFS is often described as O(N) time. Space is often described as O(H), where H is the tree height, because the traversal stack only holds the current path.
That still fits the graph result. A tree is just a constrained graph with fewer edges.
Common Pitfalls
- Forgetting that
O(V + E)assumes adjacency lists. - Ignoring the
visitedset in cyclic graphs. - Treating recursion as free. Deep graphs can overflow the call stack even when the big-O analysis looks fine.
- Confusing tree complexity with general graph complexity.
- Claiming DFS is
O(E)and skipping the cost of vertex bookkeeping.
Summary
- DFS on an adjacency-list graph runs in
O(V + E)time. - Its auxiliary space is
O(V)because of the stack and visited set. - Recursive and iterative DFS have the same asymptotic complexity.
- With an adjacency matrix, DFS becomes
O(V^2)time. - Trees are often summarized as
O(N)time because they haveN - 1edges.
Related reading
- Tinyurl-style unique code potential algorithm to prevent collisions
- Tips implementing permutation algorithm in Java
- To make a distance matrix or to repeatedly calculate distance
- To print the boundary of Binary Tree
- .toArraynew MyClass0 or .toArraynew MyClassmyList.size?
- ''too many values to unpack'', iterating over a dict. keystring, valuelist
- Tips for optimizing C/.NET programs
- to drawRect or not to drawRect when should one use drawRect/Core Graphics vs subviews/images and why?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.