You are on page 1of 12

Johnsons Algorithm

CS 566 Chang-Ju Lin amosju@bu.edu

Solve all-pairs shortest-paths problem

A dynamic-programming algorithm bases on matrix multiplication -(V log V)


3 3

 

Floyd-Warshell algorithm -- (V ) Johnsons algorithm -- Johnson has defined an algorithm that takes advantage of the sparse nature of most graphs, with directed edges and no cycles. With these assumptions, he gets better results than the naive algorithm. -- Bases on Bellman-Ford algorithm, reweighting method, and Dijkstra algorithm. -(V log V + VE)
2

Johnsons Algorithm
 

What is the good time to use it? Johnsons algorithm uses the a method reweighting.

Johnsons Algorithm


Why (V log V + VE) ? -- Do V times of Dijkstra.


{s} V[G] }, and

Following is a pseudo code of Johnsons algorithm.


Compute G, where V[G] = V[G] E[G] = E[G]

{(s, v): v

w(s, v) = 0 for all v

V[G]

If BELLMAN-FORD(G, w, s) = FALSE then print the input graph contains a negative weight cycle

else for each vertex v V[G] do set h(v) to the value of (s, v) computed by the Bellman-Ford alg. for each edge (u, v) do (u, v) for each vertex u E[G]

w(u, v) + h(u) h(v) V[G] V[G]

do run DIJKSTRA(G, , u) to compute (s, v) for all v for each vertex v do d(u, v) return D V[G] (s, v) + h(v) h(u)

Johnsons Algorithm


The reweighting function By reweighting, we have a new set of edge weights which must satisfy two important properties. (u, v) = w(u, v) + h(u) h(v) 1. For all pairs of vertice u, v V, a path p is a shortestpath from u to v using weight function w if and only if p is also a shortest path from u to v using weight function . 2. For all edges (u, v), the new weight (u, v) is nonnegative. So it doesnt change shortest paths, but reweight to a nonnegative value.

An example of Johnsons Algorithm




A sparse graph with negative weights. So we cant use Dijkstra.

4 8

-4 7

1 2 -5

Johnsons Algorithm
 

Step 1 Adds a new node with zero weight edge from it to all other nodes. Step 2 -- For each node, we run the Bellman-Ford algorithm once.
0

-1
0 3 4 8

0
0

0
-4 7 2

-5
-5

-4

Johnsons Algorithm


Step 3 -- Reweight every edge to have all positive weight edges to use Dijkstra.
5

-1
1 4 0 13

0
4

0
0 10 2

-5
0

-4

Johnsons Algorithm


Step 4 Runs Dijkstras algorithm on each node using reweighting function to have all pairs shortest paths in this graph. * Run Dijkstra reweighted value of edge / Run Dijkstra of original value edge

2/1
4 0 13

0/0
0 10 2

2/-3
0

0/-4

2/2

Johnsons Algorithm

0/0
4 0 13 4

0/4
0 13

2/3
0 10 2

0/-4
0

2/7
0 10 2

0/0
0

2/-1

0/1

2/3

0/5

Johnsons Algorithm

0/-1
4 0 13 4

2/5
0 13

2/2
0 10 2

0/-5
0

4/8
0 10 2

2/1
0

2/-2

0/0

0/0

2/6

Johnsons Algorithm
 

An efficient algorithm for shortest paths in sparse networks. For scheduling of manufacturing systems RTOS -- http://riot.ieor.berkeley.edu/riot/Applications/Scheduling/algorithms.html

You might also like