35
All-Pairs Shortest Paths

All-Pairs Shortest Paths

  • Upload
    livi

  • View
    61

  • Download
    0

Embed Size (px)

DESCRIPTION

All-Pairs Shortest Paths. 15.1 最短路徑的特性. 最短路徑的結構: 所有最短路徑的子路徑均為最短路徑。 如 : (v i ,…,v k ,v j ) 為 v i 到 v j 的最短路徑,則 (v i ,…,v k ) 必為 v i 到 v k 的最短路徑。 即: δ(v i ,v j )=δ(v i ,v k )+w(v k ,v j ). 最短路徑的特性. 一個直覺的遞迴解: 定義 d (m) (i,j) 為包含至多 m 個邊自 i j 的最短路徑長度。則:. 利用遞迴解計算出最短路徑. - PowerPoint PPT Presentation

Citation preview

Page 1: All-Pairs Shortest Paths

All-Pairs Shortest Paths

Page 2: All-Pairs Shortest Paths

All-Pairs Shortest Paths 2

15.1 最短路徑的特性• 最短路徑的結構:所有最短路徑的子路徑均為最短路徑。• 如: (vi,…,vk,vj) 為 vi 到 vj 的最短路徑,則 (vi,

…,vk) 必為 vi 到 vk 的最短路徑。• 即: δ(vi,vj)=δ(vi,vk)+w(vk,vj)

Page 3: All-Pairs Shortest Paths

All-Pairs Shortest Paths 3

最短路徑的特性• 一個直覺的遞迴解:定義 d(m)(i,j) 為包含至多 m 個邊自 ij 的最短路徑長度。則:

)},(),(),,(min{),(

,,0

),(

)1()1()(

)0(

jkwkidjidjid

jiji

jid

mmm

Page 4: All-Pairs Shortest Paths

All-Pairs Shortest Paths 4

利用遞迴解計算出最短路徑• 令 n=|V| ,如一圖無負迴圈,則 d(n-1)(i,j) 即為 ij的最短路徑長度。• 可以直覺使用動態規劃的方式來求出解,但耗時

O(|V|3log|V|) 反不如直接利用 Dijkstra 演算法直接求出所有點作為起點到其他點的最短路徑 ( 以Linear array 實做耗時 O(|V|3)) 。

Page 5: All-Pairs Shortest Paths

All-Pairs Shortest Paths 5

All-pairs Shortest Paths 演算法• 輸入: 一無負迴圈圖 G=(V,E) , |V|=n 。

n×n adjacency matrix W=(W[i,j])

EvuEvujiw

jijiW

),(,),(,),(

,0],[

Page 6: All-Pairs Shortest Paths

All-Pairs Shortest Paths 6

All-pairs Shortest Paths 演算法• 輸出: n×n minimum distance matrix D=(D[i,j])

D[i,j]=δ(i,j)n×n predecessor matrix π=(π[i,j])若 ij 無路徑則 π[i,j]=NIL ,否則 π[i,j] 紀錄 ij 最短路徑上 j 之前的點

i k jπ[i,j]

Page 7: All-Pairs Shortest Paths

All-Pairs Shortest Paths 7

Extend-Shortest-Paths(D,W){ nrows[D] Let D’ = (D’[i,j]) be an nn matrixfor i=1 to n do

for j=1 to n do D’[i,j] for k=1 to n do

D’[i,j]min(D’[i,j],D[i,k]+W[k,j])return D’

}

Time Complexity: O(n3)

Page 8: All-Pairs Shortest Paths

All-Pairs Shortest Paths 8

Slow-All-Pairs-Shortest-Paths(G,W){ n|V|D(1)Wfor m=2 to n-1 do

D(m)Extend-Shortest-Paths(D(m-1),W)return D(n-1)

}

Time Complexity: O(n4)

Page 9: All-Pairs Shortest Paths

All-Pairs Shortest Paths 9

Faster-All-Pairs-Shortest-Paths(G,W){ n|V|D(1)=Wm=1while n-1>m do D(2m)Extend-Shortest-Paths(D(m),D(m)) m = 2mreturn D(m)

}Time Complexity: O(n3logn)

Page 10: All-Pairs Shortest Paths

All-Pairs Shortest Paths 10

15.2 Floyd-Warshall 演算法• 主要利用不同的觀察找出新的遞迴式,使得演算法複雜度降低至 O(n3) ,在邊數多的時候能有比

Dijkstra 演算法更迅速的求出所有的最短路徑。• 若一 ij 的路徑為 (i,u1,…,um,j) ,則我們稱 u1,…,um為該路徑的 Intermediate vertex( 中間點 ) 。

Page 11: All-Pairs Shortest Paths

All-Pairs Shortest Paths 11

Floyd-Warshall 演算法• 假定點集合 V={1,…,n} ,定義 d(m)(i,j) 為中間點僅可能為 {1,…,m} 最短的 ij 路徑長。故:

)},(),(),,(min{),(

,),(,0

),(

)1()1()1()(

)0(

jmdmidjidjid

jijiwji

jid

mmmm

Page 12: All-Pairs Shortest Paths

All-Pairs Shortest Paths 12

Floyd-Warshall 演算法正確性分析• 考慮中間點僅可能為 {1,…,k} 。

– 如 k 不在 ij 最短路徑上,則中間點僅可能為{1,…,k-1} ,故此時: d(k)(i,j)=d(k-1)(i,j) 。

Page 13: All-Pairs Shortest Paths

All-Pairs Shortest Paths 13

Floyd-Warshall 演算法正確性分析– 如 k 在 ij 最短路徑上,則將 ik 及 kj 兩段分開看,這兩個路徑必然不可能有中間點為 k ,否則依照無負迴圈的假定, ik 或 kj 不是最短路徑,違背最短路徑的性質。故此時: d(k)(i,j)=d(k-1)(i,k)+d(k-1)(k,j) 。

i k j中間點僅有{1,…,k-1}

中間點僅有{1,…,k-1}

Page 14: All-Pairs Shortest Paths

All-Pairs Shortest Paths 14

Floyd-Warshall 演算法Floyd-Warshall(G,W){ n|V|D(0)Wfor k = 1 to n do for i = 1 to n do for j = 1 to n do

if D(k-1)[i,j]>D(k-1)[i,k]+D(k-1)[k,j] then D(k)[i,j]D(k-1)[i,k]+D(k-1)[k,j] π[i,j] π[k,j] else D(k)[i,j]D(k-1)[i,j]

return D(n)

} Time Complexity: O(n3)

Page 15: All-Pairs Shortest Paths

All-Pairs Shortest Paths 15

建造 Shortest path

• 初始化 π[i,j] 時,如 i=j 或 (i,j)∉E 則初始為 NIL ,否則初始為 i 。• 等執行完演算法後,則可利用 Single-Source

shortest path 的方式,藉由 Predecessor graph 來建立出 ij 的最短路徑。

Page 16: All-Pairs Shortest Paths

All-Pairs Shortest Paths 16

Floyd-Warshall 範例1

2 5

3 4

38

-4

1

7

4

-5

26

Page 17: All-Pairs Shortest Paths

All-Pairs Shortest Paths 17

Floyd-Warshall 範例

NILNILNILNILNILNILNILNILNILNILNIL

NILNILNILNILNIL

D

544

322111

06052

047104830

)0()0(

Page 18: All-Pairs Shortest Paths

All-Pairs Shortest Paths 18

Floyd-Warshall 範例

NILNILNILNILNIL

NILNILNILNILNILNILNIL

NILNIL

D

51414

322111

0620552

047104830

)1()1(

Page 19: All-Pairs Shortest Paths

All-Pairs Shortest Paths 19

Floyd-Warshall 範例

NILNILNILNILNIL

NILNILNILNILNIL

NIL

D

51414223221211

0620552

1150471044830

)2()2(

Page 20: All-Pairs Shortest Paths

All-Pairs Shortest Paths 20

Floyd-Warshall 範例

NILNILNILNILNIL

NILNILNILNILNIL

NIL

D

51434223221211

0620512

1150471044830

)3()3(

Page 21: All-Pairs Shortest Paths

All-Pairs Shortest Paths 21

Floyd-Warshall 範例

NILNIL

NILNIL

NIL

D

54341434123412441241

0615820512

350471140344130

)4()4(

Page 22: All-Pairs Shortest Paths

All-Pairs Shortest Paths 22

Floyd-Warshall 範例

NILNIL

NILNIL

NIL

D

54341434123412441543

0615820512

350471140342310

)5()5(

Page 23: All-Pairs Shortest Paths

All-Pairs Shortest Paths 23

15.3 Johnson’s algorithm

• Johnson’s 演算法可用於計算 All pairs shortest path問題。• 在邊的數量不多的時候,如 |E|=O(|V|log|V|) 時,能有比 Warshall-Floyd 演算法較佳的效能。• 其輸入需求是利用 Adjacency list 表示的圖。

Page 24: All-Pairs Shortest Paths

All-Pairs Shortest Paths 24

Johnson’s algorithm

• Johnson’s 演算法利用 reweighing 來除去負邊,使得該圖可以套用 Dijkstra 演算法,來達到較高的效能。• Reweighing 是將每個點 v 設定一個高度 h(v) ,並且調整邊的 weight function w(u,v) 成為

w’(u,v)=w(u,v)+h(u)-h(v) 。• 令 δ‘(u,v) 如此調整之後的最短距離,則原先的最短距離 δ(u,v)=δ‘(u,v)-h(u)+h(v) 。

Page 25: All-Pairs Shortest Paths

All-Pairs Shortest Paths 25

Johnson’s algorithm

Johnson(G){ compute G’, where V[G’]=V[G]{s} and

E[G’]=E[G]{(s,v):vV[G]if Bellman-Ford(G’,w,s)=False then print “ a neg-weight cycle”else for each vertex v V[G’]

set h(v)=(s,v) computed by Bellman-Ford algo.

for each edge (u,v)E[G’] w’(u,v)=w(u,v)+h(u)-h(v)

for each vertex u V[G] run Dijkstra(G,w’,u) to compute ’(u,v)

for each v V[G] duv=’(u,v)-h(u)+h(v)

return D}

Page 26: All-Pairs Shortest Paths

All-Pairs Shortest Paths 26

Johnson’s algorithm 範例

37

4

2

8

-4

6

1 -5

Page 27: All-Pairs Shortest Paths

All-Pairs Shortest Paths 27

Johnson’s algorithm 範例

37

4

2

8

-4

6

1 -5

s

0

0

0

0

0

加入一個點s ,以及自 s 拉一條 weight 為0 的邊到每一點。

Page 28: All-Pairs Shortest Paths

All-Pairs Shortest Paths 28

Johnson’s algorithm 範例-1

0 -5

-4 0

37

4

2

8

-4

6

1 -5

s

0

0

0

0

0

執行 Bellman-Ford 演算法,得到自 s 出發每一點的最短距離。

Page 29: All-Pairs Shortest Paths

All-Pairs Shortest Paths 29

Johnson’s algorithm 範例-1

0 -5

-4 0

410

0

2

13

0

2

0 0

做完reweighting

Page 30: All-Pairs Shortest Paths

All-Pairs Shortest Paths 30

Johnson’s algorithm 範例2/1

0/0 2/-3

0/-4 2/0

410

0

2

13

0

2

0 0

紅線部分是 Shortest-paths tree 。點的數字 a/b 代表自出發點 ( 綠色點 ) 出發,到達該點的最短路徑(Reweighting 後的圖 / 原圖 ) 。

Page 31: All-Pairs Shortest Paths

All-Pairs Shortest Paths 31

Johnson’s algorithm 範例0/0

2/3 0/-4

2/-1 0/1

410

0

2

13

0

2

0 0

Page 32: All-Pairs Shortest Paths

All-Pairs Shortest Paths 32

Johnson’s algorithm 範例0/4

2/7 0/0

2/3 0/5

410

0

2

13

0

2

0 0

Page 33: All-Pairs Shortest Paths

All-Pairs Shortest Paths 33

Johnson’s algorithm 範例0/-1

2/2 0/-5

2/-2 0/0

410

0

2

13

0

2

0 0

Page 34: All-Pairs Shortest Paths

All-Pairs Shortest Paths 34

Johnson’s algorithm 範例2/5

4/8 2/1

0/0 2/6

410

0

2

13

0

2

0 0

Page 35: All-Pairs Shortest Paths

All-Pairs Shortest Paths 35

Johnson’s algorithm 複雜度分析 • 執行一次 Bellman-Ford 。 O(|V||E|) 。• 執行 |V| 次 Dijkstra 。

– 使用 Fibonacci heap ,總計 O(|V|2log|V|+|V||E|) 。

– 使用 Binary heap ,總計 O(|V||E|log|V|) 。• 當 |E| 足夠小,比 Warshall-Floyd 快。