Académique Documents
Professionnel Documents
Culture Documents
Proof: suppose some subpath is not a shortest path There must then exist a shorter subpath Could substitute the shorter subpath for a shorter path But then overall path is not shortest path. Contradiction
Relaxation
For all v, maintain upper bound d[v] on (s,v) Relax(u,v,w) { if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v); }
< 0
5 2 9 5 2 6
Relax
5
5
Relax
7 5 2 6
6
Dijkstras Algorithm
B Dijkstra(G,s) 2 10 for each v V 4 3 d[v] = ; (v)=NULL; A d[s] = 0; S = ; Q = V; (s)=NULL; 5 1 C while (Q ) u = ExtractMin(Q); S = S U {u}; for each v Adj[u] if (d[v] > d[u]+w(u,v)) Relaxation d[v] = d[u]+w(u,v); Step (v)=u What will be the total running time?
7
Question
Can you give an example of a graph that includes negative weights, and does not work with Dijkstras algorithm? A 4 0 B -2 4 D A 4 4 B
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;
Initialize d[], which will converge to shortest-path value Relaxation: Make |V|-1 passes, relaxing each edge Test for solution Under what condition do we get a solution?
3 D 1
3 3 1 C
11
12
Bellman-Ford Algorithm
BellmanFord() What will be the running time? for each v V A: O(|V||E|) 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;
Bellman-Ford Algorithm
BellmanFord() for each v V s 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; -1 A 4 C 3 1 D B 2 2 E -3
5 Example
13
14
Bellman-Ford
Correctness: show d[v] = (s,v) after |V|-1 passes Lemma: d[v] (s,v) always Initially true Let v be first vertex for which d[v] < (s,v) Let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u,v) Then d[v] < (s,v) (s,v) (s,u) + w(u,v) (Why?) (s,u) + w(u,v) d[u] + w(u,v) (Why?) So d[v] < d[u] + w(u,v). Contradiction.
15
Bellman-Ford
Prove: after |V|-1 passes, all d values correct Consider shortest path from s to v: s v1 v2 v3 v Initially, d[s] = 0 is correct, and doesnt change (Why?) After 1 pass through edges, d[v1] is correct (Why?) and doesnt change After 2 passes, d[v2] is correct and doesnt change Terminates in |V| - 1 passes: (Why?)
What if it doesnt?
16
Bellman-Ford Example
t 6 s
5 -2
0
7
3 7 2 4 9 z
17