LOGBOOK

HELP

Quiz Entry - updated: 2026.11.27

What does a depth-first search (DFS) do, and what does it tell you about a graph?

DFS starts at a vertex and keeps moving on to an unvisited neighbour, as deep as it can; only when the current vertex has no unexplored edges left does it back up to the vertex it came from and carry on there. In $O(n + m)$ time it visits every vertex and edge, decides whether the graph is connected, finds its connected components and builds a spanning forest.

The name describes the order: DFS commits to one route and follows it to the end before trying alternatives, the way a caver follows one passage to its end before coming back to try the next fork. Backing up is what makes the search complete: a vertex is left for good only after all its unexplored edges have been tried.

What one run provides:

  • Every vertex and edge visited, each edge labelled a discovery edge (it led to a new vertex) or a back edge (it led to a vertex reached before).
  • Connectivity: a search started at one vertex reaches exactly that vertex's connected component, so the graph is connected exactly when that one search reaches all $n$ vertices.
  • Connected components and a spanning forest: restarting from every vertex not yet visited explores one component per start, and the discovery edges form a spanning tree of each.
  • Extensions: with a stack of the current route, DFS also finds a path between two given vertices, or a cycle.

Go deeper:

  • doc Depth-first search โ€” its properties, the kinds of edges it produces, vertex orderings and applications.

From Quiz: ADS / Graph Traversals: Depth-First and Breadth-First Search | Updated: Nov 27, 2026