# Lecture 5

Document Sample

```					Algorithms and Complexity Theory

UKZN - Computer Science - 2009

ALGORITHMS and COMPLEXITY THEORY

Lecture 5
Induction and Recursion 2

Lecture 5: Induction and Recursion 2

1

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Assertions
• Consider an algorithm that is designed to produce a certain ﬁnal state from an initial state. • Initial state, Final state = Predicates( input variables, output variables) • pre-condition = predicate describing the initial state • post-condition = predicate describing the ﬁnal state.

Lecture 5: Induction and Recursion 2

2

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Examples
• The algorithm to compute a product of nonnegative integers – pre-condition : The variable m and n are nonnegative integers. – post-condition: The output variable p equal m × n. • The algorithm to ﬁnd the quotient and the remainder of the division of one positive integer by another. – pre-condition : The inputs a and b are positives integers – post-condition :The output variables q and r are integers such that a = b × q + r and 0 ≤ r < b. • The Algorithm to sort a one-dimension array of real numbers. – pre-condition : the input variable A[1], A[2], . . . , A[n] is a one-dimensional array of real numbers. – post-condition :The array B[1], B[2], . . . , B[n] is a one-dimension array with same elements as A[1], A[2], . . . , A[n], such that B[i] ≤ B[j] for i ≤ j.

Lecture 5: Induction and Recursion 2

3

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Correctness of an algorithm
• The proof of correctness of an algorithm consists of showing that if the preconditions for the algorithm is true for collection of values for the input variables and if the statements of the algorithm are executed , then the post-condition is also true. • To prove the correctness of an algorithm we will divide this algorithm into sections with assertions about current state of algorithm variables inserted at adequate chosen points;
[Assertion 1: precondition of the algorithm] {algorithm statements} [Assertions 2] {algorithm statements} ..... ..... [Assertions k-1] {algorithm statements} [Assertion k: post-condition of the algorithm]

successive pairs of assertions are treated as pre- and post-conditions for algorithms between them. Then for i = 1, 2, . . . , k − 1 , IF we can prove that if assertion i is true and algorithm statement between assertion i and assertion i+1 are executed , then assertion i+1 is true, THEN the algorithm is correct.

Lecture 5: Induction and Recursion 2

4

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Loop Invariants
• Loop invariant is a condition on the variables within a loop , which remains true during the iterations of the loop. • The method of loop invariants is used to prove the correctness of a loop with respect to certain pre- and post-conditions. • It is base on the principle of Mathematical induction. • Suppose an algorithm contains a while loop and that entry to this loop is restricted by a condition G, called guard. And this loop is speciﬁed as follows:
[pre-condition of the loop] WHILE(G) [Statements in the body of loop. with no statement branching outside the loop] [END WHILE]

Then there theorem assuring that the loop is correct.
Lecture 5: Induction and Recursion 2 5

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Loop Invariant Theorem
Let a while Loop with a Guard G be given , together with pre- and post -conditions, and a loop invariant I(n). If the following four properties are true, then the loop is correct with respect to its pre- and post-conditions:
A. Basis property : the precondition for the loop implies that I(0) is true before the ﬁrst iteration of the loop. B. Inductive property : If the G ∧ I(k) =true for k ≥ 0 before an iteration of the loop, then I(k + 1) is true. C. Eventual Falsity of Guard : After a ﬁnite number iterations of the loop, the guard becomes false. D. Correctness of the post-condition: If N is the least number of the iterations after which G is false and I(N ) is true, then the values of the algorithm will be as speciﬁed in the post-condition of the loop.

Lecture 5: Induction and Recursion 2

6

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Example : loop to compute product
• we want to use a loop to compute the product m × x, we have the variables i and product with i = 0 and product = 0. • the loop is deﬁned as follows:
[pre-condition: m is a nonnegative integer, x is a real, i=0, and product =0] WHILE ( i<>m ) (1) product := product +x (2) i := i+1 ENDWHILE [post-condition: product = m*x]

• the loop invariant is I(n) : i = n and product = n.x and the guard G of the while loop is G : n <> i.

Lecture 5: Induction and Recursion 2

7

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

loop to compute product (cont’d)
A. Basis property : implies that I(0) is true before the ﬁrst iteration of the loop. I(0) is " i = 0 and product = 0.x" which is true before the ﬁrst iteration. B. Inductive property : If the G ∧ I(k) =true for k ≥ 0 before an iteration of the loop, then I(k + 1) is true. productold = k.x and i = k since i <> m, the guard is passed and from the statement (1) we have a productnew = productold + x = k.x + x = (k + 1)x, after execution of statement (2) we have inew = iold + 1 = k + 1 C. Eventual Falsity of Guard : After a ﬁnite number iterations of the loop, the guard becomes false. for all n ≥ 0 , if the loop is iterated n times, then i = n and product =n.x. so after m iteration of the loop, i =m. thus G becomes false after m iterations. D. Correctness of the post-condition: If N is the least number of the iterations after which G is false and I(N ) is true, then the values of the algorithm will be as speciﬁed in the post-condition. G is false and I(n) is true means product = m.x

Lecture 5: Induction and Recursion 2

8

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Example : Binary search
public static void search(int First, int Last, int Key, int [] Vector) { if (First > Last ) // Base case Found = false; else { // Recurrent cases int Middle = (First+Last)/ 2; if (Key < Vector[Middle]) search(First, Middle-1,Key, Vector); else if (Key >Vector[Middle]) search(Middle+1,Last,Key,Vector); else { Position = Middle; Found = true; } } }

Lecture 5: Induction and Recursion 2

9

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

Example : Binary search(cont’d)
We will prove by induction that search algorithm is correct. Induction basis The base case is the case in which there is no element between First and Last, and in this case Found = false. Induction hypothesis Choose any value for First and Last. These 2 values provide an interval, and the inductive hypothesis is that the procedure works for all proper subintervals of this interval. Induction step Since the array Vector is sorted., Key must be in the interval from First to Middle -1 if Key< Vector[Middle],a nd it must be in the interval Middle+1 to Last if Key > Vector[Middle]. Both interval are proper subintervals of the interval with the end points First and Last, and by induction the procedure is correct for those intervals. Then the procedure works correctly.

Lecture 5: Induction and Recursion 2

10

Algorithms and Complexity Theory

UKZN - Computer Science - 2009

• Read section 3 of Chapter 2 [ Induction and recursion] of your notes. • Do some Extra Reading: chapter 2 of Wilf(Algorithms and Complexity). • Prepare your tutorial 2 • Do your homework 2

Lecture 5: Induction and Recursion 2

11

```
DOCUMENT INFO
Shared By:
Categories:
Tags: Lecture
Stats:
 views: 126 posted: 12/16/2009 language: English pages: 6
Description: Lecture 5
How are you planning on using Docstoc?