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ß0;
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
Assertion P(𝑖) : At
? the end of 𝒊th iteration Sum stores the sum of numbers from 𝟎 to 𝒊.
Max-sum-subarray-algo(A[0 … 𝒏 − 𝟏])
{ S[0] ß A[0]
for 𝑖 = 1 to 𝒏 − 𝟏
{ If S[𝑖 − 1] > 0 then S[𝑖] ß S[𝑖 − 1] + 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 𝑖 ≤ 𝑛 − 1
9
LOCAL MINIMA IN A GRID
10
Local minima in a grid
31
𝒊 5 3 10
99
11
Local minima in a grid
31
𝒊 5 3 10
99
12
Two simple principles
2. Principle of simplification:
If you find a problem difficult,
è try to solve its simpler version, and then
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
𝑖
è We can confine our search for local minima to only A[0,…, 𝑖 − 1].
è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)
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 local21
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[∗, 𝒎𝒊𝒅 ] A What
local minima
circumstances
exists
if thereeven
is nothis
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.
23
… and do not forget to prove its correctness J.
Make sincere attempts to
answer all questions raised in this lecture.
24