Lecture 6 Shortest Path Problem. s t Dynamic Programming.

76
Lecture 6 Shortest Path Problem

Transcript of Lecture 6 Shortest Path Problem. s t Dynamic Programming.

Lecture 6

Shortest Path Problem

s t

j).(i, arc ofcost theis ijc

Dynamic Programming

Dynamic Programming

})(*{min )(*

Then . node to nodeorigin from

pathshortest oflength thedenote )(*Let

)(vu

uNvcvdud

us

ud

Dijkstra’s Algorithm is motivated from a way to implement of this dynamic programming.

Dynamic Programming

. if Stop

};{

}{

)},()(*{min)(* compute

.)(: find iteration,each In

.},{ Initially,

. to frompath shortest theoflength the)(* Define

)(

T

uTT

uSS

uvcvdud

SuNTu

SVTsS

usud

uNv

Lemma

S.(z)such that find ll we'Finally, forever. go

cannot process This .such that )(exist there

then,)( If .such that )(exist there

then ,)( If .)(,any for that Note

.)(such that exists Then there

. with ofpartition a is ),( Suppose . nodesink a and

node source a with ),(network acyclican Consider

NTz

TwvNw

SvNTvuNv

SuNuNTu

SuNTu

SsVTSt

sEVG

Proof

Theorem

weight).negative (even with

network acyclicany on worksgprogrammin Dynamic

2 -1

-1

2 1

Counterexample

cycle. a

th network wi ain not work may gprogrammin Dynamic

Smart Implementation

).()(*)( :markRe

. if Stop

;for )( update

}{

}{

.)(: find iteration,each In

.},{ Initially,

.for )},()(*{min)( Define)(

ududSuN

T

Twwd

uTT

uSS

SuNTu

SVTsS

TuuvcvdudSuNv

An Example

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

Initialize

1

0

Select the node with the minimum temporary distance label.

Update Step

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

1

Choose u such that N_(u) S

1

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

2

Update Step

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

6

43

0

The predecessor of node 3 is now node 2

Choose u Such That N_(u) S

1

2 4

5

6

2

4

2 1

3

4

2

3

2

2

3

6

4

0

3

Update

1

2 4

5

6

2

4

2 1

3

4

2

3

2

0

d(5) is not changed.

3

2

3

6

4

Choose u s.t . N_(u) S

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

Update

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

d(4) is not changed

6

Choose u s.t. N_(u) S

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

Update

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

d(6) is not updated

Choose u s.t. N_(u) S

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

There is nothing to update

End of Algorithm

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

All nodes are now permanent

The predecessors form a tree

The shortest path from node 1 to node 6 can be found by tracing back predecessors

Dijkstra’s Algorithm

Dijkstra’s Algorithm

. if Stop

;for )( update

}{

}{

).(min)(: find iteration,each In

.},{ Initially,

.for )},()(*{min)( Define)(

T

Twwd

uTT

uSS

wdudTu

SVTsS

Tuuvcvdud

Tw

SuNv

Lemma

).()(*)( min)(

then e,nonnegativ are weights-arc If

. with ofpartition a is ),( Suppose . nodesink a and

node source a with ),(network aConsider

ududwdud

SsVTSt

sEVG

Tw

Lemma

).()(*)( min)(

then e,nonnegativ are weights-arc If

. with ofpartition a is ),( Suppose . nodesink a and

node source a with ),(network aConsider

ududwdud

SsVTSt

sEVG

Tw

Proof of Lemma

)(

)()(*)()()),(()(

e,nonnegativ are weights-arc all Since

. to from path of piece theis ),( where

)),(()(

Then .path on in nodefirst thebe Let

).()(*)(

such that to from path a exists Then there

).(*)(min)( suppose ion,contradictFor

plengthududwdwsplengthplength

wspwsp

wsplengthwd

pTw

ududplength

usp

udvdudTv

s

uw

S

T

Theorem

weights.-arc enonnegativ

th network wiany on worksAlgorithm sDijkstra'

Counterexample

3 -1

-2

2 1

weight.-arc negative

th network wi ain not work may algorithm sDijkstra'

Dijkstra’s Algorithm

An Example

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

Initialize

1

0

Select the node with the minimum temporary distance label.

Update Step

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

1

Choose Minimum Temporary Label

1

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

2

Update Step

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

6

43

0

The predecessor of node 3 is now node 2

Choose Minimum Temporary Label

1

2 4

5

6

2

4

2 1

3

4

2

3

2

2

3

6

4

0

3

Update

1

2 4

5

6

2

4

2 1

3

4

2

3

2

0

d(5) is not changed.

3

2

3

6

4

Choose Minimum Temporary Label

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

Update

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

d(4) is not changed

6

Choose Minimum Temporary Label

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

Update

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

d(6) is not updated

Choose Minimum Temporary Label

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

There is nothing to update

End of Algorithm

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

All nodes are now permanent

The predecessors form a tree

The shortest path from node 1 to node 6 can be found by tracing back predecessors

Dijkstra’s Algorithm with simple buckets

(also known as Dial’s algorithm)

An Example

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

Initialize distance labels

1

0

Select the node with the minimum temporary distance label.

0 1 2 3 4 5 6 7

1

23456

Initialize buckets.

Update Step

0 1 2 3 4 5 6 7

1

23456

2 3

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

1

Choose Minimum Temporary Label

0 1 2 3 4 5 6 7

4562 3

Find Min by starting at the leftmost bucket and scanning right till there is a non-empty bucket.

1

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

0

2

Update Step

1

2

3

4

5

6

2

4

2 1

3

4

2

3

2

2

4

6

43

0

0 1 2 3 4 5 6 7

4562 33 4

5

Choose Minimum Temporary Label

1

2 4

5

6

2

4

2 1

3

4

2

3

2

2

3

6

4

0

3

0 1 2 3 4 5 6 7

63 45

Find Min by starting at the leftmost bucket and scanning right till there is a non-empty bucket.

Update

1

2 4

5

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

0 1 2 3 4 5 6 7

63 45

Choose Minimum Temporary Label

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

0 1 2 3 4 5 6 7

645

Update

1

2 4

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

0 1 2 3 4 5 6 7

6456

Choose Minimum Temporary Label

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

0 1 2 3 4 5 6 7

46

Update

1

2

6

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

0 1 2 3 4 5 6 7

46

Choose Minimum Temporary Label

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

0 1 2 3 4 5 6 7

6

There is nothing to update

End of Algorithm

1

2

2

4

2 1

3

4

2

3

2

0

3

2

3

6

4

5

6

4

6

All nodes are now permanent

The predecessors form a tree

The shortest path from node 1 to node 6 can be found by tracing back predecessors

Implementations

• With min-priority queue, Dijkstra algorithm can be implemented in time

• With Fibonacci heap, Dijkstra algorithm can be implemented in time

• With Radix heap, Dijkstra algorithm can be implemented in time

).lg)(( nnmO

).lg( cnmO

).lg( nnmO