19
5. 最最最最最 Shortest Path Problem

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

Embed Size (px)

Citation preview

5. 最短路問題Shortest Path Problem

Problem Definition(1)Given : digraph G=(V, A) w:A →R (length w(a) of each arc a)

W=(a1, a2, … ak): a directed walklength of the walk W : w(W)=w(a1)+w(a2)+ … w(ak)

11

1 2

1

1

2

22 35

W

Problem Definition(2)

11

1 2

1

1

2

22 35

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’)=4shortest path from v to v’

Problem Definition(3)

11

1 2

1

1

2

22 35

s v’

d(s, v’)=4shortest 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

11

1 2

1

1

2

22 -55

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 Propertyd(v) := d(s, v) :distance between the start vertex s and v

Lemma 5.1d(s) = 0

d(v) = min{d(x) + w(x, v) | (x, v) A} v V - {s}∈ ∀ ∈

Bellman’s equation

vs

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

11

1 2

1

1

2

22 35

s

0 1

2

3 4

35

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 endend.

Dijkstra algorithm - example

11

1 2

1

1

2

22 35

s

0 ∞

∞∞

d(v)

∈T: u

s a, f

a b, c

b d

c e, f

d a

e a, d

f e

Av

a

b

c

d

e f

1 5

3

2

3

4

5

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) = (sa1v1a2v2… vk-1 ak u) : shortest path from s to uChoose i to be the maximal index such that vi was removed from T before u.

s

a1

v1

a2

v2

vi

u

ak

P(vi)

by induction hypothesis, d(vi)=distance from s to vi = w(P(vi))

T

In the next iteration, we remove u from T.

Show that d(t) distance from s to t≦

Theorem5.3 (proof 3)

s

a1

v1

a2v2

vi

u

ak

T

s

a1

v1

a2

v2

vi

uak

Twhen vi=vk-1when vi ≠vk-1

vi+1

d(vi+1) ≦ d(vi)+w(ai+1) = distance from s to vi+1

≦distance from s to u

Show that d(t) distance from s to t≦

If d(u) > distance from s to u ⇒ d(vi+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.

LPFinding 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)

minimize w(a)x(a)a∈A∑

subject to x(u,v) − x(w,u)=w:(w,u)∈A

∑v:(u,v)∈A

∑1 (u=s)

−1 (u=t)

0 (o.w.)

⎨ ⎪

⎩ ⎪

x(a)∈{0,1} (a∈ A)

minimize w(a)x(a)a∈A∑

subject to x(u,v) − x(w,u)=w:(w,u)∈A

∑v:(u,v)∈A

∑1 (u=s)

−1 (u=t)

0 (o.w.)

⎨ ⎪

⎩ ⎪

x(a)≥0 (a∈ A)

LP - dual

minimize w(a)x(a)a∈A∑

subject to x(u,v) − x(w,u)=w:(w,u)∈A

∑v:(u,v)∈A

∑1 (u=s)

−1 (u=t)

0 (o.w.)

⎨ ⎪

⎩ ⎪

x(a)≥0 (a∈ A)

maximized(t)−d(s)

subject tod(v)≤w(u,v)+d(u) ((u,v)∈ A)