BellmanFord
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t 0
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t 0 7 2
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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 = s z y x t 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
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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)
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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)
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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)
BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| 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)
DAG Shortest Path
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)
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
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/ 2/ 3/4
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
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/
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/
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/
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/
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/ Correct?
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/ Correct? Yes, follows directly from L4 and L5
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/ Time?
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/ O(V+E) Time?
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/ O(V+E) Time? O(V)
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/ O(V+E) Time? O(V) O(E)
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/ O(V+E) Time: O(V+E) – linear in |adj| O(V) O(E)
Dijkstra’s Algorithm (no negative edges) Greedy
0
0
Could these be optimal?
Could these be optimal? I don’t know yet 1?
optimal?
optimal? Yes any path from “3” and “4” will be non-neg, and there is no unexplored paths from “0”
Optimal?
Optimal? No, as before there could be frontier edges causing better paths
optimal?
optimal? Yes! As before no better path from frontier, No better path from explored vertices
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)
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)
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
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)
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
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 )