Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | ambrose-logan |
View: | 216 times |
Download: | 0 times |
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
Example
• Define the factorial function n! as…
Example
• For a non-zero real number a, define the non-negative integer powers of a as follows:
Example: The Fibonacci Numbers
• Basis: f0 = 0 and f1 = 1.
• Recursive step:
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.
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.
Example: Give a recursive definition of the set of positive integers not divisible by 5.
Example: Strings over an alphabet
Let be an alphabet of symbols, say or . We define the set of strings over as follows:
4.4 Recursive Algorithms• Definition
2/n
•Example:procedure intPower(a: real, n: integer)
{We assume that if a = 0 then n > 0.}
if a = 0 then result := 0else if n = 0 then result := 1else 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 * aend
{‘result’ contains the value an. }
procedure intPower(a: real, n: integer){We assume that if a = 0 then n
> 0.}if a = 0 then result := 0else if n = 0 then result := 1else 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 * aend
{‘result’ contains the value an. }
Example:
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.
Theorem: For all real numbers and integers , provided when , the previous algorithm correctly computes .
Proof:Let and n be specific numbers satisfying the
hypothesis.If , then and , as computed by the algorithm, so it
suffices to show the correct value is achieved when .
If a is 0, then n > 0, so an is 0, the value returned.
On the other hand, if is not 0, let P(n) be the statement, “The algorithm computes correctly.”
BASIS STEP: P(0) is true, since when n = 0 the value 1 = a0 is assigned to result.
INDUCTION HYPOTHESIS: Now let k be a positive integer for which P(0), P(1), P(2), …, P(k) are all true.
INDUCTION STEP: Then when the algorithm is invoked with n = k+1, we compute m as . Since when k+1 > 0 we have < k+1, we know that P(m) is true. So intPower(a, m) correctly computes am.
If k+1 is even, then m = (k+1)/2 and ak+1 = a(k+1)/2a(k+1)/2 = amam = ak+1, and so the algorithm correctly computes ak+1.
2/)1( k
2/)1( k
22
1
22/)1(
kkkm
Finally, if k+1 is odd, then is even and
So ak+1 = aka = ak/2ak/2a = amama and again the algorithm correctly computes ak+1. So P(k+1) is true.
By the 2PMI, P(n) is true for all natural numbers n.
Another Example: Merge Sortprocedure mergesort(a1, a2, …, an: integers)
if n > 2 then beginmergesort(a1,a2,…,an/2 )mergesort(a n/2 ,a n/2+1,…,an)
endi := 1j:= n/2k := 1while i≤ n/2 or j ≤ n do begin
if j > n or ai < aj then begin bk := ai , i := i+1 endelse begin bk := aj , j := j+1 endk := k + 1
endcopy b1, b2, …, bn into the corresponding elements a1, a2, …, an
{ The values a1, a2, …, an now appear in ascending order }
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
Terminology:
• A program is said to be correct if
• Partial Correctness is established by
• The Initial Assertion gives
• The Final Assertion gives
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
Rules of Inference• Composition
S = S1;S2
p{S1}q
q{S2}r
• Conditional Statements
(pcondition){S}q(pcondition)q
if condition then
Rules of Inference Continued
(pcondition){S1}q
(pcondition){S2}q
(pcondition){S}p
if condition then
else
while condition
• Loop Invariants
Example: Verify that the program segment
x := 2z := x+yif y>0 then z := z+1else z := 0
is correct with respect to the initial assertion y=3 and the final assertion z=6.