liveBook · Manning

Depth-first search

As explained in Advanced Algorithms and Data Structures

Depth-first search is a graph traversal algorithm that explores a path as far as possible before backtracking to explore remaining branches.

Depth-first search (DFS) traverses a graph by following one path as far as possible, then backtracking to the previous branching point. Like exploring a maze, it continues until reaching a dead end before choosing another route. Unlike breadth-first search, DFS is not designed to find shortest paths; it is useful for understanding graph structure and visiting vertices broadly. section 14.3.4 section 14.3.5

A recursive DFS maintains the traversal state through a call stack:

  • For a complete traversal, restart DFS from each still-unvisited vertex; one start may not reach every component. section 14.3.4

Choosing a traversal

Starting DFS at v1 produces one depth-oriented visitation order. Starting at v4 may leave vertices v1–v3 unreachable when the graph contains separate strongly connected components, so a later restart is required. Entry and exit times record when each vertex is entered and completed.

How Advanced Algorithms and Data Structures covers it

How deep it goes
Explains recursive and stack-based traversal, visited checks, restarts, and entry or exit times, but stops short of shortest-path computation.
Examples
Demonstrates maze-like backtracking, graph starts at v1 or v4, disconnected components, and recursive traversal pseudocode.
What it assumes
Assumes adjacency lists, vertices, edges, and directed graphs while treating the starting vertex and graph connectivity explicitly.
What you can do after
Apply it to traverse graphs recursively or with an explicit stack, restart across components, and record vertex processing times.
How it connects
Contrasts with breadth-first search and connects to stacks, directed graphs, topological sorting, and strongly connected components.

Also explained in

Compare how 7 books explain depth-first search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.