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 Analysis→→Graph 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 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 → Addition Within 10 → Number Bonds to 10 → 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 → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Literal Equations → Slope-Intercept Form → Point-Slope Form → Writing Linear Equations → Parallel and Perpendicular Line Slopes → Graphing Linear Equations → Piecewise Functions → Step Functions → Composition of Functions → Inverse Functions → Radical Functions and Graphs → Rational Exponents → Exponential Functions and Graphs → Logarithms Introduction → Big-O Notation and Asymptotic Analysis → Breadth-First Search (BFS)

Longest path: 77 steps · 407 total prerequisite topics

Prerequisites (2)

Leads To (2)