A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Shortest Paths in Unweighted Graphs

College Depth 77 in the knowledge graph I know this Set as goal
422topics build on this
408prerequisites beneath it
See this on the map →
Breadth-First Search (BFS)Dijkstra's Shortest Path AlgorithmTonnetz Navigation and Voice Leading
shortest-paths graph-algorithms

Core Idea

In unweighted graphs, the shortest path between two vertices is the one with fewest edges. BFS directly computes shortest paths by tracking distances and parent pointers. This is the foundation for understanding shortest-path problems in more complex settings.

Explainer

You already know how BFS works: it explores a graph level by level, starting from a source vertex and visiting all neighbors before moving on to their neighbors. The key insight here is that this "level by level" property is exactly what defines distance in an unweighted graph — the level at which BFS first visits a vertex equals its shortest-path distance from the source, measured in number of edges.

To compute shortest paths, augment BFS with two arrays: `dist[]` and `parent[]`. Initialize `dist[source] = 0` and all others to infinity. When BFS first visits a vertex v from vertex u, record `dist[v] = dist[u] + 1` and `parent[v] = u`. Since BFS visits vertices in non-decreasing order of distance, the first time it reaches any vertex v is always via a shortest path — any later arrival would be at equal or greater distance. To reconstruct the actual path from source to a target, follow the parent pointers backward from target to source and reverse the result.

The correctness argument rests on the FIFO invariant of the queue: all vertices at distance d are processed before any at distance d+1. When a vertex v is first dequeued, every possible shorter path has already been processed, and the parent pointer records the optimal route. This reasoning depends critically on all edges having equal weight. In a weighted graph, BFS fails because a long sequence of cheap edges might produce a shorter total cost than a direct but expensive edge — equal edge counts do not imply equal costs. That gap motivates Dijkstra's algorithm, which generalizes BFS by replacing the FIFO queue with a priority queue.

The parent pointers produced by BFS form a shortest path tree: a subgraph rooted at the source where every root-to-leaf path is a shortest path in the original graph. This tree is not necessarily unique when multiple shortest paths exist, but every choice of parent pointers gives a valid one. BFS-based shortest paths solve a wide range of practical problems — minimum moves on a game board, degrees of separation in a social network, hops in a communication network — anywhere that edge count is the right measure of cost.

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 IntegersDividing IntegersUnit RatesProportionsPercent ConceptConverting Between Fractions, Decimals, and PercentsOperations with Rational NumbersTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionBig-O Notation and Asymptotic AnalysisBreadth-First Search (BFS)Shortest Paths in Unweighted Graphs

Longest path: 78 steps · 408 total prerequisite topics

Prerequisites (1)

Leads To (2)