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

College Depth 86 in the knowledge graph ☐ I know this ☆ Set as goal
38topics build on this
441prerequisites beneath it
See this on the map →
Graph Representations: Adjacency List vs. Adjacency MatrixBreadth-First Search (BFS)→→Dijkstra's AlgorithmQueue Applications: Level-Order Traversal and Breadth-First Search
bfs search graph-algorithm

Core Idea

BFS explores a graph level-by-level via a queue, visiting all distance-k neighbors before distance-(k+1). It finds shortest paths in unweighted graphs, connected components, and bipartiteness. Both run in O(V + E) time.

Explainer

You know how graphs are stored — adjacency lists mapping each vertex to its neighbors, or adjacency matrices encoding edges in a 2D array — and you have seen the basic idea of breadth-first search: start at a source vertex, explore all its neighbors, then all *their* neighbors, radiating outward in concentric layers. The implementation uses a queue: enqueue the source, then repeatedly dequeue a vertex, process it, and enqueue its unvisited neighbors. A visited set prevents revisiting. What makes BFS powerful is not just the traversal itself, but the guarantees it provides and the problems those guarantees solve.

The most fundamental guarantee is shortest paths in unweighted graphs. Because BFS visits vertices in order of their distance from the source — all distance-1 vertices before any distance-2 vertex, all distance-2 before any distance-3 — the first time BFS reaches a vertex, it has found the shortest path to it. By storing each vertex's predecessor during traversal, you can reconstruct the shortest path by following predecessor pointers backward from the destination to the source. No other unweighted shortest-path algorithm is simpler or faster — it runs in O(V + E), visiting every vertex and edge exactly once.

BFS also finds connected components in undirected graphs. Start BFS from any unvisited vertex; every vertex it reaches belongs to the same component. When BFS finishes, pick another unvisited vertex and repeat. Each BFS run discovers one complete component. This is how you answer questions like "is the graph connected?" (one component) or "how many separate pieces does it have?" The same idea extends to directed graphs using BFS from each vertex, though there you distinguish between weakly and strongly connected components.

A more surprising application is bipartiteness testing. A graph is bipartite if its vertices can be colored with two colors such that no edge connects same-colored vertices — equivalently, it contains no odd-length cycles. BFS tests this naturally: color the source one color, its neighbors the opposite color, their neighbors the first color again, and so on. If you ever try to color a vertex and find its neighbor already has the same color, the graph is not bipartite. This works because BFS assigns levels (distances from the source), and a graph is bipartite exactly when every edge connects vertices at adjacent levels. Beyond these core applications, BFS serves as a building block for more advanced algorithms like Dijkstra's (which generalizes BFS to weighted graphs using a priority queue instead of a plain queue) and for solving puzzles where states are vertices and transitions are edges — the classic example being the shortest sequence of moves to solve a Rubik's cube or a sliding-tile puzzle.

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 → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Tree Structure and Node Properties → Binary Trees → Tree Traversals → Breadth-First Search (BFS) → Breadth-First Search: Implementation and Applications

Longest path: 87 steps · 441 total prerequisite topics

Prerequisites (2)

Leads To (2)