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 moreA* 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 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 IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsTree Structure and Node PropertiesBinary TreesTree TraversalsBreadth-First Search (BFS)

Longest path: 86 steps · 440 total prerequisite topics

Prerequisites (4)

Leads To (11)