Post · 3 min read· pp. 623–624

D´Esopo-Pape algorithm

Given a graph with nn vertices and mm edges with weights wiw_i and a starting vertex v0v_0. The task is to find the shortest path from the vertex v0v_0 to every other vertex.

The algorithm from D´Esopo-Pape will work faster than Dijkstra's algorithm and the Bellman-Ford algorithm in most cases, and will also work for negative edges. However not for negative cycles.

Description

Let the array dd contain the shortest path lengths, i.e. did_i is the current length of the shortest path from the vertex v0v_0 to the vertex ii. Initially this array is filled with infinity for every vertex, except dv0=0d_{v_0} = 0. After the algorithm finishes, this array will contain the shortest distances.

Let the array pp contain the current ancestors, i.e. pip_i is the direct ancestor of the vertex ii on the current shortest path from v0v_0 to ii. Just like the array dd, the array pp changes gradually during the algorithm and at the end takes its final values.

Now to the algorithm. At each step three sets of vertices are maintained:

  • M0M_0 - vertices, for which the distance has already been calculated (although it might not be the final distance)
  • M1M_1 - vertices, for which the distance currently is calculated
  • M2M_2 - vertices, for which the distance has not yet been calculated

The vertices in the set M1M_1 are stored in a bidirectional queue (deque).

At each step of the algorithm we take a vertex from the set M1M_1 (from the front of the queue). Let uu be the selected vertex. We put this vertex uu into the set M0M_0. Then we iterate over all edges coming out of this vertex. Let vv be the second end of the current edge, and ww its weight.

  • If vv belongs to M2M_2, then vv is inserted into the set M1M_1 by inserting it at the back of the queue. dvd_v is set to du+wd_u + w.
  • If vv belongs to M1M_1, then we try to improve the value of dvd_v: dv=min⁡(dv,du+w)d_v = \min(d_v, d_u + w). Since vv is already in M1M_1, we don't need to insert it into M1M_1 and the queue.
  • If vv belongs to M0M_0, and if dvd_v can be improved dv>du+wd_v > d_u + w, then we improve dvd_v and insert the vertex vv back to the set M1M_1, placing it at the beginning of the queue.

And of course, with each update in the array dd we also have to update the corresponding element in the array pp.

Implementation

We will use an array mm to store in which set each vertex is currently.

text
struct Edge {
    int to, w;
};

int n;
vector<vector<Edge>> adj;

const int INF = 1e9;

void shortest_paths(int v0, vector<int>& d, vector<int>& p) {
    d.assign(n, INF);
    d[v0] = 0;
    vector<int> m(n, 2);
    deque<int> q;
    q.push_back(v0);
    p.assign(n, -1);

    while (!q.empty()) {
        int u = q.front();
        q.pop_front();
        m[u] = 0;
        for (Edge e : adj[u]) {
            if (d[e.to] > d[u] + e.w) {
                d[e.to] = d[u] + e.w;
                p[e.to] = u;
                if (m[e.to] == 2) {
                    m[e.to] = 1;
                    q.push_back(e.to);
                } else if (m[e.to] == 0) {
                    m[e.to] = 1;
                    q.push_front(e.to);
                }
            }
        }
    }
}

Complexity

The algorithm usually performs quite fast - in most cases, even faster than Dijkstra's algorithm. However there exist cases for which the algorithm takes exponential time, making it unsuitable in the worst-case. See discussions on Stack Overflow and Codeforces for reference.