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: Implementation and Applications

College Depth 86 in the knowledge graph I know this Set as goal
156topics build on this
440prerequisites 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 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)Depth-First Search: Implementation and Applications

Longest path: 87 steps · 440 total prerequisite topics

Prerequisites (2)

Leads To (3)