# recursive algorithms & program correctness 1.algorithm. 2.recursive algorithms 3.algorithm...

Post on 05-Jan-2016

219 views

Embed Size (px)

TRANSCRIPT

Slide 1

Recursive Algorithms &Program CorrectnessAlgorithm.Recursive AlgorithmsAlgorithm Correctness-Program. Problem : Calculate area = *r21) Input the radius of the circle, r.2) Calculate the area of the circle, area.3) Print the value of the area

Definition: An algorithm is a finite set of instructions for solving a problem.Definition: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.Example :Give an recursive algorithm for computing n!, where n is a nonnegative integer.Solution: The factorial function is defined recursively as:0! =1n! = n (n-1)!, n>0The recursive algorithm for n! is as follows:Procedure factorial (n:nonnegative integer)If n=0 then factorial(n) :=1else factorial(n) := n factorial(n -1)

Example : Give a recursive algorithm for computing an, where a is nonzero real number and n is a nonnegative integer.Solution: The function an is defined recursively as a0 = 1an = a an-1The recursive algorithm for an is as follows:

Procedure power(a:nonzero real number, n: nonnegative integer)If n = 0 then power(a, n) :=1else power(a, n) := a power(a, n -1)Algorithm Correctness :The algorithm produce the desired output for all possible input values.Example :Prove that the algorithm which computes powers of real numbers, power(a,n), is correct.Solution: We use mathematical induction on n.Basic step: If n=0 power(a,0)=1. This is correct because a0 = 1 for every nonzero real number a.Inductive step: assume that power(a,k)= ak is correct for all nonzero real number a and for all nonnegative integer k.We will prove that the algorithm correctly computes ak+1. When the algorithm computes ak+1 , the algorithm setsPower(a,k+1)=a . Power(a,k) = a ak = ak+1Procedure power(a,n) If n = 0 then power(a, n) :=1else power(a, n) := a power(a, n -1)

A proof that a program is correct consists of two parts. It shows that the correct answer is obtained if the program terminates. This is called partial correctness of the program.It shows that program always terminates.Program VerificationDefinition: A program is said to be correct if it produces the correct output for every possible input.

To specify what it means for a program to produce the correct output, two propositions are used:Initial Assertion: It gives the properties that input values must have.Final Assertion: It gives the properties that the output of the program should have, if the program did what was intended.Definition: A program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program segment, S is partially correct with respect to the initial assertion p and the final assertion q.

Example :Show that the program segment y := 2z := x + y , is correct with respect to the initial assertion p : x = 1 and the final assertion q: z = 3.Solution: Initial assertion // P : X = 1 Final assertion // q : z = 3 xyz121+2 = 3 Thus, p{S}q is true.Suppose that p is true, so that x=1 as the program begins.Then y is assigned the value 2, and z is assigned the sum of x and y, which is 3.Hence, S is correct with respect to the initial assertion p and the final assertion q.Thus, p{S}q is trueA useful rule of inference proves that a program is correct by splitting the program into a series of subprograms and then showing that each subprogram is correct.Rules of Inference Method:(the composition rule)# Suppose that the program S is split into subprogram S1 and S2. Write S = S1: S2 to indicate that S is made up of S1 followed by S2. # Suppose that the correctness of S1 with respect to the initial assertion p and final assertion q, and the correctness of S2 with respect to the initial assertion q and the final assertion r, have been established. It follows that: (i) if p is true and S1 is executed and terminates, then q is true; and (ii) if q is true; and S2 executes and terminates, then r is true. Thus, if p is true and S = S1: S2 is executed and terminates, then r is true. This rule of inference, called the composition rule, can be stated asp{S1}qq{S2}r p{S1; S2}r.

The general format for conditional statement is: If condition then S Where S is a block of statements. Meaning: the statement S is executed if condition is true. And it is not executed when condition is false.Conditional StatementsTo verify that this segment is correct with respect to the initial assertion p and final assertion q, First, it must be shown that when p is true and condition is also true, then q is true after S terminates. Second, it must be shown that when p is true and condition is false, then q is true (in this case S does not execute).The following rules of inference applied;(p ^ condition) {S} q(p ^ condition ) .q p {if condition then S} qConditional StatementsExample : Verify that program segmentIf x > y theny := xIs correct with respect to the initial assertion T and the final assertion y x.Solution:When the initial assertion is true and x > y, then assignment y:=x is carried out. Hence the final assertion, which assert that y >= x, is true in this case. When the initial assertion is true and x > y is false, so that x = 0. Initial assertion // P : y = 3 Final assertion // q : z = 6 As initial assertion y = 3, then from the program segment x = 2 z = x + y = 2 + 3 = 5 as y = 3 > 0 then z = 5 + 1 = 6 , which is the final assertion.

Recommended