Professional Documents
Culture Documents
(ESO207)
Lecture 6:
• Design of O(𝑛) time algorithm for Local Minima in a grid
• Data structure gem: Range minima Problem
1
Local minima in an array
(Proof of correctness)
2
Local minima in a grid
(extending the solution from 1-D to 2-D)
Under what
Search for a local minima in the column M[∗, 𝒎𝒊𝒅 ] A What
local minima
circumstances
exists
if thereeven
is nothis
inlocal
this minima
region. Why
in the?
smallest element is not
aentire
localcolumn.
minima ?
𝒎𝒊𝒅
Smallest element
Execute Explore()
of the column
from M[𝒊, 𝒎𝒊𝒅 + 𝟏 ]
𝒊 9 7
Homework:
Use this idea to design an O(𝒏 log 𝒏) time algorithm for this problem.
3
… and do not forget to prove its correctness J.
Local minima in a grid
Int Local-minima-in-grid(M) // returns the column containing a local minima
{ L ß 0;
R ß 𝒏 − 𝟏;
found ß FALSE;
while(not found)
{ 𝒎𝒊𝒅 ß (L + R)/2;
If (M[*, 𝒎𝒊𝒅] has a local minima) found ß TRUE; O(𝒏) time
else {
let M[𝒌, 𝒎𝒊𝒅] be the smallest element in M[*, 𝒎𝒊𝒅] O(𝒏) time
O(𝒏)
if(M[𝒌, 𝒎𝒊𝒅 + 𝟏] < M[𝒌, 𝒎𝒊𝒅]) Lß 𝒎𝒊𝒅 ?? + 𝟏 ;
time
else ??Rß 𝒎𝒊𝒅 − 𝟏
}
}
return 𝒎𝒊𝒅;
}
Proof of correctness ?
è Running time of the algorithm = O(𝒏 log 𝒏)
4
Local minima in a grid
(Proof of Correctness)
𝑳 𝑹
𝒋
𝒋′
P(𝑖) :
“A local minima of grid M exists in M[𝑳,…, 𝑹].”
∃ 𝒋 such that M[𝒋, 𝑳] < M[∗, 𝑳 − 𝟏]” and ∃ 𝒋′ such that M[𝒋′, 𝑹] < M[∗, 𝑹 + 𝟏]”5
Local minima in a grid
How to improve it to
O(𝒏) ?
6
Local minima in a grid in O(𝒏) time
Let us carefully look at the calculations of the running time of the current algo.
𝒏 𝒏 𝒏
𝒄 + 𝒄 + 𝒄 + … (log 𝒏 terms) … + 𝒄𝒏 = ?
𝟐 𝟒 𝟖
It is 2𝒄𝒏 = O(𝒏) .
7
Local minima in a grid in O(𝒏) time
INCORRECT
Exercise: Think of a
counterexample!
8
Lessons learnt
9
Let us revisit the (𝒏 𝐥𝐨𝐠 𝒏) algorithm
10
Local minima in a grid
(Proof of Correctness)
𝑳 𝑹
Is there an
alternate and
simpler P(𝑖) ?
𝒋
𝒋′
P(𝑖) :
“A local minima of grid M exists in M[𝑳,…, 𝑹].”
∃ 𝒋 such that M[𝒋, 𝑳] < M[∗, 𝑳 − 𝟏]” and ∃ 𝒋′ such that M[𝒋′, 𝑹] < M[∗, 𝑹 + 𝟏]”11
At any stage, what guarantees the existence of
local minima in the region ?
Region
Boundary
11
smallest
7
7 9 5
13
Local minima in a grid
Alternate O(𝒏 𝐥𝐨𝐠 𝒏) algorithm
𝑳 𝑹
7
5
14
Local minima in a grid
Alternate O(𝒏 𝐥𝐨𝐠 𝒏) algorithm
𝑳 𝑹
smallest 7
18 9 7
15
Local minima in a grid
Alternate O(𝒏 𝐥𝐨𝐠 𝒏) algorithm
𝑳 𝑹
17
Theorem:
Given an 𝒏 × 𝒏 grid storing 𝒏𝟐 distinct elements, a local minima can be found
in O(𝒏) time.
Question:
On which algorithm paradigm, was this algorithm based on ?
• Greedy
• Divide and Conquer
• Dynamic Programming
Proof of correctness of algorithms
19
GCD
GCD(𝑎,𝑏) // 𝑎 ≥ 𝑏 Proof of correctness of GCD(𝑎,𝑏) :
{ Let 𝑎1 : the value of variable 𝑎 after 𝑖th iteration.
while (𝑏 <> 0) 𝑏1 : the value of variable 𝑏 after 𝑖th iteration.
{ 𝑡 ß 𝑏;
𝑏 ß 𝑎 mod 𝑏 ; Assertion 𝑃(𝑖) : gcd(𝑎! , 𝑏! ) = gcd(𝑎,𝑏)
𝑎ß𝑡 Theorem : 𝑃(𝑖) holds for each iteration 𝑖 ≥ 0.
} Proof: (By induction on 𝑖).
return 𝑎; Base case: (𝑖 = 0) hold trivially.
} Induction step:
Lemma (Euclid): (Assume 𝑃(𝑗) holds, show that 𝑃(𝑗 + 1) holds too)
If 𝑛 ≥ 𝑚 > 0, then 𝑃(𝑗) è gcd(𝑎" , 𝑏"?) = gcd(𝑎,𝑏). ----(1)
gcd(𝑛,𝑚) = gcd(𝑚,𝑛 mod 𝑚) (𝑗 + 1) iteration è 𝑎"#$ = 𝑏" and? 𝑏"#$ = 𝑎" mod 𝑏" ---(2)
Using Euclid’s Lemma and (2),
gcd(𝑎2 , 𝑏2 ) = gcd(𝑎234 , 𝑏234 ) ------(3).
Using (1) and (3),assertion 𝑃(𝑗 + 1) holds too. 20
Binary Search
Binary-Search(A[𝟎. . . 𝒏 − 𝟏], 𝒙)
L ß 𝟎; Observation: If the code returns
R ß 𝒏 − 𝟏; true, then indeed output is correct.
Found ß false;
While ( L ≤ R and ? Found = false )
{ mid ß (L+R)/𝟐; So all we need to prove is that whenever
If (A[mid] = 𝒙) Found ß true; code returns false , then indeed 𝒙 is not
else if (A[mid] < 𝒙) L ß mid?+ 1 ; present in A[].
This is because Found is set to
else ? -1 ;
R ß mid true only when 𝒙 is indeed found.
}
if Found return true;
else return false;
21
Binary Search
Binary-Search(A[𝟎. . . 𝒏 − 𝟏], 𝒙) Assertion 𝑃(𝑖) : 𝒙 ∉ { A[0],…,A[L−1] }
L ß 𝟎; and 𝒙 ∉ { A[R+1],…,A[𝑛 −1] }
R ß 𝒏 − 𝟏;
L R
Found ß false;
While ( L ≤ R and ? Found = false )
{ mid ß (L+R)/𝟐; A
If (A[mid] = 𝒙) Found ß true;
else if (A[mid] < 𝒙) L ß mid?+ 1 ;
else ? -1 ;
R ß mid
}
if Found return true;
else return false;
22
Range-Minima Problem
A Motivating example
to realize the importance of data structures
23
Range-Minima Problem
Given: an array A storing 𝒏 numbers,
Aim: a data structure to answer a sequence of queries of the following type
Range-minima(i,j) : report the smallest element from A[i],…,A[j]
A 3 5 1 8 19 0 -1 30 99 -6 10 2 40 27 44 67
i=4 j=11
Range-Minima Problem
• Compact
(O(𝒏 log 𝒏) size )
• Can answer each query efficiently ?
(O(𝟏) time)
25