Presentation is loading. Please wait.

Presentation is loading. Please wait.

4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively.

Similar presentations


Presentation on theme: "4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively."— Presentation transcript:

1 4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively defined algorithms They are also valuable in investigating the soundness of a recursive definition We say that a definition is recursive provided the term, concept, set, or function being defined is referred to (referenced) within the text of its own definition Like an induction proof, a properly formed recursive definition has a BASIS step and a RECURSIVE step

2 Example Define the factorial function n! as…

3 Example For a non-zero real number a, define the non- negative integer powers of a as follows:

4 Example: The Fibonacci Numbers Basis: f 0 = 0 and f 1 = 1. Recursive step:

5 Recursively Defined Sets We can define a set S recursively. The basis step and recursive step have a slightly different flavor from that of function definitions and sequence definitions. BASIS: Here we simply list one or more elements which must be in S, to get the recursion started. RECURSIVE STEP: Here we usually have one or more implications, which state that if certain elements are in the set then other elements derived from them are also in the set.

6 Recursively Defined Sets Example Give a recursive definition for the set S of all integers which can be the total amount of postage represented by some nonempty set of 3 and 5 cent stamps.

7 Example: Give a recursive definition of the set of positive integers not divisible by 5.

8 Example: Strings over an alphabet

9 4.4 Recursive Algorithms DefinitionExample: procedure intPower(a: real, n: integer) {We assume that if a = 0 then n > 0.} if a = 0 then result := 0 else if n = 0 then result := 1 else if n < 0 then result := 1 / intPower(a,-n) else begin m := p := intPower(a,m) result := p*p if n mod 2 = 1 then result := result * a end {‘result’ contains the value a n. }

10 procedure intPower(a: real, n: integer) {We assume that if a = 0 then n > 0.} if a = 0 then result := 0 else if n = 0 then result := 1 else if n < 0 then result := 1 / intPower(a,-n) else begin m := p := intPower(a,m) result := p*p if n mod 2 = 1 then result := result * a end {‘result’ contains the value a n. }

11 Strong Induction Proofs of Correctness Every recursive algorithm should have at least one base case, which is a set of conditions under which no recursive call is made. In every case in which a recursive algorithm calls itself, it should supply a set of parameters which move it “closer to” a base case. Strong induction is the tool of choice for proving the correctness of recursive algorithms To prove using strong induction that a recursive algorithm works: – Prove that it works for all the base cases – Prove that if it works for all cases “closer to” the base case, then it works for the current case.

12

13

14

15 Another Example: Merge Sort procedure mergesort(a 1, a 2, …, a n : integers) if n > 2 then begin mergesort(a 1,a 2,…,a  n/2  ) mergesort(a  n/2 ,a  n/2  +1,…,a n ) end i := 1 j:=  n/2  k := 1 while i≤  n/2  or j ≤ n do begin if j > n or a i < a j then begin b k := a i, i := i+1 end else begin b k := a j, j := j+1 end k := k + 1 end copy b 1, b 2, …, b n into the corresponding elements a 1, a 2, …, a n { The values a 1, a 2, …, a n now appear in ascending order }

16

17 4.5 Program Correctness “There is nothing a mere scientist can say that will stand against the flood of a hundred million dollars. But there is one quality that cannot be purchased in this way — and that is reliability. The price of reliability is the pursuit of the utmost simplicity. It is a price which the very rich find most hard to pay." – C.A.R. (Tony) Hoare

18 Terminology: A program is said to be correct if Partial Correctness is established by The Initial Assertion gives The Final Assertion gives

19 Hoare Triples A Hoare triple has the form p {S} q – Which means “S is partially correct with respect to the initial assertion p and the final assertion q” – Assumes S terminates

20 Rules of Inference Composition S = S 1 ;S 2 p{S 1 }q q{S 2 }r Conditional Statements (p  condition){S}q (p  condition)  q

21 Rules of Inference Continued (p  condition){S 1 }q (p  condition){S 2 }q (p  condition){S}p Loop Invariants

22 Example: Verify that the program segment x := 2 z := x+y if y>0 then z := z+1 else z := 0 is correct with respect to the initial assertion y=3 and the final assertion z=6.


Download ppt "4.3 Recursive Definitions and Structural Induction The PMI, 2PMI, and WOP are all valuable tools in proving the correctness of loops and of recursively."

Similar presentations


Ads by Google