48
Ch17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design Jiraporn Pooksook Naresuan University

Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Ch17: Single Source Shortest

PathPath

305233, 305234

Algorithm Analysis and Design

Jiraporn PooksookNaresuan University

Page 2: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

• A motorist wishes to find the shortest possible

route from Chicago to Boston.

• Given a road map of the US on which the

distance between each pair of adjacent distance between each pair of adjacent

intersections is marked.

• How can we determine the shortest route?

Page 3: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

• Given a weighted directed graph G =(V,E) with weight function

w : E -> R

• Mapping edges to read valued weights.

• Let path p = (v0 , v1 ,…, vk ) and (vi , vi+1 ) ϵ E for 0 ≤ i < k• Let path p = (v0 , v1 ,…, vk ) and (vi , vi+1 ) ϵ E for 0 ≤ i < k

• The weight of path p = (v0 , v1 ,…, vk ) is the sum of the weights of its constituent edges:

• We define the shortest-path weight from u to v by

If there is a path from u to v , otherwise

∑=

−=

k

iii

vvwpw1

1),()(

}:)(min{),( vupwvup

→=δ

∞=),( vuδ

Page 4: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

0

s

a c

e1

5 3

2

0

b d

f2

1

1

31

1

42

d(u)=current weight

Page 5: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

0

s

1

a c

e1

5 3

2

0

2

b d

f2

1

1

31

1

42

d(u)=current weight

Page 6: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

0

s

1

a

6

c

3 e1

5 3

2

0

2

b d

f2

1

1

31

1

42

d(u)=current weight

Can we find a shorter path??6),( =csδ

Page 7: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest Path Problem

0

s

1

a

5

c

3 e1

5 3

2

0

2

b d

f2

1

1

31

1

42

d(u)=current weight

5),( =csδ

Page 8: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Representing Shortest Path

• Given a graph G=(V,E)

• For each vertex v ϵ V, a predecessor ¶[v] that

is either another vertex or NIL.

• We denote d(v) as a value inside a • We denote d(v) as a value inside a

circle(graph) to be a current weight.

• We denote ¶[v], for any vertex v, as a

predecessor on the current best path to v.

• ¶[s]=NIL

Page 9: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Representing Short Path

0

s

1

a

6

c

3 e1

5 3

2

0

2

b d

f2

1

1

31

1

42

¶[e] = a

¶[a] = s

Page 10: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Negative-weight Edges

• There may be edges whose weights are

negative.

• If there is a negative-weight cycle reachable

from s, shortest-path weights are not well from s, shortest-path weights are not well

defined.

• If there is a negative-weight cycle on some

path from s to v, we define −∞=),( vsδ

Page 11: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Negative-weight Edges

3

a

-1

b

c dgs

3

-4

6

5 8

4

5

c

11

d

-ꚙ

e

-ꚙ

f

-ꚙ0

-3

3

-6

5

2

8

7

Page 12: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Negative-weight Edges

h

i

s

2

-8 3

j

0 -8 3

Page 13: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

General Structure of Shortest Path

• Initialize single source

– For u ϵ V, we set d[v] = ꚙ, ¶[u] = NIL and d[s] = 0

• Relaxation

– Repeatedly select edge(u,v) and relax(u,v) by – Repeatedly select edge(u,v) and relax(u,v) by checking the condition:

if d[v] > d[u] + w(u,v)

then d[v] = d[u] + w(u,v)

¶[v] = u

Page 14: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Initialize-Single-Source(G,s)

for each vertex v in V[G]

do d[v] = ꚙ

¶[v] = NIL

d[s] = 0

Page 15: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Initialize-Single-Source(G,s)

0

s

a

c

ꚙ e1

5 3

2

0

b

d

ꚙf

2

1

1

31

1

42

d(u)=current weight

Page 16: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Relaxation(u,v,w)

if d[v] > d[u] + w(u,v)

then d[v] = d[u] + w(u,v)

¶[v] = u

Page 17: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Relaxation(d, c, w(d,c) )

0

s

1

a

6

c

3 e1

5 3

2

0

2

b

3

d

f2

1

1

31

1

42

d[c] > d[d] + w(d,c)

6 > 3 + 2

Hence, d[c] = 5 and ¶[c] = d

Page 18: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Shortest path in Directed Acyclic

Graphs

• We can compute shortest paths from a single

source in O(V+E) time using relaxation on

edges of a weighted directed acyclic

graph(dag).graph(dag).

Page 19: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

DAG-SHORTEST-PATHS(G,w,s)

topologically sort the vertices of G

INITIALIZE-SINGLE-SOURCE(G,s)

for each vertex u, taken in topologically sorted order

do for each vertex v ϵ Adj[u]

do RELAX(u, v, w)

do for each vertex v ϵ Adj[u]

do RELAX(u, v, w)

Page 20: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

t

x

y

z5

6

2 7 -1 -2ꚙ 0 ꚙ ꚙ ꚙ ꚙ

5 2 7 -1 -2

3 4

2

Page 21: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

t

x

y

z5

6

2 7 -1 -2ꚙ 0 ꚙ ꚙ ꚙ ꚙ

5 2 7 -1 -2

3 4

2

Page 22: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

2

t

6

x

y

z5

6

2 7 -1 -2ꚙ 0 2 6 ꚙ ꚙ

5 2 7 -1 -2

3 4

2

Page 23: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

2

t

6

x

6

y

4

z5

6

2 7 -1 -2ꚙ 0 2 6 6 4

5 2 7 -1 -2

3 4

2

Page 24: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

2

t

6

x

5

y

4

z5

6

2 7 -1 -2ꚙ 0 2 6 5 4

5 2 7 -1 -2

3 4

2

Page 25: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

2

t

6

x

5

y

3

z5

6

2 7 -1 -2ꚙ 0 2 6 5 3

5 2 7 -1 -2

3 4

2

Page 26: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: DAG

r

0

s

2

t

6

x

5

y

3

z5

6

2 7 -1 -2ꚙ 0 2 6 5 3

5 2 7 -1 -2

3 4

2

Page 27: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Dijkstra Algorithm

• Solves the single-source shortest-paths

problem on a weighted directed graph G =

(V,E) for the case in which all edge weights are

nonnegative.nonnegative.

• We assume that w(u,v) ≥ 0 for each edge (u,v)

ϵ E.

Page 28: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Dijkstra(G,w,s)

INITIALIZE-SINGLE-SOURCE(G,s)

S =

Q = V[G]

while Q !=

do u = EXTRACT-MIN(Q)

do u = EXTRACT-MIN(Q)

S = S ꓴ {u}

for each vertex v ϵ Adj[u]

do RELAX(u,v,w)

Page 29: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Analyze Dijkstra

• The running time of Dijkstra depends on how

to implement the min-priority queue.

• If we implement the min-priority queue with a

binary min-heap which has running time binary min-heap which has running time

O(lg V) if all vertices are reachable from the

source. Hence total time is O((V+E)lg V)

= O( E lg V)

Page 30: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

t

x

10

1

6

923

0s

y

z

4 6

2

7

23

5

S ={ }

Q = {0, ꚙ, ꚙ, ꚙ, ꚙ}

Page 31: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

10

t

x

10

1

6

923

0s

5

y

z

4 6

2

7

23

5

S ={ s }

Q = {0, 10, 5, ꚙ, ꚙ}

Page 32: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

8

t

14

x

10

1

6

923

0s

5

y

7

z

4 6

2

7

23

5

S ={ s , y }

Q = {0, 8, 5, 14, 7}

Page 33: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

8

t

13

x

10

1

6

923

0s

5

y

7

z

4 6

2

7

23

5

S ={ s , y, z }

Q = {0, 8, 5, 13, 7}

Page 34: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

8

t

9

x

10

1

6

923

0s

5

y

7

z

4 6

2

7

23

5

S ={ s ,t, y, z }

Q = {0, 8, 5, 9, 7}

Page 35: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Dijkstra

0s

8

t

9

x

10

1

6

923

0s

5

y

7

z

4 6

2

7

23

5

S ={ s ,t,x, y, z }

Q = {0, 8, 5, 9, 7}

Page 36: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Bellman-Ford Algorithm

• Solves the single-source shortest-paths problem in general case in which edge weights may be

negative.

• The Bellman-Ford algorithm returns a booleanvalue indicating whether or not there is a value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortespaths and their weights.

Page 37: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Bellman-Ford(G,w,s)

INITIALIZE-SINGLE-SOURCE(G,s)

for i =1 to |V[G]| -1

do for each edge (u,v) ϵ E[G]

do RELAX(u, v , w)

for each edge (u,v) ϵ E[G]for each edge (u,v) ϵ E[G]

do if d[v] > d[u] + w(u,v)

then return false

return true

Page 38: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Analyze Bellman-Ford

• The running time is O(VE) since the

initialization take O(V) and each of |V|-1

passes over edges in lines 2-4 takes O(E), and

for loop in lines 5-7 takes O(E) time.for loop in lines 5-7 takes O(E) time.

Page 39: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s ꚙ

x

z2 2

0s ꚙ ꚙ

y

2 2

Page 40: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s 2

x

4

z2 2

0s 2 4

4

y

2 2

Page 41: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s ꚙ

x

y-2 -2

0s ꚙ ꚙ

z

-2 -2

Page 42: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s -2

x

-4

y-2 -2

-4 > -8 + -2

Then

d[y] = -10

0s -2 -4

-6

z

-2 -2

RETURN false !!!

-2 > -6 + -2

Then

d[x] = -8

Page 43: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s

t

x

6

-2

-3

8

5

0s

y

z

7-4

9

2

8

7

Page 44: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s

6

t

x

6

-2

-3

8

5

0s

7

y

z

7-4

9

2

8

7

Page 45: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s

6

t

4

x

6

-2

-3

8

5

0s

7

y

2

z

7-4

9

2

8

7

Page 46: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s

2

t

4

x

6

-2

-3

8

5

0s

7

y

2

z

7-4

9

2

8

7

Page 47: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Example: Bellman-Ford

0s

2

t

4

x

6

-2

-3

8

5

0s

7

y

-2

z

7-4

9

2

8

7

RETURN true

With shortest path 0, 2, 4, 7, -2

Page 48: Ch17: Single Source Shortest Path - มหาวิทยาลัยนเรศวรCh17: Single Source Shortest Path 305233, 305234 Algorithm Analysis and Design JirapornPooksook

Negative weighted cycle

0

s3 2 3 3 2

-6

v

03 2

The shortest simple path to reach v from s = 13

If we have negative edge cycle in a path , it takes

exponential running time to solve.