Presentation is loading. Please wait.

Presentation is loading. Please wait.

BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)

Similar presentations


Presentation on theme: "BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)"— Presentation transcript:

1 BellmanFord

2 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true

3 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 0 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0

4 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 1 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0 7 2

5 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 2 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0 7 2 5 13

6 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 2 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0 7 2 5 9

7 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 3 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0 6 2 5 9

8 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true i = 4 6 7 9 2 -4 -3 7 5 -2 8 s z y x t 0 6 2 4 9 Correctness I) If no negative cycle reachable from s: BF returns true, BF finds shortest paths, BF builds predecessor tree II) Otherwise: BF returns false

9 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true

10 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true O(V)

11 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true O(V) O(V*E)

12 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true O(V) O(V*E) O(E)

13 BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v) E[G] 6 do if d[v] > d[u] + w[u,v] 7 then return false 8 return true O(V) O(V*E) O(E) O(V*E)

14 DAG Shortest Path

15 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w)

16 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) 6 7 2 -3 7 -2 8 s z y x t

17 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) 6 7 2 -3 7 -2 8 s z y x t 1/ 2/ 3/4

18 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) 6 7 2 -3 7 -2 8 s z y x t 1/10 2/7 3/4 5/6 8/9

19 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0

20 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 72

21 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 725

22 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259

23 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 Correct?

24 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 Correct? Yes, follows directly from L4 and L5

25 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 Time?

26 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 O(V+E) Time?

27 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 O(V+E) Time? O(V)

28 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 O(V+E) Time? O(V) O(E)

29 DAGshortestPaths(G,w,s) 1 topologically sort the vertices of G 2 InitializeSingleSource(G,s) 3 for each vertex u, taken in topological order 4 do for each vertex v adj[u] 5 do Relax(u,v,w) s z y x t 1/10 2/7 3/4 5/6 8/9 7 2-2 6 7 8 0 7259 O(V+E) Time: O(V+E) – linear in |adj| O(V) O(E)

30 Dijkstra’s Algorithm (no negative edges) Greedy

31 0

32 0

33 0 1 3 4 1 3 4

34 0 1 3 4 1 3 4 Could these be optimal?

35 0 1 3 4 1 3 4 Could these be optimal? I don’t know yet 1?

36 0 1 3 4 1 3 4 optimal?

37 0 1 3 4 1 3 4 optimal? Yes any path from “3” and “4” will be non-neg, and there is no unexplored paths from “0”

38 0 1 3 4 1 3 4

39 0 1 3 4 1 2 4 1 2 4 3 5 4

40 0 1 3 4 1 2 4 1 2 4 3 5 4 Optimal?

41 0 1 3 4 1 2 4 1 2 4 3 5 4 Optimal? No, as before there could be frontier edges causing better paths

42 0 1 3 4 1 2 4 1 2 4 3 5 4 optimal?

43 0 1 3 4 1 2 4 1 2 4 3 5 4 optimal? Yes! As before no better path from frontier, No better path from explored vertices

44 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w)

45 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w) O(V)

46 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w) O(V) BinH O(V) – Build Heap

47 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w) O(V) BinH O(V) – Build Heap O(VlgV)

48 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w) O(V) BinH O(V) – Build Heap O(V*lgV) O(E*lgV) – Dec.Key

49 Dijkstra(G,w,s) 1 InitializeSingleSource(G,s) 2 S Ø 3 Q V[G] 4 while Q Ø 5 do u ExtractMin(Q) 6 S S {u} 7 for each vertex v Adj[u] 8 do Relax(u,v,w) O(V) BinH O(V) – Build Heap O(V*lgV) O(E*lgV) – Dec.Key Time: O( (V+E)*lgV )


Download ppt "BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)"

Similar presentations


Ads by Google