Lecture 7 ESO207 O (N Logn) Algorithm For Local Minima in Grid and Proofs of Correctness
Lecture 7 ESO207 O (N Logn) Algorithm For Local Minima in Grid and Proofs of Correctness
Lecture 7 ESO207 O (N Logn) Algorithm For Local Minima in Grid and Proofs of Correctness
(ESO207A)
Lecture 7:
• Assignment 1: Accuracy of word RAM model
• Design of O() time algorithm for Local Minima in a grid
• Proof of Correctness.
1
Optional session
2
Fibonacci numbers
Fibonacci numbers
;
;
for all ;
3
Fibonacci numbers
Fibonacci numbers
;
;
for all ;
1+ √ 5
where 𝜙= ≈ 1.61803
2
1− √ 5
𝜓= ≈ − 0 .61803
2
4
Recursive algorithm for F() mod 2021
RFib()
{ if =0 return 0;
else if =1 return 1;
else return((RFib() + RFib() ) mod )
}
Let G() denote the number of instructions executed by RFib()
• G(0) = 1;
• G(1) = 2;
• For >1
G() G()G() 4
5
Recursive algorithm for F() mod 2021
RFib()
{ if =0 return 0;
else if =1 return 1;
else return((RFib() + RFib() ) mod )
}
Let G() denote the number of instructions executed by RFib()
• G(0) = 1;
• G(1) = 2; G*()
• For >1
G() 4 G() 4 G() 4
𝜙 ≈ 1.61 803
G()
How will the graph of
log G()) versus look like ?
log G()) = ()
8
What does correctness of an algorithm mean ?
For every possible valid input, the algorithm must output correct answer.
9
Algorithm for computing
sum of numbers from to
Sum_of_Numbers()
{ Sum;
for = 1 to
{
Sum Sum + ;
}
return Sum;
How will you convince any person
} that Sum_of_Numbers()
indeed is correct ?
Natural responses:
• It is obvious !
• Compile it and run it for some random values of .
• Go over first few iterations explaining what happens to Sum.
10
How will you respond
if you have to do it for the following code ?
11
Think for some time to realize
• the non-triviality
• the Importance
of proof of correctness of an iterative algorithm.
12
Proof of correctness
For an iterative algorithm
Start
of Loop 1 2 3 4 𝑖 −1 𝑖 Iterations
Assertion P() : ? At the end of th iteration Sum stores the sum of numbers from to .
Max-sum-subarray-algo(A[])
{ S[] A[]
for = 1 to
{ If S[] > 0 then S[] S[] + A[]
else S[] A[]
}
“Scan S to return the maximum entry”
}
Loop Invariant/Assertion P() : S[] stores the sum of maximum sum subarray ending at A[].
Homework: Prove that P() holds for all
15
(Mathematical Induction) 1. Prove the formula for the smallest number that can
be used in the given statement.
2. Assume it's true for an arbitrary number n.
3. Use the previous steps to prove that it's true for the next number n + 1.
• Proof by Induction
• Induction has three steps:
• 1. Base case
• 2. Assume true for n
• 3. Show true for n + 1
16
Recap from Lecture 6
An algorithm for
Local Minima in a Grid
17
Local minima in a GRID
Does a local
minima exist
always ?
31
𝒊 5 33 10
99
Yes. After all, global
minima is also a
local minima.
18
Local minima in a GRID
31
𝒊 5 33 10
99
19
A new approach
Repeat : if current entry is not local minima, explore the neighbor storing smaller value.
20
A new approach
Explore()
{ Let c be any entry to start with;
While(c is not a local minima)
{
c a neighbor of c storing smaller value
}
return c;
}
21
A new approach
Explore()
{ Let c be any entry to start with;
While(c is not a local minima)
{
c a neighbor of c storing smaller value How to apply this
} principle ?
return c;
}
Time complexity : ? ()
23
Local minima in an
a GRID
ARRAY
24
Algorithm for Local minima in an array
Execute Explore() from A[]
23
17
A 9 17 23
𝑖
There is a local minima
in A[,…,]. Give reasons.
Reason:
Explore() from A[] will terminate without ever
entering A[].
We can discard A[] . But what if …
25
Algorithm for Local minima in an array
𝑛 −1
𝑖= ;
2
A 23 17 9
26
Local minima in an array
(Similar to binary search)
27
Proof of correctness
What can you say about the
algorithm at the end of th
iteration.
𝑳 𝑹
A
P() : At the end of th iteration,
“A local minima of array A exists in A[,…, ].”
Homework:
• Make sincere attempts to prove the assertion P(𝑖).
• Complete the proof.
28
Local minima in an array
(Proof of correctness)
29
Local minima in a GRID
in O(log ) time
30
Local minima in a GRID
Treat each row as an array and compute its local minima. INCORRECT
Reason: None of them may be the local minima of the grid.
31
Think …
32
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[, ] AWhat
local minima
circumstances
exists
if thereeven
is no this
inlocal
this minima
region. Why
in the?
smallest element is not
aentire
localcolumn.
minima ?
Explore() from M[, ] 𝒎𝒊𝒅
Smallest element
of the column
𝒊 9 7
Homework:
Use this idea to design an O(log ) time algorithm for this problem.
… and do not forget to prove its correctness . 33