Prerequisites
To understand the workings of Johnson’s algorithm, it’s necessary to be familiar with two other shortest path algorithms:
Idea of the Algorithm
- Adding a Vertex: We add a vertex to the graph along with edges connecting to each vertex of .
- We use Bellman-Ford’s algorithm from towards all other vertices of . gives us the distance between and . If Bellman-Ford detects a negative cycle, the algorithm stops.
- Removal of Vertex : This vertex is no longer useful. Remove this vertex and all the edges added in step 1.
- Re-weighting the Edges of the Graph: For each edge in the graph, do: .
- Calculating the Shortest Path: For each vertex in , apply Dijkstra’s algorithm to determine the shortest path between all vertices.
It is important to note that the distances are not preserved. If for an edge, we had a weight of , the new weight will be . However, this is not the point. Johnson’s algorithm gives us the shortest path from point to point “only”. If you need to retrieve the sum of the weights of this path, just return to the original graph and follow the path given by Johnson.
Algorithm / Solution / Complexity
This article only presents an example of the application of Johnson’s algorithm. However, you can easily find this information on the internet:
Example:
Let be a graph with negative weight edges.

Step 1 - Adding Vertex w

Step 2 - Shortest Paths from w to Other Vertices Using Bellman-Ford

In purple are the results of Bellman-Ford’s algorithm.
Steps 3 and 4 - Re-weighting the Edges

Step 5 - Dijkstra’s Algorithm on All Edges
The final graph is as follows:

Step 5 is simply an application of Dijkstra’s algorithm. An example of its use is already detailed on this site: Dijkstra’s Algorithm - Example.
To take multiple results from Dijkstra’s algorithm:
- To go from a to d, the path to take is with a weight of -9 (not 0, the weights from and not must be used).
- To go from f to d, the path to take is with a weight of -2.
- There is no possible path from d to e.
The rest is left as an exercise for the reader.