A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Depth-First Search (DFS)

College Depth 76 in the knowledge graph ☐ I know this ☆ Set as goal
7topics build on this
407prerequisites beneath it
See this on the map →
Introduction to Graph TheoryBig-O Notation and Asymptotic Analysis→→Cycle Detection in Directed GraphsGraph Traversal: Depth-First and Breadth-First Search+2 more
graph-algorithms traversal dfs

Core Idea

Depth-first search systematically explores a graph by going as deep as possible before backtracking. Starting from a source vertex, DFS visits adjacent unvisited vertices recursively, generating a DFS tree. It runs in O(V+E) time and is fundamental to many graph algorithms.

How It's Best Learned

Trace DFS by hand on small graphs, maintaining a stack of vertices to visit. Observe how DFS discovers edges as tree edges, back edges, and cross edges in directed graphs.

Common Misconceptions

Explainer

Think of DFS as exploring a maze using the rule "always go as deep as you can before turning back." You enter a corridor, keep walking forward into new corridors, and only backtrack when you hit a dead end — then you return to the last junction you hadn't fully explored. This explore-deep-first strategy is exactly what DFS implements on a graph. Starting from a source vertex, DFS visits an unvisited neighbor, then immediately recurses into *that* vertex's neighbors before returning. The call stack (or an explicit stack data structure) remembers where to backtrack.

From your prerequisite knowledge of graph theory, you know that a graph G = (V, E) consists of vertices and edges. DFS systematically marks each vertex as discovered when first visited and finished when all its neighbors have been explored. The edges DFS traverses form a DFS tree — a spanning substructure rooted at the source. Not all edges of the original graph end up in this tree, and classifying the non-tree edges is one of DFS's most powerful features. In a directed graph, an edge (u, v) is a back edge if v is an ancestor of u in the DFS tree (meaning there's a cycle), a forward edge if v is a descendant but not a child, or a cross edge otherwise.

The runtime of O(V + E) follows naturally from the structure: each vertex is discovered and finished exactly once (contributing O(V)), and each edge is examined exactly once when DFS processes the vertex it leaves from (contributing O(E)). If you've seen Big-O notation, this is as good as it gets for graph traversal — you can't process a graph without at least touching every vertex and edge.

The real power of DFS lies in what the discovery and finish times reveal. If you record a timestamp when each vertex is discovered and when it's finished, these intervals nest cleanly: if v is a descendant of u in the DFS tree, then u's discovery time < v's discovery time < v's finish time < u's finish time. This parenthesis structure is the engine behind topological sorting (process vertices in reverse finish order), cycle detection (a back edge exists if and only if a cycle exists), and finding strongly connected components. DFS is not just a traversal — it is a framework that, with small instrumentation changes, solves an entire family of graph problems.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Literal Equations → Slope-Intercept Form → Point-Slope Form → Writing Linear Equations → Parallel and Perpendicular Line Slopes → Graphing Linear Equations → Piecewise Functions → Step Functions → Composition of Functions → Inverse Functions → Radical Functions and Graphs → Rational Exponents → Exponential Functions and Graphs → Logarithms Introduction → Big-O Notation and Asymptotic Analysis → Depth-First Search (DFS)

Longest path: 77 steps · 407 total prerequisite topics

Prerequisites (2)

Leads To (4)