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

Graph Traversal: Depth-First and Breadth-First Search

College Depth 77 in the knowledge graph I know this Set as goal
409prerequisites beneath it
See this on the map →
Breadth-First Search (BFS)Depth-First Search (DFS)
DFS BFS traversal tree-edges back-edges

Core Idea

Depth-first search (DFS) explores as far as possible along each branch (recursively), while breadth-first search (BFS) explores level-by-level using a queue. Both visit all reachable vertices, producing a spanning tree. DFS finds back-edges (identifying cycles); BFS finds shortest paths in unweighted graphs.

How It's Best Learned

Trace DFS and BFS by hand on small graphs, noting discovery and finish times for DFS. Implement both iteratively. Recognize DFS orderings and topological sorting applications.

Common Misconceptions

DFS can visit vertices in many different orders depending on starting vertex and edge order; BFS finds the shortest path in unweighted graphs, not weighted ones.

Explainer

You already know the mechanics of depth-first search and breadth-first search individually. This topic is about understanding them together — their structural differences, what each one reveals about a graph, and when to reach for one versus the other.

The key distinction is the data structure each algorithm uses to decide which vertex to explore next. DFS uses a stack (or the call stack via recursion): push neighbors, pop the most recent, explore it, repeat. This sends the search diving deep before it backtracks. BFS uses a queue: push neighbors, dequeue the oldest, explore it, repeat. This sends the search spreading outward one layer at a time, like ripples from a stone dropped in water. The same logic, two different data structures, two completely different exploration patterns.

Both algorithms produce a spanning tree of the reachable portion of the graph. But the shape of that tree differs. A DFS spanning tree tends to be tall and thin — the algorithm follows long chains before backtracking. A BFS spanning tree tends to be short and wide — all vertices at distance 1 are found before any at distance 2. This is why BFS gives you shortest paths in unweighted graphs: every vertex is first discovered via the shortest possible route from the source. DFS makes no such guarantee.

DFS reveals something BFS cannot: back-edges. When DFS revisits a vertex that is still on the current recursion stack (marked "in progress"), it has found a back-edge — an edge that points backward in the DFS tree, closing a loop. Back-edges are exactly how you detect cycles in a graph using DFS. BFS, exploring layer by layer, can detect cross-edges between already-visited vertices, but it does not naturally expose cycle structure the same way. This connects directly to topological sorting and DAG detection: run DFS, check for back-edges. If none exist, the graph is acyclic and the reverse finish-time order is a valid topological ordering.

A practical summary: use BFS when you need shortest paths (in unweighted graphs) or want to explore a graph level by level. Use DFS when you need to detect cycles, produce a topological sort, find strongly connected components, or exhaustively enumerate paths. Both are linear-time O(V + E) — the difference is not efficiency but what structure each exposes.

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 IntegersDividing IntegersUnit RatesProportionsPercent ConceptConverting Between Fractions, Decimals, and PercentsOperations with Rational NumbersTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionBig-O Notation and Asymptotic AnalysisBreadth-First Search (BFS)Graph Traversal: Depth-First and Breadth-First Search

Longest path: 78 steps · 409 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.