0% found this document useful (0 votes)
4 views33 pages

DIjkstra's Algorithm

Dijkstra's Algorithm is designed for finding the shortest paths in a weighted, directed graph with nonnegative edge weights. The algorithm initializes a source vertex and iteratively extracts the minimum distance vertex, updating the distances of its adjacent vertices. It is more efficient than the Bellman-Ford algorithm in terms of running time.

Uploaded by

meghdoshi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

DIjkstra's Algorithm

Dijkstra's Algorithm is designed for finding the shortest paths in a weighted, directed graph with nonnegative edge weights. The algorithm initializes a source vertex and iteratively extracts the minimum distance vertex, updating the distances of its adjacent vertices. It is more efficient than the Bellman-Ford algorithm in terms of running time.

Uploaded by

meghdoshi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Dijkstra’s Algorithm

Dijkstra’s Algorithm
 On weighted, directed graph G=(V,E)
for which all edge weights are
nonnegative.
 The running time of Dijkstra’s algorithm
is lower than that of the Bellman-Ford
algorithm.
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
5. u = Extract-Min(Q)
6. S = S U {u}
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)

𝑡 𝑥
1
1  
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
5. u = Extract-Min(Q)
6. S = S U {u}
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)

𝑡 𝑥
1
1  
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S=
5. u = Extract-Min(Q)
6. S = S U {u}
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)

𝑡 𝑥
1
1  
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S=
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒔 𝒕 𝒙 𝒚 𝒛
7. for each vertex v  G.Adj[u] d 0    
8. RELAX(u,v,w)

𝑡 𝑥
1
1  
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒚 𝒛
7. for each vertex v  G.Adj[u] d    
8. RELAX(u,v,w)
u=𝑠
G.adj[𝑠] = {𝑡, 𝑦}

𝑡 𝑥
1
1  
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒚 𝒛
7. for each vertex v  G.Adj[u] d 10   
8. RELAX(u,v,w)
u=𝑠
G.adj[𝑠] = {𝑡, 𝑦}

𝑡 𝑥
1
1 10 
0
9
𝑠 0 2 3 4 6
7
5
 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒚 𝒛
7. for each vertex v  G.Adj[u] d 10  5 
8. RELAX(u,v,w)
u=𝑠
G.adj[𝑠] = {𝑡, 𝑦}

𝑡 𝑥
1
1 10 
0
9
𝑠 0 2 3 4 6
7
5
5 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒛
7. for each vertex v  G.Adj[u] d 10  
8. RELAX(u,v,w)
u=𝑦
G.adj[𝑦] = {𝑡, 𝑥, 𝑧}

𝑡 𝑥
1
1 10 
0
9
𝑠 0 2 3 4 6
7
5
5 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒛
7. for each vertex v  G.Adj[u] d 8  
8. RELAX(u,v,w)
u=𝑦
G.adj[𝑦] = {𝑡, 𝑥, 𝑧}

𝑡 𝑥
1
1 8 
0
9
𝑠 0 2 3 4 6
7
5
5 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒛
7. for each vertex v  G.Adj[u] d 8 14 
8. RELAX(u,v,w)
u=𝑦
G.adj[𝑦] = {𝑡, 𝑥, 𝑧}

𝑡 𝑥
1
1 8 14
0
9
𝑠 0 2 3 4 6
7
5
5 
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙 𝒛
7. for each vertex v  G.Adj[u] d 8 14 7
8. RELAX(u,v,w)
u=𝑦
G.adj[𝑦] = {𝑡, 𝑥, 𝑧}

𝑡 𝑥
1
1 8 14
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙
7. for each vertex v  G.Adj[u] d 8 14
8. RELAX(u,v,w)
u=𝑧
G.adj[𝑧] = {𝑥, s}

𝑡 𝑥
1
1 8 14
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙
7. for each vertex v  G.Adj[u] d 8 13
8. RELAX(u,v,w)
u=𝑧
G.adj[𝑧] = {𝑥, s}

𝑡 𝑥
1
1 8 13
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒕 𝒙
7. for each vertex v  G.Adj[u] d 8 13
8. RELAX(u,v,w)
u=𝑧
G.adj[𝑧] = {𝑥, s}

𝑡 𝑥
1
1 8 13
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒙
7. for each vertex v  G.Adj[u] d 13
8. RELAX(u,v,w)
u=𝑡
G.adj[𝑡] = {𝑥, 𝑦}

𝑡 𝑥
1
1 8 13
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒙
7. for each vertex v  G.Adj[u] d 9
8. RELAX(u,v,w)
u=𝑡
G.adj[𝑡] = {𝑥, 𝑦}

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡}
5. u = Extract-Min(Q)
6. S = S U {u} Q G.V 𝒙
7. for each vertex v  G.Adj[u] d 9
8. RELAX(u,v,w)
u=𝑡
G.adj[𝑡] = {𝑥, 𝑦}

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡, 𝑥}
5. u = Extract-Min(Q)
6. S = S U {u} Q=
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)
u=𝑥
G.adj[𝑥] = {𝑧}

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡, 𝑥}
5. u = Extract-Min(Q)
6. S = S U {u} Q=
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)
u=𝑥
G.adj[𝑥] = {𝑧}

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡, 𝑥}
5. u = Extract-Min(Q)
6. S = S U {u} Q=
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
S={𝑠, 𝑦, 𝑧, 𝑡, 𝑥}
5. u = Extract-Min(Q)
6. S = S U {u}
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)

𝑡 𝑥
1
1 8 9
0
9
𝑠 0 2 3 4 6
7
5
5 7
2 𝑧
𝑦
Running Time of Dijkstra’s Algorithm
 It depends on implementations of the min-priority queue Q.
 If we implement Q as a binary min-heap,
 EXTRACT-MIN takes O(lg |V|) time.
 DECREASE-KEY takes O(lg |V|) time.
 If we implement Q as a simple array,
 EXTRACT-MIN takes O(|V|) time.
 DECREASE-KEY O(1) time.
 If we implement Q as a Fibonacci heap,
 EXTRACT-MIN takes O(lg |V|) amortized time.
 DECREASE-KEY O(1) amortized time.
Dijkstra’s Algorithm
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S=
3. Q = G.V
4. while Q  
5. u = Extract-Min(Q)
6. S = S U {u}
7. for each vertex v  G.Adj[u]
8. RELAX(u,v,w)
Dijkstra’s Algorithm
 min-priority queue : array
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s) O(|V|)
2. S= O(1)
3. Q = G.V O(|V|)
4. while Q  
5. u = Extract-Min(Q) O(|V|2)
6. S = S U {u} O(|V|)
7. for each vertex v  G.Adj[u] O(|E|)
8. RELAX(u,v,w)

Dijkstra’s algorithm running time is O(|V|2)


Dijkstra’s Algorithm
 min-priority queue : binary min-heap
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s) O(|V|)
2. S= O(1)
3. Q = G.V O(|V|)
4. while Q  
5. u = Extract-Min(Q) O(|V| lg |V|)

6. S = S U {u} O(|V|)
7. for each vertex v  G.Adj[u] O(|E| lg |V|)
8. RELAX(u,v,w)
 Running time:
 O((|V|+|E|) lg |V|) , if all vertices are reachable O(|E| lg |V|).
 Better than O(|V|2), if the graph is sufficiently sparse: |E|= o( |V|2 / lg |V|).
Dijkstra’s Algorithm
 min-priority queue : Fibonacci heap
DIJKSTRA(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s) O(|V|)
2. S= O(1)
3. Q = G.V O(|V|)
4. while Q  
5. u = Extract-Min(Q) O(|V| lg |V|)

6. S = S U {u} O(|V|)
7. for each vertex v  G.Adj[u] O(|E|)
8. RELAX(u,v,w)

Dijkstra’s algorithm running time is O(|V| lg |V|+ |E|)


Theorem 24.6 (Correctness of Dijkstra’s
Algorithm)
 Dijkstra’s algorithm, run on a weighted, directed graph G=(V,E)
with non-negative weight function w and source s, terminates
with u.d=δ(s,u) for all vertices u∈V.
Theorem 24.6 (Proof)
 Loop invariant
 At the start of each iteration of the while loop of lines 4-8,
v.d= δ (s,v) for each vertex v in S
 Initialization: S={}, so true
 Maintenance:
 Let u be the first vertex for which u.d ≠ δ(s,u) when it is added to
set S
 u ≠ s because s is the first vertex added to set S and s.d= δ(s,s)=0
 Because u ≠ s, we also have that S ≠ {} just before u is added to S
 There must be some path from s to u, for otherwise u.d=δ (s,u)=
∞ by no-path property
 There is a shortest path p from s to u
 Prior to adding u to S, path p connects a vertex in S, namely s, to a
vertex in V-S, namely u.
Theorem 24.6 (Proof)
 Let us consider the first vertex y along p such that y ∈ V-S, and let x ∈ S be y’s
predecessor along p
 Figure shown below illustrates, we can decompose path p into s↝x → y↝u (s↝x: p1 ,
y↝u: p2)
 Claim: y.d=δ(s,y) when u is added to S
 x.d=δ(s,x) when x was added to s
(∵ we chose u as the first vertex for which u.d≠δ(s,u))
 Edge (x,y) was relaxed at that time, and the claim follows from the convergence property

p2 u
s S
y
p1 x
Theorem 24.6 (Proof)
 We can now obtain a contradiction to prove u.d= δ(s,u)
 δ(s,y)≤δ(s,u) (∵ y appears before u on a shortest path from
s to u and all edge weights are non-negative)
 y.d=δ(s,y)≤δ(s,u)≤u.d (by the upper-bound property)
 But because both vertices u and y were in V-S when u was
chosen in line 5, u.d≤y.d
 y.d=δ(s,y)=δ(s,u)=u.d
 Consequently u.d=δ(s,u), which contradicts our choice of u.
 u.d=δ(s,u) when u is added to S, and that this equality
is maintained at all times thereafter
 Termination : At termination, Q={} which, along
with our earlier invariant that Q=V-S, implies that
S=V. u.d=δ(s,u) for all vertices u ∈V
Shortest-Path Algorithms

Bellman-Ford Dijkstra

Negative Edge O X

Positive Cycle O O

Negative Cycle X X

Time Complexity O(|V||E|) Array: O(|V|2)


Min-heap: O((|V|+|E|)lg|V|)
Fibonacci heap: O(|V|lg|V|+|E|)

You might also like