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 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)Breadth-First Search: Implementation and Applications

Longest path: 87 steps · 441 total prerequisite topics

Prerequisites (2)

Leads To (2)