Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Source Shortest Paths

Similar presentations


Presentation on theme: "Single-Source Shortest Paths"— Presentation transcript:

1 Single-Source Shortest Paths

2 Shortest-path problem
即是在一圖上找出兩點間最短路徑。 G=(V,E)是一個Weighted Directed Graph(加權有向圖)透過Weight function w: ER界定出每個邊的權重。 以p=(v0,v1,…,vk)表一個自v0到vk的Path(路徑)。 此處新介紹一個定義,Weighted graph即是在每一個邊上加上一個實數作為權重, 可正,可負,可零並無什麼特別限制。 另外在此Path的定義僅是方便下列路徑距離及最短距離的定義, 並非各處的路徑均如此定義。 Single-Source Shortest Paths

3 Shortest-path problem
定義 定義自u到v的最短距離 Single-Source Shortest Paths

4 Shortest-path tree rooted at s
對應於圖G=(V,E)的Shortest-path tree rooted at s (根於s之最短路徑樹) G’=(V’,E’),滿足下列三點: V’是s可達的點集合。 G’是一個以s為根的Rooted Tree。 在G’中s到v的simple path即為G中s到v的最短路徑。 V’是s可達的點集合,代表for every v’ in V’, there exists a path from s to v’。 Rooted tree是一種有向圖,只有一個點in degree = 0 其餘均為 1, in degree 0的點稱之為root。 Single-Source Shortest Paths

5 Shortest-path tree rooted at s範例
10 10 1 1 10 1 1 x w x w t t 10 1 10 1 10 u 10 y y v 1 v 10 1 1 z z Original Graph G Shortest-path tree rooted at s Single-Source Shortest Paths

6 Single-Source Shortest Paths
Predecessor graph 對一圖G=(V,E),根據一個表π來建構的子圖Gπ=(Vπ,Eπ),滿足以下條件: π[s]=NIL,且s∈Vπ。 若π[v]≠NIL則(π[v],v)∈Eπ且v∈Vπ 。 Shortest-path tree rooted at s是Predecessor graph的特例。 Single-source shortest path演算法主要利用不斷更新Predecessor graph, 讓他最後變成Shortest-path tree rooted at s來求出s到其他點的最短距離。 Single-Source Shortest Paths

7 Single-Source Shortest Paths
Predecessor graph範例 π[s] π[t] π[u] π[v] π[w] π[x] π[y] π[z] NIL s t x v s s 10 10 1 1 10 1 1 x w x w t t 10 在此舉的例子同前,但補列對應的表格。 1 10 1 10 u 10 y y v 1 v 10 1 1 z z Original Graph G Shortest-path tree rooted at s Single-Source Shortest Paths

8 Initialize-Single-Source演算法
定義變數d[v]代表目前已知之自s至v的最短距離。 定義變數π[v]代表目前已知自s至v的最短路徑上,v之前的那一點。 初始時,d[v]=∞,π[v]=NIL,d[s]=0。即除自s到s的最短路徑已知之外,其餘均設為未知。 Single-Source Shortest Paths

9 Initialize-Single-Source演算法
Initialize-Single-Source(G,s) { for each vertex v∈V[G] do d[v]∞ π[v]NIL d[s]0 } Single-Source Shortest Paths

10 Single-Source Shortest Paths
Relaxation 演算法 主要的目的在於利用邊(u,v)的資訊來更新目前所知的最短路徑。 Relax(u,v,w) { if d[v]>d[u]+w(u,v) then d[v]d[u]+w(u,v) π[v]u } Single-Source Shortest Paths

11 Single-Source Shortest Paths
Relaxation 範例 Relax(u,v,w)後 Relax(u,v,w)前 s if w(u,v)=2 (<3) 此時更新sv的最短距離 以及更新π[v]u s 4 6 w(u,v) u v 4 7 s 紅線代表目前已知的最短路徑。 如果Relax(u,v,w)發現經由已知的su最短路徑加上(u,v)抵達v, 可以比原先已知sv的最短路徑距離更短, 則可以更新sv的最短路徑資訊。 w(u,v) if w(u,v)=4 (>3) 此時不更新sv的最短距離 u v 4 7 w(u,v) u v Single-Source Shortest Paths

12 Single-Source Shortest Paths
最短路徑與Relaxation的性質 三角不等式: 對所有的邊(u,v),δ(s,v)δ(s,u)+w(u,v)。 上限性質: δ(s,v)d[v],即d[v]總是sv的最短距離上限。一旦d[v]=δ(s,v),則Relaxation不會更改d[v]值。 δ(s,v)代表s到v的最短距離。 Single-Source Shortest Paths

13 Single-Source Shortest Paths
最短路徑與Relaxation的性質 無路徑性質: 如果s到v並無路徑,則d[v]=δ(s,v)=∞。 收斂性質: 若sv的最短路徑包含邊(u,v)且d[u]=δ(s,u),則此時執行Relax(u,v,w)會使得d[v]=δ(s,v)。 Single-Source Shortest Paths

14 Single-Source Shortest Paths
最短路徑與Relaxation的性質 Path-relaxation性質: 如p=(v0,v1,…,vk)是一個自s=v0vk的最短路徑, 則依序執行Relax(v0,v1,w), Relax(v1,v2,w)…, Relax(vk-1,vk,w)會使得d[vk]=δ(s,vk)。 Predecessor graph性質: 當經過一連串的Relaxation後,對所有的點v, d[v]=δ(s,v)時,此時對應的Predecessor graph Gπ即是一個Shortest-path tree rooted at s。 Single-Source Shortest Paths

15 Single-Source Shortest Paths
Bellman-Ford演算法 可以計算出沒有負迴圈的圖之最短路徑。 Bellman-Ford(G,w,s) { Initialize-Single-Source(G,s) for i = 1 to |V-1| do for each edge (u,v)∈E do Relex(u,v,w) for each edge (u,v)∈E do if d[v]>d[u]+w(u,v) then return false //代表有負迴圈 return true //代表計算成功 } Single-Source Shortest Paths

16 Single-Source Shortest Paths
Bellman-Ford演算法運作範例 5 5 (a) -2 (b) -2 6 6 6 8 8 -3 -3 s s 7 7 -4 -4 7 7 (a)是初始狀態,(b)是first iteration之後的狀況,(c)跟(d)類推。 塗成藍色的虛線邊是對應的Predecessor graph所有的邊, 點內的數字代表d[v],是現存最短路徑長度。 2 7 2 9 9 Single-Source Shortest Paths

17 Single-Source Shortest Paths
Bellman-Ford演算法運作範例 5 5 (c) -2 (d) (e) -2 6 4 2 4 6 6 8 8 -3 -3 s s 7 7 -4 -4 7 7 7 2 2 7 2 -2 9 9 Single-Source Shortest Paths

18 Single-Source Shortest Paths
Bellman-Ford演算法分析 正確性:對所有的邊作一次Relaxation則可將在Shortest-path tree rooted at s中一步可及的path正確計算出。由path-relaxation性質,做完|V|-1次之後,所有的Shortest simple path終點v,d[v]=δ(s,v)。 能檢測出有負迴圈,是因為做了|V|-1次relaxation之後, 仍然可以透過經過額外的邊讓d[v]下降, 則代表最短路徑長度已經超過|V|-1, 由鴿籠原理可以得知必有迴圈存在, 但正回圏不存在最短路徑上, 因故有負迴圈存在。 Single-Source Shortest Paths

19 Single-Source Shortest Paths
Bellman-Ford演算法分析 就時間複雜度的分析如下: Initialize-Single-Source花去O(|V|)的時間。 對所有的邊做了O(|V|)次的Relaxation,花去O(|V||E|)的時間。 最後花去O(|E|)的時間檢查是否有負迴圈。 故總共花去O(|V||E|)的時間。 Single-Source Shortest Paths

20 Single-source shortest paths in DAGs
跟Bellman-Ford不同的是,按照特定的順序作Relaxation,可較短的時間內計算出最短路徑。 DAG-Shortest-Path(G,w,s) { Topologically sort V[G] Initialize-Single-Source(G,s) for each u taken in topological order do for each v∈adj[u] do Relax(u,v,w) } 以上演算法僅需O(|V|+|E|)的時間。 Topological sort僅需O(|V|+|E|)的時間 而Initialize-Single-Source花去O(|V|) 最後的迴圈花去O(|V|+|E|)的時間 Single-Source Shortest Paths

21 DAG-Shortest-Path操作範例
6 1 s 5 2 7 -1 -2 3 4 2 (a)是初始狀態,(b)是first iteration之後的狀況,(c)跟(d)類推。 塗成藍色的虛線邊是對應的Predecessor graph所有的邊, 點內的數字代表d[v],是現存最短路徑, 綠色的點代表已經將該點的所有邊Relax過了。 (b) 6 1 s 5 2 7 -1 -2 3 4 2 Single-Source Shortest Paths

22 DAG-Shortest-Path操作範例
(c) 6 1 s 5 2 7 -1 -2 2 6 3 4 2 (d) 6 1 s 5 2 7 -1 -2 2 6 6 4 3 4 2 Single-Source Shortest Paths

23 DAG-Shortest-Path操作範例
6 1 s 5 2 7 -1 -2 2 6 5 4 3 4 2 (f) 6 1 s 5 2 7 -1 -2 2 6 5 3 3 4 2 (g) 6 1 s 5 2 7 -1 -2 2 6 5 3 3 4 2 Single-Source Shortest Paths

24 Single-Source Shortest Paths
Dijkstra演算法 僅能處理無負邊的圖。 比Bellman-ford演算法快,也是藉由挑選特定順序來做Relaxation來達成目的。 利用Priority queue來實做。 主要關鍵想法:利用收斂性質。 Single-Source Shortest Paths

25 Single-Source Shortest Paths
Dijkstra演算法 Q: Priority queue with d as the key Dijkstra(G,w,s) { Initialize-Single-Source(G,s) Q=V[G] while Q is not empty do u=Extract-Min(Q) for each v∈adj[u] do Relax(u,v,w) } 每次藉由Extract-min所得到的點v,d[v]已經是最短路徑長度了。 Single-Source Shortest Paths

26 Single-Source Shortest Paths
Dijkstra演算法操作範例 10 5 1 2 3 7 9 4 6 (a) (b) s (a)是初始狀態,(b)是first iteration之後的狀況,(c)跟(d)類推。 塗成藍色的虛線邊是對應的Predecessor graph所有的邊, 點內的數字代表d[v],是現存最短路徑, 綠色的點代表已經將該點的所有邊Relax過了。 Single-Source Shortest Paths

27 Single-Source Shortest Paths
Dijkstra演算法操作範例 8 11 5 7 10 1 2 3 9 4 6 (d) s 14 (c) Single-Source Shortest Paths

28 Single-Source Shortest Paths
Dijkstra演算法操作範例 8 9 5 7 10 1 2 3 4 6 (e) s Single-Source Shortest Paths

29 Single-Source Shortest Paths
Dijkstra演算法分析 依照使用的Priority queue實做方式不同,會有不同的執行時間。 使用Linear array來實做,消耗O(|V|2)的時間。 使用Binary heap來實做,消耗O(|E|log|V|)的時間。 使用Fibonacci heap來實做,消耗O(|E|+|V|log|V|)的 時間。 Single-Source Shortest Paths


Download ppt "Single-Source Shortest Paths"

Similar presentations


Ads by Google