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 85 in the knowledge graph I know this Set as goal
159topics build on this
439prerequisites beneath it
See this on the map →
Graph Representations: Adjacency List vs. Adjacency MatrixRecursion Basics+2 moreArticulation Points and Bridges in GraphsBacktracking Search for CSPs+4 more
DFS graph-traversal recursion cycle-detection connected-components

Core Idea

Depth-first search (DFS) explores a graph by going as deep as possible along each branch before backtracking. It can be implemented recursively or iteratively with an explicit stack, and runs in O(V + E) time. DFS is the foundation for cycle detection, topological sorting, strongly connected components (Tarjan's and Kosaraju's algorithms), and solving maze-like problems. Tracking discovery and finish times during DFS produces edge classifications: tree edges, back edges (cycles), forward edges, and cross edges.

How It's Best Learned

Implement recursive DFS tracking discovery and finish timestamps. Then implement the iterative stack version and verify equivalent results. Use DFS to detect cycles in both directed and undirected graphs as a practical exercise.

Common Misconceptions

Explainer

If you have written recursive tree traversals (like inorder or postorder), you have already implemented a special case of depth-first search — trees are acyclic graphs, and DFS on a tree is exactly what those traversals do. Generalizing to arbitrary graphs requires only one addition: a visited set to avoid processing the same node twice (and to prevent infinite loops in cyclic graphs).

The DFS algorithm works like this: start at a source node, mark it visited, then recursively visit each unvisited neighbor before returning. The "depth-first" name captures the behavior — you commit fully to one branch, following it to its dead end, before backtracking and trying another. The call stack (or an explicit stack data structure if implemented iteratively) holds the current path from the source to wherever you are. This is fundamentally different from BFS, which explores level by level. Neither is universally better — they answer different questions.

DFS has a remarkable side effect: the order in which nodes are *entered* (discovery time) and *exited* (finish time) produces a classification of every edge in the graph. A tree edge is one you traverse to an unvisited node. A back edge points from a node to an ancestor currently on the stack — finding one proves a cycle exists in the directed graph. Forward and cross edges cover other cases. This edge classification is the foundation for algorithms like topological sort (process nodes in reverse finish-time order) and strongly connected components (Tarjan's algorithm uses discovery and finish times directly).

Cycle detection deserves special care. In an undirected graph, any edge to an already-visited node that isn't your direct parent means a cycle exists (you reached the same node via two different paths). In a directed graph, the rule is stricter: only a back edge — one that points to a node still on the active recursion stack — proves a cycle. A directed edge to a node that was visited in a previous DFS branch (a cross edge) doesn't create a cycle, because that edge goes from one branch to another, not backward along the current path.

The O(V + E) time complexity follows naturally from the algorithm's structure: each vertex is visited exactly once, and each edge is examined exactly once when its source vertex is being processed. This makes DFS efficient even on large sparse graphs, and it's why DFS is the go-to building block for so many graph algorithms.

Practice Questions 3 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree TraversalsDepth-First Search (DFS)

Longest path: 86 steps · 439 total prerequisite topics

Prerequisites (4)

Leads To (6)