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

CS310-slides-06-02-Dijkstra-Algorithm

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)
26 views

CS310-slides-06-02-Dijkstra-Algorithm

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/ 27

Algorithms

Single Source Shortest Path

Weighted Graphs and Shortest Paths


Dijkstra Algorithm
Proof of Correctness
Runtime
Basic Implementation
Vertex-Centric Implementation
Heap Based Implementation

Imdad ullah Khan

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 1 / 27


Shortest Paths
Weight of a path in weighted graphs is sum of weights of its edges

3 4
S A B
Three S − G paths
4 8 6
5 C(p1 ) = 3 + 4 + 8
9 8
5 3
C E F C(p2 ) = 4 + 5 + 3 + 3
3 3
2 12 C(p3 ) = 9 + 14
D 14 G

Shortest path from s to t is a path of smallest weight

Distance from s to t, d(s, t): weight of the shortest s − t path

There can be multiple shortest paths

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 2 / 27


Shortest Path Problems

1 Shortest s − t path:
Given G = (V , E , w ) and s, t ∈ V , find a shortest path from s to t
For an undirected graph, it will be a path between s and t
Unweighted graphs are weighted graphs with all edge weights = 1
Shortest path is not unique, any path with minimum weight will work

2 Single source shortest paths (sssp):


Given G = (V , E , w ) and s ∈ V , find shortest paths from s to all t ∈ V
Problems of undirected and unweighted graphs are covered as above
It includes the first problem

We focus on sssp

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 3 / 27


SSSP Problem
Input: A weighted graph G and a source vertex s ∈ V
Output: Shortest paths from s to all vertices v ∈ V
For unweighted graphs (unit weights) bfs from s will work
▷ bfs running time: O(n + m)
For weighted graph replace each edge e by a directed path of w (e) unit
weight edges
2
B D
1
Dummy Vertex
A 4 3 2 2 1 1
B D B D
5
C E
6

What if weights are not integers or are negative


Blows up size of the graph a lot

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 4 / 27


Dijkstra Algorithm
Input: A weighted graph G and a source vertex s ∈ V
Output: Shortest paths from s to all vertices v ∈ V
Dijkstra’s algorithm solves sssp for both directed and undirected graphs

Assumptions:

1 All vertices are reachable from s


Otherwise there is no shortest path (distance = ∞)
Easy to get R(s) in preprocessing (e.g., bfs or dfs)

2 All edge weights are non-negative


Bellman-Ford algorithm deals with negative weights

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 5 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 6 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 7 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 8 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 9 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 10 / 27


Dijkstra Algorithm
First step: only find distances d[1 . . . n] d[i] = d(s, vi )
d[s] = 0
Maintains a set R ⊂ V (known region), d[x ∈ R] is finalized
Initially R = {s} and iteratively add one vertex to R

d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 x
y

R ← {s}
while R ̸= V do
s
Select v ∈ R
u v
R ← R ∪ {v }
d[v ] ← d(s, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 11 / 27


Dijkstra Algorithm: Greedy Criteria
d[1 . . . n] ← [∞ . . . ∞]
y
d[s] ← 0 R ← {s}
x
while R ̸= V do
Select v ∈ R
s
u
R ← R ∪ {v }
v
d[v ] ← d(s, v )

Which vertex from R to add to R?


The vertex v ∈ R that is closest to s s R

Such a v must be at the “frontier” of R


∈R
z }| { w(uv) ≥ 0
s u v
Shortest path to v ∈ R, closest to s

Let v ∈ R be the closest to s and let a shortest s − v path be s, . . . , u, v


w (uv ) ≥ 0 =⇒ d(s, u) ≤ d(s, v ) =⇒ u is closer to s than v =⇒ u ∈ R
Otherwise we get contradiction to v being closest to s in R
This implies that v is only one edge away from R, i.e. (u, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 12 / 27


Dijkstra Algorithm: Greedy Criteria
d[1 . . . n] ← [∞ . . . ∞]
y
d[s] ← 0 R ← {s}
x
while R ̸= V do
Select v ∈ R
s
u
R ← R ∪ {v }
v
d[v ] ← d(s, v )

Which vertex from R to add to R?


The vertex v ∈ R that is closest to s s R

Such a v must be at the “frontier” of R


∈R
z }| { w(uv) ≥ 0
s u v
Shortest path to v ∈ R, closest to s

Let v ∈ R be the closest to s and let a shortest s − v path be s, . . . , u, v


w (uv ) ≥ 0 =⇒ d(s, u) ≤ d(s, v ) =⇒ u is closer to s than v =⇒ u ∈ R
Otherwise we get contradiction to v being closest to s in R
This implies that v is only one edge away from R, i.e. (u, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 13 / 27


Dijkstra Algorithm: Greedy Criteria
d[1 . . . n] ← [∞ . . . ∞]
y
d[s] ← 0 R ← {s}
x
while R ̸= V do
Select v ∈ R
s
u
R ← R ∪ {v }
v
d[v ] ← d(s, v )

Which vertex from R to add to R?


The vertex v ∈ R that is closest to s s R

Such a v must be at the “frontier” of R


∈R
z }| { w(uv) ≥ 0
s u v
Shortest path to v ∈ R, closest to s

Let v ∈ R be the closest to s and let a shortest s − v path be s, . . . , u, v


w (uv ) ≥ 0 =⇒ d(s, u) ≤ d(s, v ) =⇒ u is closer to s than v =⇒ u ∈ R
Otherwise we get contradiction to v being closest to s in R
This implies that v is only one edge away from R, i.e. (u, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 14 / 27


Dijkstra Algorithm: Greedy Criteria
d[1 . . . n] ← [∞ . . . ∞]
y
d[s] ← 0 R ← {s}
x
while R ̸= V do
Select v ∈ R
s
u
R ← R ∪ {v }
v
d[v ] ← d(s, v )

Which vertex from R to add to R?


The vertex v ∈ R that is closest to s s R

Such a v must be at the “frontier” of R


∈R
z }| { w(uv) ≥ 0
s u v
Shortest path to v ∈ R, closest to s

Let v ∈ R be the closest to s and let a shortest s − v path be s, . . . , u, v


w (uv ) ≥ 0 =⇒ d(s, u) ≤ d(s, v ) =⇒ u is closer to s than v =⇒ u ∈ R
Otherwise we get contradiction to v being closest to s in R
This implies that v is only one edge away from R, i.e. (u, v )

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 15 / 27


Dijkstra Algorithm: Greedy Criteria
d[1 . . . n] ← [∞ . . . ∞]
y
d[s] ← 0 R ← {s}
x
while R ̸= V do
Select v ∈ R
s
u
R ← R ∪ {v }
v
d[v ] ← d(s, v )

Which vertex from R to add to R?


The vertex v ∈ R that is closest to s s R

Such a v must be at the “frontier” of R

Restrict search to “single edge extensions” of paths to u ∈ R

Dijkstra assigns a score to each crossing edge


score(u, v ) = d[u] + w (uv ) for (u, v ) ∈ E , u ∈ R, v ∈
/R
Add a frontier vertex adjacent through minimum scoring edge

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 16 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S ∞
S A B
A ∞
4 8 B ∞
6
5 C ∞
9 8
5 3 D ∞
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 17 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A ∞
4 8 B ∞
6
5 C ∞
9 8
5 3 D ∞
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 18 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B ∞
6
5 C ∞
9 8
5 3 D ∞
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 19 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B ∞
6
5 C 4
9 8
5 3 D ∞
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 20 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B 7
6
5 C 4
9 8
5 3 D ∞
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 21 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B 7
6
5 C 4
9 8
5 3 D 7
C E F
3 3 E ∞
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 22 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B 7
6
5 C 4
9 8
5 3 D 9
C E F
3 3 E 9
2 12 F ∞
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 23 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B 7
6
5 C 4
9 8
5 3 D 9
C E F
3 3 E 9
2 12 F 12
D 14 G G ∞

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 24 / 27


Dijkstra Algorithm
Algorithm Dijkstra’s Algorithm for distances from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈
/ R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )

d
3 4 S 0
S A B
A 3
4 8 B 7
6
5 C 4
9 8
5 3 D 9
C E F
3 3 E 9
2 12 F 12
D 14 G G 15

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 25 / 27


Dijkstra Algorithm with paths
Record predecessor relationships (sources of used edges)
Implicitly builds a tree (shortest path tree)

Algorithm Dijkstra’s Algorithm for Shortest Paths from s to all vertices


d[1 . . . n] ← [∞ . . . ∞]
prev [1 . . . n] ← [null . . . null]
d[s] ← 0
R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈ / R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )
prev [v ] ← u ▷ predecessor is the vertex whose path is single-edge extended

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 26 / 27


Dijkstra Algorithm with paths
Algorithm Dijkstra’s Algorithm for Shortest Paths from s to all vertices
d[1 . . . n] ← [∞ . . . ∞]
prev [1 . . . n] ← [null . . . null]
d[s] ← 0 R ← {s}
while R ̸= V do
Select e = (u, v ), u ∈ R, v ∈ / R, with minimum d[u] + w (uv )
R ← R ∪ {v }
d[v ] ← d[u] + w (uv )
prev [v ] ← u ▷ predecessor is the vertex whose path is single-edge extended

d prev
3 4 S 0 N il
S A B
A 3 S
4 8 B 7 A
6
5 C 4 S
9 8
5 3 D 9 C
C E F
3 3 E 9 D
2 12 F 12 E
D 14 G G 15 B

Imdad ullah Khan (LUMS) Dijkstra’s Algorithm 27 / 27

You might also like