Slide 12:
Johnson’s Algorithm
Explanation and step-by-step process
UCS-20240020 : @ThorNayMaan
What is the Johnson's algorithm:
Johnson's algorithm is a way to find the shortest paths between
all pairs of vertices in a weighted graph.
It works by transforming the original graph into a new graph
where all edge weights are non-negative, allowing the use of
combination of Bellman Ford’s algorithm & Dijkstra's algorithm.
2
What make special John’s algorithm:
Johnson's algorithm is particularly useful when dealing with
graphs that have negative edge weights but no negative-weight
cycles.
It overcomes the limitations of algorithms like Dijkstra's algorithm,
which cannot handle negative weights directly. By transforming
the graph into a non-negative weighted graph, Johnson's
algorithm enables the use of more efficient algorithms like
Dijkstra's.
3
Time Complexity
Johnson’s Algorithm O(V^2 Log V + VE) Time
Dijkstra’s Algorithm + Bellman Ford’s Algorithm
O(V log V) O(VE)
single source Single source
O(V^2 log V)
All Pair
4
Here's the step-by-step process
1) Add a new vertex: Introduce a new vertex, s, to the graph and connect it to all existing
vertices with zero-weight edges.
2) Run Bellman-Ford: Execute the Bellman-Ford algorithm on the modified graph, using
the new vertex `s` as the source. This step serves two purposes:
1) Detecting negative-weight cycles: If Bellman-Ford detects a negative-weight
cycle, it means the original graph has a negative-weight cycle, and Johnson's
algorithm cannot be applied.
2) Calculating potential values: If no negative-weight cycle is found, Bellman-Ford
calculates a potential value, h(v), for each vertex v in the graph.
3) Reweigh the edges: For each edge (u, v) in the original graph, reweigh it as follows:
new_weight(u, v) = original_weight(u, v) + h(u) - h(v)
5
Here's the step-by-step process
4)
Run Dijkstra's algorithm: For each vertex u in the original graph, run Dijkstra's algorithm
on the reweighted graph using `u` as the source. This will find the shortest paths from u to all
other vertices in the reweighted graph.
5)
Recover original distances: For each pair of vertices (u, v), the original shortest path
distance can be recovered using the following formula:
original_distance(u, v) = new_distance(u, v) - h(u) + h(v)
6
Going through the process (Demo)
4
0 1
1 1 7
4 -5
3 2
2
7
Going through the process (Demo)
4
4 4
0 1 0 1
-3
1 1 7 1 1 7
4 -5
3 2
2 4 -5
3 2
2
8
Going through the process (Demo)
10
10 10
0 1 0 1
12
6 6 7 6 6 7
4 0
3 2
2 4 0
3 2
2
+5
9
How does John’s algorithm solve the problem
0
4 4
0 1 0 1
0
ce
ur
S 1 1 7 So S 1 1 7
u rce 0
So
4 -5 3 2
2 4 -5 3 2
2
0
Relax လုပ်မယ့် formula -
d[u] + w(u,v) < d[v] 0
10
How does John’s algorithm solve the problem
0
0
4
1 d[u] + w(u,v) < d[v]
ce
0 ∞ ∞ u v
ur
So S 0 1 1 7
0
∞ ∞ ∞ starting node(u) ရဲ့
4 3 2 property(distance value) နဲ့
-5 2
weighted edge value ပေါင်းခြင်း
0 တန်းဖိုးဟာ destination node(v)
ရဲ့ distance value ထက်ငယ်ရင် d[u]
0 + w(u,v) နှစ်ခုပေါင်းခြင်းက ရ
တဲ့တန်ဖိုးကို Update လုပ်သွား
မယ်။
11
How does John’s algorithm solve the problem
0
5–0
5–1 Graph ထဲက edges တွေ
4
5–2 0 1 အကုန်လုံးကို list ထုတ်လိုက်ာ
5–3 ce
0 ∞ ∞ တာပါ။
ur
5–4 So S 0 1 1 7
0
0–1 ∞ ∞ ∞
0–4 4 3 2
-5 2
2–1
2–3 0
3–1
4-3 0
12
How does John’s algorithm solve the problem
0
5–0
5–1 0
4
1 d[u] + w(u,v) < d[v]
5–2 ce
0 0 ∞
ur
5–3 So S 0 1 1 7
5–4 0
∞ ∞ ∞ 0+0<∞
0–1 4 3 2
-5 2
0–4
2–1 0
2–3
3–1 0
4-3
13
How does John’s algorithm solve the problem
0
5–0
5–1
0
4
1 d[u] + w(u,v) < d[v]
5–2 ce
0 0 0
ur
5–3 So S 0 1 1 7
5–4 0
∞ ∞ ∞ 0+0<∞
0–1 4 3 2
-5 2
0–4
2–1 0
2–3
3–1 0
4-3
14
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 0
ur
5–4 So S 0 1 1 7
0
0 0 0 0+0<∞
0–1 4 3 2
-5 2
0–4
2–1 0
2–3
3–1 0
4-3
15
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 0
ur
5–4 So S 0 1 1 7
0–1 0
0 0 0 0+4<0
4 -5 3 2
2
0–4
2–1 0
2–3
3–1 0 Vertex 1(d[v]) တန်ဖိုးဖြစ်တဲ့ 0 ထက် မ
4-3 ငယ်တဲ့အတွက် update မလုပ်ပါ
16
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 0
ur
5–4 So S 0 1 1 7
0–1 0
0 0 0 0+1<0
0–4 4 3 2
-5 2
2–1 0
2–3
3–1 0 Vertex 4(d[v]) တန်ဖိုးဖြစ်တဲ့ 0 ထက် မ
4-3 ငယ်တဲ့အတွက် update မလုပ်ပါ
17
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 0
ur
5–4 So S 0 1 1 7
0–1 0
0 0 0 0+7<0
0–4 4 3 2
-5 2
2–1
0
2–3
3–1 0 Vertex 1(d[v]) တန်ဖိုးဖြစ်တဲ့ 0 ထက် မ
4-3 ငယ်တဲ့အတွက် update မလုပ်ပါ
18
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 0
ur
5–4 So S 0 1 1 7
0–1 0
0 -2 0 0 + (-2) < 0
0–4 4 3 2
-5 -2
2–1
2–3 0
Vertex 3(d[v]) တန်ဖိုးဖြစ်တဲ့ 0 ထက်
3–1 0 ငယ်တဲ့အတွက် ရလာတဲ့တန်ဖိုး -2 ကို
4-3 vertex 3 မှာ update လုပ်ပါမယ်
19
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 -1
ur
5–4 So S 0 1 1 7
0–1 0
0 -2 0 -2 + 1 < -1
0–4 4 3 2
-5 -2
2–1
2–3 0
Vertex 1(d[v]) တန်ဖိုးဖြစ်တဲ့ -1 ထက် မ
3–1 ငယ်တဲ့အတွက် update မလုပ်ပါ။
0
4-3
20
How does John’s algorithm solve the problem
0
5–0
5–1
5–2 0
4
1 d[u] + w(u,v) < d[v]
5–3 ce
0 0 -1
ur
5–4 So S 0 1 1 7
0–1 0
0 -5 0 0 + (-5) < -2
0–4 4 3 2
-5 -2
2–1
2–3 0
3–1 Vertex 3(d[v]) တန်ဖိုးဖြစ်တဲ့ -2 ထက်
4-3 0 ငယ်တဲ့အတွက် ရလာတဲ့တန်ဖိုး -5 ကို
vertex 3 မှာ update လုပ်ပါမယ်
21
How does John’s algorithm solve the problem
0
Relax: V – 1 = 6 – 1 = 5 times
5–0
5–1 0 1 2 3 4
4
5–2 0 1
0 0 -4 0 -1 0 -5 0
5–3 ce
ur
5–4 So S 0 1 1 7 0 -4 0 -5 0
0
0–1 0 -5 0
0–4 0 -4 0 -5 0
4 -5 3 -2
2
2–1
0 0 -4 0 -5 0
2–3
3–1 0 -4 0 -5 0
4-3 0
22
How does John’s algorithm solve the problem
4 d[v] <= d[u] + w(u,v)
0 1
0 -4
1 1 7
0 -5 0 0 <= d[u] + w(u,v) – d[v]
4 -5 3 -2
2
w(u,v)’ <= d[u] + w(u,v) - d[v]
23
How does John’s algorithm solve the problem
w(u,v)’ <= d[u] + w(u,v) – d[v]
0 + 4 - (-4) = 8 From node 0 to 1
4
8
0 1 0 1
0 -4 0 -4
1 1 7 1 1 7
0 -5 0 0 -5 0
4 -5 3 -2
2 4 -5 3 -2
2
24
How does John’s algorithm solve the problem
w(u,v)’ <= d[u] + w(u,v) – d[v] From node 2 to 1
0 + 7 - (-4) = 11
8
From node 2 to 3
0 1 0 + (-2) - (-5) = 3
0 -4
1 0 11 From node 3 to 1
0 -5 0 -5 + 1 - (-4) = 0
4 0 3 3
2
From node 4 to 3
0 + (-5) - (-5) = 0
From node 0 to 4
0+1-0=1
25
How does John’s algorithm solve the problem
8
0 1
0 -4
1 0 11
0 -5 0
4 0 3 3
2
26
Let's sum up what we have learned in this topic.
Johnson's algorithm is a relatively fast algorithm for solving the all-pairs shortest path problem, and
it is able to handle negative cycles as well. It can be easily implemented following these five steps:
1. Add a new node. Connect it to every other node using zero-weight edges.
2. Calculate the shortest paths from this node to each node using the Bellman-Ford algorithm.
3. Reweight the graph so that no negative edges are left.
4. Apply Dijkstra's algorithm ∣V∣ times to find the shortest paths between each pair of nodes.
5. Using the values found in last step, calculate the weights of all shortest paths in the original
graph.
In the case of sparse graphs, Johnson's algorithm works remarkably faster than many other all-
pairs algorithms, as its time complexity depends on the number of edges
27