Dijkstra Algorithm

Decision Sciences
Introduced by
Edsger W. Dijkstra
Year
1956
Type
algorithm
Family
Graph Algorithms

What does it do?

Dijkstra's algorithm, published by Edsger W. Dijkstra in 1959, is a fundamental method for the single-source shortest path problem.

Starting from one vertex, it computes the shortest path to every other vertex in a weighted graph whose edge weights are non-negative.

The intuition behind the method

The algorithm maintains a set of vertices whose shortest distances from the source are already known, and iteratively expands this set by selecting the nearest unprocessed vertex and updating distances to its neighbors. The key insight is the greedy property: once a vertex is selected, its distance from the source is final because all edge weights are non-negative, so any longer path through unvisited vertices cannot be shorter. This guarantees correctness and optimality.

History

Edsger W. Dijkstra published the algorithm in 1959 in a short note in Numerische Mathematik. Later work introduced improved data structures, such as Fibonacci heaps, which give O(E + V log V) complexity, along with bidirectional variants. The algorithm is also the shortest-path core of link-state routing protocols; OSPF is one example.

When to use it

Choose Dijkstra's algorithm when all edge weights are non-negative and you need shortest distances from one source to all vertices, or to one destination with early termination. It suits road networks, routing tables and similar graphs where edge costs are distances, times or other non-negative quantities. Do not use it when some edges have negative weights; Bellman-Ford handles that case. If you need the path to a single known target and a reliable estimate of remaining distance is available, A* is usually more efficient. Bidirectional search is a separate technique that searches from both ends and can be combined with Dijkstra's algorithm.

Limitations and considerations

  • It cannot handle negative edge weights, because the finality of selected distances no longer holds.
  • The basic form computes distances to all reachable vertices. Early termination saves work only for a single destination, and only once that vertex has been selected. With several targets, run the algorithm until all of them are selected or the queue is empty.
  • When a heuristic estimate of distance to the target is available, A* generally expands fewer vertices.
  • With an array-based priority queue, the O(V²) cost becomes a bottleneck on large sparse graphs, where a heap implementation is preferred.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • The logic is simple, and the correctness argument rests on one property: once a vertex is selected, its distance is final when all edge weights are non-negative.
  • Complexity depends on the priority queue: O((V+E) log V) with a binary heap, and O(V²) with a plain array, which is competitive mainly on dense graphs.
  • It works on both directed and undirected graphs; an undirected edge is represented as two directed edges of equal weight.
  • Its relaxation scheme is the basis for many later shortest-path methods and their variants.

Applications

  • Route finding on road and transport networks, where edge weights are travel distances or times.
  • Link-state routing in computer networks, where shortest paths are computed over link costs.
  • Shortest-path queries in weighted graphs with non-negative costs, such as network distance or cost-of-service analysis.
  • Pathfinding subroutines in simulations and games on weighted grids or graphs.

How it works

How it works

1

Set the distance to the source to zero and every other distance to infinity. Mark all vertices as unvisited and place the source in a priority queue keyed by distance.

2

Remove the unvisited vertex with the smallest tentative distance from the priority queue. Its distance is now final, because no later path can be shorter when all edge weights are non-negative.

3

For each neighbour of the selected vertex, check whether a path through it gives a smaller distance than the neighbour's current value. If so, update that distance, record the selected vertex as its predecessor, and insert the neighbour into the priority queue.

Common pitfalls

  • Applying the algorithm to a graph with negative edge weights. The output may look plausible but is not guaranteed to be the shortest path.
  • Using a priority queue without a decrease-key operation and then treating outdated queue entries as current. Stale entries should be skipped when they are removed, or the distances used will be wrong.
  • Forgetting to record predecessors during relaxation, which makes it impossible to reconstruct the path after the distances are computed.

Frequently asked questions

Why does Dijkstra's algorithm fail on negative weights?

The algorithm assumes that once a vertex is finalized, no shorter path can be found through unvisited vertices. With negative weights, a longer path might later improve when a negative edge is used, violating this assumption.

What data structure should be used for the priority queue?

The total cost depends on the queue. An array gives O(V²), which is reasonable mainly when the graph is small or dense. A binary heap gives O((V+E) log V), because each insertion, extraction and decrease-key operation costs O(log V). A Fibonacci heap gives O(E + V log V), but it has large constant factors. For large sparse graphs, a binary heap is usually preferred.

How can Dijkstra's algorithm find the path to one specific destination?

Terminate the algorithm as soon as the destination vertex is selected (its distance is finalized), avoiding unnecessary computation. Alternatively, reconstruct the path by maintaining parent pointers during execution.

Can Dijkstra's algorithm be used in undirected graphs?

Yes, undirected graphs can be represented as directed graphs with edges in both directions for each undirected edge, each with the same weight. The algorithm works identically on the bidirectional representation.

How do I read the result and recover the path to a target?

The algorithm returns two arrays: a distance for each vertex and a predecessor for each vertex. A vertex that cannot be reached from the source keeps an infinite distance and has no predecessor. To obtain the path to a target, start at the target, follow predecessors back to the source, and reverse the resulting sequence.

Related methods

Sources

  1. Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. Numerische Mathematik, 1(1), 269-271.

    Open DOI reference
  2. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to Algorithms (3rd ed.). MIT Press.

    ISBN: 978-0-262-03384-8