1 / 11

CSCE 411 Design and Analysis of Algorithms

CSCE 411 Design and Analysis of Algorithms. Set 5a: Bellman-Ford SSSP Algorithm Prof. Jennifer Welch Spring 2013. Bellman-Ford Idea. Consider each edge (u,v) and see if u offers v a cheaper path from s compare d[v] to d[u] + w(u,v)

zuriel
Download Presentation

CSCE 411 Design and Analysis of Algorithms

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. CSCE 411Design and Analysis of Algorithms Set 5a: Bellman-Ford SSSP Algorithm Prof. Jennifer Welch Spring 2013 CSCE 411, Spring 2013: Set 5a

  2. Bellman-Ford Idea • Consider each edge (u,v) and see if u offers v a cheaper path from s • compare d[v] to d[u] + w(u,v) • Repeat this process |V| - 1 times to ensure that accurate information propgates from s, no matter what order the edges are considered in CSCE 411, Spring 2013: Set 5a

  3. Bellman-Ford SSSP Algorithm • input: directed or undirected graph G = (V,E,w) //initialization • initialize d[v] to infinity and parent[v] to nil for all v in V other than the source • initialize d[s] to 0 and parent[s] to s // main body • for i := 1 to |V| - 1 do • for each (u,v) in E do // consider in arbitrary order • if d[u] + w(u,v) < d[v] then • d[v] := d[u] + w(u,v) • parent[v] := u CSCE 411, Spring 2013: Set 5a

  4. Bellman-Ford SSSP Algorithm // check for negative weight cycles • for each (u,v) in E do • if d[u] + w(u,v) < d[v] then • output "negative weight cycle exists" CSCE 411, Spring 2013: Set 5a

  5. Running Time of Bellman-Ford • O(V) iterations of outer for loop • O(E) iterations of inner for loop • O(VE) time total CSCE 411, Spring 2013: Set 5a

  6. s c a b Bellman-Ford Example process edges in order (c,b) (a,b) (c,a) (s,a) (s,c) 3 2 —4 4 1 <board work> CSCE 411, Spring 2013: Set 5a

  7. Correctness of Bellman-Ford Assume no negative-weight cycles. Lemma:d[v] is never an underestimate of the actual shortest path distance from s to v. Lemma:If there is a shortest s-to-v path containing at most i edges, then after iteration i of the outer for loop, d[v] is at most the actual shortest path distance from s to v. Theorem:Bellman-Ford is correct. Proof:Follows from these 2 lemmas and fact that every shortest path has at most |V| - 1 edges. CSCE 411, Spring 2013: Set 5a

  8. Correctness of Bellman-Ford • Suppose there is a negative weight cycle. • Then the distance will decrease even after iteration |V| - 1 • shortest path distance is negative infinity • This is what the last part of the code checks for. CSCE 411, Spring 2013: Set 5a

  9. Speeding Up Bellman-Ford • The previous example would have converged faster if we had considered the edges in a different order in the for loop • move outward from s • If the graph is a DAG (no cycles), we can fully exploit this idea to speed up the running time CSCE 411, Spring 2013: Set 5a

  10. DAG Shortest Path Algorithm • input: directed graph G = (V,E,w) and source vertex s in V • topologically sort G • d[v] := infinity for all v in V • d[s] := 0 • for each u in V in topological sort order do • for each neighbor v of u do • d[v] := min{d[v],d[u] + w(u,v)} CSCE 411, Spring 2013: Set 5a

  11. DAG Shortest Path Algorithm • Running Time is O(V + E). • Example: <board work> CSCE 411, Spring 2013: Set 5a

More Related