Lecture 7 ESO207 O (N Logn) Algorithm For Local Minima in Grid and Proofs of Correctness

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Data Structures and Algorithms

(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

• Practice sheet on Time complexity and big “O” notation

Time: 11:00 AM, 13 August, 2022.


Venue : KD101.

Please attempt problems first and then only come 


Otherwise, you won’t learn much from this session.

2
Fibonacci numbers

Fibonacci numbers
;
;
for all ;

Simple exercise: Using induction or otherwise, show that

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

Observation 1: G()>F() for all ;


G()> !!!

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

Observation: G*() = G*() F()


For 1, G*() = G*() G*() G*()
6
RAM model is quite an accurate model
for measuring efficiency of an algorithm
• G() F()

𝜙 ≈ 1.61 803
 G()
How will the graph of
log G()) versus look like ?
log G()) = ()

A line with slope


If you coded Rfib correctly and measured its time correctly,
log 2 𝜙=0 . 69 …
Your plot must have been a line with slope
7
PROOF OF CORRECTNESS

8
What does correctness of an algorithm mean ?

For every possible valid input, the algorithm must output correct answer.

Let us take a toy algorithm 

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.

In the following slide, we present an overview of


the proof of correctness.

Interestingly, such a proof will be just


Expressing our intuition/insight of the algorithm in a formal way .

12
Proof of correctness
For an iterative algorithm

Insight of the algorithm Theorem


Assertion Assertion
P() P()

Start
of Loop 1 2 3 4 𝑖 −1 𝑖 Iterations

Prove P() by What would you expect


1. ?Assuming P() at the end of th
iteration ?
2. Theorem
? Proof by induction
3. Body
? of the Loop
The most difficult/creative part of proof : To come? up with the right assertion P()
13
Algorithm for computing
sum of numbers from to
{ Sum;
for = 1 to
{
Sum Sum + ;
}
return Sum;
}

Assertion P() : ? At the end of th iteration Sum stores the sum of numbers from to .

Base case: P() holds.


Assuming P(), assertion P() also holds.
P() holds.
14
An O() time Algorithm for Max-sum subarray
Let S(): the sum of the maximum-sum subarray ending at index .

Theorem 1 : If S() > 0 then S() = S() + A[]


else S() = A[]

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

• Proof by Loop Invariant Is...


• Invariant: something that is always true
• After nding a candidate loop invariant, we prove:
• 1. Initialization: How does the invariant get initialized?
• 2. Loop Maintenance: How does the invariant change at each pass through the
loop?
• 3. Termination: Does the loop stop? When?

16
Recap from Lecture 6

An algorithm for
Local Minima in a Grid

17
Local minima in a GRID

Definition: Given a × grid storing distinct numbers,


an entry is local minima if it is smaller than each of its neighbors.
𝒋

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

Problem: Given a × grid storing distinct numbers, output any local


minima in () time. 𝒋

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;
}

Question: What is the proof of correctness of Explore ?


Answer:
It suffices if we can prove that While loop eventually terminates.
Indeed, the loop terminates since we never visit a cell twice.

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 : ? ()

First principle: Second principle:


Do not discard Explore() Simplify the problem
Homework of last class: Show that there is an instance of the grid
for which Explore() will indeed explore nearly cell. 22
Local minima in a GRID

How to simplify the


problem ?

23
Local minima in an
a GRID
ARRAY

Theorem 2: A local minima in an array storing distinct elements can be found in


O(log ) time.

Homework of last class:


• Design the algorithm stated in Theorem 2.
• Spend sufficient time to extend this algorithm to grid with running time= O().

You were asked to come prepared for today’s class 

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

We can discard A[] .


What should be so that we always discard a significant portion of A ?
…Can you see the algorithm now ?...

26
Local minima in an array
(Similar to binary search)

int Local-minima-in-array(A) { O(log )


L  0;
How many
R; iterations ?
found  FALSE;
while( not?? found )
{
 (L + R)/2;
If ( is a local minima)
found  TRUE; O() time
in one iteration
else if(A[] < A[]) ?? ;
L
else ?? R
}
return ; }
 Running time of the algorithm = O(log ) Proof of correctness ?

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)

Theorem: A local minima in an array storing distinct elements


can be found in O(log ) time.

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

You might also like