0% found this document useful (0 votes)
85 views

Lecture 8 ESO207 Complete Solution O (N) Local Minima in Grid and Examples Proofs of Correctness

This document summarizes two lectures on algorithms and data structures: 1) It discusses proof of correctness for iterative algorithms, including establishing an assertion P() at each iteration and proving P() holds by induction. 2) It presents an O(n) time algorithm for finding the maximum sum subarray using dynamic programming. 3) It also presents an algorithm for finding a local minima in an array and proves its correctness by establishing the assertion P() at each iteration that the subarray from L to R contains a local minima.

Uploaded by

Iitian
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Lecture 8 ESO207 Complete Solution O (N) Local Minima in Grid and Examples Proofs of Correctness

This document summarizes two lectures on algorithms and data structures: 1) It discusses proof of correctness for iterative algorithms, including establishing an assertion P() at each iteration and proving P() holds by induction. 2) It presents an O(n) time algorithm for finding the maximum sum subarray using dynamic programming. 3) It also presents an algorithm for finding a local minima in an array and proves its correctness by establishing the assertion P() at each iteration that the subarray from L to R contains a local minima.

Uploaded by

Iitian
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures and Algorithms

(ESO207A)

Lecture 8
• Proof of Correctness.
• Design of O() time algorithm for Local Minima in a grid

1
RECAP FROM LAST LECTURE

2
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()
3
EXAMPLE
MAXIMUM SUM SUBARRAY

4
An O() time Algorithm for Max-sum subarray

Max-sum-subarray-algo(A[]) Let S(): the sum of the maximum-sum


{ S[]  A[] subarray ending at index .
for = 1 to Theorem 1 :
{ If S[] > 0 then S[]  S[] + A[] If S() > 0 then S() = S() + A[]
else S[]  A[] else S() = A[]
}
“Scan S to return the maximum entry”
}

5
An O() time Algorithm for Max-sum subarray
P() holds for each

Max-sum-subarray-algo(A[]) Let Opt(): the sum of the maximum-sum


{ S[]  A[] subarray ending at index .
for = 1 to Theorem 1 :
{ If S[] > 0 then S[]  S[] + A[] If Opt() > 0 then Opt() = Opt() + A[]
else S[]  A[] else Opt() = A[]
}
“Scan S to return the maximum entry”
}
P() : ? S[] = Opt()

Assume P() holds.


P() holds. S[] = Opt()
P() holds.
How to show that P() holds ?

6
EXAMPLE
LOCAL MINIMA IN AN ARRAY

7
Algorithm for Local minima in an array
P() holds for th iteration, if executed
int Local-minima-in-array(A) P() :
{ L; LThere
R is a local
A[L] <minima
A[L] amongA[R]
A[L],...,A[R].
< A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1;
}
return mid;
} A

P()Assume
holds. P() holds.
L R L R

8
Algorithm for Local minima in an array
P() holds for th iteration, if executed.
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1;
}
return mid;
} A

Assume P() holds. P() holds.


L R L R

9
Algorithm for Local minima in an array
P() holds for th iteration, if executed.
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1;
}
return mid;
} A

Assume P() holds. P() holds.


L mid R
L R

10
Algorithm for Local minima in an array
P() holds for th iteration, if executed.
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1;
}
return mid;
} A

Assume P() holds.


L mid R
L R

11
Algorithm for Local minima in an array
P() holds for th iteration, if executed.
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1
}
return mid;
} A

Assume P() holds. P() holds.


mid L R
L R

12
Algorithm for Local minima in an array
P() holds for th iteration, if executed.
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1
}
return mid;
} A

Assume P() holds.


L mid R
L R

13
Algorithm for Local minima in an array
P() holds for th iteration, if executed
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found )
{ mid  (L R)/;
If (mid is a local minima) found  TRUE;
else if(A[mid] < A[mid]) ?? mid;
L
else ??R mid1
}
return mid;
} A

Assume P() holds. P() holds.


L R L R mid

14
Algorithm for Local minima in an array
P() holds
P()for
holds
th iteration,
for each if executed
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found ) Question 1:
{ mid  (L R)/; What happens during an iteration
If (mid is a local minima) found  TRUE; that begins with L R.
else if(A[mid] < A[mid]) ?? mid; Answer:
L
else ??R mid1 Local Minima is found 
}
return mid;
}

15
Algorithm for Local minima in an array
P() holds for th iteration, if executed
int Local-minima-in-array(A) P() :
{ L; L R A[L] < A[L] A[R] < A[R]
R;
found  FALSE;
while( not ?? found ) Question 2:
{ mid  (L R)/; During an iteration that begins with L R,
If (mid is a local minima) found  TRUE; if local minima is not found,
else if(A[mid] < A[mid]) ?? mid; what happens to R L ?
L
else ??R mid1 Answer:
} Strictly decreases.
return mid;
}

Algorithm is correct.

16
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.

17
Local minima in a GRID
in O(log ) time

18
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

19
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 )
Homework 20
Local minima in a grid

Theorem: A local minima in an × grid storing distinct elements


can be found in O( log ) time.

How to improve it to O()


?

21
Local minima in a grid in O() time
Let us carefully look at the calculations of the running time of the current algo.

= O() (log terms)

What about the following series

=? (log terms)

It is = O() .

Get an !DEA from this series to modify our current algorithm

22
Local minima in a grid in O() time

Bisect alternatively along rows and column

Try on your own.

We shall revisit it
towards the end of
the course.

23
Quiz 1
• Date: 20th August, 2022
• Time: 9:00 AM to 10:45 AM

Details of seating plan will be posted today.

24

You might also like