Chap 5 - Greedy (SingleShortestPath)
Chap 5 - Greedy (SingleShortestPath)
Lecture # 4
Greedy Technique
03:55:20
Dijkstra Algorithm: Single Source Shortest Path
Single Source Shortest Paths Problem: Given a weighted connected graph G, find
shortest paths from source vertex s to each of the other vertices
Input
Connected weighted graph, G.
03:55:20
Main Idea
We divide the set of vertices into two groups, selected & unselected:
Among vertices not already in the tree, it finds vertex u with the
: smallest sum
dv + w(v,u)
where
v is a vertex for which shortest path has been already found #
on preceding iterations (such vertices form a tree rooted at s)
dv is the length of the shortest path from source s to v #
# w(v,u) is the length (weight) of edge from v to u
03:55:20
Algorithm
Algorithm: Dijkstra
Input: A weighted connected graph G=(V,E) with n vertices.
Output: The distance from vertex 1 to every other vertex in G.
Begin
X={1}; Y=V-X; D[1]=0 .1
.For each vertex v ϵ V if there is an edge from 1 to v then let D[v]=w(1,v) .2
∞=Otherwise, D[v]
While Y ≠ { } do .2
Let y ϵ Y such that D[y] is minimum
X = X U {y}
Y = Y – {y}
.Update the distance (labels) of those vertices in Y that are adjacent to y
for each edge (y,w): if w ϵ Y and D[y] + w(y,w) < D[w] then //
// D[w]=D[w]+w(y,w)
03:55:20
.End
Running Time
1st step: calculate the direct distance between the vertex “1” and
all other vertices, Dv. If no direct edge between the vertex “1” and
any vertex,v, then the distance equals , Dv= .
Iteration X Y D2 D3 D4 D5 D6
Initial {1} {2,3,4,5,6} 3 2 5
03:55:21
Trace of Dijkstra’s algorithm
1
2
3 1 2nd step: (i)select the vertex y Y such that
5 6 Dy is minimum. y=3
2
3
1 4 2 2nd step: (ii) Update the distance from the vertex “1”
3
2
4
55 to every veterx via the vertex y (selected)
Iteration X Y D2 D3 D4 D5 D6
Initial {1} {2,3,4,5,6} 3 2 5
1 {1,3} {2,4,5,6} 3 2 4 3
1 3 y
y=2
2 ? <3
1 3 2
y=4 2
2 4
1 3 4
y=5 ?5<
2
1 3 5
y=6 1 ? <
2 3
1 3 4
Trace of Dijkstra’s algorithm
1
2
3 1 2nd step: (i)select the vertex y Y such that
5 6 Dy is minimum. y=2
2
3
1 4 2 2nd step: (ii) Update the distance from the vertex “1”
3
2
4
55 to every veterx via the vertex y (selected)
Iteration X Y D2 D3 D4 D5 D6
Initial {1} {2,3,4,5,6} 3 2 5
1 {1,3} {2,4,5,6} 3 2 4 3
2 {1,2,3} {4,5,6} 3 2 4 7 3
1 2 y
y=4 1
3 4
1 2 4
? 4<
y=5 4
3 <7
1 2 5
y=6 ?
3
1 2 6
? <3
Trace of Dijkstra’s algorithm
1
2
3 1 2nd step: (i)select the vertex y Y such that
5 6 Dy is minimum. y=6
2
3
1 4 2 2nd step: (ii) Update the distance from the vertex “1”
3
2
4
55 to every veterx via the vertex y (selected)
Iteration X Y D2 D3 D4 D5 D6
Initial {1} {2,3,4,5,6} 3 2 5
1 {1,3} {2,4,5,6} 3 2 4 3
2 {1,2,3} {4,5,6} 3 2 4 7 3
3 {1,2,3,6} {4,5} 3 2 4 5 3
1 6 y
y=4
3 ? <4
1 6 4
y=5 2
3 7<5
1 6 5
?
Trace of Dijkstra’s algorithm
1
2
3 1 2nd step: (i)select the vertex y Y such that
5 6 Dy is minimum. y=4
2
3
1 4 2 2nd step: (ii) Update the distance from the vertex “1”
3
2
4
55 to every veterx via the vertex y (selected)
Iteration X Y D2 D3 D4 D5 D6
Initial {1} {2,3,4,5,6} 3 2 5
1 {1,3} {2,4,5,6} 3 2 4 3
2 {1,2,3} {4,5,6} 3 2 4 7 3
3 {1,2,3,6} {4,5} 3 2 4 5 3
4 {1,2,3,4,6} {5} 3 2 4 5 3
2 1
1 4 y 11 3
5 6
2
y=5 3 3 44
4 ? 5<7 1
3
2
1 4 5 2 55
4
b 4
c
3 2 5 6
7 4
a d
d e
Iteration X Y Db Dc Dd De
Initial {a} {b, c, d, e} 3 7
1 {a,b} {c,d,e} 3 7 5
2 {a,b,d} {c,e} 3 7 5 9
3 {a,b,c,d} {e} 3 7 5 9
b 4
c
3 2 5 6
7 4
a d
d e
4
b c
3 6
7 2 5 4
a d
d e
4
b c
b(a,3) c(b,3+4) d(b,3+2) e(-,∞)
3 6
2 5
a d e
7 4
4
d(b,5) c(b,7) e(d,5+4) b c
3 6
2 5
a d e
7 4
4
c(b,7) e(d,9) b c
3 6
2 5
a d e
7 4
e(d,9)