0% found this document useful (0 votes)
40 views

CS341 Lec09 Annotated Feb27

Uploaded by

artem
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)
40 views

CS341 Lec09 Annotated Feb27

Uploaded by

artem
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/ 53

CS 341: Algorithms

Lec 09: Dijkstra’s Algorithms

Armin Jamshidpey Yang Lu

David R. Cheriton School of Computer Science, University of Waterloo

Winter 2024

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 1 / 15


Preliminaries

vk
w(vk−1 , vk )
G = (V, E) a directed graph
with a weight function:
w(v2 , v3 )
w:E→R
v2
The weight of path w(v1 , v2 )
P = ⟨v0 ,P
. . . , vk ⟩ is:
w(P ) = ki=1 w(vi−1 , vi ) w(v0 , v1 ) v1
v0

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 2 / 15


Preliminaries

Padlet (True/False)
Shortest path exists in any directed weighted graph.
https://padlet.com/arminjamshidpey/CS341

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 3 / 15


Preliminaries

Padlet (True/False)
Shortest path exists in any directed weighted graph.
https://padlet.com/arminjamshidpey/CS341

Assumption: G has no negative-weight cycles

The shortest path weight from u to v:


(
P
min{w(P ) : u ⇝ v} if there exists a path from u to v
δ(u, v) =
∞ otherwise

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 3 / 15


Preliminaries
Single-Source Shortest Path Problem
Input: G = (V, E), w : E → R and a source s ∈ V

Output: A shortest path from s to each v ∈ V

True/False: If ⟨v0 , v1 , . . . , vk ⟩ is a shortest path from v0 to vk , then ⟨v0 , v1 , . . . , vi ⟩


is a shortest path from v0 to vi , for any 0 ≤ i ≤ k.
vi
vk
v0

Padlet: https://padlet.com/arminjamshidpey/CS341
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 4 / 15
Overview

Explanation of Dijkstra’s algorithm

Pseudocode of the algorithm

An example
E. Dijkstra (1930-2002)
Complexity analysis Turing Award (1972)

Proof of correctness “Computer Science is no more about


computers than astronomy is about
telescopes.”
-E. Dijkstra
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 5 / 15
Dijkstra’s algorithm: Explanation

Dijkstra’s algorithm is a greedy algorithm


v
Input: A weighted directed graph with
non-negative edge weights

For all vertices, maintain quantities



s
d[v]: a shortest-path estimate from s to v
▶ π[v]: predecessor in the path (a vertex or NIL)

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 6 / 15


Dijkstra’s algorithm: Explanation

Dijkstra’s algorithm is a greedy algorithm π[v]

v
Input: A weighted directed graph with d[v]
non-negative edge weights

For all vertices, maintain quantities


▶ d[v]: a shortest-path estimate from s to v s
▶ π[v]: predecessor in the path (a vertex or NIL)

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 6 / 15


Dijkstra’s algorithm: Explanation

Initialize C = ∅, repeat the following until


C =V: d[v] v
▶ Add u ∈ V − C with smallest d-value to C w(u, v)
▶ Update d-values of vertices v with (u, v) ∈ E: u
d[v] ← min{d[v], d[u] + w(u, v)}
▶ Update π[v] if d[v] is changed s d[u]

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 7 / 15


Dijkstra’s algorithm: Explanation

Which Abstract Data Type (ADT) should we use for vertices?

Padlet: https://padlet.com/arminjamshidpey/CS341

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 8 / 15


Dijkstra’s algorithm: Explanation

Which Abstract Data Type (ADT) should we use for vertices?

Padlet: https://padlet.com/arminjamshidpey/CS341

12 15

23 21 17 16

53 41 22

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 8 / 15


Dijkstra’s algorithm: Explanation

Which Abstract Data Type (ADT) should we use for vertices?

Padlet: https://padlet.com/arminjamshidpey/CS341

5
Cost of operations of a
binary min-heap
12 15 (of size n):
Insert: O(log n)
23 21 17 16 Extract-Min: O(log n)
Update-Key: O(log n)
53 41 22

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 8 / 15


Dijkstra’s algorithm: Pseudocode
Dijkstra(G, w, s)
1 for each vertex v ∈ V [G]
2 d[v] ← ∞
3 π[v] ← NIL
4 d[s] ← 0

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 9 / 15


Dijkstra’s algorithm: Pseudocode
Dijkstra(G, w, s)
1 for each vertex v ∈ V [G]
2 d[v] ← ∞
3 π[v] ← NIL
4 d[s] ← 0
5 C←∅
6 Q ← V [G]

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 9 / 15


Dijkstra’s algorithm: Pseudocode
Dijkstra(G, w, s)
1 for each vertex v ∈ V [G]
2 d[v] ← ∞
3 π[v] ← NIL
4 d[s] ← 0
5 C←∅
6 Q ← V [G]
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u}

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 9 / 15


Dijkstra’s algorithm: Pseudocode
Dijkstra(G, w, s)
1 for each vertex v ∈ V [G]
2 d[v] ← ∞
3 π[v] ← NIL
4 d[s] ← 0
5 C←∅
6 Q ← V [G]
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u}
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v)
13 π[v] ← u

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 9 / 15


Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a
1 for each vertex v ∈ V [G] 10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0
5 C←∅ 9
6 Q ← V [G] 5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q) c
d
9 C ← C ∪ {u} 2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a
∞ NIL
1 for each vertex v ∈ V [G] 10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL

5 C←∅ 9
6 Q ← V [G] 5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q) c
d
9 C ← C ∪ {u} ∞ NIL 2 ∞ NIL
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
∞ NIL Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} ∞ NIL ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
∞ NIL Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} ∞ NIL ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
∞ NIL Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} ∞ NIL ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
10s Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
10s Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
10s Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
10 S Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL ∞ NIL
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s ∞ NIL
2
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 14 d
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 14 d
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 14 d
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 14 d
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 13 c
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) a C
8 d Q
1 for each vertex v ∈ V [G]
10 1
2 d[v] ← ∞
3 π[v] ← NIL 2
s 3 b
4 d[s] ← 0 0 NIL 13 c
5 C←∅
9
6 Q ← V [G] 5 4 6
7
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
d c
9 C ← C ∪ {u} 5 s 7
2 d
10 for each vertex v ∈ Adj[u]
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
5 C←∅ 0 NIL 13 c
6 Q ← V [G] 9
5 4 6
7 while Q ̸= ∅ 7
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
5 C←∅ 0 NIL 13 c
6 Q ← V [G] 9
5 4 6
7 while Q ̸= ∅ 7
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
5 C←∅ 0 NIL 9 a
6 Q ← V [G] 9
5 4 6
7 while Q ̸= ∅ 7
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
5 C←∅ 0 NIL 9 a
6 Q ← V [G] 9
5 4 6
7 while Q ̸= ∅ 7
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
0 9 a
5 C←∅ NIL

6 Q ← V [G] 9
5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
0 9 a
5 C←∅ NIL

6 Q ← V [G] 9
5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
0 9 a
5 C←∅ NIL

6 Q ← V [G] 9
5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Example

Dijkstra(G, w, s) C
a
1 for each vertex v ∈ V [G] 8 d Q
2 d[v] ← ∞ 10 1
3 π[v] ← NIL
2
4 d[s] ← 0 s 3 b
0 9 a
5 C←∅ NIL

6 Q ← V [G] 9
5 7 4 6
7 while Q ̸= ∅
8 u ← Extract-Min(Q)
9 C ← C ∪ {u} d c
10 for each vertex v ∈ Adj[u] 5 s 2 7 d
11 if d[v] > d[u] + w(u, v)
12 d[v] ← d[u] + w(u, v) Figure: An example from CLRS
13 π[v] ← u
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 10 / 15
Dijkstra’s algorithm: Complexity analysis
Dijkstra(G, w, s) line Array Imp. Heap Imp.
1
1 for each vertex v ∈ V [G] 2
2 d[v] ← ∞ 3 O(|V |)
3 π[v] ← NIL 4
4 d[s] ← 0 5
5 C←∅ 6 O(|V |)
6 Q ← V [G] 7 O(|V |) iterations
8 O(|V |2 )
7 while Q ̸= ∅
9
8 u ← Extract-Min(Q)
10 O(|E|) iterations
9 C ← C ∪ {u} 11
10 for each vertex v ∈ Adj[u] 12 O(|E|)
11 if d[v] > d[u] + w(u, v) 13
12 d[v] ← d[u] + w(u, v) Tot. O(|V |2 )
13 π[v] ← u Note: The effects of number of iterations on lines 7
and 10, are already considered in the cost of lines 8-9
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’sand 11-13
Algorithms Winter 2024 11 / 15
Dijkstra’s algorithm: Complexity analysis
Dijkstra(G, w, s) line Array Imp. Heap Imp.
1
1 for each vertex v ∈ V [G]
2
2 d[v] ← ∞ 3 O(|V |) O(|V |)
3 π[v] ← NIL 4
4 d[s] ← 0 5
5 C←∅ 6 O(|V |) O(|V |)
6 Q ← V [G] 7 O(|V |) iter. O(|V |) iter.
7 while Q ̸= ∅ 8 O(|V |2 ) O(|V | log |V |)
9
8 u ← Extract-Min(Q)
10 O(|E|) iter. O(|E|) iter.
9 C ← C ∪ {u}
11
10 for each vertex v ∈ Adj[u] 12 O(|E|) O(|E| log |V |)
11 if d[v] > d[u] + w(u, v) 13

12 d[v] ← d[u] + w(u, v) Tot. O(|V |2 ) O (|V | + |E|) log |V |
13 π[v] ← u Note: The of number of iterations on lines 7 and 10,
are already considered in the cost of lines 8-9 and
A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s11-13
Algorithms Winter 2024 11 / 15
Dijkstra’s algorithm: Proof of Correctness
Claim: For each vertex v ∈ V , we have d[v] = δ(s, v) at the time when v is added
to set C. To prove the claim, we follow these steps:

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 12 / 15


Dijkstra’s algorithm: Proof of Correctness
Claim: For each vertex v ∈ V , we have d[v] = δ(s, v) at the time when v is added
to set C. To prove the claim, we follow these steps:

1 Proof by contradiction: Assume the claim is not correct and u ∈ V is the


first vertex for which d[u] ̸= δ(s, u) when it is added to C.

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 12 / 15


Dijkstra’s algorithm: Proof of Correctness
Claim: For each vertex v ∈ V , we have d[v] = δ(s, v) at the time when v is added
to set C. To prove the claim, we follow these steps:

1 Proof by contradiction: Assume the claim is not correct and u ∈ V is the


first vertex for which d[u] ̸= δ(s, u) when it is added to C.
2 Time t: Beginning of the iteration in which u is added to C.

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 12 / 15


Dijkstra’s algorithm: Proof of Correctness
Claim: For each vertex v ∈ V , we have d[v] = δ(s, v) at the time when v is added
to set C. To prove the claim, we follow these steps:

1 Proof by contradiction: Assume the claim is not correct and u ∈ V is the


first vertex for which d[u] ̸= δ(s, u) when it is added to C.
2 Time t: Beginning of the iteration in which u is added to C.

3 Use a shortest path P , from s to u (needs justification)


C u
s
P

C covers a part of P at time t


A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 12 / 15
Dijkstra’s algorithm: Proof of Correctness
C u
s
4 On P , find y (with predecessor x), the first vertex in P
V − C.
x y

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 13 / 15


Dijkstra’s algorithm: Proof of Correctness
C u
s
4 On P , find y (with predecessor x), the first vertex in P
V − C.
x y

5 At time t, we have d[y] = δ(s, y). (needs Justification)

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 13 / 15


Dijkstra’s algorithm: Proof of Correctness
C u
s
4 On P , find y (with predecessor x), the first vertex in P
V − C.
x y

5 At time t, we have d[y] = δ(s, y). (needs Justification)


6 Fact 1: d[y] = δ(s, y) ≤ δ(s, u) ≤ d[u]

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 13 / 15


Dijkstra’s algorithm: Proof of Correctness
C u
s
4 On P , find y (with predecessor x), the first vertex in P
V − C.
x y

5 At time t, we have d[y] = δ(s, y). (needs Justification)


6 Fact 1: d[y] = δ(s, y) ≤ δ(s, u) ≤ d[u]
7 Fact 2: at time t the algorithm chose u. Hence

d[u] ≤ d[y]

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 13 / 15


Dijkstra’s algorithm: Proof of Correctness
C u
s
4 On P , find y (with predecessor x), the first vertex in P
V − C.
x y

5 At time t, we have d[y] = δ(s, y). (needs Justification)


6 Fact 1: d[y] = δ(s, y) ≤ δ(s, u) ≤ d[u]
7 Fact 2: at time t the algorithm chose u. Hence

d[u] ≤ d[y]

8 From equations facts 1 and 2 we conclude: d[y] = δ(s, y) = δ(s, u) = d[u]

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 13 / 15


Dijkstra’s algorithm: Proof of Correctness
3 Use a shortest path P , from s to u
(needs justification)
Proof: C u
s
P

C covers a part of P at time t

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 14 / 15


Dijkstra’s algorithm: Proof of Correctness
5 At time t, we have d[y] = δ(s, y).
Proof:
C u
s
P

x y

A. Jamshidpey Y. Lu (CS, UW) Lec 09: Dijkstra’s Algorithms Winter 2024 15 / 15

You might also like