0% found this document useful (0 votes)
9 views12 pages

Chap 5 - Greedy (SingleShortestPath)

The document discusses the Greedy Technique, specifically focusing on Dijkstra's Algorithm for finding the single source shortest path in a weighted connected graph. It outlines the algorithm's main idea, input and output requirements, and provides a step-by-step trace of the algorithm's execution. The running time is noted as O(n^2), and the algorithm's effectiveness is attributed to its greedy nature and the assumption of no negative edge weights.

Uploaded by

3ashry.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Chap 5 - Greedy (SingleShortestPath)

The document discusses the Greedy Technique, specifically focusing on Dijkstra's Algorithm for finding the single source shortest path in a weighted connected graph. It outlines the algorithm's main idea, input and output requirements, and provides a step-by-step trace of the algorithm's execution. The running time is noted as O(n^2), and the algorithm's effectiveness is attributed to its greedy nature and the assumption of no negative edge weights.

Uploaded by

3ashry.stud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter # 5

Lecture # 4

Greedy Technique

Applications of Greedy Technique:


Single Shortest Path (SP).

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.

Output The distance from vertex 1 to every other vertex in 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

?Running time: O(n2), why

That’s why Dijkstra’s works out well as a greedy algorithm


 It’s greedy because we assume we have a shortest distance
to a vertex before we ever examine all the edges that even lead
into that vertex.
 It works since all shortest paths contain subpaths that are
also shortest paths.
 This also works because we assume no negative edge
weights.
03:55:20
Trace of Dijkstra’s algorithm
1
2 1 We need to calculate the shortest path
3
5
6 between the vertex “1” and all other
2
3 vertices.
1 4
3 2
2
5
4

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

Tree vertices Remaining vertices


4
b c
a(-,0) b(a,3) c(-,∞) d(a,7) e(-,∞) 3 6
2 5
a d e
7 4

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)

You might also like