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 85 in the knowledge graph ☐ I know this ☆ Set as goal
162topics build on this
440prerequisites beneath it
See this on the map →
Graph Representations: Adjacency List vs. Adjacency MatrixQueue ADT: Circular Array and Linked-List Implementations+2 more→→A* Search AlgorithmAdvanced Network Flow Algorithms+9 more
BFS graph-traversal shortest-path level-order

Core Idea

Breadth-first search (BFS) explores a graph layer by layer, visiting all neighbors of a node before moving deeper. It uses a queue and a visited set, running in O(V + E) time for V vertices and E edges. BFS finds the shortest path in terms of edge count between two nodes in an unweighted graph. It also determines connected components, checks bipartiteness, and forms the basis for Dijkstra's algorithm when extended to weighted graphs.

How It's Best Learned

Implement BFS on both adjacency list and adjacency matrix representations. Trace through a small graph by hand showing the queue state at each step. Then add path reconstruction using a parent-pointer array.

Common Misconceptions

Explainer

BFS is built directly on top of the queue data structure you already know. The queue's FIFO property is not incidental — it is precisely what produces the layer-by-layer exploration. When you enqueue all unvisited neighbors of the current node, those neighbors sit behind any nodes already in the queue from the previous layer. By the time you process them, you will have finished their entire layer first. This is why BFS is sometimes called "level-order traversal" on trees.

The algorithm has three moving parts: a queue that drives the traversal order, a visited set that prevents cycles from looping forever, and a parent pointer array (optional but useful) that lets you reconstruct the path after the search finishes. Start by enqueuing the source node and marking it visited. Then repeatedly dequeue a node, check if it is the target, and enqueue all its unvisited neighbors (marking each visited immediately on enqueue). Stop when the queue is empty or the target is found.

BFS guarantees the shortest path by edge count in unweighted graphs. The argument is simple: the source is at distance 0, its neighbors are at distance 1, their unvisited neighbors are at distance 2, and so on. Because each node is processed in non-decreasing distance order, the first time BFS reaches a target node it has taken the minimum number of edges to get there. This guarantee disappears in weighted graphs because a longer path (more edges) might have lower total cost — that is where Dijkstra's algorithm takes over.

The time complexity is O(V + E): every vertex is enqueued and dequeued at most once (O(V)), and every edge is examined at most twice, once from each endpoint (O(E)). The space complexity is also O(V) for the queue and visited set. This makes BFS practical even for large graphs, as long as they fit in memory.

A subtle but important implementation detail: mark nodes visited *before* enqueuing, not after dequeuing. If you wait until dequeue, two different neighbors of the same node can both see it as unvisited and both enqueue it, leading to redundant (or incorrect) processing. The rule is: the moment you decide to enqueue a node is the moment it is "claimed" and should be marked.

Practice Questions 3 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)

Longest path: 86 steps · 440 total prerequisite topics

Prerequisites (4)

Leads To (11)