Docsity

Dijkstra’s Algorithm-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Dijkstra’s Algorithm-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 35

Dijkstra’s Algorithm

-^ Given a graph G = (V, E) with a source vertex s,weight function w, edges are non-negative, i.e.,w(u, v)

≥^ 0,^ 

(u, v)^

^ E

-^ The graph is directed, i.e., if (u, v)

^ E then (v, u)

may or may not be in E.• The objective is to find shortest path from s to everyvertex u

Problem Statement  V.

Input Given graph G(V, E) with source s, weights wAssumption•^ Edges non-negative, w(u, v)

≥^ 0,^ ^

(u, v)^ 

E

-^ Directed, if (u, v)

^ E then (v, u) may be in E Objective: Find shortest paths from s to every u

^ V

Approach•^ Maintain a set S of vertices whose final shortest-pathweights from s have been determined•^ Repeatedly select, u

^ V – S with minimum shortest

Mathematical Statement of Problempath estimate, add u to S, relax all edges leaving u.• Greedy, always choose light vertex in V-S , add to S

Edge Relaxation

-^ Consider edge e = (u, z)such that• u is vertex most recentlyadded to the cloud S• z is not in the cloud•^ Relaxation of edge eupdates distance d(z) asd(z) =min {d(z), d(u) + weight(e)}

d ( z )^ ^75 d ( u )^ ^50

z

s^

u

d ( z )^ ^60 d ( u )^ ^50

z

s^

V- S e S eu S V - S

(^0) s^5

∞^

∞ 10

1 (^32) (^94 )

6 t^

x y^

z

Example: Dijkstra’s Algorithm ∞^

∞

For each vertex

v^ ^ V(G) d [ v ]^ ←

π[ v ]^ ←

NIL

Considering

s^ as root node d [ s ]^ 

S^  

st^ x yQ

z 0

s^5 t

∞ 10

1 2 3

(^94 )

6 t^

x y^

z (^100) 5

∞

x y Q

z

s^ is extracted form queue S^ ^ S^ 

{s} Adj [ s ] =^

t, y d [ t ] >^ d [ s ] +^ w

( s, t ) (∞^ > 0 + 10)^ d [ t ]^ ←

d [ s ] +^ w

( s, t ) 0 + 10 = 10 π[ t ]^ ←^ s d [ y ] >^ d [ s ] +

w ( s, y ) (∞^ > 0 + 5)^ d [ y ]^ ←

d [ s ] +^ w

( s, y ) 0 + 5 = 5 π[ y ]^ ←^ s

10^5

Example: Dijkstra’s Algorithm

(^10) s^5

1 2 3

(^94 )

6 t^

x y^

z 0

8

13 7 5

z^ is extracted form queue S^ ^ S^ 

{z} Adj [ z ] =

s, x d [ s ] >^ d [ z ] +

w ( s, z ) But (0 < 7 + 7) d [ x ] >^ d [

z ] +^ w ( z, x

) (14 > 7 + 6)^ d [ x ]^ ←

d [ z ] +^ w

( z, x ) 7 + 6 = 13 π[ x ]^ ←^ z

Example: Dijkstra’s Algorithm txQ 8 13

(^10) s^5

1 2 3

(^94 )

6 t^

x y^

z 0

8

9 7 5

t^ is extracted form queue S^ ^ S^ 

{t} Adj [ t ] =^

x, y d [ x ] >^ d [ t ] +^ w

( t, x ) (13 > 8 + 1)^ d [ x ]^ ←

d [ t ] +^ w

( t, x ) 8 + 1 = 9 π[ x ]^ ←^ t d [ y ] >^ d [ t ] +

w ( t, y ) But (5 < 8 + 3)

Example: Dijkstra’s Algorithm xQ 13

Cost depends on implementation of min-priority queueCase 1:Vertices being numbered 1 to

|V|

-^ INSERT, DECREASE-KEY operations takes

O(1)

-^ EXTRACT-MIN operation takes

O(V)^ time

-^ Sub cost is O(V

-^ Total number of edges in all adjacency list is |E| •^ Total Running time =

(^2) O (V+ E)

(^2) = O(V )

Analysis: Dijkstra’s Algorithm

Case 2:Graph is sufficiently spare, e.g.,

E = O (V

2 /lgV)

Implement min-priority queue with binary min heapVertices being numbered 1 to

|V|

-^ Each EXTRACT-MIN operation takes

O(lgV)

-^ There |V| operations, time to build min heap O(V)•^ Sub cost is O(V lgV) •^ Each DECREASE-KEY operation takes time O(lgV),and there are |E| such operation. •^ Sub cost is O(E lgV)^ Hence Total Running time =

O (V + E)

lgV =^

E^ lgV

Analysis: Dijkstra’s Algorithm

Case 1: Computation Time

1.^ INITIALIZE-SINGLE-SOURCE(V, s)2.^ S^

←^ 

3.^ Q^

←^ V[G]

4.^ while

Q^  

5.^

do^ u^ ←

EXTRACT-MIN(Q)

6.^

S^ ←^ S

^ {u}

7.^

for^ each vertex v

^ Adj[u]

8.^

do^ RELAX(u, v, w) Running time: O(V

2 + E) = O((V

Note:Running time depends on Impl. Of min-priority (Q)

(V)

O(V) build min-heap

O(V)

O(V) O(E)

Case 2 : Binary min Heap

1.^ INITIALIZE-SINGLE-SOURCE(V, s)2.^ S^

←^ 

3.^ Q^

←^ V[G]

4.^ while

Q^  

5.^

do^ u^ ←

EXTRACT-MIN(Q)

6.^

S^ ←^ S

^ {u}

7.^

for^ each vertex v

^ Adj[u]

8.^

do^ RELAX(u, v, w)

9.^ Running time: O(VlgV + ElgV) = O(ElgV)

(V)

O(V) build min-heap

Executed O(V) times

O(lgV) O(E) times O(lgV)

Dijkstra’s algorithm, runs on a weighted, directedgraph^

G = (V, E)

with non-negative weight function

w^ and source

s,^ terminates with

d[u] =

δ(s, u)

for all vertices

u^ ^ V.

Proof•^ We use the following loop invariant:– At start of each iteration of the

while^

loop of

lines 4-8,

d [ v ] =^

δ ( s ,^ v ) for each vertex

v^ ^ S

Theorem : Correctness of Dijkstra’s Algorithm

-^ It suffices to show for each vertex

u^ ^ V

, we have

d [ u ] =^ δ

( s ,^ u ) at the time when

u^ is added to set

S.

-^ Once we show that

d [ u ] =^

δ ( s ,^ u ), we rely on the

upper-bound property to show that the equalityholds at all times thereafter.Initialization

-^ Initially,

S^ = Ø, and so the invariant is trivially true

Maintenance

-^ We wish to show that in each iteration,^ d

[ u ] =^ δ (

s ,^ u ), for the vertex added to set

S.

Contd..