BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each...
-
date post
20-Dec-2015 -
Category
Documents
-
view
215 -
download
0
Transcript of BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| - 1 3 do for each...
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) 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]| - 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
xt
0
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
xt
0
7
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
i = 2
6
7
9
2
-4
-3
7
5
-2
8
s
z
y
xt
0
7
2
5
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
i = 2
6
7
9
2
-4
-3
7
5
-2
8
s
z
y
xt
0
7
2
5
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
i = 3
6
7
9
2
-4
-3
7
5
-2
8
s
z
y
xt
0
6
2
5
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
i = 4
6
7
9
2
-4
-3
7
5
-2
8
s
z
y
xt
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
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
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)
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)
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)
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)
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)
6
72
-3
7
-2
8
s
z
y
xt
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
72
-3
7
-2
8
s
z
y
xt
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)
6
72
-3
7
-2
8
s
z
y
xt
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
8 0
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
8 0 7 2
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
8 0 7 2 5
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
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
xt
1/10
2/7
3/4
5/68/97
2 -2
6
7
80 7 2 5 9
O(V+E)
Time: O(V+E) – linear in |adj|
O(V)
O(E)
Dijkstra’s Algorithm(no negative edges)
Greedy
0
0
0
1
34
1
3
4
0
1
34
1
3
4
Could these be optimal?
0
1
34
1
3
4
Could these be optimal?I don’t know yet
1?
1 ?
0
1
34
1
3
4
optimal?
0
1
34
1
3
4
optimal? Yes any path from “3” and “4” will be non-neg, and there is no unexplored paths from “0”
0
1
34
1
3
4
0
1
34
1
2
4
12
4
3
5
4
0
1
34
1
2
4
12
4
3
5
4
Optimal?
0
1
34
1
2
4
12
4
3
5
4
Optimal? No, as before there could be frontier edges causing better paths
0
1
34
1
2
4
12
4
3
5
4
optimal?
0
1
34
1
2
4
12
4
3
5
4
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)
BinHO(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)
BinHO(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)
BinHO(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)
BinHO(V) – Build Heap
O(V*lgV)
O(E*lgV) – Dec.Key
Time: O( (V+E)*lgV )