1 / 30

Lecture 11

Lecture 11. Application of BFS Shortest Path. Topics. Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths. Dijkstra's Shortest Path Algorithm.

elkan
Download Presentation

Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11 • Application of BFS • Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths Data Structure and Algorithm

  2. Dijkstra's Shortest Path Algorithm • Dijkstra’s algorithm solves the single- source shortest paths problem on a weighted, directed graph G =(V,E) fro the case in which all edge weights are nonnegative. • We assume that w(u,v) >=0 for each edge (u,v) Data Structure and Algorithm

  3. Algorithm • Dijkstra(G,w,s) • Initialize(G,s) • S = 0 • Q = V[G] • While Q != 0 do • U = ExtractMin(Q) • S = S union {u} • For each vertex v of adj[u] do • Relax(u,v,w) Complexty: Using priority queue:O (V2 +E) Using heap as priority queue: O( (V+E) lg2V) cost of building heap is O(V) cost of ExtracMin is O(lgV) and there are |V| such operations cost of relax is O(lgV) and there are |E| such operations. Data Structure and Algorithm

  4. Algorithm( Cont.) • Initialize(G,s) • for each vertex of V[G] do • d[v] = infinity • Л[v] = NIL • d[s] = 0 • relax(u,v,w) • if d[v] >d[u] +w(u,v) then • d[v] = d[u] + w(u,v) • Л[v] = u Data Structure and Algorithm

  5. Complexity • Using priority queue:O (V2 +E) • Using heap as priority queue: O( (V+E) lg2V) • cost of building heap is O(V) • cost of ExtracMin is O(lgV) and there are |V| such operations • cost of relax is O(lgV) and there are |E| such operations. Data Structure and Algorithm

  6. Dijkstra's Shortest Path Algorithm • Find shortest path from s to t. 2 23 3 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 Data Structure and Algorithm

  7. Dijkstra's Shortest Path Algorithm S = { } Q = { s, 2, 3, 4, 5, 6, 7, t }   2 23 3 0 9 s 18  14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44  distance label  Data Structure and Algorithm

  8. Dijkstra's Shortest Path Algorithm S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } ExtractMin()   2 23 3 0 9 s 18  14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44  distance label  Data Structure and Algorithm

  9. Dijkstra's Shortest Path Algorithm S = { s } Q = { 2, 3, 4, 5, 6, 7, t } decrease key   9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44  distance label  15 X Data Structure and Algorithm

  10. Dijkstra's Shortest Path Algorithm S = { s } Q = { 2, 3, 4, 5, 6, 7, t } ExtractMin()   9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44  distance label  15 X Data Structure and Algorithm

  11. Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t }   9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44   15 X Data Structure and Algorithm

  12. Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } decrease key  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44   15 X Data Structure and Algorithm

  13. Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t }  32 X  9 X 2 23 3 0 9 ExtractMin() s 18  14 X 14 6 2 6  4  19 30 11 5 15 5 6 16 20 t 7 44   15 X Data Structure and Algorithm

  14. Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } Q = { 3, 4, 5, 7, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  44 4  X 19 30 11 5 15 5 6 16 20 t 7 44   15 X Data Structure and Algorithm

  15. Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } Q = { 3, 4, 5, 7, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  44 4  X 19 30 11 5 15 5 6 16 20 t 7 44   15 ExtractMin() X Data Structure and Algorithm

  16. Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } Q = { 3, 4, 5, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  35 44 X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  59 X  15 X Data Structure and Algorithm

  17. Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } ExtractMin  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  35 44 X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  59 X  15 X Data Structure and Algorithm

  18. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } Q = { 4, 5, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  35 34 44 X X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  51 59 X X  15 X Data Structure and Algorithm

  19. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } Q = { 4, 5, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  35 34 44 X X 4  X 19 30 11 5 15 5 6 16 ExtractMin 20 t 7 44  51 59 X X  15 X Data Structure and Algorithm

  20. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } Q = { 4, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  45 35 34 X 44 X X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  50 51 59 X X X  15 X Data Structure and Algorithm

  21. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } Q = { 4, t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  45 35 34 X 44 X X 4  X 19 30 11 5 ExtractMin 15 5 6 16 20 t 7 44  50 51 59 X X X  15 X Data Structure and Algorithm

  22. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } Q = { t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  45 35 34 X 44 X X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  50 51 59 X X X  15 X Data Structure and Algorithm

  23. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } Q = { t }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  45 35 34 X 44 X X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  ExtractMin 50 51 59 X X X  15 X Data Structure and Algorithm

  24. Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } Q = { }  32 X  9 X 2 23 3 0 9 s 18  14 X 14 6 2 6  45 35 34 X 44 X X 4  X 19 30 11 5 15 5 6 16 20 t 7 44  ExtractMin 50 51 59 X X X  15 X Data Structure and Algorithm

  25. The Bellman-Form Algorithm • BELLMAN-FORD(G,w,s) • Initialize() • for i = 1 to |V[G]| -1 do • for each edge (u,v) of E[G] do • relax(u,v,w) • for each edge (u,v) of E[G] do • if d[v] > d[u] + w(u,v) then • return FALSE • return TRUE Complexity O(VE) Data Structure and Algorithm

  26. Example:Bellman-Ford 5   v u -2 6 -3 z 8 7 2 0 -4 7 x y 9   Data Structure and Algorithm

  27. Example:Bellman-Ford 5 6  v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7  Data Structure and Algorithm

  28. Example:Bellman-Ford 5 6 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 2 Data Structure and Algorithm

  29. Example:Bellman-Ford 5 2 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 2 Data Structure and Algorithm

  30. Example:Bellman-Ford 5 2 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 -2 Data Structure and Algorithm

More Related