0% found this document useful (0 votes)
316 views24 pages

Lec05 Proof of Correctness Solving Local Minima in Grid

This document discusses algorithms and proofs of correctness. It begins with an example algorithm to compute the sum of numbers from 0 to n and proves its correctness through induction. It then discusses finding the maximum sum subarray in O(n) time by storing partial sums in an array S and proving a theorem about S. Finally, it discusses finding a local minimum in a grid by reducing the 2D problem to 1D, showing there is a local minimum in each row/column, and using a binary search approach to find it in O(logn) time. The key ideas are using induction to prove iterative algorithms, reducing multidimensional problems to 1D, and exploiting properties like existence of local minima to design efficient algorithms.

Uploaded by

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

Lec05 Proof of Correctness Solving Local Minima in Grid

This document discusses algorithms and proofs of correctness. It begins with an example algorithm to compute the sum of numbers from 0 to n and proves its correctness through induction. It then discusses finding the maximum sum subarray in O(n) time by storing partial sums in an array S and proving a theorem about S. Finally, it discusses finding a local minimum in a grid by reducing the 2D problem to 1D, showing there is a local minimum in each row/column, and using a binary search approach to find it in O(logn) time. The key ideas are using induction to prove iterative algorithms, reducing multidimensional problems to 1D, and exploiting properties like existence of local minima to design efficient algorithms.

Uploaded by

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

Data Structures and Algorithms

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

Let us take a toy algorithm J

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.

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

6
Proof of correctness
For an iterative algorithm

Insight of the algorithm Theorem


Assertion Assertion
P(𝑖 − 1) P(𝑖)

Start 𝑖−1 𝑖 Iterations


1 2 3 4
of Loop

Prove P(𝑖) by What would you expect


at the end of 𝑖th
1. Assuming
? P(𝑖 − 1) 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
sum of numbers from 𝟎 to 𝒏
{ Sumß0;
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(0) holds.


Assuming P(𝑖 − 1), 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(𝑖 − 1) > 0 then S(𝑖) = S(𝑖 − 1) + A[𝑖]


else S(𝑖) = A[𝑖]

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

Definition: Given a 𝒏 × 𝒏 grid storing distinct numbers, an entry is local


minima if it is smaller than each of its neighbors.
𝒋

31
𝒊 5 3 10
99

11
Local minima in a grid

Problem: Given a 𝒏 × 𝒏 grid storing distinct numbers, output any local


minima in O(𝒏) time. 𝒋

31
𝒊 5 3 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,…, 𝑖 − 1].


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

17
Local minima in an array

23
17

A 9 17 23
𝑖

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

è 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)

int Local-minima-in-array(A) { O(log 𝒏)


L ß 0;
How many
R ß 𝒏 − 𝟏; iterations ?
found ß FALSE;
while( not?? found )
{
𝒎𝒊𝒅 ß (L + R)/2;
If (𝒎𝒊𝒅 is a local minima)
O(𝟏) time
found ß TRUE; in one iteration
else if(A[𝒎𝒊𝒅 + 𝟏] < A[𝒎𝒊𝒅]) Lß 𝒎𝒊𝒅
?? + 𝟏 ;
else ??Rß 𝒎𝒊𝒅 − 𝟏
}
return 𝒎𝒊𝒅; }
Proof of correctness ?
è Running time of the algorithm = O(log 𝒏)
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 local21
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[∗, 𝒎𝒊𝒅 ] 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

You might also like