## All-Pairs Shortest Paths

HONG-MING CHU 2013/10/31

### Refernece

Lecture slides from Prof. Hsin-Mu Tsai’s course slides and Prof. Ya- Yuin Su course slides.

### Today’s Goal

Quick recap of single source shortest path

Floyd-Warshall algorithm

Johson algorithm

### Things we have learned so far

Single-source shortest paths problem

Two algorithms

Bellman Ford algorithm

Dijakstra’s algorithm

Several properties about shorthest path

### Optimal Substructure

Theorem: A subpath of a shortest path is a shortest path

If we decompose a path from 𝑣_{0} to 𝑣_{𝑘} to the following, then 𝑤 𝑝 = 𝑤 𝑝_{0𝑖} +
𝑤 𝑝_{𝑖𝑗} + 𝑤(𝑝_{𝑗𝑘})

If a shorter path 𝑝_{𝑖𝑗}^{′} exists, then 𝑤 𝑝 = 𝑤 𝑝_{0𝑖} + 𝑤 𝑝_{𝑖𝑗}^{′} + 𝑤 𝑝_{𝑗𝑘} < 𝑤 𝑝 ,
contradiction.

V^{0} V^{i} V^{j} V^{k}

### Triangle inequality

For all vertices 𝑢, 𝑣, 𝑤 ∈ 𝑉 , 𝛿 𝑢, 𝑣 ≤ 𝛿 𝑢, 𝑤 + 𝛿 𝑤, 𝑣

Idea: among all paths from u to v, a shortest path 𝛿 𝑢, 𝑣 will be shorter (or equal to) the path going from u to v through an intermediate node w by taking shortest path 𝛿 𝑢, 𝑤 and 𝛿 𝑤, 𝑣 .

### Algortihms we have learned

**Graph type** **Algorithm** **Runnging Time**

Unweighted graph BFS O(V+E)

Non-negative edge weight graph

Dijkstra O(E+VlgV)

General graph Bellman-Ford O(VE)

DAG Bellman-Ford O(V+E)

### Algortihms we have learned

But what happen when the graph is dense?

ex. When |𝐸| ≈ |𝑉|^{𝟐}?

And what happen when the graph is relatively sparse?

ex. When |E| = 𝜃 𝑉 ?

### Algorithms we have learned

**Graph type** **Algorithm** **Runnging Time** |𝐸| ≈ |𝑉|^{𝟐} |𝐸| = 𝜃 |𝑉|

Unweighted graph

BFS O(V+E) O(V^{2}) O(V)

Non-negative edge weight

graph

Dijsktra O(E+VlgV) O(V^{2}) O(VlgV)

General graph Bellman-Ford O(VE) O(V^{3}) O(V^{2})

DAG Bellman-Ford O(V+E) O(V^{2}) O(V)

### All-pair shortest paths

How about using the previous algorithms to solve all-pair shortest path problem ?

Unweighted graph: run BFS |V| times → O(VE)

Non-negative graph: run Dijkstra |V| times → Ο(VE+V^{2}lgV)

General case: run Bellman-Ford |V| times → Ο(V^{2}E)

When handling general cases, the time complexity is at most O(V^{4})……….

We can do better!

### But how…….?

Recall that shortest paths has some useful properties.

One of them is that a shortest path has an optimal substructure.

As soon as we realize this…….

Dynamic programming may be a good choice to solve this problem!

### Floyd-Warshall algorithm

First we label all vertices from 1 to |V|.

Then define 𝐷^{(𝑘)} (k from 0 to |V|) to be an |V| * |V| matrix, and define each
of its entry 𝑑_{𝑖𝑗} to be the shortest path from i to j with intermediate vertices in
set {1, 2, …, k}, if such path doesn’t exist, 𝑑_{𝑖𝑗} = ∞ .

Then define 𝑐_{𝑖𝑗}^{(𝑘)} to be the 𝑑_{𝑖𝑗} in matrix 𝐷^{(𝑘)}.

So 𝑐_{𝑖𝑗}^{(0)} = 𝑤_{𝑖𝑗} and 𝛿 𝑖, 𝑗 = 𝑐_{𝑖𝑗}^{(|𝑉|)}.

### The transition function

###

Idea : The shortest path from i to j with intermediate vertices in set {1, 2, …, k}, which is denoted by 𝑐_{𝑖𝑗}

^{(𝑘)}, can either goes through k or not.

If not → 𝑐_{𝑖𝑗}^{(𝑘)} = 𝑐_{𝑖𝑗}^{(𝑘−1)}

Else → 𝑐_{𝑖𝑗}^{(𝑘)} = 𝑐_{𝑖𝑘}^{(𝑘−1)} + 𝑐_{𝑘𝑗}^{(𝑘−1)}

### The transition function(Cont.)

Since we are not yet sure if the intermediate vertices of 𝑐_{𝑖𝑗}^{(𝑘)} contains k, so
both circumstances are possible.

But fortunately, we know how to determine it!

THE SHORTER, THE BETTER!

So 𝑐_{𝑖𝑗}^{(𝑘)} = min(𝑐_{𝑖𝑘}^{(𝑘−1)} + 𝑐_{𝑘𝑗}^{(𝑘−1)}, 𝑐_{𝑖𝑗}^{(𝑘−1)})

### Pseudo code

for k = 1 to n

for i = 1 to n

for j = 1 to n

if 𝑐_{𝑖𝑗}>𝑐_{𝑖𝑘}+𝑐_{𝑘𝑗}

𝑐_{𝑖𝑗} = 𝑐_{𝑖𝑘}+𝑐_{𝑘𝑗}
Running time : 𝜃 𝑉^{3}

### An Alternative: Transitive closure of a directed graph

Determine if a graph G contains a path from vertex i to j for all vertices pairs

𝑡_{𝑖𝑗} = 1, 𝑖𝑓 𝑡ℎ𝑒𝑟𝑒 𝑒𝑥𝑖𝑠𝑡𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑖 𝑡𝑜 𝑗
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Idea: The key concepts of Floyd-warshall algorithm can be used on this question, but some modifications are needed.

### An Alternative: Transitive closure of a directed graph(Cont.)

The modification is shown as follow:

Replace min with ∨ (logical OR)

Replace + with ∧ (logical AND)

𝑡_{𝑖𝑗}^{(𝑘)} = 𝑡_{𝑖𝑗}^{(𝑘−1)} ∧ ^{(𝑡}_{𝑖𝑘}^{(𝑘−1)} ^{∨ 𝑡}_{𝑘𝑗}^{(𝑘−1)}^{)}

The running time is also 𝜃 𝑉^{3} .

### Another idea

Floyd-Warshall yields a great improvement on time complexity.

But when |𝐸| is relatively smaller, i.e. when 𝐸 = 𝜃 𝑉 , the improvement is not that significant……..

Using Dijakstra’s algorithm 𝑉 times is now a good idea, but negative- weighted edge is a critical issue.

Can we fix this?

### Johnson’s algorithm

Idea:

Try to make all edges posstive.

Then run Dijkstra’s algorithm |V| times.

Solution: Graph Reweighting

Given function ℎ : 𝑉 → ℛ, reweight each edge (𝑢,𝑣) ∈ 𝐸 by 𝑤_{ℎ} 𝑢, 𝑣 =

𝑤 𝑢, 𝑣 + ℎ 𝑢 − ℎ(𝑣), 𝑣 ∈ 𝑉. Then, for any vertices 𝑢, 𝑣 ∈ 𝑉, all paths have reweighted by the same amount.

### Theorem

Given function ℎ : 𝑉 → ℛ, reweight each edge (𝑢,𝑣)∈𝐸 by 𝑤_{ℎ} 𝑢, 𝑣 =

𝑤 𝑢, 𝑣 + ℎ 𝑢 − ℎ(𝑣), 𝑣 ∈ 𝑉. Then, for any vertices 𝑢, 𝑣 ∈ 𝑉, all paths are equally reweighted.

Proof:

Let 𝑝 = 𝑣_{1} → 𝑣_{2} → 𝑣_{3} … 𝑣_{𝑘} be a path in G

### Theorem(Cont.)

Proof:

Let 𝑝 = 𝑣_{1} → 𝑣_{2} → 𝑣_{3} … 𝑣_{𝑘} be a path in G

𝑖=1 𝑘

𝑤_{ℎ}(𝑣_{𝑖−1}, 𝑣_{𝑖}) =

𝑖=1 𝑘

(𝑤_{ℎ} 𝑣_{𝑖−1}, 𝑣_{𝑖} + ℎ 𝑣_{𝑖−1} − ℎ(𝑣_{𝑖})) =

𝑖=1 𝑘

𝑤_{ℎ}(𝑣_{𝑖−1}, 𝑣_{𝑖}) +

𝑖=1 𝑘

ℎ 𝑣_{𝑖−1} − ℎ 𝑣_{𝑖} = 𝑤 𝑝 + ℎ 𝑣_{1} − ℎ 𝑣_{𝑘}

The same for every path

### Collorary

𝛿_{ℎ} 𝑢, 𝑣 = 𝛿 𝑢, 𝑣 + ℎ 𝑢 − ℎ(𝑣)

Now we try to find ℎ : 𝑉 → ℛ such that 𝑤_{ℎ} 𝑢, 𝑣 ≥ 0 for all edges (𝑢, 𝑣) ∈ 𝐸

𝑤_{ℎ} 𝑢, 𝑣 = 𝑤 𝑢, 𝑣 + ℎ 𝑢 − ℎ 𝑣 ≥ 0

ℎ 𝑣 − ℎ 𝑢 ≤ 𝑤 𝑢, 𝑣

The equations given by ℎ 𝑣 − ℎ 𝑢 , for all 𝑢, 𝑣 ∈ 𝑉 can form a difference constraints system!

### Difference constraints

The difference constraints system problem is to find a solution to a difference constraint system, where each equation has the following form:

𝑥_{𝑖} − 𝑥_{𝑗} ≤ 𝑐_{𝑘}, where 𝑐_{𝑘} is a constant that can be negative.

An example of a difference constraints system is as followed:

𝑥_{1} − 𝑥_{2} ≤ 3
𝑥_{2} − 𝑥_{3} ≤ −2
𝑥_{1} − 𝑥_{3} ≤ 2

### Difference constraints(Cont.)

Observe the fact that shortest paths have triangular inequality.

Then for each equation 𝑥_{𝑖} − 𝑥_{𝑗} ≤ 𝑐_{𝑘}, we can construct an edge 𝑣_{𝑗} → 𝑣_{𝑖}, and
𝑤(𝑣_{𝑗} → 𝑣_{𝑖}) = 𝑐_{𝑘}

Finally, add a new node s, and add for all 𝑣 ∈ 𝑉, add edges 𝑠 → 𝑣_{𝑖}, and
𝑤 𝑠 → 𝑣_{𝑖} = 0

For the example of last page, we can construct the graph as followed using the rule above.

### Difference constraints(Cont.)

𝑥_{1} − 𝑥_{2} ≤ 3

𝑥_{2}−𝑥_{3} ≤ −2

𝑥_{1} − 𝑥_{3} ≤ 2

V^{1} V^{2}

V^{3}
2

3

-2 S

0

0 0

### Difference constraints(End)

After the graph is constructed, we can realize that for all variables 𝑥_{𝑖} in the
difference equations system, 𝑥_{𝑖} = 𝛿 𝑠, 𝑣_{𝑖} is a set of 𝑥 that can sastisfy the
constraint due to triangular inequality if no negative cycle exists.

So using Bellman-Ford algorithm, we can either tell that the difference constraints system is unsolvable, or find a set of solution in O(VE).

### Return to Johnson Algorthim

So for all 𝑢, 𝑣 ∈ 𝑉, ℎ 𝑣 − ℎ 𝑢 ≤ 𝑤 𝑢, 𝑣 , and these equations form a

difference constraints system, so Bellman-Ford algorithm can be used to detect negative cycles and determine the function ℎ. → 𝑶(𝑽𝑬)

Then reweight all edges by 𝑤_{ℎ} 𝑢, 𝑣 = 𝑤 𝑢, 𝑣 + ℎ 𝑢 − ℎ 𝑣 . → 𝑶(𝑽𝑬)

Then for all 𝑢 ∈ 𝑉, run Dijkstra’s algorithm on the rewiehted graph to find
𝛿_{ℎ} 𝑢, 𝑣 for all 𝑣 ∈ 𝑉. → 𝑶(𝑽^{𝟐}𝒍𝒈𝑽)

𝛿 𝑢, 𝑣 = 𝛿_{ℎ} 𝑢, 𝑣 + ℎ 𝑣 − ℎ(𝑢) for all 𝑢, 𝑣 ∈ 𝑉. → 𝑶(𝑽^{𝟐})

The total running time is 𝑶(𝑽𝑬 + 𝑽^{𝟐}**𝒍𝒈𝑽).**