Floyd- Warshall algorithm - PowerPoint PPT Presentation

hilda-hewitt
floyd warshall algorithm n.
Skip this Video
Loading SlideShow in 5 Seconds..
Floyd- Warshall algorithm PowerPoint Presentation
Download Presentation
Floyd- Warshall algorithm

play fullscreen
1 / 19
Download Presentation
Floyd- Warshall algorithm
283 Views
Download Presentation

Floyd- Warshall algorithm

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Floyd-Warshall algorithm 95360794 康峻岳

  2. All-pairs shortest paths • Input: Digraph G = (V, E),where |V| = n, with edge-weight function w: E → R. • Output:n × n matrix of shortest-path lengths δ(i, j) for all i, j ∈ V.

  3. Floyd-Warshall algorithm Define cij(k) = weight of a shortest path from ito j with intermediate vertices belonging to the set {1, 2, …, k} Thus, δ(i, j) = cij(n). Also, cij(0) = aij

  4. Floyd-Warshall recurrence • cij (k) = mink{cij(k–1), cik(k–1) + ckj(k–1)} intermediate vertices in {1, 2, …, k}

  5. Pseudocode for Floyd-Warshall • for k ← 1 to n do for i← 1 to n do for j ← 1 to n do if cij> cik+ ckj then cij← cik+ ckj Time Complexity: O(n3)

  6. 任意兩節點最短路徑

  7. Johnson’s algorithm 1. Find a vertex labeling h such that ŵ(u, v) ≥ 0 for all (u, v) ∈ E by using Bellman-Ford to solve thedifferenceconstraints h(v) – h(u) ≤ w(u, v), or determine that a negative-weight cycle exists. • Time = O(VE). 2. Run Dijkstra’s algorithm from each vertex using ŵ. • Time = O(VE + V2 lg V). 3. Reweight each shortest-path length ŵ(p)to produce the shortest-path lengths w(p)of the original graph. • Time = O(V2). Total time = O(VE + V2 lg V)

  8. 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)。

  9. Johnson’s algorithm範例 4 3 7 8 -5 1 -4 2 6

  10. Johnson’s algorithm範例 0 0 加入一個點s,以及自s拉一條weight為0的邊到每一點。 4 3 7 0 8 s -5 1 -4 2 0 6 0

  11. Johnson’s algorithm範例 0 -1 0 執行Bellman-Ford演算法,得到自s出發每一點的最短距離。 4 3 7 0 8 s 0 -5 -5 1 -4 2 0 -4 0 6 0

  12. Johnson’s algorithm範例 -1 做完reweighting 0 4 10 13 0 -5 0 0 0 2 -4 0 2

  13. Johnson’s algorithm範例 2/1 紅線部分是Shortest-paths tree。點的數字a/b代表自出發點(綠色點)出發,到達該點的最短路徑(Reweighting後的圖/原圖)。 0 4 10 13 0/0 2/-3 0 0 0 2 0/-4 2/0 2

  14. Johnson’s algorithm範例 0/0 0 4 10 13 2/3 0/-4 0 0 0 2 2/-1 0/1 2

  15. Johnson’s algorithm範例 0/4 0 4 10 13 2/7 0/0 0 0 0 2 2/3 0/5 2

  16. Johnson’s algorithm範例 0/-1 0 4 10 13 2/2 0/-5 0 0 0 2 2/-2 0/0 2

  17. Johnson’s algorithm範例 2/5 0 4 10 13 4/8 2/1 0 0 0 2 0/0 2/6 2

  18. 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|足夠小,比Floyd-Warshall快。

  19. 資料來源及參考網站 • http://www.csie.nctu.edu.tw/~sctsai/algo/notes/ • http://tinyurl.com/cglkn6 • http://www.csie.ntnu.edu.tw/~u91029/index.html