Time complexity of depth-first graph algorithm
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 (DFS) is a fundamental algorithm used for traversing or searching through graph structures, such as trees and graphs. It's particularly useful thanks to its simplicity and utility in numerous applications, from maze-solving to pathfinding and topological sorting. An essential aspect of understanding DFS is its time complexity — a measure that tells us how the execution time of the algorithm grows with the input size.
Basics of Depth-First Search
DFS works by starting at a node (usually termed the "starting" node) and explores as far as possible along each branch before backtracking. This approach is implemented using a stack data structure, either implicitly through recursive calls (which use the call stack) or explicitly where you manage the stack yourself.
Time Complexity Analysis
Time Complexity
The time complexity of DFS can be analyzed based on the:
- Vertices: The nodes present in the graph.
- Edges: The connections or paths between the nodes.
For a graph with `V` vertices and `E` edges, the time complexity of DFS can be observed as:
- Time Complexity:
This reflects that every vertex and edge in the graph is visited once in the course of the algorithm.
Explanation
- Vertices (`V`): Each vertex is visited exactly once, and once visited, it retrieves all its adjacent vertices. This operation takes constant time proportional to the number of vertices.
- Edges (`E`): Each edge in the graph is evaluated once when the algorithm checks adjacency for each node. Thus, the additional time required is proportional to the number of edges.
Why ?
- Sparse vs. Dense Graphs:
- In a sparse graph, where the number of edges is significantly less than the square of the number of vertices (`E << V^2`), the upper bound of is closer to . Here, the complexity can be perceived closer to .
- In a dense graph, where the number of edges approaches (`E ≈ V^2`), the complexity tends towards . However, regardless of the sparsity or density, the time complexity remains since every necessary check and traversal is made.
Space Complexity
Depth-first search also entails some considerations regarding space:
- Space Complexity:
When implemented recursively, DFS requires stack space proportional to the height (or depth) of the graph. In the worst-case scenario (in a skewed tree or a chain of nodes), stack space can be as large as `V` in the case of recursion.
Use Cases of DFS
Understanding the time complexity of DFS lends a hand in recognizing its practical applications:
- Pathfinding: In maze solving, DFS can efficiently explore possible paths.
- Cycle Detection: Identifying cycles in a graph can be achieved through DFS.
- Topological Sorting: Used frequently on directed acyclic graphs (DAGs), DFS can order nodes linearly.
- Connected Components: In undirected graphs, DFS can label different connected subgraphs.
- Solving Puzzles: Many puzzle-related problems, such as the Sudoku solver, can apply DFS to navigate potential solutions.
Optimization and Variations
While the standard DFS covers most needs, variations and optimizations can be employed based on specific requirements:
- Iterative DFS: To avoid issues with stack overflows, an explicit stack may be used. This iterative approach mitigates depth limitations seen in recursive solutions.
- Bidirectional Search: When searching for paths between two nodes, searching from both the start and end nodes simultaneously can offer improvements.
Summary Table
Here's a quick breakdown of the time complexity in various contexts:
| Element | Count | Contribution to Time Complexity |
| Vertices | V | Visited once |
| Edges | E | Evaluated once |
| Total | -- |
Conclusion
Depth-first search is a versatile and essential algorithm in the realm of computer science, particularly in graph-related applications. Its time complexity of makes it adaptable for use in complex structures, both sparse and dense, making it a staple tool in any developer's toolkit. Understanding and leveraging DFS effectively can help solve a myriad of computational problems efficiently.
Related reading
- Time complexity of Euclid's Algorithm
- Time complexity of fun?
- Time Complexity of Genetic Algorithm
- Time complexity of N Queen using backtracking?
- Time Complexity of the Kruskal Algorithm?
- Time complexity to generate all pairs in an array
- Time complexity of power
- Time complexity of Python 3.8's integer square root math.isqrt function

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.