Dijkstra's Shortest Path Algorithm in Routing

Graduate Depth 52 in the knowledge graph I know this Set as goal
Unlocks 30 downstream topics
dijkstra link-state shortest-path ospf

Core Idea

Dijkstra's algorithm is used in link-state routing protocols like OSPF to compute the shortest path from a router to all other routers in a network. Each router runs Dijkstra independently on its synchronized view of the network topology, producing a shortest-path tree rooted at that router.

Explainer

From your overview of routing algorithms, you know that routers must decide which interface to forward each packet through, and that link-state protocols work by giving every router a complete map of the network. Dijkstra's algorithm is the engine that turns that map into actual forwarding decisions. It answers the question: given a weighted graph where nodes are routers and edge weights represent link costs (delay, bandwidth, or administrative metrics), what is the least-cost path from me to every other router?

The algorithm works by maintaining two sets: a confirmed set of routers whose shortest path is already known, and a tentative set of candidates. Initially, only the source router (you) is confirmed, with cost 0. You examine all links from confirmed routers to unconfirmed neighbors, recording the total cost to reach each neighbor through the best known path. Then you move the lowest-cost tentative node into the confirmed set — this is the greedy step, and it works because all link costs are non-negative, so no cheaper path to that node can exist through an unvisited router. You then examine that newly confirmed node's neighbors and update their tentative costs if a cheaper path through the new node exists. This process repeats until every router is confirmed.

The result is a shortest-path tree (SPT) rooted at the source router. Each branch of this tree represents the optimal forwarding path to a destination. The router only needs the first hop of each path to build its forwarding table: to reach destination X, send the packet out the interface that leads to the next hop on the shortest path to X. Because every router in the network has the same topology database (synchronized via link-state advertisements), every router independently computes its own SPT, and the forwarding decisions are globally consistent — packets follow optimal paths without loops.

In practice, OSPF (Open Shortest Path First) runs Dijkstra every time the topology changes — a link goes down, a cost changes, or a new router joins. The algorithm's time complexity is O(N² ) for a simple implementation or O(N log N + E) with a priority queue, where N is the number of routers and E is the number of links. For networks of hundreds or even thousands of routers, this runs in milliseconds. The real cost is not computation but convergence — the time for all routers to receive the updated topology information and recompute their trees. During convergence, routers may briefly have inconsistent views, potentially causing transient loops or packet drops until the network stabilizes.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication 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 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsOperators and ExpressionsArithmetic Operators and Operator PrecedenceComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsCombinational Circuit DesignFlip-Flops and LatchesBinary Counters: Design and AnalysisBinary ArithmeticSubnetting and CIDR NotationIP Routing and ForwardingRouting Algorithms and ProtocolsDijkstra's Shortest Path Algorithm in Routing

Longest path: 53 steps · 216 total prerequisite topics

Prerequisites (1)

Leads To (3)