Professional Documents
Culture Documents
Lec05 Proof of Correctness Solving Local Minima in Grid
Lec05 Proof of Correctness Solving Local Minima in Grid
Lec05 Proof of Correctness Solving Local Minima in Grid
(ESO207)
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
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.
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
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
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
𝑖
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,…, ].
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
minima ? 21
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 exists
if thereeven
circumstances is no this
inlocal
this minima
region. Why
in the?
smallest 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