Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Johnson.H File Reference

Johnson's algorithm for all-pairs shortest paths. More...

#include <ah-graph-concepts.H>
#include <Dijkstra.H>
#include <Bellman_Ford.H>
#include <tpl_graph.H>
#include <tpl_dynList.H>
#include <tpl_dynMapTree.H>
#include <ah-errors.H>
#include <algorithm>
#include <concepts>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for Johnson.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::johnson_detail::Wide
 An integer of 128 bits, in two words of 64. More...
 
class  Aleph::Johnson< GT, Distance, Ait, NAit, SA >
 Johnson's algorithm for all-pairs shortest paths. More...
 
struct  Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Shown_Arc
 
struct  Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Kept_Source
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::johnson_detail
 

Detailed Description

Johnson's algorithm for all-pairs shortest paths.

Implements Johnson's algorithm combining Bellman-Ford and Dijkstra for finding all-pairs shortest paths in sparse graphs, including graphs with negative edge weights. O(VE log V) complexity.

Algorithm Overview

Johnson's algorithm works in the following steps:

  1. Run Bellman-Ford from a virtual source, which reaches every node by an arc of weight 0 and is not added to the graph (Bellman_Ford::compute_nodes_weights()), to compute the potential h(v) of each node: min(0, the least distance from a node to v)
  2. Reweight all arcs: w'(u,v) = w(u,v) + h(u) - h(v), which is >= 0
  3. For each source, run Dijkstra on the reweighted arcs
  4. Adjust distances back: d(s,t) = d'(s,t) - h(s) + h(t)

With an integer distance type, w' and the sums of Dijkstra are kept in the unsigned type of the same width: as lowest() <= h <= 0, w' is at most max() + max() + 1, and d'(s,t) fits whenever d(s,t) fits. If a potential is below lowest(), the potentials, w' and the sums are kept in an integer of 128 bits instead, where they fit too. So every distance that fits in the type is given, and only one that does not makes a query throw.

Complexity

Phase Time
Bellman-Ford O(VE)
V × Dijkstra O(V E log V)
Total O(V E log V)

For sparse graphs (E = O(V)), this is O(V² log V), better than Floyd-Warshall's O(V³).

Author
Leandro Rabindranath León

Definition in file Johnson.H.