You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

11.2 Depth-First Search (DFS) Algorithm

2 min readjuly 19, 2024

(DFS) is a powerful traversal algorithm that explores branches deeply before . It's like exploring a maze, going as far as possible down each path before turning back. DFS uses a to keep track of vertices, making it perfect for solving maze-like problems and detecting cycles.

DFS can be implemented recursively or iteratively, with each approach having its own advantages. The algorithm's is O(V + E), where V is the number of vertices and E is the number of edges. DFS has numerous applications, from finding paths in graphs to and solving puzzles.

Depth-First Search (DFS) Algorithm

DFS algorithm fundamentals

Top images from around the web for DFS algorithm fundamentals
Top images from around the web for DFS algorithm fundamentals
  • Graph traversal algorithm explores as far as possible along each branch before backtracking (maze, decision )
  • Visits all vertices by exploring each 's neighbors before moving to next unvisited vertex (city map, social network)
  • Traverses both directed (one-way roads) and undirected graphs (two-way paths)
  • Explores graph by going deep before backtracking
  • Uses data structure to track vertices to visit (stack of papers, browser history)
  • Implemented using recursion or iteration with stack
  • Detects cycles, explores all paths, solves maze-like problems (labyrinth, escape room)

Stack-based and recursive DFS implementation

  • Recursive implementation:
    • Base case: If current vertex already visited, return
    • Mark current vertex as visited
    • Recursively visit all unvisited neighbors of current vertex (explore all rooms before returning)
  • Iterative implementation using stack:
    • Create stack and push starting vertex (stack of locations to visit)
    • While stack not empty:
      1. Pop vertex from stack (remove top location)
      2. If popped vertex not visited:
      • Mark vertex as visited (check off location)
      • Push all unvisited neighbors onto stack (add adjacent unvisited locations)

Time and space complexity of DFS

  • Time complexity:
    • Worst case: DFS visits all vertices and edges (explore every room and hallway)
    • Graph with VV vertices and EE edges, time complexity is O(V+E)O(V + E)
  • :
    • DFS requires space to store stack of vertices (locations to remember)
    • Worst case: stack may contain all vertices, space complexity O(V)O(V)
    • Recursive implementation uses stack space for function calls, up to O(V)O(V) worst case (call stack)

Applications of DFS in graph problems

  • Detecting cycles in graph:
    • During DFS, if leads to already visited vertex (except parent), cycle exists (circular path)
    • Detects cycles in directed (one-way streets) and undirected graphs (bidirectional paths)
  • Exploring all paths:
    • Explores all possible paths from starting to target vertex (all routes from A to B)
    • Solves maze-like problems, finds path between vertices (escape room, treasure hunt)
  • Topological sorting of directed acyclic graph
    DAG
    (task dependencies, course prerequisites)
  • Finding strongly connected components in directed graph (social circles, web pages)
  • Solving puzzles and games involving exploring all states or configurations (Rubik's cube, chess)
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary