Graph Theory and Shortsest Path Dijsktra Lecture Notes
Graph Theory and Shortsest Path Dijsktra Lecture Notes
Graph Theory and Shortsest Path Dijsktra Lecture Notes
ALGORITHMS
SINGLE SOURCE ….
Dr Karanja
4/23/2021 Dr Karanja 1
Introduction
• Many problems can be modeled using graphs
with weights assigned to their edges:
– Airline flight times
– Telephone communication costs
– Computer networks response times
4/23/2021 Dr Karanja 2
Where’s our motivation?
• Fastest way to get to school
• Traffic jam …. How to evade it / avoid it
• Finding the cheapest flight To location X
• E.g., a road map: what is the shortest path from JUJA
to Uhuru Park ?
4/23/2021 Dr Karanja 3
Heading to Juja to Uhuru Park
4/23/2021 Dr Karanja 4
Rushing for a date in traffic
4/23/2021 Dr Karanja 5
Single-Source Shortest Path
• Problem: given a weighted directed graph G,
find the minimum-weight path from a given
source vertex s to another vertex v
– “Shortest-path” = minimum weight
– Weight of path is sum of edges
Dr Karanja
4/23/2021 6
Shortest Paths in Dijkstra’s Algorithm
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2
4/23/2021 Dr Karanja 2 7
4 5 4 5
Outline of Algorithm
• Label a with 0 and all others • Once u is added to Sk we
with . L0(a) = 0 and update the labels of all the
L0(v) = vertices not in Sk
• Labels are shortest paths To update labels:
from a to vertices
• Sk = the distinguished set of Lk(a, v) = min{Lk-1(a, v),
vertices after k iterations. S0 Lk-1(a, u) + w(u, v)}
= . The set Sk is formed by
adding a vertex u NOT in Sk-1
with the smallest label.
4/23/2021 Dr Karanja 8
Dijkstra's Shortest Path Algorithm
Find shortest path from s to t.
24
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
9
4/23/2021 Dr Karanja
Dijkstra's Shortest
S={ }
Path Algorithm
PQ = { s, 2, 3, 4, 5, 6, 7, t }
24
2 3
0 9
s
18
14 2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
distance
10 label
4/23/2021 Dr Karanja
Dijkstra's Shortest
S={ }
Path Algorithm
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
24
2 3
0 9
s
18
14 2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
distance
11 label
4/23/2021 Dr Karanja
Dijkstra's Shortest
S={s}
Path Algorithm
PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
distance
12 label
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S={s}
Path Algorithm
PQ = { 2, 3, 4, 5, 6, 7, t }
delmin
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
distance
13 label
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S = { s, 2 }
Path Algorithm
PQ = { 3, 4, 5, 6, 7, t }
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
14
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S = { s, 2 }
Path Algorithm
PQ = { 3, 4, 5, 6, 7, t }
decrease key
33
X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
15
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S = { s, 2 }
Path Algorithm
PQ = { 3, 4, 5, 6, 7, t }
33
X
X 9
24
2 3
0 9
delmin
s
18
14
X 14
2 6
6
30 11
4 19
15 5
5
6
20 16
t
7 44
16
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S = { s, 2, 6 }
Path Algorithm
PQ = { 3, 4, 5, 7, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
17
4/23/2021 15
X Dr Karanja
Dijkstra's Shortest
S = { s, 2, 6 }
Path Algorithm
PQ = { 3, 4, 5, 7, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
18
4/23/2021 15
X delmin Dr Karanja
Dijkstra's Shortest
S = { s, 2, 6, 7 }
Path Algorithm
PQ = { 3, 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
19
4/23/2021 15
X Dr Karanja
59 X
Dijkstra's Shortest
S = { s, 2, 6, 7 }
Path Algorithm
PQ = { 3, 4, 5, t } delmin
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44
30
X 11
4 19
15 5
5
6
20 16
t
7 44
20
4/23/2021 15
X Dr Karanja
59 X
Dijkstra's Shortest
S = { s, 2, 3, 6, 7 }
Path Algorithm
PQ = { 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
21
4/23/2021 15
X Dr Karanja 51 59
X X
Dijkstra's Shortest
S = { s, 2, 3, 6, 7 }
Path Algorithm
PQ = { 4, 5, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
delmin
t
7 44
22
4/23/2021 15
X Dr Karanja 51 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 5, 6, 7 }
Algorithm
PQ = { 4, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
23
4/23/2021 15
X Dr Karanja 50 51
X 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 5, 6, 7 }
Algorithm
PQ = { 4, t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5 delmin
5
6
20 16
t
7 44
24
4/23/2021 15
X Dr Karanja 50 51
X 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 4, 5, 6, 7 }
Algorithm
PQ = { t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
25
4/23/2021 15
X Dr Karanja 50 51
X 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 4, 5, 6, 7 }
Algorithm
PQ = { t }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
26
4/23/2021 15
X Dr Karanja
delmin 50 51
X 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 4, 5, 6, 7, t }
Algorithm
PQ = { }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
27
4/23/2021 15
X Dr Karanja 50 51
X 59
X X
Dijkstra's Shortest Path
S = { s, 2, 3, 4, 5, 6, 7, t }
Algorithm
PQ = { }
32
33
X X
X 9
24
2 3
0 9
s
18
14
X 14
2 6
6
45 X
X 35
44 X 34
30
X 11
4 19
15 5
5
6
20 16
t
7 44
28
4/23/2021 15
X Dr Karanja 50 51
X 59
X X