You are on page 1of 24

Data Structures and Algorithms

(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

What does correctness of an


algorithm mean ?

For every possible valid


the input,
algorithm must output correct answer.
Let us take a toy
algorithm

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 ?

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
in algorithm
a formal way .

Proof of correctness
For an iterative algorithm

Insight of the algorithm Theorem


Asserti

Asserti

on
P()

Start
of Loop

on
P()

Iterations

Prove P() by
What would you
expect at the end
1. Assuming
?
P()
of th iteration ?
2.Theorem
?
Proof by induction
3.Body
? of the Loop
The most difficult/creative part of
: with the right
?
To proof
come

up
assertion P()
7

Algorithm for computing

sum of numbers
from to
Sum;
for = 1 to
{

{
Sum Sum + ;

}
return Sum;
}

Assertion
: ? of th iteration Sum stores the sum of numbers from t
At P()
the
end
Base case: P() holds.
Assuming P(), assertion P() also holds.
P() holds.
8

An O() time Algorithm for Maxsum subarray

Let S(): the sum of the maximum-sum subarray ending at index .

Theorem 1 : If S() > 0 then S() = S() + A[]


else S() = A[]
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
? sum of maximum sum subarray ending at A[].
S[]P()
stores
:
the
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


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

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

How to apply this


principle ?

Worst case time complexity : O()

First principle:
Do not discard
Explore()

Second principle:
Simplify the problem
16

Local minima in an array


A local minima
exists
in this region.
Why ?

1
7

2
3

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



1
7

2
3

17 23

Theorem: There is a local minima in A[0,, ].


We can confine our search for local minima to only A[0,, ].
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
Running time of the algorithm = O(log )
correctness ?
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)
21
outputs a local 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


tominima
2-D)
Under
what
A local

circumstances
exists
Search for a local minima in the column M[, ] What
if there is no

Execute

Explore() from
M[, ]

Smallest element
of the column

even
smallest
in
thisthis
region.
local
minima
in
element
is
not
a
Whyentire
?
the
column.
local minima ?

9 7

Homework:
Use this idea to design an O(log ) time algorithm for this
problem.
23

Make sincere attempts to


answer all questions raised in this lecture.

24

You might also like