Chapter25

3,342 views

Published on

Published in: Technology, Self Improvement
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,342
On SlideShare
0
From Embeds
0
Number of Embeds
46
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter25

  1. 1. 25.All-Pairs Shortest Paths <ul><li>25.1 Shortest paths and matrix multiplication, O(V 3 lg V) </li></ul><ul><li>25.2 The Floyd-Warshall algorithm, O(V 3 ) </li></ul><ul><li>25.3 Johnson's algorithm for sparse graphs, O(V 2 lg V + VE) </li></ul>
  2. 2. <ul><li>任兩點之間的 shortest path ,可以執行 |V| 次 single-source shortest path 的演算法作到 ( 將每個點都當作 source 一次 ) </li></ul><ul><li>若沒有 negative weight edge ,執行 |V| 次 Dijkstra's algorithm ,時間為 O(V 2 lg V + VE) ;若有 negative weight edge ,執行 |V| 次 Bellman-Ford 演算法時間為 O(V 2 E) = O(V 4 ) 。 </li></ul><ul><li>這一章將介紹更快速的 all-pair shortest path 演算法。 </li></ul>25.All-Pairs Shortest Paths
  3. 3. <ul><li>任兩點之間的 shortest path ,圖形的輸入大多用類似 n×n 的 adjacent matrix W=(w ij ) 來輸入, w ij = 0, if i = j; w ij = w(i, j) if i ≠ j and (i, j) ∈ E; w ij = ∞ if i ≠ j and (i, j) ∉ E. 並假設沒有 negative weight cycle 存在。 </li></ul><ul><li>輸出的結果也同樣是 n ×n 的 matrix D = (d ij ), d ij 代表 i 到 j 的 shortest path 之長度,即 δ(i, j) 。 </li></ul><ul><li>除了長度之外,還要記錄 shortest path 的走法,因此要記錄 predecessor matrix Π = (π ij ), π ij 代表 i 到 j 的 shortest path 中 j 的前一個點。 </li></ul>25.All-Pairs Shortest Paths
  4. 4. <ul><li>For each vertex i ∈ V, we define the predecessor subgraph of G for i as G π,i = (V π,i , E π,i ), where V π,i = { j ∈ V : π ij ≠ NIL} ∪ {i} and E π,i = {(π ij , j) : j ∈ V π,i – {i} }. </li></ul><ul><li>G π,i 代表 shortest-path tree , </li></ul><ul><li>PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, j) </li></ul><ul><ul><li>if i = j then </li></ul></ul><ul><ul><ul><li>print i </li></ul></ul></ul><ul><ul><li>else if π ij = NIL then </li></ul></ul><ul><ul><ul><li>print &quot;no path from &quot; i &quot;to&quot; j &quot;exists&quot; </li></ul></ul></ul><ul><ul><li>else PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, π ij ) </li></ul></ul><ul><ul><ul><li>print j </li></ul></ul></ul>25.All-Pairs Shortest Paths
  5. 5. <ul><li>這一小節我們將先發展一個 O(V 4 ) 的 dynamic programming 演算法,之後再改進到 O(V 3 lg V) 。 </li></ul><ul><li>令 l ij (m) 代表點 i 到點 j 最多經過 m 條邊的 shortest path 長度。 </li></ul><ul><li>因此 l ii (0) = 0, l ij (0) = ∞ if i ≠ j ; l ij (m) = min{l ik (m-1) + w kj } for 1 ≤ k ≤ n. </li></ul><ul><li>若沒有 negative weight cycle 的話,任何 shortest path 最多只會經過 n-1 條邊 (n 代表點數 ) ,因此只需算到 l ij (n-1) 。 </li></ul><ul><li>令 L (m) = (l ij (m) ) ,按照 L (0) , L (1) , L (2) , ..., L (n-1) 的順序來計算。 L ij (1) = w ij , 所以 L (1) = W 。 </li></ul>25.1 Shortest paths and matrix multiplication
  6. 6. <ul><li>這個演算法的重點在於如何用 L (m-1) 與 W 來計算 L (m) 。可用下列的演算法來作 </li></ul><ul><li>EXTEND-SHORTEST-PATHS(L, W) </li></ul><ul><ul><li>n ← rows[L] </li></ul></ul><ul><ul><li>let L' = (l ij ') be an n × n matrix </li></ul></ul><ul><ul><li>for i ← 1 to n do </li></ul></ul><ul><ul><ul><li>for j ← 1 to n do </li></ul></ul></ul><ul><ul><ul><ul><li>l ij ' ← ∞ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>for k ← 1 to n do </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>l ij ' ← min(l ij ', l ik + w kj ) </li></ul></ul></ul></ul></ul><ul><ul><li>return L' </li></ul></ul>25.1 Shortest paths and matrix multiplication 需要花 Θ (n 3 ) 的時間
  7. 7. <ul><li>因此直覺的作法如下: </li></ul><ul><li>SLOW-ALL-PAIRS-SHORTEST-PATHS(W) </li></ul><ul><ul><li>n ← rows[W] </li></ul></ul><ul><ul><li>L (1) ← W </li></ul></ul><ul><ul><li>for m ← 2 to n-1 do </li></ul></ul><ul><ul><ul><li>L (m) ← EXTEND-SHORTEST-PATHS(L (m-1) , W) </li></ul></ul></ul><ul><ul><li>return L (n-1) </li></ul></ul><ul><li>只是這個演算法所需時間為 Θ(n 4 ) 。 </li></ul>25.1 Shortest paths and matrix multiplication
  8. 8. 25.1 Shortest paths and matrix multiplication 3 4 8 1 -5 7 2 6 -4
  9. 9. <ul><li>只是這個演算法所需時間為 Θ(n 4 ) ,速度太慢,以下對這個演算法的速度作點改進。 </li></ul><ul><li>EXTEND-SHORTEST-PATHS(L, W) 這個演算法與矩陣相乘類似,要計算 L (n-1) 並不需要乘 n-2 次,例如: L (8) = L (4) * L (4) , L (4) = L (2) * L (2) , L (2) = L (1) * L (1) ,只需要三次乘法。要計算 L (n-1) 也同樣只需要 lg n 次的乘法即可。 </li></ul>25.1 Shortest paths and matrix multiplication
  10. 10. <ul><li>FASTER-ALL-PAIRS-SHORTEST-PATHS(W) </li></ul><ul><ul><li>n ← rows[W] </li></ul></ul><ul><ul><li>L (1) ← W </li></ul></ul><ul><ul><li>m ← 1 </li></ul></ul><ul><ul><li>while m < n – 1 do </li></ul></ul><ul><ul><ul><li>L (2m) ← EXTEND-SHORTEST-PATHS(L (m) , L (m) ) </li></ul></ul></ul><ul><ul><ul><li>m ← 2m </li></ul></ul></ul><ul><ul><li>return L (m) </li></ul></ul><ul><li>這個演算法的時間為 Θ(n 3 lg n) 。 </li></ul>25.1 Shortest paths and matrix multiplication
  11. 11. <ul><li>25.1-1 Run SLOW-ALL-PAIRS-SHORTEST-PATHS on the weighted, directed graph of Figure 25.2, showing the matrices that result for each iteration of the loop. Then do the same for FASTER-ALL-PAIRS-SHORTEST-PATHS. </li></ul><ul><li>25.1-9 Modify FASTER-ALL-PAIRS-SHORTEST-PATHS so that it can detect the presence of a negative-weight cycle. </li></ul><ul><li>25.1-10 Give an efficient algorithm to find the length(number of edges) of a minimum-length negative-weight cycle in a graph. </li></ul>Exercise 25.1
  12. 12. <ul><li>這一小節介紹另一個 dynamic-programming 的 all-pairs shortest-paths 的演算法,所需時間為 Θ(V 3 ) 。 </li></ul><ul><li>The Floyd-Warshall algorithm 與前一節的方法不同,是按照 intermediate vertex 來定義,所謂 intermdeiate vertex 代表起點與終點之外的點。 </li></ul><ul><li>令圖 G 的點集合 V = {1, 2, ..., n} ,考慮 i 到 j 只經過 {1, 2, ..., k} 點的 shortest path 。 </li></ul><ul><li>只經過 {1, 2, ..., k-1} 的 path 也算是經過 {1, 2, ..., k} 的 path 。 </li></ul><ul><li>若 k 是 i 到 j 經過 {1, 2, ..., k} 的 shortest path 上的一個點,則可將此 shortest path 分成 i 到 k 與 k 到 j 兩段經過 {1, 2, ..., k-1} 的 shortest path 。 </li></ul>25.2 The Floyd-Warshall algorithm
  13. 13. <ul><li>若 k 是 i 到 j 經過 {1, 2, ..., k} 的 shortest path 上的一個點,則可將此 shortest path 分成 i 到 k 與 k 到 j 兩段經過 {1, 2, ..., k-1} 的 shortest path 。 </li></ul><ul><li>Let d ij (k) be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1, 2, ..., k}. </li></ul>25.2 The Floyd-Warshall algorithm p 1 p 2
  14. 14. <ul><li>Let d ij (k) be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1, 2, ..., k}. </li></ul><ul><li>d ij (0) = w ij , </li></ul><ul><li>d ij (k) = min{d ij (k-1) , d ik (k-1) + d kj (k-1) } for k ≥ 1. </li></ul><ul><li>D (n) = (d ij (n) ) = δ(i, j) for all i, j ∈ V. </li></ul><ul><li>計算的順序還是從 D (0) , D (1) , ..., D (n) 為止。 </li></ul>25.2 The Floyd-Warshall algorithm
  15. 15. <ul><li>計算的順序還是從 D (0) , D (1) , ..., D (n) 為止。 </li></ul><ul><li>FLOYD-WARSHALL(W) </li></ul><ul><ul><li>n ← rows[W] </li></ul></ul><ul><ul><li>D (0) ← W </li></ul></ul><ul><ul><li>for k ← 1 to n do </li></ul></ul><ul><ul><ul><li>for i ← 1 to n do </li></ul></ul></ul><ul><ul><ul><ul><li>for j ← 1 to n do </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>d ij (k) ← min (d ij (k-1) , d ik (k-1) + d kj (k-1) ) </li></ul></ul></ul></ul></ul><ul><ul><li>return D (n) </li></ul></ul><ul><li>時間複雜度為 Θ(n 3 ) </li></ul>25.2 The Floyd-Warshall algorithm
  16. 16. 25.2 The Floyd-Warshall algorithm 3 4 8 1 -5 7 2 6 -4 π ij (0) = NIL if i = j or w ij = ∞ π ij (0) = i if i ≠ j and w ij < ∞ π ij (k) = π ij (k-1) if d ij (k-1) ≤ d ik (k-1) + d kj (k-1) π ij (k) = π kj (k-1) if d ij (k-1) > d ik (k-1) + d kj (k-1)
  17. 17. <ul><li>Transitive closure of a directed graph </li></ul><ul><li>The transitive closure of G is defined as the graph G * = (V, E * ), where E * = {(i, j) : there is a path from vertex i to vertex j in G}. </li></ul><ul><li>可以給 G 的每條邊的 weight 為 1 、執行 Floyd-Warshall algorithm ,如果 d ij < n 就代表從 i 到 j 有 path ,否則 d ij = ∞ 。 </li></ul><ul><li>也可以用 t ij (k) =1 代表點 i 到 j 只經過 {1, 2, ..., k} 的點有 path 可走。 </li></ul><ul><li>t ij (0) = 0 if i ≠ j and (i, j) ∉ E, t ij (0) = 1 if i = j or (i, j) ∈ E. </li></ul><ul><li>t ij (k) = t ij (k-1) ∨ (t ik (k-1) ∧ t kj (k-1) ). </li></ul>25.2 The Floyd-Warshall algorithm
  18. 18. <ul><li>TRANSITIVE-CLOSURE(G) </li></ul><ul><ul><li>n ← |V[G]| </li></ul></ul><ul><ul><li>for i ← 1 to n do </li></ul></ul><ul><ul><ul><li>for j ← 1 to n do </li></ul></ul></ul><ul><ul><ul><ul><li>if i = j or (i, j) ∈ E[G] then </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>t ij (0) ← 1 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>else t ij (0) ← 0 </li></ul></ul></ul></ul><ul><ul><li>for k ← 1 to n do </li></ul></ul><ul><ul><ul><li>for i ← 1 to n do </li></ul></ul></ul><ul><ul><ul><ul><li>for j ← 1 to n do </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>t ij (k) = t ij (k-1) ∨ (t ik (k-1) ∧ t kj (k-1) ) </li></ul></ul></ul></ul></ul><ul><ul><li>return T (n) </li></ul></ul>25.2 The Floyd-Warshall algorithm
  19. 19. <ul><li>25.2-1 Run the Floyd-Warshall algorithm on the weighted, directed graph of Figure 25.2. Show the matrix D (k) that results for each iteration of the outer loop. </li></ul><ul><li>25.2-6 How can the output of the Floyd-Warshall algorithm be used to detect the presence of a negative-weight cycle? </li></ul><ul><li>25.2-8 Give an O(VE)-time algorithm for computing the transitive closure of a directed graph G(V, E). </li></ul>Exercise 25.2
  20. 20. <ul><li>Johnson's algorithm 用 O(V 2 lg V + VE) 的時間來解決 all-pairs shortest path 的問題。輸出結果為任兩點之間的 shortest path 長度、或者報告圖形中包含有 negative-weight cycle 。 </li></ul><ul><li>如果圖形中都沒有 negative weight edge 的話,就用 Dijkstra's algorithm 執行 |V| 次,就得到結果。 </li></ul><ul><li>若有 negative weight edge ,則想辦法計算新的 edge weights w' ,使之滿足兩個重要條件: </li></ul><ul><ul><li>For all pairs of vertices u, v ∈ V, a path p is shortest path 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 w'. </li></ul></ul><ul><ul><li>For all edges (u, v), the new weight w'(u, v) is nonnegative. </li></ul></ul>25.3 Johnson's algorithm for sparse graphs
  21. 21. <ul><li>我們將用 O(VE) 的時間來計算新的 weight function w'. </li></ul><ul><li>Lemma 25.1 (Reweighting does not change shortest paths) Given a weighted, directed graph G = (V, E) with weight function w : E -> R, let h : V -> R be any function mapping vertices to real numbers. For each edge (u, v) ∈ E, define w'(u, v) = w(u, v) + h(u) – h(v). Let p = 〈 v 0 , v 1 , ..., v k 〉 be any path from vertex v 0 to vertex v k . Then p is a shortest path from v 0 to v k with weight function w if and only if it is a shortest path with weight function w'. That is, w(p) = δ(v 0 , v k ) if and only if w'(p) = δ'(v 0 , v k ). Also, G has a negative-weight cycle using weight function w if and only if G has a negative-weight cycle using weight function w'. </li></ul>25.3 Johnson's algorithm for sparse graphs
  22. 22. <ul><li>前面的 Lemma 25.1 滿足第一個性質,接下來要想辦法滿足第二個性質。 </li></ul><ul><li>若我們要找 G 的 all-pairs shortest paths ,先作一個輔助圖 G' 。計算點 0 到其他點的 shortest path 的長度,用 Bellman-Ford 的演算法。 </li></ul>25.3 Johnson's algorithm for sparse graphs 3 4 8 1 -5 7 2 6 -4 1 2 3 4 5 0 0 0 0 0 0 -4 0 -1 -5 0
  23. 23. <ul><li>令 h(u) = δ(0, u), w'(u, v) = w(u, v) + h(u) - h(v) </li></ul>25.3 Johnson's algorithm for sparse graphs 3 4 8 1 -5 7 2 6 -4 0 0 0 0 0 1 2 3 4 5 -4 0 -1 -5 0 4 0 13 0 0 10 2 2 0 0 5 1 0 4 0 1 2 3 4 5 -4 0 -1 -5 0
  24. 24. <ul><li>改寫後的 edge 都不再是 negative weight ,因此可以用 Dijkstra's algorithm 執行 |V| 次,即可得到 all-pair shortest path ,其時間為 O(V 2 lg V + VE) 。 </li></ul>25.3 Johnson's algorithm for sparse graphs 4 0 13 0 0 10 2 2 0 1 2 3 4 5 4 0 13 0 0 10 2 2 0 0 5 1 0 4 0 1 2 3 4 5 -4 0 -1 -5 0 0/0 2/1 2/-3 2/2 0/-4 2/3 0/0 0/-4 2/6 2/-1 2/7 0/4 0/0 0/5 2/3 2/2 0/-1 0/-5 0/0 2/-2 4/8 2/5 2/1 2/6 0/0
  25. 25. 25.3 Johnson's algorithm for sparse graphs <ul><li>JOHNSON(G) </li></ul><ul><li>compute G', where V[G'] = V[G] ∪ {s}, E[G'] = E[G] ∪ {(s, v) : v Î V[G]}, and w(s, v) = 0 for all v ∈ V[G] </li></ul><ul><li>if BELLMAN-FORD(G', w, s) = FALSE then </li></ul><ul><ul><li>print &quot;the input graph contains a negative-weight cycle&quot; </li></ul></ul><ul><li>else for each vertex v ∈ V[G'] do </li></ul><ul><ul><ul><li>set h(v) to the value of δ (s, v) </li></ul></ul></ul><ul><ul><ul><li>computed by the Bellman-Ford algorithm </li></ul></ul></ul><ul><ul><li>for each edge (u, v) ∈ E[G'] do </li></ul></ul><ul><ul><ul><li>w'(u, v) ← w(u, v) + h(u) -h(v) </li></ul></ul></ul><ul><ul><li>for each vertex u ∈ V[G] do </li></ul></ul><ul><ul><ul><li>run DIJKSTRA(G, w', u) to compute δ'(u, v) for all v ∈ V[G] </li></ul></ul></ul><ul><ul><ul><li>for each vertex v ∈ V[G] do </li></ul></ul></ul><ul><ul><ul><ul><li>d uv ← δ'(u, v) + h(v) – h(u) </li></ul></ul></ul></ul><ul><ul><li>return D </li></ul></ul>

×