5. 最短路問題 Shortest Path Problem
Problem Definition(1) Given : digraph G=(V, A) w:A →R (length w(a) of each arc a) W=(a 1, a 2, … a k ): a directed walk length of the walk W : w(W)=w(a 1 )+w(a 2 )+ … w(a k ) W
Problem Definition(2) v, v’ ∈ V distance between v and v’ mininum of all lengths of walk from v to v’ d(v, v’) = ( ∃ walk from v to v’) ∞ ( no walk from v to v’) v v’ d(v, v’)=4 shortest path from v to v’
Problem Definition(3) s v’ d(s, v’)=4 shortest path from s to v’ Given : digraph G=(V, A) w:A →R (length w(a) of each arc a) start vertex s (root of G) Find : shortest paths from s to other vertices
If G contains Negative cycles v v’ negative cycle : directed cycles of negative length there might be walks of arbitrary short length Assume that G does not contain any negative cycles.
Main Property d(v) := d(s, v) :distance between the start vertex s and v Lemma 5.1 d(s) = 0 d(v) = min{d(x) + w(x, v) | (x, v) ∈ A} ∀ v ∈ V - {s} Bellman’s equation v s
Lemma5.1(proof) ∀ v ∈ V - {s} (1) d(v) ≦ min{d(x) + w(x, v) | (x, v) ∈ A} (x, v) ∈ A, W(x): a walk from s to x of length d(x) W(x)+(x, v) is a walk from s to v (2) d(v) ≧ min{d(x) + w(x, v) | (x, v) ∈ A} W(v): a walk from s to x of length d(v) W(v) = (s, …, x’, v) W(v) - (x’, v) is a walk from s to x’ of length w(W(v))-w(x’, v)
Corollary ∀ v ∈ V - {s} d(x’)+w(x’, v) := min{d(x) + w(x, v) | (x, v) ∈ A} ⇒ there exists a shortest path from s to v containing a last arc (x’, v) A’ denotes a set of such an arc for each vertex v ∈ V - {s}. Then, (V, A’) is a spanning arborescence for G shortest path tree a unique path from s to each vertex v is a shoretst path from s to v
Shortest path tree s d(v)
When w(a)=1 ∀ a ∈ A Breadth first search label d satisfies Bellman’s equation breadth first search tree is a shortest path tree
When w(a) ≧ 0 ∀ a ∈ A Dijkstra algorithm begin set d(s)←0, d(v)←∞ for all v ∈ V- {s} and T←V; while T≠φ do begin find some u ∈ T such that d(u) is minimal; delete u from T; for v ∈ T∩Au do begin set d(v) ←min{d(v), d(u)+w(u, v) }; end end.
Dijkstra algorithm - example s 0 ∞ ∞ ∞ ∞ ∞ ∞ d(v) ∈T∈T : u sa, f ab, c bd ce, f da ea, d fe AvAv a b c d e f
Correctness Theorem 5.3 At the end of algorithm, d(v) is a distance from s to v for each v ∈ V.
Theorem5.3 (proof 1) d(t)<∞ ⇒ there exists a directed path of length d(t) from s to t. d(t) = ∞ ⇔ there exists no directed path from s to t. d(t) ≧ distance from s to t Show that d(t) ≦ distance from s to t using induction on the order in which vertices are removed from T. the first vertex removed from T = s d(s) = 0 ….. OK Assume that the inequality is true for all vertices t that were removed from T before u
Theorem5.3 (proof 2) P(u) = (sa 1 v 1 a 2 v 2 … v k-1 a k u) : shortest path from s to u Choose i to be the maximal index such that v i was removed from T before u. s a1a1 v1v1 a2a2 v2v2 vivi u akak P(v i ) by induction hypothesis, d(v i )=distance from s to v i = w(P(v i )) T In the next iteration, we remove u from T. Show that d(t) ≦ distance from s to t
Theorem5.3 (proof 3) s a1a1 v1v1 a2a2 v2v2 vivi u akak T s a1a1 v1v1 a2a2 v2v2 vivi u akak T when v i =v k-1 when v i ≠v k-1 v i+1 d(v i+1 ) ≦ d(v i )+w(a i+1 ) = distance from s to v i+1 ≦ distance from s to u Show that d(t) ≦ distance from s to t If d(u) > distance from s to u ⇒ d(v i+1 ) < d(u) contradict!
Think Think Think Modify Dijkstra algorithm such that it finds a shortest path form s to t as well not only a distance. That is, construct a shortest path tree. Solve a shortest path problem on an undirected graph. Solve a shortest path problem on a models with magnets and loops.
begin set d(s)←0, d(v)←∞ for all v ∈ V- {s} and T←V; set p(v) ←v for all v ∈ V; while T≠φ do begin find some u ∈ T such that d(u) is minimal; delete u from T; for v ∈ T∩Au do begin if (d(v) < d(u)+w(u, v)) do set d(v) ←d(u)+w(u, v) and p(v)←u; end end. Modified Dijkstra Algorithm
undirected -> directed
0 s magnets and loops model When you want to find a shortest path from s to v, why do you keep away v from s?
LP Finding a shortest path from s to t variable x :A→{0, 1} 1 ( when a shortest path contains an arc a) x(a) = 0 ( otherwise)
LP - dual