based on asuitable Theorem about Opt()
A generic way to design and analyse a greedy algorithm
P: a given optimization problem
1. Try to establish a relation between OPT() and OPT();
2. Try to prove the relation formally by
deriving a solution of from OPT()
deriving a solution of from OPT() 3
𝑨
𝑨 ′
instance of size of problem P
Greedy
step
instance of size of problem P
Opt()
Opt()
use Theorem
If you succeed,
you have a recursive algorithm
from construction
4.
Single Source ShortestPaths
in
Graphs with non-negative weights
4
Internalizing an algorithm is different from just knowing an algorithm.
Go through these slides slowly, pondering over the observations,
and attempting questions posed there.
This is an opportunity for you to
(1) re-invent the algorithm and
(2) realize the proof of correctness.
5.
Notations and Terminologies
Adirected graph
• Represented as Adjacency lists or Adjacency matrix
• ,
Question: what is a path in ?
Answer: A sequence , ,…, such that (,) for all .
Length of a path =
5
𝒗 𝟏 𝒗 𝟐 𝒗 𝟑 𝒗𝒌 −𝟏 𝒗 𝒌
…
12 3 29
No vertex is repeated
6.
Notations and Terminologies
Definition:The path from to of minimum length is called the shortest path
from to
Definition: Distance from to is the length of the shortest path from to .
Notations:
: distance from to .
: The shortest path from to .
6
7.
Problem Definition
Input: Adirected graph with and a source vertex
Aim:
• Compute for all
• Compute for all
This problem is simple and beautiful enough to convince anyone about
the importance of designing efficient algorithms.
7
8.
An example toget
an insight into this problem
Inference:
The distance to any vertex depends upon global parameters.
8
𝒔
G
𝒒
12
6
16
11
9
4
7
5
9.
An example toget
an insight into this problem
Question: Is there any vertex in this picture for which you are certain about
the distance from ?
Answer: vertex .
9
𝒔
𝒖
𝒙
𝒚
𝒛 𝒗
3
5
12
10
7
G
Give reasons.
10.
An example toget
an insight into this problem
The shortest path to vertex is edge (,).
10
𝒔
𝒖
𝒙
𝒚
𝒛 𝒗
3
5
12
10
7
G
¿ 𝟎
The nearest vertex of must be ’s neighbor.
11.
Designing a greedyalgorithm for shortest paths
What should be the greedy step such that
“we can retrieve distance from in using distance from in ” ?
11
𝑮
𝑮 ′
instance of size
Greedy
step
instance of size
Opt()
Opt()
?
Using generic strategy
12.
We know thedistance to .
Question: Can you remove vertex without affecting the distance from ?
An example to get
an insight into this problem
12
𝒔
𝒖
𝒙
𝒚
𝒛
3
5
12
10
7
G
3
2
8
21
𝒓
𝒕
𝒗
Since we have computed
distance to , perhaps we can
remove .
No. This is because there could be vertices
whose shortest path from passes through .
13.
An example toget
an insight into this problem
13
𝒙
𝒚
𝒛
5
12
10
7
G’
3
2
8
21
𝒓
𝒕
+3
+3
+3
+3
𝒔
𝒗
14.
An example toget
an insight into this problem
Theorem: For each ,
14
𝒙
𝒚
𝒛
5
12
10
7
G’
6
5
11
24
𝒓
𝒕
𝒔
𝒗
15.
How to computeinstance
Let (,) be the least weight edge from in =(, ).
Transform into as follows.
1. For each edge (,)ϵ ,
add edge (,);
(,) ??
2. In case of two edges from to any vertex , keep only the lighter edge.
3. Remove vertex .
Theorem: For each ,
an algorithm for distances from with time complexity.
15
(,) + (,);
Can you see some negative
points of this algorithm ?
16.
Shortcomings of thealgorithm
• No insight into the (beautiful) structure of shortest paths.
• Just convinces that we can solve the shortest paths problem in polynomial time.
• Very few options to improve the time complexity.
• Silent about a compact data structure for storing all shortest paths from the
source.
We shall now design a very insightful algorithm based on properties of shortest paths.
16
Optimal subpath property
Considerany shortest path .
= 51
Lemma 1: Every subpath of a shortest path is also a shortest path.
18
𝒔
?
< 27
𝒂 𝒈 𝒙
12 3 9
𝒚 𝒖
15
5
7
Can you realize
an assumption
made by you in
this proof ?
Homework: Write a complete and formal proof for Lemma 1
Proof:
If there is a shorter path from to ,
replacing the current path using the shorter path
will lead to a shorter path from to .
19.
Exploiting the non-negativeweight on edges
Consider once again a shortest path .
This leads to an alternate proof to the following assertion :
The nearest vertex of must be ’s neighbor.
Can you realize this observation ?
19
𝒂 𝒈 𝒙
𝒚 𝒖
𝒔
≥ 𝟎 ≥ 𝟎 ≥ 𝟎 ≥ 𝟎 ≥ 𝟎 ≥ 𝟎
(1)
20.
More insights …
Let: th nearest vertex from .
= .
Consider the shortest path .
Lemma 2: must be of the form for some .
20
𝒔 𝒙
What picture
captures all shortest
paths from ?
What can we say
about ?
must be for some .
shortest
Designing the algorithm…
Lemma 2: must be of the form for some .
Question: Can we use Lemma 2 to design an algorithm ?
Incremental way to compute shortest paths.
Ponder over it before going to the next slide.
22
shortest
23.
Suppose we havecomputed , ,…, .
How to find out ?
23
𝒔𝟏
𝒔𝟐
𝒔
𝒔𝟒
𝒔𝟑
G
𝒗
If ,
How will the shortest
path look like ?
Or
Or
Using Lemma 2
We need to just focus on
the edges incident on from , ,…, .
How to use it to find ?
Isn’t it nice ?
Ponder over its usefulness before proceeding further.
Let us define a function
Consider any
24.
Suppose we havecomputed , ,…, .
How to find out ?
For each
is ??
24
𝒔𝟏
𝒔𝟐
𝒔
𝒔𝟒
𝒔𝟑
G
𝒗
the vertex with minimum value of .
min
¿ ¿
(𝜹(𝒔 ,𝒔 𝒋)+𝝎(𝒔𝒋 ,𝒗))
51
76
101
90
82
62
88
Which vertex is ?
Using Lemma 2
25.
Dijkstra’s algorithm
Dijkstra-algo(,)
;
;
For = 0 to do
{ vertex from with minimum value of ;
;
move from to ;
For each do
{ ;
For each with do
min( , )
}
}
25
a lot of re-computation
for all ;
Computing labels for the
next iteration
Only neighbors of
What are the vertices
whose value may change
in this iteration ?
26.
Dijkstra’s algorithm
Dijkstra-algo(,)
;
;
For = 0 to do
{ vertex from with minimum value of ;
;
move from to ;
For each with do
{
min( , )
}
}
26
1 extract-min
operation
deg() Decrease-key
operations
for all ;
27.
Homework
• What datastructure is to be used to efficiently implement the Dijkstra’s
algorithm?
• Write a neat pseudocode in terms of the data structure.
• What is the time complexity of the implementation ?