What is dfs
Last updated: April 1, 2026
Key Facts
- DFS has a time complexity of O(V + E), where V represents the number of vertices and E represents the number of edges in a graph
- The algorithm can be implemented recursively using the call stack or iteratively using an explicit stack data structure
- Unlike Breadth-First Search (BFS), DFS does not guarantee finding the shortest path in unweighted graphs
- DFS is commonly used for topological sorting, cycle detection, connected components analysis, and solving maze problems
- The algorithm explores one branch completely before moving to the next, making it useful for detecting all reachable nodes in a graph
Definition and How It Works
Depth-First Search is a graph traversal algorithm that systematically explores vertices (nodes) in a graph. Starting from a source vertex, DFS visits a vertex, marks it as visited, and then recursively visits all unvisited adjacent vertices. When no unvisited adjacent vertices remain, the algorithm backtracks to explore other branches.
Algorithm Steps
The basic DFS algorithm follows these steps:
- Start at a designated source vertex and mark it as visited
- Explore one adjacent unvisited vertex by recursively applying DFS
- When all adjacent vertices of the current vertex have been visited, backtrack to the previous vertex
- Continue until all reachable vertices have been visited
- If the graph is disconnected, start DFS from an unvisited vertex to explore remaining components
Implementation Methods
DFS can be implemented in two primary ways. The recursive approach uses the call stack to automatically manage the backtracking process, resulting in clean and intuitive code. The iterative approach uses an explicit stack data structure to simulate the recursive behavior, offering more control and avoiding potential stack overflow issues with very deep graphs.
DFS vs. Breadth-First Search
While both DFS and BFS are graph traversal algorithms, they differ significantly. DFS explores as deeply as possible before backtracking, using a stack structure. BFS explores all neighbors at the current distance before moving further, using a queue structure. BFS guarantees the shortest path in unweighted graphs, while DFS does not. The choice between them depends on the specific problem requirements.
Applications and Use Cases
DFS has numerous practical applications in computer science. It's used for detecting cycles in graphs, performing topological sorting for directed acyclic graphs, finding strongly connected components, solving puzzles and mazes, and analyzing social networks. DFS is also fundamental in artificial intelligence for game tree searching and backtracking algorithms.
Complexity Analysis
The time complexity of DFS is O(V + E), where V is the number of vertices and E is the number of edges. This linear complexity makes it efficient for large graphs. The space complexity is O(V) for the recursive call stack or explicit stack, depending on implementation. These favorable complexities make DFS suitable for real-world applications with large datasets.
Related Questions
What is the difference between DFS and BFS?
DFS explores as deeply as possible before backtracking using a stack, while BFS explores level by level using a queue. BFS guarantees the shortest path in unweighted graphs, whereas DFS does not. Choose BFS for shortest path problems and DFS for problems requiring deep exploration.
What are practical applications of DFS?
DFS is used for topological sorting, cycle detection, finding connected components, solving mazes and puzzles, analyzing networks, and game tree searching. Its efficiency makes it suitable for problems requiring deep exploration or backtracking algorithms.
How does recursive DFS differ from iterative DFS?
Recursive DFS uses the call stack automatically, resulting in simpler code but risk of stack overflow with very deep graphs. Iterative DFS uses an explicit stack, offering more control and avoiding stack overflow issues while requiring more complex implementation.
More What Is in Daily Life
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Wikipedia - Depth-first searchCC-BY-SA-4.0
- GeeksforGeeks - DFS for a GraphEducational use