1 / 11

Bellman-Ford Algorithm

Bellman-Ford Algorithm. Shortest path alg , even for negative weights. 2. 2. 9. 6. 5. 5. Relax. Relax. 2. 2. 7. 6. 5. 5. Relaxation. A key technique in shortest path algorithms is relaxation Idea: for all v , maintain upper bound on distance Relax(u,v,w ) {

eilis
Download Presentation

Bellman-Ford Algorithm

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. Bellman-Ford Algorithm Shortest path alg, even for negative weights

  2. 2 2 9 6 5 5 Relax Relax 2 2 7 6 5 5 Relaxation • A key technique in shortest path algorithms is relaxation • Idea: for all v, maintain upper bound on distance Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; }

  3. Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solutionUnder what condition do we get a solution? Bellman-Ford Algorithm BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

  4. Bellman-Ford Algorithm BellmanFord() for each v  V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w What will be the running time?

  5. Bellman-Ford Algorithm BellmanFord() for each v  V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w What will be the running time? A: O(VE)

  6. 5 t x -2 ¥ ¥ 6 -3 8 s 0 7 -4 2 7 ¥ ¥ 9 y z Bellman-Ford Example

  7. 5 5 t t x x -2 -2 6 ¥ ¥ ¥ 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 ¥ ¥ ¥ 9 9 y z y z Bellman-Ford Example

  8. 5 5 t t x x -2 -2 6 ¥ ¥ ¥ 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 ¥ ¥ ¥ 9 9 y z y z 5 t x -2 6 4 6 -3 8 s 0 7 -4 2 7 7 2 9 y z Bellman-Ford Example

  9. 5 5 t t x x -2 -2 6 ¥ ¥ ¥ 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 ¥ ¥ ¥ 9 9 y z y z 5 5 t t x x -2 -2 6 4 2 4 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 2 7 2 9 9 y z y z Bellman-Ford Example

  10. t x -2 2 4 6 -3 8 s 0 7 -4 2 7 7 -2 9 y z Bellman-Ford Example • Bellman-Ford running time: • (|V|-1)|E| + |E| = Q(VE) 5

  11. Bellman-Ford • Prove: after |V|-1 passes, all d values correct • Consider shortest path from s to v:s  v1  v2  v3  v4  v • Initially, d[s] = 0 is correct, and doesn’t change (Why?) • After 1 pass through edges, d[v1] is correct (Why?) and doesn’t change • After 2 passes, d[v2] is correct and doesn’t change • … • Terminates in |V| - 1 passes: (Why?) • What if it doesn’t?

More Related