What is dfs

Last updated: April 1, 2026

Quick Answer: Depth-First Search (DFS) is a graph traversal algorithm that explores vertices by visiting nodes as far as possible along each branch before backtracking. It uses a stack data structure and is fundamental in computer science for searching and problem-solving.

Key Facts

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:

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.

Sources

  1. Wikipedia - Depth-first searchCC-BY-SA-4.0
  2. GeeksforGeeks - DFS for a GraphEducational use