Hubbry Logo
Dijkstra's algorithmDijkstra's algorithmMain
Open search
Dijkstra's algorithm
Community hub
Dijkstra's algorithm
logo
8 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Dijkstra's algorithm
Dijkstra's algorithm
from Wikipedia

Dijkstra's algorithm
Dijkstra's algorithm to find the shortest path between a and b. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors.
ClassSearch algorithm
Greedy algorithm
Dynamic programming[1]
Data structureGraph
Usually used with priority queue or heap for optimization[2][3]
Worst-case performance[3]

Dijkstra's algorithm (/ˈdkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.[4][5][6]

Dijkstra's algorithm finds the shortest path from a given source node to every other node.[7]: 196–206  It can be used to find the shortest path to a specific destination node, by terminating the algorithm after determining the shortest path to the destination node. For example, if the nodes of the graph represent cities, and the costs of edges represent the distances between pairs of cities connected by a direct road, then Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. A common application of shortest path algorithms is network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and OSPF (Open Shortest Path First). It is also employed as a subroutine in algorithms such as Johnson's algorithm.

The algorithm uses a min-priority queue data structure for selecting the shortest paths known so far. Before more advanced priority queue structures were discovered, Dijkstra's original algorithm ran in time, where is the number of nodes.[8][9] Fredman & Tarjan 1984 proposed a Fibonacci heap priority queue to optimize the running time complexity to . This is asymptotically the fastest known single-source shortest-path algorithm for arbitrary directed graphs with unbounded non-negative weights. However, specialized cases (such as bounded/integer weights, directed acyclic graphs etc.) can be improved further. If preprocessing is allowed, algorithms such as contraction hierarchies can be up to seven orders of magnitude faster.

Dijkstra's algorithm is commonly used on graphs where the edge weights are positive integers or real numbers. It can be generalized to any graph where the edge weights are partially ordered, provided the subsequent labels (a subsequent label is produced when traversing an edge) are monotonically non-decreasing.[10][11]

In many fields, particularly artificial intelligence, Dijkstra's algorithm or a variant offers a uniform cost search and is formulated as an instance of the more general idea of best-first search.[12]

History

[edit]

What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to given city. It is the algorithm for the shortest path, which I designed in about twenty minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. In fact, it was published in '59, three years later. The publication is still readable, it is, in fact, quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one of the advantages of designing without pencil and paper is that you are almost forced to avoid all avoidable complexities. Eventually, that algorithm became to my great amazement, one of the cornerstones of my fame.

— Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001[5]

Dijkstra thought about the shortest path problem while working as a programmer at the Mathematical Center in Amsterdam in 1956. He wanted to demonstrate the capabilities of the new ARMAC computer.[13] His objective was to choose a problem and a computer solution that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (he limited it to 64, so that 6 bits would be sufficient to encode the city number).[5] A year later, he came across another problem advanced by hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the machine's back panel. As a solution, he re-discovered Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim).[14][15] Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.[16][17]

Algorithm

[edit]
Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a target node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are the visited ones, with color representing the distance: the redder, the closer (to the start node). Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic of picking the shortest known path so far.

The algorithm requires a starting node, and computes the shortest distance from that starting node to each other node. Dijkstra's algorithm starts with infinite distances and tries to improve them step by step:

  1. Create a set of all unvisited nodes: the unvisited set.
  2. Assign to every node a distance from start value: for the starting node, it is zero, and for all other nodes, it is infinity, since initially no path is known to these nodes. During execution, the distance of a node N is the length of the shortest path discovered so far between the starting node and N.[18]
  3. From the unvisited set, select the current node to be the one with the smallest (finite) distance; initially, this is the starting node (distance zero). If the unvisited set is empty, or contains only nodes with infinite distance (which are unreachable), then the algorithm terminates by skipping to step 6. If the only concern is the path to a target node, the algorithm terminates once the current node is the target node. Otherwise, the algorithm continues.
  4. For the current node, consider all of its unvisited neighbors and update their distances through the current node; compare the newly calculated distance to the one currently assigned to the neighbor and assign the smaller one to it. For example, if the current node A is marked with a distance of 6, and the edge connecting it with its neighbor B has length 2, then the distance to B through A is 6 + 2 = 8. If B was previously marked with a distance greater than 8, then update it to 8 (the path to B through A is shorter). Otherwise, keep its current distance (the path to B through A is not the shortest).
  5. After considering all of the current node's unvisited neighbors, the current node is removed from the unvisited set. Thus a visited node is never rechecked, which is correct because the distance recorded on the current node is minimal (as ensured in step 3), and thus final. Repeat from step 3.
  6. Once the loop exits (steps 3–5), every visited node contains its shortest distance from the starting node.

Description

[edit]

The shortest path between two intersections on a city map can be found by this algorithm using pencil and paper. Every intersection is listed on a separate line: one is the starting point and is labeled (given a distance of) 0. Every other intersection is initially labeled with a distance of infinity. This is done to note that no path to these intersections has yet been established. At each iteration one intersection becomes the current intersection. For the first iteration, this is the starting point.

From the current intersection, the distance to every neighbor (directly-connected) intersection is assessed by summing the label (value) of the current intersection and the distance to the neighbor and then relabeling the neighbor with the lesser of that sum and the neighbor's existing label. I.e., the neighbor is relabeled if the path to it through the current intersection is shorter than previously assessed paths. If so, mark the road to the neighbor with an arrow pointing to it, and erase any other arrow that points to it. After the distances to each of the current intersection's neighbors have been assessed, the current intersection is marked as visited. The unvisited intersection with the smallest label becomes the current intersection and the process repeats until all nodes with labels less than the destination's label have been visited.

Once no unvisited nodes remain with a label smaller than the destination's label, the remaining arrows show the shortest path.

Pseudocode

[edit]

In the following pseudocode, dist is an array that contains the current distances from the source to other vertices, i.e. dist[u] is the current distance from the source to the vertex u. The prev array contains pointers to previous-hop nodes on the shortest path from source to the given vertex (equivalently, it is the next-hop on the path from the given vertex to the source). The code u ← vertex in Q with min dist[u], searches for the vertex u in the vertex set Q that has the least dist[u] value. Graph.Edges(u, v) returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes u and v. The variable alt on line 14 is the length of the path from the source node to the neighbor node v if it were to go through u. If this path is shorter than the current shortest path recorded for v, then the distance of v is updated to alt.[7]

A demo of Dijkstra's algorithm based on Euclidean distance. Red lines are the shortest path covering, i.e., connecting u and prev[u]. Blue lines indicate where relaxing happens, i.e., connecting v with a node u in Q, which gives a shorter path from the source to v.
 1  function Dijkstra(Graph, source):
 2     
 3      for each vertex v in Graph.Vertices:
 4          dist[v] ← INFINITY
 5          prev[v] ← UNDEFINED
 6          add v to Q
 7      dist[source] ← 0
 8     
 9      while Q is not empty:
10          u ← vertex in Q with minimum dist[u]
11          Q.remove(u)
12         
13          for each arc (u, v) in Q:
14              alt ← dist[u] + Graph.Edges(u, v)
15              if alt < dist[v]:
16                  dist[v] ← alt
17                  prev[v] ← u
18
19      return dist[], prev[]

To find the shortest path between vertices source and target, the search terminates after line 10 if u = target. The shortest path from source to target can be obtained by reverse iteration:

1  S ← empty sequence
2  utarget
3  if prev[u] is defined or u = source:          // Proceed if the vertex is reachable
4      while u is defined:                       // Construct the shortest path with a stack S
5          S.push(u)                             // Push the vertex onto the stack
6          u ← prev[u]                           // Traverse from target to source

Now sequence S is the list of vertices constituting one of the shortest paths from source to target, or the empty sequence if no path exists.

A more general problem is to find all the shortest paths between source and target (there might be several of the same length). Then instead of storing only a single node in each entry of prev[] all nodes satisfying the relaxation condition can be stored. For example, if both r and source connect to target and they lie on different shortest paths through target (because the edge cost is the same in both cases), then both r and source are added to prev[target]. When the algorithm completes, prev[] data structure describes a graph that is a subset of the original graph with some edges removed. Its key property is that if the algorithm was run with some starting node, then every path from that node to any other node in the new graph is the shortest path between those nodes graph, and all paths of that length from the original graph are present in the new graph. Then to actually find all these shortest paths between two given nodes, a path finding algorithm on the new graph, such as depth-first search would work.

Using a priority queue

[edit]

A min-priority queue is an abstract data type that provides 3 basic operations: add_with_priority(), decrease_priority() and extract_min(). As mentioned earlier, using such a data structure can lead to faster computing times than using a basic queue. Notably, Fibonacci heap[19] or Brodal queue offer optimal implementations for those 3 operations. As the algorithm is slightly different in appearance, it is mentioned here, in pseudocode as well:

1   function Dijkstra(Graph, source):
2       Q ← Queue storing vertex priority
3       
4       dist[source] ← 0                          // Initialization
5       Q.add_with_priority(source, 0)            // associated priority equals dist[·]
6
7       for each vertex v in Graph.Vertices:
8           if vsource
9               prev[v] ← UNDEFINED               // Predecessor of v
10              dist[v] ← INFINITY                // Unknown distance from source to v
11              Q.add_with_priority(v, INFINITY)
12
13
14      while Q is not empty:                     // The main loop
15          uQ.extract_min()                   // Remove and return best vertex
16          for each arc (u, v) :                 // Go through all v neighbors of u
17              alt ← dist[u] + Graph.Edges(u, v)
18              if alt < dist[v]:
19                  prev[v] ← u
20                  dist[v] ← alt
21                  Q.decrease_priority(v, alt)
22
23      return (dist, prev)

Instead of filling the priority queue with all nodes in the initialization phase, it is possible to initialize it to contain only source; then, inside the if alt < dist[v] block, the decrease_priority() becomes an add_with_priority() operation.[7]: 198 

Yet another alternative is to add nodes unconditionally to the priority queue and to instead check after extraction (uQ.extract_min()) that it isn't revisiting, or that no shorter connection was found yet in the if alt < dist[v] block. This can be done by additionally extracting the associated priority p from the queue and only processing further if p == dist[u] inside the while Q is not empty loop.[20]

These alternatives can use entirely array-based priority queues without decrease-key functionality, which have been found to achieve even faster computing times in practice. However, the difference in performance was found to be narrower for denser graphs.[21]

Proof

[edit]

To prove the correctness of Dijkstra's algorithm, mathematical induction can be used on the number of visited nodes.[22]

Invariant hypothesis: For each visited node v, dist[v] is the shortest distance from source to v, and for each unvisited node u, dist[u] is the shortest distance from source to u when traveling via visited nodes only, or infinity if no such path exists. (Note: we do not assume dist[u] is the actual shortest distance for unvisited nodes, while dist[v] is the actual shortest distance)

Base case

[edit]

The base case is when there is just one visited node, source. Its distance is defined to be zero, which is the shortest distance, since negative weights are not allowed. Hence, the hypothesis holds.

Induction

[edit]

Assuming that the hypothesis holds for visited nodes, to show it holds for nodes, let u be the next visited node, i.e. the node with minimum dist[u]. The claim is that dist[u] is the shortest distance from source to u.

The proof is by contradiction. If a shorter path were available, then this shorter path either contains another unvisited node or not.

  • In the former case, let w be the first unvisited node on this shorter path. By induction, the shortest paths from source to u and w through visited nodes only have costs dist[u] and dist[w] respectively. This means the cost of going from source to u via w has the cost of at least dist[w] + the minimal cost of going from w to u. As the edge costs are positive, the minimal cost of going from w to u is a positive number. However, dist[u] is at most dist[w] because otherwise w would have been picked by the priority queue instead of u. This is a contradiction, since it has already been established that dist[w] + a positive number < dist[u].
  • In the latter case, let w be the last but one node on the shortest path. That means dist[w] + Graph.Edges[w,u] < dist[u]. That is a contradiction because by the time w is visited, it should have set dist[u] to at most dist[w] + Graph.Edges[w,u].

For all other visited nodes v, the dist[v] is already known to be the shortest distance from source already, because of the inductive hypothesis, and these values are unchanged.

After processing u, it is still true that for each unvisited node w, dist[w] is the shortest distance from source to w using visited nodes only. Any shorter path that did not use u, would already have been found, and if a shorter path used u it would have been updated when processing u.

After all nodes are visited, the shortest path from source to any node v consists only of visited nodes. Therefore, dist[v] is the shortest distance.

Running time

[edit]

Bounds of the running time of Dijkstra's algorithm on a graph with edges E and vertices V can be expressed as a function of the number of edges, denoted , and the number of vertices, denoted , using big-O notation. The complexity bound depends mainly on the data structure used to represent the set Q. In the following, upper bounds can be simplified because is for any simple graph, but that simplification disregards the fact that in some problems, other upper bounds on may hold.

For any data structure for the vertex set Q, the running time is:[2]

where and are the complexities of the decrease-key and extract-minimum operations in Q, respectively.

The simplest version of Dijkstra's algorithm stores the vertex set Q as a linked list or array, and edges as an adjacency list or matrix. In this case, extract-minimum is simply a linear search through all vertices in Q, so the running time is .

For sparse graphs, that is, graphs with far fewer than edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a self-balancing binary search tree, binary heap, pairing heap, Fibonacci heap or a priority heap as a priority queue to implement extracting minimum efficiently. To perform decrease-key steps in a binary heap efficiently, it is necessary to use an auxiliary data structure that maps each vertex to its position in the heap, and to update this structure as the priority queue Q changes. With a self-balancing binary search tree or binary heap, the algorithm requires

time in the worst case; for connected graphs this time bound can be simplified to . The Fibonacci heap improves this to

When using binary heaps, the average case time complexity is lower than the worst-case: assuming edge costs are drawn independently from a common probability distribution, the expected number of decrease-key operations is bounded by , giving a total running time of[7]: 199–200 

Practical optimizations and infinite graphs

[edit]

In common presentations of Dijkstra's algorithm, initially all nodes are entered into the priority queue. This is, however, not necessary: the algorithm can start with a priority queue that contains only one item, and insert new items as they are discovered (instead of doing a decrease-key, check whether the key is in the queue; if it is, decrease its key, otherwise insert it).[7]: 198  This variant has the same worst-case bounds as the common variant, but maintains a smaller priority queue in practice, speeding up queue operations.[12]

Moreover, not inserting all nodes in a graph makes it possible to extend the algorithm to find the shortest path from a single source to the closest of a set of target nodes on infinite graphs or those too large to represent in memory. The resulting algorithm is called uniform-cost search (UCS) in the artificial intelligence literature[12][23][24] and can be expressed in pseudocode as

procedure uniform_cost_search(start) is
    node ← start
    frontier ← priority queue containing node only
    expanded ← empty set
    do
        if frontier is empty then
            return failure
        node ← frontier.pop()
        if node is a goal state then
            return solution(node)
        expanded.add(node)
        for each of node's neighbors n do
            if n is not in expanded and not in frontier then
                frontier.add(n)
            else if n is in frontier with higher cost
                replace existing node with n

Its complexity can be expressed in an alternative way for very large graphs: when C* is the length of the shortest path from the start node to any node satisfying the "goal" predicate, each edge has cost at least ε, and the number of neighbors per node is bounded by b, then the algorithm's worst-case time and space complexity are both in O(b1+⌊C* ε).[23]

Further optimizations for the single-target case include bidirectional variants, goal-directed variants such as the A* algorithm (see § Related problems and algorithms), graph pruning to determine which nodes are likely to form the middle segment of shortest paths (reach-based routing), and hierarchical decompositions of the input graph that reduce st routing to connecting s and t to their respective "transit nodes" followed by shortest-path computation between these transit nodes using a "highway".[25] Combinations of such techniques may be needed for optimal practical performance on specific problems.[26]

Bidirectional Dijkstra

[edit]

Bidirectional Dijkstra is a variant of Dijkstra’s algorithm designed to efficiently compute the shortest path between a given source vertex s and target vertex t, rather than all vertices. The key idea is to run two simultaneous searches: one forward from s on the original graph and one backward from t on the graph with edges reversed. Each search maintains its own tentative distance estimates, priority queue, and set of “settled” vertices. The two frontiers advance toward each other and meet somewhere along the shortest s–t path.[27]

Two distance arrays are maintained: df[v] (distance from s to v) and db[v] (distance from v to t), initialized to ∞ except df[s] = 0 and db[t] = 0. Two priority queues Qf and Qb hold vertices not yet fully processed, and two sets Sf and Sb store vertices whose shortest-path distance is finalized in each direction. A variable μ stores the length of the best full s–t path found so far, initially ∞.

The algorithm repeatedly extracts the minimum-distance vertex from whichever queue currently has the smaller key, relaxes its outgoing (or incoming) edges, and updates μ whenever it discovers a connection between the two settled sets:

when relaxing edge (u,x) in the forward search and x ∈ Sb,

and symmetrically for the backward search.

A key feature is the stopping condition: once the sum of the current minimum priorities in both queues is at least μ, no yet-unsettled path can improve upon μ. At this point μ equals the true shortest-path distance δ(s,t), and the search terminates.[27]

This criterion avoids a common mistake of halting as soon as the two frontiers touch, which may overlook a shorter alternative crossing elsewhere.

Because each search only needs to explore roughly half of the search space in typical graphs, the bidirectional method often visits far fewer vertices and relaxes far fewer edges than one-directional Dijkstra on single-pair queries. In road-network–like graphs the savings can be dramatic, sometimes reducing the explored region from an exponential-size ball to two smaller half-balls.

The method is widely used in point-to-point routing for maps and navigation software and serves as a base for many speed-up techniques such as the ALT, contraction hierarchies, and reach-based routing.[28]

If the actual shortest path (not just its distance) is desired, predecessor pointers can be stored in both searches. When μ is updated through some crossing vertex x, the algorithm records this meeting point and reconstructs the final route as the forward path from s to x concatenated with the backward path from x to t.

Theoretical research shows that an optimized bidirectional approach can be instance-optimal, meaning no correct algorithm can asymptotically relax fewer edges on the same graph instance.[29]


Optimality for comparison-sorting by distance

[edit]

As well as simply computing distances and paths, Dijkstra's algorithm can be used to sort vertices by their distances from a given starting vertex. In 2023, Haeupler, Rozhoň, Tětek, Hladík, and Tarjan (one of the inventors of the 1984 heap), proved that, for this sorting problem on a positively-weighted directed graph, a version of Dijkstra's algorithm with a special heap data structure has a runtime and number of comparisons that is within a constant factor of optimal among comparison-based algorithms for the same sorting problem on the same graph and starting vertex but with variable edge weights. To achieve this, they use a comparison-based heap whose cost of returning/removing the minimum element from the heap is logarithmic in the number of elements inserted after it rather than in the number of elements in the heap.[30][31]

Specialized variants

[edit]

When arc weights are small integers (bounded by a parameter ), specialized queues can be used for increased speed. The first algorithm of this type was Dial's algorithm[32] for graphs with positive integer edge weights, which uses a bucket queue to obtain a running time . The use of a Van Emde Boas tree as the priority queue brings the complexity to .[33] Another interesting variant based on a combination of a new radix heap and the well-known Fibonacci heap runs in time .[33] Finally, the best algorithms in this special case run in [34] time and time.[35]

[edit]

Dijkstra's original algorithm can be extended with modifications. For example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution.

Dijkstra's algorithm is usually the working principle behind link-state routing protocols. OSPF and IS-IS are the most common.

Unlike Dijkstra's algorithm, the Bellman–Ford algorithm can be used on graphs with negative edge weights, as long as the graph contains no negative cycle reachable from the source vertex s. The presence of such cycles means that no shortest path can be found, since the label becomes lower each time the cycle is traversed. (This statement assumes that a "path" is allowed to repeat vertices. In graph theory that is normally not allowed. In theoretical computer science it often is allowed.) It is possible to adapt Dijkstra's algorithm to handle negative weights by combining it with the Bellman-Ford algorithm (to remove negative edges and detect negative cycles): Johnson's algorithm.

The A* algorithm is a generalization of Dijkstra's algorithm that reduces the size of the subgraph that must be explored, if additional information is available that provides a lower bound on the distance to the target.

The process that underlies Dijkstra's algorithm is similar to the greedy process used in Prim's algorithm. Prim's purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only two nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual edges.

Breadth-first search can be viewed as a special-case of Dijkstra's algorithm on unweighted graphs, where the priority queue degenerates into a FIFO queue.

The fast marching method can be viewed as a continuous version of Dijkstra's algorithm which computes the geodesic distance on a triangle mesh.

Dynamic programming perspective

[edit]

From a dynamic programming point of view, Dijkstra's algorithm is a successive approximation scheme that solves the dynamic programming functional equation for the shortest path problem by the Reaching method.[36][37][38]

In fact, Dijkstra's explanation of the logic behind the algorithm:[39]

Problem 2. Find the path of minimum total length between two given nodes P and Q. We use the fact that, if R is a node on the minimal path from P to Q, knowledge of the latter implies the knowledge of the minimal path from P to R.

is a paraphrasing of Bellman's principle of optimality in the context of the shortest path problem.

See also

[edit]

Notes

[edit]

References

[edit]
[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
Dijkstra's algorithm is a graph that solves the for a graph with non-negative edge weights, determining the shortest paths from a designated source vertex to all other vertices in the graph. Developed by Dutch , it was conceived in 1956 and first published in 1959 in the journal Numerische Mathematik. The algorithm operates using a greedy approach, maintaining a priority queue to always select and expand the vertex with the smallest known distance from the source. It builds a shortest path tree incrementally, ensuring that once a vertex is processed, its shortest path distance is finalized due to the non-negative weights preventing later reductions. With an efficient implementation using a binary heap, the time complexity is O((V + E) log V), where V is the number of vertices and E is the number of edges, making it suitable for large graphs. Dijkstra's algorithm is foundational in and has broad applications in , including network routing protocols such as OSPF () for internet traffic optimization and GPS navigation systems for finding minimal-distance routes. It assumes non-negative weights, distinguishing it from alternatives like the , which handles negative weights but at higher computational cost. Variations, such as the bidirectional version or use with heaps for improved performance, extend its utility in specialized scenarios.

History

Origins and Development

, a pioneering Dutch and theoretical physicist by training, joined the Mathematical Centre in in 1952 as a programmer, where he shifted his focus from physics to computing under the influence of Adriaan van Wijngaarden. At the Centre, a key institution for early Dutch computing research, Dijkstra contributed to significant hardware initiatives, including the development of the ARMAC, one of the first stored-program computers in . His work there emphasized practical programming challenges in an era without high-level languages, relying instead on assembly and . In 1956, Dijkstra invented the algorithm while addressing a need for the ARMAC's official inauguration demonstration, aiming to showcase the machine's potential to a non-technical audience through a relatable problem: the shortest path in a weighted graph representing a road network between Dutch cities such as and . This creation was motivated by routing optimization challenges in computer hardware design and communication networks, where efficient was essential for interconnecting components or signals. He devised the core method mentally in approximately 20 minutes during a break while in with his fiancée, Maria Debets, without using pencil or paper. The algorithm, initially implemented on the ARMAC using a simplified map of 64 cities encoded in 6 bits, highlighted the practical utility of graph-based computations in hardware contexts. Although formulated and applied in 1956, the algorithm remained unpublished for three years, circulating initially through internal notes at the Mathematical Centre. Dijkstra formalized it in a brief unpublished in before its peer-reviewed appearance later that year in Numerische Mathematik as part of the paper "A note on two problems in connexion with graphs," where it was presented alongside a solution to the problem. This publication marked the algorithm's entry into the academic , establishing Dijkstra's early reputation in . The method, now known as Dijkstra's algorithm, reflects his emphasis on elegant, efficient solutions to real-world routing dilemmas in both hardware and network systems.

Initial Publication and Impact

Dijkstra's algorithm was formally introduced in the paper "A note on two problems in connexion with graphs," published in the journal Numerische Mathematik. The three-page article presented as a solution to finding shortest paths in weighted graphs with non-negative edge weights, alongside a solution to the problem. Following its publication, the algorithm saw rapid adoption within and communities during the 1960s, where it became a standard tool for solving network optimization problems. This early embrace highlighted its efficiency and versatility, distinguishing it from contemporaneous methods like those proposed by Ford and Fulkerson. The algorithm's influence extended to practical network applications, notably shaping the shortest path first (SPF) routing protocol implemented in the ARPANET in the late , which computed optimal packet paths across . By establishing reliable methods for path computation in dynamic graphs, it solidified shortest path algorithms as a foundational topic in curricula and research. Edsger W. Dijkstra received the 1972 ACM for his fundamental contributions to programming and algorithm design, with the shortest path algorithm recognized as a key element of his enduring legacy in the field. Since the 1970s, the algorithm has appeared prominently in seminal textbooks, such as Donald Knuth's (Volume 3, 1973), ensuring its central role in education and further developments in graph algorithms.

Problem Formulation

Shortest Path Problem

The single-source shortest path (SSSP) problem, which Dijkstra's algorithm solves, requires computing the minimum-weight paths from a given source vertex to all other vertices in a graph. Formally, the input is a graph G=(V,E)G = (V, E), where VV is the set of vertices and EE is the set of edges (which may be directed or undirected), along with a w:ER0w: E \to \mathbb{R}_{\geq 0} assigning non-negative weights to edges, and a source vertex sVs \in V. The objective is to determine the shortest path distances δ(s,v)\delta(s, v) for every vertex vVv \in V, where δ(s,v)\delta(s, v) denotes the minimum total weight of any path from ss to vv, or infinity if no such path exists. The length of a path in this context is the sum of the weights of its constituent edges. The algorithm's output is typically an of these distances δ(s,v)\delta(s, v), and often includes a predecessor structure—such as an or —that allows reconstruction of the actual shortest paths by tracing back from each target vertex to the source. To illustrate, consider a simple undirected graph with vertices {A,B,C,D}\{A, B, C, D\}, source s=As = A, and edges with weights: AA-BB: 1, AA-CC: 4, BB-CC: 2, BB-DD: 5, CC-DD: 1. The shortest path distances from AA are δ(A,A)=0\delta(A, A) = 0, δ(A,B)=1\delta(A, B) = 1 (direct edge), δ(A,C)=3\delta(A, C) = 3 (path AA-BB-CC), and δ(A,D)=4\delta(A, D) = 4 (path AA-BB-CC-DD). This problem formulation differs from the all-pairs shortest paths problem, which computes distances between every pair of vertices and is addressed by algorithms such as Floyd-Warshall. Unlike variants permitting negative edge weights, which necessitate approaches like Bellman-Ford to handle potential negative cycles, the SSSP assumes non-negative weights to ensure optimality without such complications.

Graph Assumptions and Inputs

Dijkstra's algorithm requires that all edge weights in the graph are non-negative, denoted as w(e)0w(e) \geq 0 for every edge eEe \in E, to ensure the greedy selection of vertices produces optimal shortest paths. This assumption prevents the possibility of negative cycles, as negative weights would violate the non-negativity condition and could lead to undefined shortest paths; however, the non-negativity alone suffices to guarantee correctness without needing an explicit check for negative cycles. The algorithm applies to finite graphs, which may be directed or undirected; in the undirected case, edges are treated as bidirectional with symmetric weights. For disconnected graphs, the algorithm correctly assigns infinite distances to vertices unreachable from the source, using \infty to represent such cases. The input to Dijkstra's algorithm consists of a graph G=(V,E)G = (V, E), a source vertex sVs \in V, and a weight function w:E[0,)w: E \to [0, \infty) assigning non-negative weights to edges. The graph is typically represented using an adjacency list, where each vertex points to a list of its neighboring vertices along with the corresponding edge weights, enabling efficient traversal; alternatively, an adjacency matrix can be used, storing weights in a V×V|V| \times |V| array for denser graphs. The primary output is an array dd for each vertex vVv \in V, where dd holds the shortest-path distance from ss to vv, initialized to \infty for all vsv \neq s and updated to finite values for reachable vertices. Optionally, a predecessor array π\pi records the parent of vv in the , allowing reconstruction of the actual paths from ss to any reachable vv; unreachable vertices retain d=d = \infty and have no defined predecessor.

Algorithm Overview

Intuitive Explanation

Imagine navigating a using a to find the quickest routes from your starting point to various destinations. You begin at a central location and repeatedly choose to explore the nearest unvisited neighborhood that you haven't fully checked yet, updating your estimated travel times to surrounding areas based on the roads you've just traversed. This process mirrors Dijkstra's algorithm, where the "neighborhoods" are graph vertices and the "roads" are weighted edges representing distances or costs. By always prioritizing the closest option, the algorithm systematically uncovers the shortest paths without unnecessarily. At its core, operates greedily: it maintains tentative estimates from the source vertex to all others, initially setting the source's to zero and others to . It then selects the unprocessed vertex with the smallest known , "settles" it as final, and relaxes its outgoing edges to potentially shorten the estimates for neighboring vertices. This selection and update cycle continues until all vertices are settled, building a of shortest paths. A can efficiently manage the selection of the next vertex, though the intuition holds regardless of implementation details. The algorithm's reliability stems from the assumption of non-negative edge weights, which ensures that once a vertex's distance is settled, no shorter path can emerge later through unprocessed vertices—preventing the need to revisit and revise earlier choices. Intuitively, with positive or zero costs, the "wavefront" of explored paths expands outward without contractions, guaranteeing that greedy selections capture optimal routes. If negative weights were present, this expansion could loop indefinitely or require more complex handling, but non-negative weights keep the process straightforward and correct. Consider a simple weighted graph with vertices S (source), A, B, and T (target), and edges: S-A (weight 4), S-B (weight 2), A-T (weight 1), B-T (weight 5). Start with distances: S=0, A=∞, B=∞, T=∞.
  • Select S (distance 0); relax edges to update A=4 and B=2.
  • Next, select B (smallest 2); relax to T, updating T=7 (2+5).
  • Then, select A ( 4); relax to T, improving T=5 (4+1).
  • Finally, select T ( 5), completing the paths.
This yields shortest paths like S → A → T (total 5) or S → B → T (total 7, but the former is better). Each step refines estimates without altering settled distances, illustrating the progressive discovery.

Key Data Structures

Dijkstra's algorithm employs a set of structures to track and update shortest path estimates across the graph's vertices. These structures enable the systematic selection of vertices for permanent distance assignment and the of distance improvements to adjacent nodes. The distance array, typically denoted as dd for each vertex vv, holds the current estimate of the shortest path distance from the source vertex ss to vv. Upon initialization, d=0d = 0 and d=d = \infty for all vsv \neq s, reflecting that no paths have been discovered yet except to the source itself. As the algorithm progresses, dd is updated only if a shorter path is found through relaxation, ensuring it always represents the best-known distance. Complementing the distance array is the predecessor array π\pi, which records the immediate predecessor of each vertex vv along the shortest path from ss. Initialized to nil for all vertices, π\pi is updated during relaxation whenever dd decreases, effectively building a that allows path reconstruction by from any target vertex to the source. To distinguish vertices whose distances are finalized from those still under consideration, the algorithm uses a set SS of settled vertices, starting empty and growing as vertices are selected and their distances confirmed as shortest. The unsettled vertices, implicitly VSV \setminus S where VV is the vertex set, represent nodes with tentative distances subject to potential improvement. This partitioning ensures that once a vertex enters SS, no further updates occur to its distance. For efficient selection of the unsettled vertex with the minimum dd, a QQ stores all unsettled vertices, prioritized by their current distance estimates. QQ supports insertion of vertices initially and extract-min operations to retrieve and settle the next candidate, facilitating the core loop of distance minimization and relaxation.

Detailed Description

Initialization Phase

The initialization phase of Dijkstra's algorithm establishes the starting conditions for computing shortest paths from a designated source vertex ss in a weighted graph G=(V,E)G = (V, E) with nonnegative edge weights. This phase sets up the distance estimates and predecessor information for all vertices, prepares a collection of unsettled vertices, and populates a priority queue to facilitate the selection of the next vertex to process. These steps ensure that the algorithm begins with a well-defined state where only the source has a finite distance, and all other vertices are marked as unreachable initially. First, the algorithm initializes a distance array dd and a predecessor array π\pi for each vertex vVv \in V. The distance to the source is set to zero, i.e., d=0d = 0, and its predecessor is set to null, π=NIL\pi = \text{NIL}, reflecting that no path precedes the source itself. For all other vertices vsv \neq s, the distances are set to infinity, d=d = \infty, and their predecessors to null, π=NIL\pi = \text{NIL}, indicating that no shortest paths have been discovered yet. This setup assumes that infinity represents an unbounded value larger than any possible path length in the graph, ensuring that initial estimates do not bias the search toward incorrect paths. Next, the algorithm defines an unsettled set UU, which initially contains all vertices in VV, i.e., U=VU = V. This set tracks vertices whose shortest-path distances are not yet finalized, allowing the main loop to iteratively select and settle vertices until UU is empty. The unsettled set plays a crucial role in maintaining the invariant that unsettled vertices may still have their distances updated based on newly discovered paths. All vertices are then inserted into a QQ, with each vertex vv keyed by its current estimate dd. For the source, the key is 0, while all others have key \infty. The enables efficient extraction of the unsettled vertex with the minimum estimate in subsequent phases, guiding the algorithm to explore paths in order of increasing cost. Optionally, if the graph is represented using an , this phase may include verifying or preparing the list structure, where each vertex points to its neighboring vertices and the corresponding edge weights, to support efficient neighbor traversal during relaxation. This representation is standard for sparse graphs and ensures that edge access is O(deg(v))O(\deg(v)) per vertex vv.

Relaxation and Selection Phase

The relaxation and selection phase forms the core iterative process of Dijkstra's algorithm, repeatedly choosing a vertex to settle and updating tentative s to its neighbors until all reachable vertices are processed. This phase builds upon the initialization, where the source vertex ss has d=0d = 0, all other vertices have d=d = \infty, and the set QQ of unsettled vertices initially includes all vertices in the graph. While QQ is not empty, the algorithm selects the vertex uQu \in Q with the minimum tentative distance dd, finalizes dd as the shortest path distance from ss to uu, and removes uu from QQ. This selection ensures that uu is the next vertex whose shortest path is guaranteed to be correctly computed, given non-negative edge weights. For each neighbor vv of uu (i.e., each outgoing edge (u,v)(u, v) with w(u,v)w(u, v)), the algorithm applies relaxation: if d>d+w(u,v)d > d + w(u, v), it updates dd to d+w(u,v)d + w(u, v), sets the predecessor π=u\pi = u to track the path, and decreases the key of vv in QQ to reflect the improved distance estimate. This step potentially propagates shorter paths through the graph by considering paths that end at the newly settled uu. The phase terminates when QQ becomes empty, indicating that all vertices have been settled and their distances finalized. Any vertex vv with d=d = \infty at termination is unreachable from ss, as no path exists to it under the graph's non-negative weights.

Pseudocode Implementations

Basic Array-Based Version

The basic array-based version of Dijkstra's algorithm performs the extract-min operation by linearly scanning all unsettled vertices to identify the one with the smallest tentative distance from the source, repeating this process |V| times. This straightforward implementation, which avoids advanced data structures, achieves correctness under the same assumptions as the original algorithm: non-negative edge weights and a connected, directed or undirected graph represented typically via an . The algorithm begins by initializing two arrays: the distance array dd, where d=0d = 0 for the source vertex ss and d=d = \infty for all other vertices vv, and the predecessor array π\pi, set to NIL for all vv. A array settled\textit{settled} is also initialized to false for all vertices, tracking which vertices have been permanently finalized. In each of the |V| main iterations, the algorithm scans the unsettled vertices to find the one uu with the minimum dd (taking O(|V|) time per scan), marks uu as settled, and then relaxes all edges outgoing from uu by updating dd and π\pi for each neighbor vv if a shorter path is found. Here is the pseudocode for this version:

DIJKSTRA(G, w, s) for each vertex v ∈ G.V d[v] = ∞ π[v] = NIL settled[v] = false d[s] = 0 for i = 1 to |G.V| // Extract-min: linear scan over unsettled vertices, O(|V|) time u = NIL min_dist = ∞ for each vertex v ∈ G.V if not settled[v] and d[v] < min_dist min_dist = d[v] u = v if u == NIL break // All remaining vertices are unreachable settled[u] = true for each neighbor v of u (i.e., (u,v) ∈ G.E) if d[v] > d[u] + w(u, v) d[v] = d[u] + w(u, v) π[v] = u

DIJKSTRA(G, w, s) for each vertex v ∈ G.V d[v] = ∞ π[v] = NIL settled[v] = false d[s] = 0 for i = 1 to |G.V| // Extract-min: linear scan over unsettled vertices, O(|V|) time u = NIL min_dist = ∞ for each vertex v ∈ G.V if not settled[v] and d[v] < min_dist min_dist = d[v] u = v if u == NIL break // All remaining vertices are unreachable settled[u] = true for each neighbor v of u (i.e., (u,v) ∈ G.E) if d[v] > d[u] + w(u, v) d[v] = d[u] + w(u, v) π[v] = u

This implementation is particularly suitable for small graphs (e.g., |V| ≤ 1000) or dense graphs where the O(|V|^2) time bound is tolerable relative to the simplicity and low constant factors involved. To illustrate, consider a small graph with vertices A (source), B, and C, and edges A→B with weight 1, A→C with weight 4, and B→C with weight 2. Initialization sets d[A] = 0, d[B] = ∞, d[C] = ∞, and all π to NIL, with no vertices settled. In the first iteration, A has the minimum distance (0), so it is settled; relaxing its edges updates d[B] = 1 (π[B] = A) and d[C] = 4 (π[C] = A). The second iteration selects B (minimum unsettled distance 1) and settles it; relaxing B→C updates d[C] = 3 (π[C] = B), as 1 + 2 < 4. The third iteration selects C (now distance 3) and settles it, with no further updates. The final distances are d[A] = 0, d[B] = 1, d[C] = 3, and the shortest path to C is A→B→C.

Priority Queue Version

The priority queue version of Dijkstra's algorithm enhances efficiency by replacing the linear-time minimum selection of the array-based implementation with a data structure that supports faster extraction of the minimum and key updates. This approach uses a generic priority queue, typically implemented as a binary heap, to maintain vertices ordered by their current shortest-path distance estimates from the source. The queue supports two key operations: EXTRACT-MIN to retrieve and remove the vertex with the smallest key, and DECREASE-KEY to reduce the key (distance) of a vertex already in the queue when a shorter path is discovered. The algorithm initializes the priority queue Q with all vertices, each keyed by their initial distance estimate d, which is infinity for all vertices except the source s where d = 0. During execution, when relaxing edges from a selected vertex u, if a neighbor v's distance can be improved, the algorithm updates d and calls DECREASE-KEY(Q, v, d) to reflect the new lower key in the queue, ensuring the heap's internal structure adjusts the vertex's position accordingly without duplicating entries. This handling avoids the inefficiency of re-inserting vertices, maintaining a single entry per vertex while keeping the queue's size at most |V|. The pseudocode assumes a graph G = (V, E) with non-negative edge weights w(u, v) ≥ 0, directed or undirected, and a priority queue supporting EXTRACT-MIN in O(log |V|) time and DECREASE-KEY in O(log |V|) time, as provided by a binary heap. It also relies on an adjacency list representation for accessing neighbors and arrays d[] and π[] to track distances and predecessors, respectively. Unlike the basic array-based version, which scans all unprocessed vertices for the minimum each time, this version achieves logarithmic-time selections, making it suitable for sparse graphs.

DIJKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S ← ∅ 3 Q ← V[G] // priority queue with all vertices, keyed by d[v] 4 while Q ≠ ∅ 5 do u ← EXTRACT-MIN(Q) 6 S ← S ∪ {u} 7 for each vertex v ∈ Adj[u] 8 do RELAX(u, v, w) // where RELAX(u, v, w) is: if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) π[v] ← u DECREASE-KEY(Q, v, d[v])

DIJKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S ← ∅ 3 Q ← V[G] // priority queue with all vertices, keyed by d[v] 4 while Q ≠ ∅ 5 do u ← EXTRACT-MIN(Q) 6 S ← S ∪ {u} 7 for each vertex v ∈ Adj[u] 8 do RELAX(u, v, w) // where RELAX(u, v, w) is: if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) π[v] ← u DECREASE-KEY(Q, v, d[v])

To illustrate, consider a simple with vertices s, a, b, c and edges s→a (weight 4), s→b (weight 2), a→c (weight 1), b→c (weight 5). Initialize Q with keys: s:0, a:∞, b:∞, c:∞. Extract s (min key 0), relax to a (update d=4, DECREASE-KEY(a,4)) and b (d=2, DECREASE-KEY(b,2)); Q now keys: b:2, a:4, c:∞. Extract b (key 2), relax to c (d=2+5=7, DECREASE-KEY(c,7)); Q: a:4, c:7. Extract a (key 4), relax to c (4+1=5 <7, update d=5, DECREASE-KEY(c,5)); Q: c:5. Extract c (key 5), done. This trace processes only 4 extractions and 3 decrease-keys, avoiding the O(V^2) scans of the basic version on this small graph, with final distances d=0, d=4, d=2, d=5.

Proof of Correctness

Base Case Analysis

The base case for the proof of correctness of Dijkstra's algorithm is established immediately following the initialization phase. In this phase, the distance estimate from the source vertex ss to itself is set to d=0d = 0, which equals the true shortest-path δ(s,s)=0\delta(s, s) = 0, as the path length to itself is zero by definition. For every other vertex vsv \neq s, the distance estimate is initialized to d=d = \infty, ensuring that dδ(s,v)d \geq \delta(s, v) holds, since δ(s,v)\delta(s, v) is either a non-negative finite value or infinite in the case of unreachable vertices. At this point, the set of settled vertices SS is empty, so the —that d=δ(s,u)d = \delta(s, u) for all uSu \in S—holds vacuously true, with no vertices yet permanently labeled. The first of the main loop then extracts the vertex u=su = s from the , as it possesses the minimum distance estimate of 0 among all vertices. Upon adding ss to SS, the set becomes S={s}S = \{s\}, and the invariant is satisfied since d=0=δ(s,s)d = 0 = \delta(s, s). No edge relaxations have been performed prior to this extraction, rendering the distances trivially correct for the settled set without any prior adjustments. This base case relies on the algorithm's assumption of non-negative edge weights, which ensures no shorter paths can exist that would contradict the initial estimates, although the trivial nature of the starting point introduces no contradictions even without relaxations.

Inductive Invariant

The inductive invariant for Dijkstra's algorithm ensures that the distance estimates remain correct throughout execution, providing a foundation for proving the algorithm's overall correctness. Specifically, after each vertex uu is settled (i.e., extracted from the and added to the set SS of settled vertices), the invariant states that d=δ(s,v)d = \delta(s, v) for all vSv \in S, where dd is the current distance estimate from the source ss to vv, and δ(s,v)\delta(s, v) is the true shortest-path distance from ss to vv; additionally, dδ(s,v)d \geq \delta(s, v) for all vSv \notin S. This invariant holds under the assumption of non-negative edge weights, as negative weights could invalidate the distance estimates during relaxation. The proof of this invariant proceeds by mathematical induction on the size of the settled set SS. The base case, where S=1|S| = 1 and S={s}S = \{s\}, is established prior to the main loop: d=0=δ(s,s)d = 0 = \delta(s, s), and d=δ(s,v)d = \infty \geq \delta(s, v) for all vsv \neq s, satisfying the invariant trivially. For the inductive hypothesis, assume the invariant holds after S=k|S| = k vertices have been settled, for some k1k \geq 1. Now consider the (k+1)(k+1)-th iteration, where the vertex uSu \notin S with the smallest dd is extracted and settled, making S=k+1|S| = k+1. To verify the invariant for the new SS, first show that d=δ(s,u)d = \delta(s, u). Suppose, for contradiction, that δ(s,u)<d\delta(s, u) < d. Consider a shortest path PP from ss to uu. Let xx be the last vertex on PP that belongs to SS (such a vertex exists since sSs \in S), and let yy be the successor of xx on PP, so ySy \notin S and the edge (x,y)(x, y) leaves SS. By the inductive hypothesis, d=δ(s,x)d = \delta(s, x). When xx was settled, the edge to yy was relaxed, so dd+w(x,y)=δ(s,x)+w(x,y)d \leq d + w(x, y) = \delta(s, x) + w(x, y). Now, δ(s,u)=δ(s,x)+w(x,y)+δ(y,u)\delta(s, u) = \delta(s, x) + w(x, y) + \delta(y, u). Since edge weights are non-negative, δ(y,u)0\delta(y, u) \geq 0, so δ(s,x)+w(x,y)=δ(s,u)δ(y,u)δ(s,u)<d\delta(s, x) + w(x, y) = \delta(s, u) - \delta(y, u) \leq \delta(s, u) < d. Therefore, dδ(s,x)+w(x,y)δ(s,u)<dd \leq \delta(s, x) + w(x, y) \leq \delta(s, u) < d. But ySy \notin S, and uu was selected as the unsettled vertex with minimum dd, so dd<dd \leq d < d, a contradiction. Thus, d=δ(s,u)d = \delta(s, u), and the invariant holds for all vSv \in S after settling uu. Next, after relaxing all edges outgoing from uu, the estimates for unsettled vertices vSv \notin S must satisfy dδ(s,v)d \geq \delta(s, v). Before relaxation, the invariant ensures dδ(s,v)d \geq \delta(s, v) for vSv \notin S. Relaxation from uu can only decrease dd if a shorter path via uu is found, setting it to d+w(u,v)=δ(s,u)+w(u,v)δ(s,v)d + w(u, v) = \delta(s, u) + w(u, v) \geq \delta(s, v), since δ(s,v)δ(s,u)+w(u,v)\delta(s, v) \leq \delta(s, u) + w(u, v) by the for shortest paths and non-negative weights. Thus, the updated dd remains δ(s,v)\geq \delta(s, v). Moreover, no overlooked shorter path from ss to vv can exist via unsettled nodes, as that would imply an even shorter path to uu or contradict the finality of settled distances. Thus, the inductive step holds, preserving the invariant for S=k+1|S| = k+1. By induction, the invariant holds after every . When S=VS = V (the full vertex set), all vertices are settled, so d=δ(s,v)d = \delta(s, v) for every vVv \in V, confirming that the algorithm computes the correct shortest-path from ss.

Complexity Analysis

Time and Space Complexity

Dijkstra's algorithm, in its basic implementation using an to track the minimum vertex, exhibits a of O(V2)O(|V|^2), where V|V| denotes the number of vertices. This arises from performing V|V| extract-minimum operations, each requiring O(V)O(|V|) time to scan the , alongside E|E| relaxation operations that each take O(1)O(1) time, where E|E| is the number of edges. An enhanced implementation employs a as a , reducing the time complexity to O((V+E)logV)O((|V| + |E|) \log |V|). Here, each of the V|V| extract-min operations costs O(logV)O(\log |V|), and up to E|E| decrease-key operations (for relaxations) each incur O(logV)O(\log |V|) time, yielding the overall bound. Regarding , the algorithm requires O(V)O(|V|) space for the distance array dd, predecessor array π\pi, and QQ, in addition to O(V+E)O(|V| + |E|) for storing the graph representation. The varies with graph density: for sparse graphs where E=O(V)|E| = O(|V|), the version approaches O(VlogV)O(|V| \log |V|); in dense graphs, E|E| can reach O(V2)O(|V|^2), making the bound O(V2logV)O(|V|^2 \log |V|).

Optimizations for Large Graphs

One key optimization for Dijkstra's algorithm on large graphs involves replacing the standard with a as the data structure. This change supports amortized constant-time decrease-key operations, leading to an overall of O(E+VlogV)O(|E| + |V| \log |V|), which is asymptotically superior to the O((E+V)logV)O((|E| + |V|) \log |V|) bound of binary heaps for dense graphs. For graphs with small non-negative integer edge weights, Dial's algorithm provides another efficient variant by using a bucket queue (an array of lists indexed by distance values) instead of a general . This approach achieves a of O(V+E+WV)O(|V| + |E| + W \cdot |V|), where WW is the maximum edge weight, making it particularly effective when WW is small compared to V|V|, as it avoids logarithmic factors altogether. In single-target shortest path problems, early termination can significantly reduce computation by halting the algorithm once the target vertex is extracted from the , as all remaining vertices will have distances at least as large. This optimization is inherent to the greedy selection process and preserves correctness, often yielding substantial speedups on large graphs where the target is reached before processing all vertices. For very large or theoretically infinite graphs, such as those arising in continuous spaces or expansive networks, pure Dijkstra implementations may fail to terminate or become impractical due to unbounded exploration. Label-correcting algorithms, which relax the strict label-setting invariant of Dijkstra and allow multiple updates per vertex, offer a more flexible approach; when combined with node potentials to shift edge weights to non-negative values, they enable efficient computation even in the presence of negative weights or expansive structures. Approximations and heuristics, such as bucketing vertices by distance intervals in Δ-stepping variants, further bound exploration by processing groups of nodes simultaneously, achieving practical performance on massive graphs with time complexities approaching linear in practice for suitable parameter choices.

Variants and Extensions

Bidirectional search extends Dijkstra's algorithm to compute the shortest path from a source vertex ss to a target vertex tt by performing simultaneous searches from both endpoints, aiming to for improved efficiency. This variant initiates two instances of Dijkstra's algorithm: one forward from ss and one backward from tt on the reversed graph, where edge directions are inverted to simulate searching toward ss. The searches proceed until their explored sets intersect, at which point the shortest path is reconstructed by combining the forward and backward paths meeting at the intersection node. This approach, first formalized as a bidirectional extension of shortest path algorithms, reduces the effective search space compared to unidirectional Dijkstra, particularly in graphs where the shortest path is roughly balanced between ss and tt. The mechanics involve maintaining two priority queues—one for each direction—and alternating extractions of the minimum-distance vertex from them to ensure balanced progress. For each extracted vertex, the algorithm relaxes outgoing edges (forward) or incoming edges (backward, via the reversed graph) and updates distances if a shorter path is found. Intersection is checked after each expansion by verifying if the newly settled vertex belongs to the opposite search's settled set, using efficient data structures like hash sets for O(1) lookups. Upon detecting an intersection at vertex vv, the total path length is the sum of the distances from ss to vv and from tt to vv, and the path is traced via predecessor pointers from both directions. This process preserves Dijkstra's correctness guarantees, as both sub-searches maintain non-negative distances and settle vertices in increasing order. In terms of complexity, bidirectional Dijkstra achieves instance-optimality, with time complexity proportional to the number of edges accessed during the searches plus a logarithmic factor for operations, often significantly less than full unidirectional Dijkstra. Using a , the overall time is O((|E_f| + |V_f| \log |V|) + (|E_b| + |V_b| \log |V|)), where subscripts denote forward and backward expansions; in practice, this is roughly half the work of standard Dijkstra for balanced expansions, as each search explores approximately the of the unidirectional search space in uniform-cost settings. For unweighted graphs, it is O(\Delta \cdot \min(|E_s|, |E_t|)), where \Delta is the maximum degree, outperforming unidirectional search by a factor up to O(\sqrt{|V|}) in tree-like structures. Empirical studies on random graphs confirm 2-4 times fewer node expansions than unidirectional methods. This variant is limited to undirected graphs or directed graphs with symmetric weights, as the backward search requires a valid reversed graph with equivalent costs; asymmetric weights can lead to incorrect paths. It also assumes non-negative edge weights, inheriting Dijkstra's constraints, and may not yield speedup if the is skewed toward one endpoint, such as in graphs with bottlenecks near ss or tt. For example, in a uniform grid graph representing a 2D (e.g., a 100x100 lattice with unit edge weights), bidirectional search from opposite corners expands roughly \sqrt{2} \times 10^4 nodes total versus 2 \times 10^4 for unidirectional, meeting near the center after exploring diamond-shaped frontiers that intersect midway.

Parallel Implementations

Parallel implementations of Dijkstra's algorithm adapt the core relaxation phase to leverage multi-core processors, distributed systems, and GPUs, enabling efficient shortest-path computation on large-scale graphs. In shared-memory environments, multi-threading focuses on parallelizing edge relaxations after selecting the minimum-distance vertex, with threads updating distances concurrently while using locks or critical sections to synchronize access to shared distance arrays. For instance, implementations partition vertices among threads, applying barriers after each relaxation round to ensure consistency, which yields performance gains on graphs with or more vertices but suffers from overheads. In distributed settings using message-passing interfaces like MPI, the algorithm propagates distance updates asynchronously across processors, each owning a subset of vertices and edges. Processors relax local edges and exchange boundary updates via messages, with global synchronization steps to determine the next minimum-distance vertex, often employing lookahead techniques to process vertices slightly beyond the current frontier for better load balance. The Parallel Boost Graph Library (PBGL) implements this via a distributed , achieving near-linear strong scaling on random graphs and sub-linear on networks when tuned with appropriate lookahead factors, such as λ=400 for road datasets on clusters with up to 128 nodes. GPU implementations exploit massive through work-efficient scheduling of relaxations, often using variants like delta-stepping to vertices by distance approximations, enabling parallel prefix sums or scans to process large fronts simultaneously. A notable approach is the Asynchronous Dynamic Delta-Stepping (ADDS) , which dynamically adjusts counts (up to 32) and uses custom allocators for memory efficiency, outperforming prior GPU methods by 2.9× on average across diverse graphs on 2080 Ti hardware. These adaptations achieve near-linear speedups on well-balanced workloads, such as synthetic random graphs, but face challenges in load balancing for irregular graphs like road networks, where uneven vertex degrees lead to thread or processor idleness and increased costs.

Real-World Uses

Dijkstra's algorithm serves as the foundational shortest-path computation mechanism in several link-state routing protocols used for IP packet forwarding in computer networks. In the Open Shortest Path First (OSPF) protocol, routers exchange link-state advertisements to build a complete topology map, upon which Dijkstra's algorithm is applied to calculate the shortest paths from a given source to all destinations, enabling efficient packet routing across autonomous systems. Similarly, the Intermediate System to Intermediate System (IS-IS) protocol integrates Dijkstra's shortest path first (SPF) algorithm to compute forwarding tables based on collected link-state information, supporting both IPv4 and IPv6 environments in large-scale ISP backbones. In transportation systems, Dijkstra's algorithm underpins route optimization in GPS navigation applications by modeling road networks as weighted graphs, where edge weights represent distances, travel times, or traffic conditions. For instance, systems like employ variants of Dijkstra's algorithm to determine the shortest routes between origins and destinations, dynamically adjusting weights to account for real-time traffic data and thereby minimizing expected time. This approach ensures scalable computation for urban scenarios, handling millions of nodes corresponding to intersections and segments. Robotics leverages Dijkstra's algorithm for path planning in environments represented as grids or graphs, where nodes denote positions and edges carry costs based on difficulty or proximity. In mobile robots navigating indoor or outdoor spaces, the algorithm identifies -avoiding paths by prioritizing low-cost traversals, as demonstrated in applications for substation inspection robots that refine search areas to enhance efficiency in cluttered settings. Such implementations are particularly effective in static or semi-static grids, providing collision-free trajectories that balance computational speed with optimality. In bioinformatics, Dijkstra's algorithm facilitates analysis of protein-protein interaction (PPI) networks by treating proteins as nodes and interactions as weighted edges, often with weights reflecting binding affinities or functional similarities. Researchers apply it to identify shortest paths between disease-associated proteins and potential drug targets, such as in studies where it traces connectivity in PPI graphs to uncover intermediate genes. This graph-theoretic approach aids in elucidating genetic architectures underlying , like , by quantifying network distances and highlighting key pathways.

Connections to Other Algorithms

Dijkstra's algorithm can be interpreted as a specialized form of dynamic programming tailored to graphs with non-negative edge weights, where it computes shortest paths by relaxing edges in a specific order that respects the principle of optimality. This approach processes vertices in non-decreasing order of their tentative distances from the source, effectively solving the problem on an implicit (DAG) induced by these increasing distances. The core of this connection lies in the , which defines the shortest-path distance δ(s,v)\delta(s, v) from the source ss to a vertex vv as: δ(s,v)=min(u,v)E(δ(s,u)+w(u,v)),\delta(s, v) = \min_{(u, v) \in E} \left( \delta(s, u) + w(u, v) \right), where EE is the set of edges and w(u,v)w(u, v) is the weight of edge (u,v)(u, v). This recursive relation, rooted in Bellman's work on routing problems, ensures that optimal subpaths contribute to the overall shortest path, allowing Dijkstra's greedy selections to align with dynamic programming's successive approximation method. A primary alternative to Dijkstra's algorithm is the Bellman-Ford algorithm, which extends the dynamic programming framework to handle graphs containing negative edge weights, provided no negative-weight cycles exist. Unlike Dijkstra's greedy prioritization, Bellman-Ford iteratively relaxes all edges V1|V|-1 times, achieving a of O(VE)O(|V| |E|), which is less efficient for dense graphs with non-negative weights but necessary when negatives are present. Another related method is the A* algorithm, which builds directly on Dijkstra's framework by incorporating an h(v)h(v) to estimate the cost to the goal, prioritizing nodes via f(v)=g(v)+h(v)f(v) = g(v) + h(v), where g(v)g(v) is the path cost from the source. This heuristic guidance reduces explored nodes in goal-oriented searches, and A* degenerates to Dijkstra when h(v)=0h(v) = 0 for all vv. For all-pairs shortest paths, leverages Dijkstra's efficiency by first applying Bellman-Ford to compute potentials p(v)p(v) for each vertex vv, reweighting edges as w^(u,v)=w(u,v)+p(u)p(v)\hat{w}(u, v) = w(u, v) + p(u) - p(v) to ensure non-negativity without altering relative path lengths. Dijkstra is then executed from each source in the reweighted graph, yielding an overall of O(V2logV+VE)O(|V|^2 \log |V| + |V| |E|) with Fibonacci heaps, making it suitable for sparse graphs with possible negative weights. The choice among these algorithms depends on graph properties and problem requirements, as summarized below:
AlgorithmHandles Negative WeightsTime ComplexityPrimary Use Case
DijkstraNo$O((V
Bellman-FordYes (no negative cycles)$O(V
A*No (admissible heuristic required)Varies with heuristic qualityInformed single-target in large spaces
Johnson'sYes (no negative cycles)$O(V
These complexities assume standard implementations with binary heaps for Dijkstra and A*; advanced data structures like heaps can improve Dijkstra and Johnson's bounds further.

References

Add your contribution
Related Hubs
User Avatar
No comments yet.