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

CS210 Lecture 5 Proof of Correctness Solving Local Minima in Grid

This document discusses algorithms and proofs of correctness. It begins with an overview of proving correctness of iterative algorithms through induction. It then presents an O(n) time algorithm for finding the maximum sum subarray using dynamic programming. Finally, it discusses finding local minima in arrays and grids. It presents an O(log n) time algorithm for finding a local minimum in an array by applying a binary search like approach. It extends this idea to search for local minima in columns of a grid to find a local minimum cell in the entire grid in O(n) time.

Uploaded by

Vinod Kanwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

CS210 Lecture 5 Proof of Correctness Solving Local Minima in Grid

This document discusses algorithms and proofs of correctness. It begins with an overview of proving correctness of iterative algorithms through induction. It then presents an O(n) time algorithm for finding the maximum sum subarray using dynamic programming. Finally, it discusses finding local minima in arrays and grids. It presents an O(log n) time algorithm for finding a local minimum in an array by applying a binary search like approach. It extends this idea to search for local minima in columns of a grid to find a local minimum cell in the entire grid in O(n) time.

Uploaded by

Vinod Kanwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures and Algorithms

(CS210A)

Lecture
  5:
• More on Proof of correctness of an algorithm
• Design of O() time algorithm for Local Minima in a grid

1
PROOF OF CORRECTNESS

2
What does correctness of an algorithm mean ?

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

Let us take a toy algorithm 

3
Algorithm for computing
  from to
sum of numbers
•  
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.
4
How will you respond
if you have to do it for the following code ?

5
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 .

6
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()
7
Algorithm for computing
  from to
sum of numbers
•  
{ 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.
8
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”
}
Assertion P() :  ?S[] stores the sum of maximum sum subarray ending at A[].
Homework: Prove that P() holds for all
9
LOCAL MINIMA IN A GRID

10
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.
 𝒋

31
 𝒊 5 33 10
99

11
Local minima in a grid

•  
Problem: Given a × grid storing distinct numbers, output any local minima
in O() time.  𝒋

31
 𝒊 5 33 10
99

12
Two simple principles

1. Respect every new idea which solves a problem even partially.

2. Principle of simplification:
If you find a problem difficult,
 try to solve its simpler version, and then
 extend this solution to the original (difficult) version.

13
A new approach
Repeat : if current entry is not local minima, explore the neighbor storing smaller
value.
j

i 3

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

15
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;
}
Worst case time complexity : O()

First principle: Second principle:


Do not discard Explore() Simplify the problem

16
Local minima in an array
A local minima exists
•  
in this region. Why ?
23
17

A 9 17 23
 𝑖

Theorem: There is a local minima in A[0,…, ].


Proof: Suppose we execute Explore() from A[].
Explore(), if terminates, will return local minima.
It will terminate without ever entering A[,…, ].
Algorithmic proof
Hence there is a local minima in A[0,…, ].

17
Local minima in an array
•  
23
17

A 9 17 23
 𝑖

Theorem: There is a local minima in A[0,…, ].

 We can confine our search for local minima to only A[0,…, ].


Our problem size has reduced.
Question: Which should we select so as to reduce problem size significantly ?
Answer: point of array A.
18
Local minima in an array
(Similar to binary search)

•  Local-minima-in-array(A) {
int   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 ?

19
Local minima in an array
(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[,…, ].”

20
Local minima in an array
(Proof of correctness)

•  
 𝑳  𝑹

A
P() : At the end of th iteration,
“A local minima of array A exists in A[,…, ].”

=
“A[]< A[]” and “A[]< A[]”.
Homework:
• Make sincere attempts to prove the assertion P().
• How will you use it to prove that Local-minima-in-array(A) outputs a local
21
minima ?
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.

22
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
inlocal
this minima
smallest
exists
if thereeven
is no this
region. Why
in the
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.
… and do not forget to prove its correctness . 23
Make sincere attempts to
answer all questions raised in this lecture.

24

You might also like