# All-Pairs Shortest Paths

(1)

## All-Pairs Shortest Paths

HONG-MING CHU 2013/10/31

(2)

### Refernece

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

(3)

### Today’s Goal

 Quick recap of single source shortest path

 Floyd-Warshall algorithm

 Johson algorithm

(4)

### Things we have learned so far

 Single-source shortest paths problem

 Two algorithms

 Bellman Ford algorithm

 Dijakstra’s algorithm

 Several properties about shorthest path

(5)

### 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.

V0 Vi Vj Vk

(6)

### 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 𝛿 𝑤, 𝑣 .

(7)

### 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)

(8)

### 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| = 𝜃 𝑉 ?

(9)

### Algorithms we have learned

Graph type Algorithm Runnging Time |𝐸| ≈ |𝑉|𝟐 |𝐸| = 𝜃 |𝑉|

Unweighted graph

BFS O(V+E) O(V2) O(V)

Non-negative edge weight

graph

Dijsktra O(E+VlgV) O(V2) O(VlgV)

General graph Bellman-Ford O(VE) O(V3) O(V2)

DAG Bellman-Ford O(V+E) O(V2) O(V)

(10)

### 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+V2lgV)

General case: run Bellman-Ford |V| times → Ο(V2E)

 When handling general cases, the time complexity is at most O(V4)……….

 We can do better!

(11)

### 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!

(12)

### 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 𝛿 𝑖, 𝑗 = 𝑐𝑖𝑗(|𝑉|).

(13)

### 

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)

(14)

### 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))

(15)

### Pseudo code

for k = 1 to n

for i = 1 to n

for j = 1 to n

if 𝑐𝑖𝑗>𝑐𝑖𝑘+𝑐𝑘𝑗

𝑐𝑖𝑗 = 𝑐𝑖𝑘+𝑐𝑘𝑗 Running time : 𝜃 𝑉3

(16)

### 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.

(17)

### 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 .

(18)

### 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?

(19)

### 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.

(20)

### 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

(21)

### 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

(22)

### 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!

(23)

### 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

(24)

### 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.

(25)

 𝑥1 − 𝑥2 ≤ 3

 𝑥2−𝑥3 ≤ −2

 𝑥1 − 𝑥3 ≤ 2

V1 V2

V3 2

3

-2 S

0

0 0

(26)

### 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).

(27)

 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 𝑶(𝑽𝑬 + 𝑽𝟐𝒍𝒈𝑽).

Updating...

## References

Related subjects :