You are on page 1of 22

Data Structures and Algorithms

(CS210/ESO207/ESO211)

Lecture 5
On proving correctness of iterative algorithms
Local minima in a grid (solution from scratch)

Proving Correctness of Binary Search


Int Binary-Search(A,x)
{

L 0;
R n-1;
found FALSE;
while(L R and found = FALSE)
{
mid (L + R)/2;
If (A[mid] = x) found TRUE;
else if(A[mid] < x) L mid + 1;
else R mid - 1
}
if found return mid else return -1;

We need to show:
If found=FALSE, then x is not present in A.

For this purpose it suffices if you show


that
In the beginning of each iteration
x is not present in A[0,..,L-1]
x is not present in A[R+1,,n-1]

Two simple principles

1. Respect every new idea which solves the problem 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.

Local minima in a grid


Problem: Given a nn grid storing distinct numbers, output any local
minima in O(n) time.

A new approach
Repeat : if current entry is not local minima, move to a neighbor storing smaller value.
j

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

Worst case time complexity : O(2 )

First principle:
Do not discard Explore()

Second principle:
Simplify the problem

Local minima in an array


23
17

9 17 23

i
There is a local minima
in A[0,,i-1]. Give reasons.

Reason (only a sketch):


If we execute Explore() from A[i-1], it will
terminate without ever entering A[i,,n-1].

Local minima in an array


(Similar to binary search)

Int Local-minima-in-array(A)
{

L 0;
R n-1;
found FALSE;
while( not??
found )
{
mid (L + R)/2;
If (mid is a local minima) found TRUE;
else if(A[mid+1] < A[mid]) L mid+1
??
;
R mid-1
else ??
}
return mid;

Proving correctness of this algorithm


We need to show that algorithm Local-minima-in-array(A) succeeds in finding
a local minima.
For this purpose, it suffices if we can show that
In the beginning of each iteration the following assertions hold.
A[L] < A[L-1]
A[R] < A[R+1]
Of course, these two assertions hold in the
beginning of 1st iteration. How to show that they
hold in the beginning of any arbitrary iteration ?
Ponder over it

Local minima in an array

Theorem: A local minima in an array storing n distinct elements


can be found in O(log n) time.

10

Local minima in a grid


(extending the solution from 1-D to 2-D)
There is a local minima
in this region. Give reasons.
mid

Execute Explore()
from M[i,mid+1]

9 7

11

Local minima in a grid


Int Local-minima-in-grid(M)
{

// returns the column containing a local minima

L 0;
R n-1;
found FALSE;
while(not found)
{
mid (L + R)/2;
If (M[*,mid] has a local minima) found TRUE;
else { let M[i,mid] be the smallest element in M[*,mid]
if(M[i,mid+1] < M[i,mid]) L mid+1
??
;
else ??R mid-1
}
}
return mid;

O(n) time
O(n) time

O(n)
time

}
12

Proving correctness of this algorithm


We need to show that algorithm Local-minima-in-grid(M) succeeds in finding
a column containing a local minima.
For this purpose, it suffices if we can show that
In the beginning of each iteration, the following two assertions hold.
There is an entry in M[*,L] which is smaller than every entry in M[*,L-1]
There is an entry in M[*,R] which is smaller than every entry in M[*,R+1]
Of course, these two assertions hold in the
beginning of 1st iteration. How to show that they
hold in the beginning of any arbitrary iteration ?
Ponder over it

13

Local minima in a grid

Theorem: A local minima in an nn grid storing distinct elements


can be found in O(n log n) time.

How to improve it to O(n) ?

14

Local minima in a grid in O(n) time


Bisect alternatively along rows and column

15

Summary of the Lecture


1.
2.

We developed an O(n) time algorithm for local minima in a grid.


(I hope you would have found it interesting and inspiring :-)
We discussed a common approach to prove correctness of iterative (loop
based) algorithms:
Try to establish a relation between correctness of algorithm in terms of some
assertions which must hold in the beginning of each iteration of the loop.
Formulating the appropriate assertions requires a better understanding of the
execution of the loop. (We formulated the assertions for three problems in
this lecture).
The student is encouraged to ponder over the ways to prove correctness of
the assertions before every iteration of the loop.
(We did not discuss this part in the lecture. We shall discuss it sometime later
in the course when you would have developed more maturity in algorithms.)

16

Important note: It is perfectly normal if you could not fully grasp


the details of point 2 mentioned in the previous slide. It takes time to
internalize a totally new concept. But it is important that you realize the
importance, non-triviality of the correctness of an algorithm. In addition, you
should understand the sketch of the process outlined in this lecture to
establish correctness of an iterative algorithm.
For the time being, experience the joy of finding an O(n) time algorithm for
local minima in a grid
17

Homework

In the next few slides, some homework problems are given.


Make sincere attempts to solve these problems.

18

Homework (general)
Merging two sorted arrays:
Given an arrays A storing 2n elements such that A[0,,n-1] is sorted, and
A[n,,2n-1] is also sorted. Design an O(n) time algorithm to sort A.
For example, if A={1,5,17,19,4,5,9,13} in the beginning, then after the
algorithm it should be A={1,4,5,5,9,13,17,19}. Your algorithm may use an
additional array C of size 2n if you wish.

19

Homework (general)
Partitioning an array:
Given an arrays A storing n elements and an element x present in A,
rearrange the elements of A such that if i is the index of x in A, then
A[0,,i-1] contains only elements of A smaller than or equal to x, and
A[i+2,,n-1] contains the rest of the elements of A. Your algorithm may use
only a constant number of additional variables (hence only O(1) extra space is
permitted). Design an O(n) time algorithm for this task.

20

Homework: revisiting 2-majority element


A simpler algorithm for 2-majority element:
Recall from Lecture 4 that an element x in an Array A storing n elements is
called a 2-majority element if it appears more than n/2 times in A. We
discussed an O(n) time algorithm for finding a 2-majority element, if exists, in
A. Here is a sketch of a simpler O(n) time algorithm which is also based on the
same idea: Pair the elements of A as follows: (A[0], A[1]),
(A[2],A[3]),,(A[2i],A[2i+1]),...and so on. Discard all pairs which have distinct
elements. For the remaining pairs, just keep a single element. If n is even, the majority
element is preserved in these elements. Proceed in this manner.

Design an O(n) time algorithm based on the above sketch. For this purpose, you will
need to answer the following question:
How will you take care of the case when A has odd number of elements ?

21

An interesting problem (optional)


We discussed an O(n) time algorithm to find a local minima in a
nn grid. Interestingly, it is also the optimal algorithm for this
problem. In other words, there can not be any algorithm for this
problem which takes less than O(n) time. You might like to
ponder over the proof of this fact.
Note: This problem is only meant for those students whose
expectation from this course is more than a good grade.

22

You might also like