Breadth-First Search (BFS)

College Depth 62 in the knowledge graph I know this Set as goal
Unlocks 377 downstream topics
graph-algorithms traversal bfs

Core Idea

Breadth-first search systematically explores a graph level by level, visiting all neighbors of a vertex before moving deeper. BFS uses a queue to process vertices, runs in O(V+E) time, and finds shortest paths in unweighted graphs.

Explainer

Think of dropping a stone into a pond and watching ripples spread outward. The first ripple ring contains all points one step from the center; the second ring contains all points two steps away; and so on. Breadth-first search works exactly like this. Starting from a source vertex, BFS visits every neighbor at distance 1 before visiting any vertex at distance 2. This "wave" property is not an accident — it is enforced by the data structure BFS uses.

The key data structure is a queue (first-in, first-out). You enqueue the source vertex and mark it as visited. Then you repeatedly dequeue the front vertex, examine all its unvisited neighbors, mark them visited, and enqueue them. Because you always process vertices in the order they were discovered, vertices at distance 1 are always fully processed before any vertex at distance 2 is dequeued. This is why BFS finds shortest paths in unweighted graphs: the first time you reach a vertex is guaranteed to be via the fewest edges possible.

The runtime is O(V+E). You visit each vertex once (O(V)) and, for each vertex, you inspect each of its edges once (O(E) total across all vertices). This efficiency depends on the visited-marking step — without it, you could revisit vertices indefinitely in a graph with cycles. Your prerequisite knowledge of graph theory (vertices, edges, adjacency) gives you the vocabulary; Big-O notation lets you reason about why O(V+E) is nearly as fast as reading the graph itself.

A useful concrete example: in a social network graph where edges represent friendships, BFS from your profile finds all your friends first, then friends-of-friends, then third-degree connections. The BFS tree — the spanning tree formed by the edges used to first discover each vertex — records the shortest-path structure. Every vertex's depth in the BFS tree equals its shortest-path distance from the source. This tree is the foundation for the next topic: shortest-path algorithms on unweighted graphs.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication 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 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineOpposites and Additive InversesAbsolute 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)

Longest path: 63 steps · 285 total prerequisite topics

Prerequisites (2)

Leads To (2)