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

Breadth-First Search (BFS)

College Depth 76 in the knowledge graph I know this Set as goal
424topics build on this
407prerequisites beneath it
See this on the map →
Introduction to Graph TheoryBig-O Notation and Asymptotic AnalysisGraph Traversal: Depth-First and Breadth-First SearchShortest Paths in Unweighted Graphs
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

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)

Longest path: 77 steps · 407 total prerequisite topics

Prerequisites (2)

Leads To (2)