Dijkstra’s Algorithm Explained: Finding the Shortest Path

Dijkstra’s algorithm explained in plain terms means understanding how to find the cheapest route through a network without trying every possible path. The central question is how a program can be sure it has found the shortest route when it has only explored part of the graph, and the answer is a beautifully simple rule: always expand the closest unexplored place first.

Dijkstra’s algorithm finds the shortest path from a starting node to every other node in a graph with non-negative edge weights, and it does so by repeatedly settling the nearest unsettled node, knowing that no undiscovered route could possibly beat it. It was published by Edsger Dijkstra in 1959, and it remains one of the most used algorithms in all of computing.

The idea feels like common sense once you see it, and it is rigorous enough to trust with real roads, real networks, and real money. This article explains how the algorithm works, why it is correct, and where it runs in the world.

Key takeaways

  • Dijkstra’s algorithm finds shortest paths from one source to all nodes.
  • It always expands the nearest unsettled node first.
  • It needs non-negative edge weights to guarantee correct answers.
  • A priority queue makes it run in near-linearithmic time.
  • It powers maps, routing, and network planning worldwide.

What Dijkstra’s algorithm is

Dijkstra’s algorithm works on a weighted graph, a set of nodes joined by edges that each carry a cost, like the length of a road or the latency of a link. Given a starting node, it computes the cheapest distance to every other node, along with the route that achieves it, and it does so without ever exploring a path that cannot improve on what it already knows.

The algorithm keeps two groups of nodes: settled nodes, whose shortest distance is known for certain, and unsettled nodes, whose best known distance is still tentative. At each step it picks the unsettled node with the smallest tentative distance, settles it, and then relaxes its outgoing edges, which means checking whether going through this node offers a cheaper route to each neighbour.

The reason this works is the key insight of the whole algorithm. The unsettled node with the smallest tentative distance cannot be reached any more cheaply through another unsettled node, because any such detour would have to start from a node at least as far away and then add a non-negative edge. That is why the algorithm can settle nodes one by one and never look back. Our guide to graph databases explains how graphs are stored when the data itself is a network.

How the algorithm runs, step by step

Imagine a small map with a start town and a handful of others joined by roads of different lengths. The algorithm begins by setting the start’s distance to zero and every other town’s distance to infinity, then places them all in a priority queue ordered by distance. The start is the nearest unsettled node, so it is settled first, and its roads are relaxed, giving its neighbours their first tentative distances.

Next the algorithm picks the nearest unsettled town, which is the closest neighbour of the start, and settles it. Its outgoing roads are relaxed, and one of its neighbours may get a better distance than the direct road from the start offered, which is exactly the kind of shortcut the algorithm exists to find. The process repeats, settling one town at a time, until every town is settled or the queue is empty.

A worked example makes the guarantee concrete. Suppose the direct road from the start to town B costs ten, but the road through town A costs three plus four, seven in total. The algorithm settles A first, because three is less than ten, relaxes the road from A to B, and records seven as B’s new tentative distance. When B is eventually settled, seven is the answer, and the direct road of ten is never used.

The priority queue is what makes the algorithm fast. Without it, finding the nearest unsettled node would mean scanning them all at each step, which is quadratic in the number of nodes. With a binary heap, each extraction and each relaxation costs logarithmic time, and the whole algorithm runs in near-linearithmic time on sparse graphs, which is why it scales to maps with millions of junctions.

A photograph of a paper road map spread on a wooden table with a glowing route traced across it in warm light
A photograph of a paper road map spread on a wooden table with a glowing route traced across it in warm light, shows how Dijkstra’s algorithm settles the nearest place first and never looks back.

Dijkstra’s algorithm versus other pathfinders

Dijkstra’s algorithm is one of several ways to find paths through a graph, and each fits a different situation. Breadth-first search is faster but only works when every edge costs the same, the Bellman-Ford algorithm handles negative weights but runs far more slowly, and A star search is faster for a single destination when a good guess of the remaining distance is available.

The table below sets out the main contrasts between the leading approaches.

Aspect Dijkstra’s algorithm Breadth-first search Bellman-Ford
Edge weights Non-negative only All edges equal Negative allowed
Finds paths to Every node at once Every node at once Every node at once
Running time Near-linearithmic with a heap Linear Much slower, repeated passes
Single destination Explores in all directions Explores in all directions Explores in all directions
Best for Weighted maps and networks Unweighted grids and mazes Graphs with negative weights

The practical result is that Dijkstra’s algorithm is the default choice for weighted shortest paths, and the alternatives are picked only when their special conditions apply.

Where Dijkstra’s algorithm is used

Mapping and navigation services are the most visible users. When a phone plans a route, the road network is a weighted graph, the junctions are nodes, and the roads are edges weighted by time or distance. The routing engine runs Dijkstra’s algorithm, or a heavily optimised descendant of it, to find the fastest way from one place to another. Our guide to knowledge graphs explains another way networks of connected facts are explored.

Computer networks use Dijkstra’s algorithm inside their routing protocols. The Open Shortest Path First protocol has every router run the algorithm over its map of the network, so that each router knows the cheapest path to every destination. When a link fails, the routers rerun it and the traffic flows around the break, which is why the internet recovers from faults so gracefully.

Logistics and planning use the algorithm for delivery routes, warehouse layouts, and supply chains. A delivery company deciding the order of its drops, or a warehouse robot planning its path between shelves, is solving a shortest-path problem, and Dijkstra’s algorithm is the workhorse underneath.

Games use Dijkstra’s algorithm for pathfinding on weighted terrain, where crossing a swamp costs more than crossing a road. When every tile has a movement cost, the cheapest path is not the straightest one, and the algorithm finds the route that a human player would recognise as sensible.

Strengths and limits

The greatest strength of Dijkstra’s algorithm is its guarantee. When the weights are non-negative, the distances it produces are provably the shortest possible, with no approximation and no luck involved, which is why safety-critical systems from air traffic to network routing trust it.

It is also a strength that the algorithm finds paths to every node at once. A single run answers the shortest path to all destinations, which is exactly what a router or a map service needs, and it does so in near-linearithmic time with a priority queue.

The limits are real, though. Negative weights break the central insight, because a detour through an unsettled node could then be cheaper than the direct route, and the algorithm’s answer would be wrong. Our guide to gossip protocols shows how distributed systems share information differently when no single node holds the whole map, and for negative weights the Bellman-Ford algorithm is the correct tool.

Dijkstra’s algorithm checklist

  • Use Dijkstra’s algorithm for shortest paths with non-negative weights.
  • Reach for a binary heap priority queue for the unsettled nodes.
  • Relax each edge when its source node is settled.
  • Stop early when only one destination is needed and it is settled.
  • Never use it on graphs with negative edge weights.
  • Prefer breadth-first search when every edge costs the same.
  • Prefer A star search for a single destination with a good heuristic.
  • Remember one run gives distances to every node at once.
  • Test on graphs with equal-cost alternative routes.

Frequently asked questions

What is Dijkstra’s algorithm in simple terms?

It is a method for finding the cheapest route from a starting point to every other point in a network, by always expanding the closest unexplored place first.

Who invented Dijkstra’s algorithm?

Edsger Dijkstra, a Dutch computer scientist, published it in 1959, reportedly designing it in about twenty minutes over coffee.

Why does Dijkstra’s algorithm need non-negative weights?

Because its guarantee rests on the fact that no detour through an unsettled node can beat the nearest one, which fails when an edge can subtract cost.

How fast is Dijkstra’s algorithm?

Near-linearithmic with a binary heap priority queue, which is fast enough for road networks with millions of junctions.

What is edge relaxation?

It is the step of checking whether reaching a neighbour through the newly settled node is cheaper than the best route known so far, and updating it if so.

When should I not use Dijkstra’s algorithm?

When the graph has negative weights, use Bellman-Ford instead, and when all edges cost the same, breadth-first search is simpler and faster.

The bottom line

Dijkstra’s algorithm explained in one line is this: always settle the nearest unexplored place first, and the shortest path can never slip past you.

It is one of the most trusted algorithms ever written, and the reason maps, routers, and robots all find their way.

This article is general information about how Dijkstra’s algorithm works, not advice about choosing an algorithm for a particular system.

Latest articles

Scroll to Top