Codacoda
Back to Academy

searching

Depth-First Search

Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It uses a stack (or recursion) to keep track of vertices to visit next. DFS is foundational for detecting cycles, topological sorting, and finding connected components in graphs.

Use Cases

  • Detecting cycles in directed and undirected graphs
  • Topological sorting of dependency graphs
  • Solving maze and puzzle problems

Complexity Analysis

MetricBestAverageWorst
TimeO(V + E)O(V + E)O(V + E)
SpaceO(V)

Visualization

4251810263ABCDEF
Speed:1x
Graph readyStep 1 / 8

Implementation

Output

Click "Run Code" to see output...