Implementation of Johnson’s algorithm for all-pairs shortest paths
Johnson’s algorithm finds the shortest paths between all pairs of vertices in a weighted directed graph. It allows some of the edge weights to be negative numbers, but no negative-weight cycles may exist. It uses the Bellman-Ford algorithm to re-weight the original graph, removing all negative weights. Dijkstra’s algorithm is applied on the re-weighted graph to compute the shortest path between all pairs of vertices.
Using Dijkstra’s algorithm, the shortest paths between all pairs of vertices in O(V2logV) can be found. However, Dijkstra does not work with negative weights. To avoid this problem, Johnson’s algorithm uses a technique called reweighting.
Reweighting is a process by which each edge weight is changed to satisfy two properties-
- For all pairs of vertices u, v in the graph, if the shortest path exists between those vertices before reweighting, it must also be the shortest path between those vertices after reweighting.
- For all edges, (u, v), in the graph, they must have a non-negative weight (u, v).
Johnson’s algorithm uses Bellman-Ford to reweight the edges. Bellman-Ford is also able to detect negative weight cycles if present in the original graph.
Adjacency List is modified a bit to represent the graph. For each source vertex s, each of its neighboring vertices has two properties associated with them:
Consider the graph –
Source vertex 0 has one neighboring vertex, one whose destination is 2 and weight is -2. Each neighboring vertex is encapsulated using a static Neighbor class.
Follow the steps below to solve the problem:
- Add a new node q to the graph, connected by zero-weight edges to all the other nodes.
- Use the Bellman-Ford algorithm, starting from the new vertex q, to find for each vertex v the minimum weight h(v) of a path from q to v. If this step detects a negative cycle, the algorithm is terminated.
- Reweight the edges of the original graph using the values computed by the Bellman-Ford algorithm: an edge from u to v, having length w(u, v) reweighted to w(u, v) + h(u) − h(v).
- Remove q and apply Dijkstra’s algorithm to find the shortest paths from each node s to every other vertex in the reweighted graph.
- Compute the distance in the original graph by adding h(v) − h(u) to the distance returned by Dijkstra’s algorithm.
Below is the implementation of the above approach:
Distance matrix: 0 1 2 3 0 0 -1 -2 0 1 4 0 2 4 2 5 1 0 2 3 3 -1 1 0
Time Complexity: O(V2log V + VE), The time complexity of Johnson’s algorithm becomes the same as Floyd Warshall when the graphs are complete (For a complete graph E = O(V2). But for sparse graphs, the algorithm performs much better than Floyd Warshall.
Auxiliary Space: O(V*V)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.