Lecture 04 Dijkstra
Lecture 04 Dijkstra
Dijkstra’s Algorithm
A A
450
60 190
C unweighted C weighted
graph graph
200 130
D D
E E
Weighted Shortest-Path Problems
G = (V, E) weighted directed graph
w: ER weight function
Weight of a path p = <v0, v1,. . ., vn>
n
w( p) w(vi 1v)
Shortest path weight fromi u1 to v
p
Shortest
(upath min{wu( to
, v) from u Any
p) :v: v} path
if a u,from
v pathuexits
to v with w(p) = (u,v)
[v] - predecessor of von a path otherwise
Shortest-Path Problems
Shortest-Path problems
Single-source. Find a shortest path from a given source to
each of the vertices
Single-pair. Given two vertices, find a shortest path
between them. Solution to single-source problem solves
this problem efficiently, too.
All-pairs. Find shortest-paths for every pair of vertices.
Dynamic programming algorithm.
Unweighted shortest-paths – BFS.
What is the problem ?
Find the shortest path from a vertex v to every
other vertex in a graph.
2 2
u v u v
EXAMPLE 2
s s
Relax
u v
u v
EXAMPLE 3
s s
Relax
u v
u v
Dijkstra’s Algorithm
Approach
maintain a set S of vertices whose final shortest
path weights from the source s have been
determined.
repeat
select vertex from V-S with the minimum shortest path
estimate
insert u in S
RELAX(u,v,w)
if d[v] > d[u]+w(u,v)
then d[v] d[u]+w(u,v)
[v] u
Continue:
DIJKSTRA(G,w,s):
INITIALIZE-SINGLE-SOURCE(G,s)
S
Q V[G]
while Q
do u EXTRACT -MIN(Q)
S S {u}
Initialization
relaxing
edges
Dijkstra’s
u
Example
v u v
1 1
10 10
9 9
s 2 3 s 2 3
4 6 4 6
7 7
5 5
2 2
x y x y
u v u v
1 1
10 10
9 9
2 3 s 2 3
s 4 6 7
4 6
7
5 5
2
2
x y x y
Dijkstra’s Example
u v u v
1 1
10 10
9 9
2 3 2 3
4 6 4 6
7 7
5 5
2 2
x y x y
Another Example
4 1 3 10
2 2 ∞
∞ C D E
5 8 ∞ 4 6
F G
1
∞ ∞
17
Example: Update neighbors' distance
0 2
2
A B
4 1 3 10
∞ C
2
D
2
E ∞
5 8 1 4 6
1
Distance(B) = 2 F G
Distance(D) = 1
∞ ∞
18
Example: Remove vertex with minimum distance
0 2
2
A B
4 1 3 10
∞ C
2
D
2
E ∞
5 8 1 4 6
1
F G
∞ ∞
19
Example: Update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
Distance(C) = 1 + 2 = 3 F G
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9 9 5
Distance(G) = 1 + 4 = 5
20
Example: Continued...
Pick vertex in List with minimum distance (B) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
Note : distance(D) not
1
F G updated since D is
already known and
9 5 distance(E) not updated
since it is larger than
previously computed
21
Example: Continued...
Pick vertex List with minimum distance (E) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
No updating
9 5
22
Example: Continued...
Pick vertex List with minimum distance (C) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
Distance(F) = 3 + 5 = 8 F G
8 5
23
Example: Continued...
Pick vertex List with minimum distance (G) and update neighbors
0 2
2
A B
1 3 10
4
2 2
3 C D E 3
5 8 1 4 6
1
F G
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6
24
Example (end)
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5
Pick vertex not in S with lowest cost (F) and update neighbors
25
Try Yourself
Thank You