Post · 10 min read· pp. 605–609

Dijkstra Algorithm

You are given a directed or undirected weighted graph with nn vertices and mm edges. The weights of all edges are non-negative. You are also given a starting vertex ss. This article discusses finding the lengths of the shortest paths from a starting vertex ss to all other vertices, and output the shortest paths themselves.

This problem is also called single-source shortest paths problem.

Algorithm

Here is an algorithm described by the Dutch computer scientist Edsger W. Dijkstra in 1959.

Let's create an array d[]d[] where for each vertex vv we store the current length of the shortest path from ss to vv in d[v]d[v]. Initially d[s]=0d[s] = 0, and for all other vertices this length equals infinity. In the implementation a sufficiently large number (which is guaranteed to be greater than any possible path length) is chosen as infinity.

d[v]=∞, v≠sd[v] = \infty,~ v \ne s

In addition, we maintain a Boolean array u[]u[] which stores for each vertex vv whether it's marked. Initially all vertices are unmarked:

u[v]=falseu[v] = {\rm false}

The Dijkstra's algorithm runs for nn iterations. At each iteration a vertex vv is chosen as unmarked vertex which has the least value d[v]d[v]:

Evidently, in the first iteration the starting vertex ss will be selected.

The selected vertex vv is marked. Next, from vertex vv relaxations are performed: all edges of the form (v,to)(v,\text{to}) are considered, and for each vertex to\text{to} the algorithm tries to improve the value d[to]d[\text{to}]. If the length of the current edge equals lenlen, the code for relaxation is:

d[to]=min⁡(d[to],d[v]+len)d[\text{to}] = \min (d[\text{to}], d[v] + len)

After all such edges are considered, the current iteration ends. Finally, after nn iterations, all vertices will be marked, and the algorithm terminates. We claim that the found values d[v]d[v] are the lengths of shortest paths from ss to all vertices vv.

Note that if some vertices are unreachable from the starting vertex ss, the values d[v]d[v] for them will remain infinite. Obviously, the last few iterations of the algorithm will choose those vertices, but no useful work will be done for them. Therefore, the algorithm can be stopped as soon as the selected vertex has infinite distance to it.

Restoring Shortest Paths

Usually one needs to know not only the lengths of shortest paths but also the shortest paths themselves. Let's see how to maintain sufficient information to restore the shortest path from ss to any vertex. We'll maintain an array of predecessors p[]p[] in which for each vertex v≠sv \ne s, p[v]p[v] is the penultimate vertex in the shortest path from ss to vv. Here we use the fact that if we take the shortest path to some vertex vv and remove vv from this path, we'll get a path ending in at vertex p[v]p[v], and this path will be the shortest for the vertex p[v]p[v]. This array of predecessors can be used to restore the shortest path to any vertex: starting with vv, repeatedly take the predecessor of the current vertex until we reach the starting vertex ss to get the required shortest path with vertices listed in reverse order. So, the shortest path PP to the vertex vv is equal to:

P=(s,…,p[p[p[v]]],p[p[v]],p[v],v)P = (s, \ldots, p[p[p[v]]], p[p[v]], p[v], v)

Building this array of predecessors is very simple: for each successful relaxation, i.e. when for some selected vertex vv, there is an improvement in the distance to some vertex to\text{to}, we update the predecessor vertex for to\text{to} with vertex vv:

p[to]=vp[\text{to}] = v

Proof

The main assertion on which Dijkstra's algorithm correctness is based is the following:

After any vertex vv becomes marked, the current distance to it d[v]d[v] is the shortest, and will no longer change.

The proof is done by induction. For the first iteration this statement is obvious: the only marked vertex is ss, and the distance to is d[s]=0d[s] = 0 is indeed the length of the shortest path to ss. Now suppose this statement is true for all previous iterations, i.e. for all already marked vertices; let's prove that it is not violated after the current iteration completes. Let vv be the vertex selected in the current iteration, i.e. vv is the vertex that the algorithm will mark. Now we have to prove that d[v]d[v] is indeed equal to the length of the shortest path to it l[v]l[v].

Consider the shortest path PP to the vertex vv. This path can be split into two parts: P1P_1 which consists of only marked nodes (at least the starting vertex ss is part of P1P_1), and the rest of the path P2P_2 (it may include a marked vertex, but it always starts with an unmarked vertex). Let's denote the first vertex of the path P2P_2 as pp, and the last vertex of the path P1P_1 as qq.

First we prove our statement for the vertex pp, i.e. let's prove that d[p]=l[p]d[p] = l[p]. This is almost obvious: on one of the previous iterations we chose the vertex qq and performed relaxation from it. Since (by virtue of the choice of vertex pp) the shortest path to pp is the shortest path to qq plus edge (p,q)(p,q), the relaxation from qq set the value of d[p]d[p] to the length of the shortest path l[p]l[p].

Since the edges' weights are non-negative, the length of the shortest path l[p]l[p] (which we just proved to be equal to d[p]d[p]) does not exceed the length l[v]l[v] of the shortest path to the vertex vv. Given that l[v]≤d[v]l[v] \le d[v] (because Dijkstra's algorithm could not have found a shorter way than the shortest possible one), we get the inequality:

d[p]=l[p]≤l[v]≤d[v]d[p] = l[p] \le l[v] \le d[v]

On the other hand, since both vertices pp and vv are unmarked, and the current iteration chose vertex vv, not pp, we get another inequality:

d[p]≥d[v]d[p] \ge d[v]

From these two inequalities we conclude that d[p]=d[v]d[p] = d[v], and then from previously found equations we get:

d[v]=l[v]d[v] = l[v]

Q.E.D.

Implementation

Dijkstra's algorithm performs nn iterations. On each iteration it selects an unmarked vertex vv with the lowest value d[v]d[v], marks it and checks all the edges (v,to)(v, \text{to}) attempting to improve the value d[to]d[\text{to}].

The running time of the algorithm consists of:

  • nn searches for a vertex with the smallest value d[v]d[v] among O(n)O(n) unmarked vertices
  • mm relaxation attempts

For the simplest implementation of these operations on each iteration vertex search requires O(n)O(n) operations, and each relaxation can be performed in O(1)O(1). Hence, the resulting asymptotic behavior of the algorithm is:

O(n2+m)O(n^2+m)

This complexity is optimal for dense graph, i.e. when m≈n2m \approx n^2. However in sparse graphs, when mm is much smaller than the maximal number of edges n2n^2, the problem can be solved in O(nlog⁡n+m)O(n \log n + m) complexity. The algorithm and implementation can be found on the article Dijkstra on sparse graphs.

text
const int INF = 1000000000;
vector<vector<pair<int, int>>> adj;

void dijkstra(int s, vector<int> & d, vector<int> & p) {
    int n = adj.size();
    d.assign(n, INF);
    p.assign(n, -1);
    vector<bool> u(n, false);

    d[s] = 0;
    for (int i = 0; i < n; i++) {
        int v = -1;
        for (int j = 0; j < n; j++) {
            if (!u[j] && (v == -1 || d[j] < d[v]))
                v = j;
        }

        if (d[v] == INF)
            break;

        u[v] = true;
        for (auto edge : adj[v]) {
            int to = edge.first;
            int len = edge.second;

            if (d[v] + len < d[to]) {
                d[to] = d[v] + len;
                p[to] = v;
            }
        }
    }
}

Here the graph adj\text{adj} is stored as adjacency list: for each vertex vv adj[v]\text{adj}[v] contains the list of edges going from this vertex, i.e. the list of pair<int,int> where the first element in the pair is the vertex at the other end of the edge, and the second element is the edge weight.

The function takes the starting vertex ss and two vectors that will be used as return values.

First of all, the code initializes arrays: distances d[]d[], labels u[]u[] and predecessors p[]p[]. Then it performs nn iterations. At each iteration the vertex vv is selected which has the smallest distance d[v]d[v] among all the unmarked vertices. If the distance to selected vertex vv is equal to infinity, the algorithm stops. Otherwise the vertex is marked, and all the edges going out from this vertex are checked. If relaxation along the edge is possible (i.e. distance d[to]d[\text{to}] can be improved), the distance d[to]d[\text{to}] and predecessor p[to]p[\text{to}] are updated.

After performing all the iterations array d[]d[] stores the lengths of the shortest paths to all vertices, and array p[]p[] stores the predecessors of all vertices (except starting vertex ss). The path to any vertex tt can be restored in the following way:

text
vector<int> restore_path(int s, int t, vector<int> const& p) {
    vector<int> path;

    for (int v = t; v != s; v = p[v])
        path.push_back(v);
    path.push_back(s);

    reverse(path.begin(), path.end());
    return path;
}

References

  • Edsger Dijkstra. A note on two problems in connexion with graphs [1959]
  • Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Introduction to Algorithms [2005]

Practice Problems