CS210 Lecture 5 Proof of Correctness Solving Local Minima in Grid
CS210 Lecture 5 Proof of Correctness Solving Local Minima in Grid
(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.
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.
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
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”
}
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
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()
16
Local minima in an array
A local minima exists
•
in this region. Why ?
23
17
A 9 17 23
𝑖
17
Local minima in an array
•
23
17
A 9 17 23
𝑖
• 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)
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)
•
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