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

Depth-First Search: Implementation and Applications

College Depth 106 in the knowledge graph ☐ I know this ☆ Set as goal
164topics build on this
518prerequisites beneath it
See this on the map →
Graph Representations: Adjacency List vs. Adjacency MatrixDepth-First Search (DFS)→→Bipartite Graphs: Detection and Two-ColoringStrongly Connected Components: Kosaraju and Tarjan Algorithms+1 more
dfs search graph-algorithm

Core Idea

DFS explores a graph deeply via recursion or an explicit stack, visiting unvisited neighbors. It finds connected components, detects cycles, computes finish times (for topological sort), and identifies strongly connected components.

Explainer

From your study of graph representations (adjacency lists and matrices) and the basic DFS traversal, you know that DFS explores a graph by going as deep as possible along each branch before backtracking. Now we focus on what DFS *produces* beyond mere traversal — the structural information it reveals about a graph, and how that information powers important algorithms.

The key to understanding DFS applications is the concept of discovery and finish times. As DFS runs, it timestamps each node twice: once when the node is first discovered (pushed onto the call stack), and once when it is finished (all descendants have been fully explored and the call returns). These timestamps encode the recursive structure of the search. If node A has a smaller discovery time and larger finish time than node B, then B was explored entirely within A's recursive call — meaning B is a descendant of A in the DFS tree. This parenthetical nesting of intervals is what makes finish times so powerful.

Cycle detection falls out naturally from DFS. During traversal, if you encounter an edge leading to a node that has been discovered but not yet finished — meaning it's still on the call stack, an ancestor in the current path — you've found a back edge, which proves a cycle exists. In an undirected graph, any edge to an already-visited node (other than the parent) indicates a cycle. In a directed graph, only back edges indicate cycles; edges to fully finished nodes (cross edges or forward edges) do not. This distinction matters for applications like determining whether a directed graph is a DAG (directed acyclic graph): run DFS, and if no back edges appear, the graph has no cycles.

Topological sorting uses finish times directly. For a DAG, if you output nodes in reverse order of their finish times, the result is a valid topological ordering — every node appears before all nodes it has edges to. This works because in a DAG (no back edges), if there is an edge from A to B, then A will always finish after B in a DFS. Topological sort is essential for dependency resolution: build systems, course prerequisite planning, and task scheduling all reduce to this operation. Connected components in an undirected graph are found by running DFS from each unvisited node — each DFS call discovers exactly one component. For directed graphs, finding strongly connected components (maximal sets of nodes where every node can reach every other) uses two DFS passes: one on the original graph to compute finish times, and one on the transposed graph processing nodes in reverse finish-time order. Each DFS call in the second pass reveals one strongly connected component.

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 → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → 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 → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Regular Language Recognition Algorithms → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Scope and Binding Resolution → Scope, Shadowing, and Variable Lifetime → Parameter Passing: Value vs. Reference → Two-Dimensional Arrays → Graph Representations: Adjacency List vs. Adjacency Matrix → Depth-First Search (DFS) → Depth-First Search: Implementation and Applications

Longest path: 107 steps · 518 total prerequisite topics

Prerequisites (2)

Leads To (3)