# Induction and Recursion - PowerPoint PPT Presentation

PPT – Induction and Recursion PowerPoint presentation | free to download - id: 82e82d-ZTc5M

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Induction and Recursion

Description:

### Discrete Mathematics Chapter 4 Induction and Recursion – PowerPoint PPT presentation

Number of Views:5
Avg rating:3.0/5.0
Slides: 59
Provided by: Cheng61
Category:
Tags:
Transcript and Presenter's Notes

Title: Induction and Recursion

1
Chapter 4
Discrete Mathematics
• Induction and Recursion

2
Contents
• 4.1 Mathematical Inductions
• 4.2 Strong Induction and Well-ordering
• 4.3 Recursive definitions Structural Induction
• 4.4 Recursive algorithms
• 4.5 Program correctness

3
4.1 Mathematical Induction (MI)
• Principle of MI To show that a property p hold
for all nonnegative integer n, it suffices to
show that
• 1. Basis step P(0) is true
• 2. Inductive step P(n) ? P(n1) is true for all
nonnegative integer n.
• P(n) in 2. is called the inductive hypothesis.
• Notes 1. Math. Ind. is exactly the inference
rule
• P(0), "n P(n)?P(n1)
• --------------------------------
• "n P(n) for any
property P
• 2. If the intended domain is all positive
integers, then the basis step should be changed
to
• Basis step P(1) is true.

4
Examples
• Show that for all positive integers n,
• 1 23 n3 n2 (n1)2 /4.
• Pf Let P(n) denote the proposition1 23
n3 n2(n1)2/4.
• The proof is by induction on n.
• Basis step n 1. Then P(1) is true since 13
12x(11)2/4.
• Ind. step n k1 gt 1 for arbitrary
integer k gt 0. Assume p(k) holds, i.e., 1 23
k3 k2 (k1)2/4.
• Then 1 k3 n3 k2 (k1)2/4
(k1)3

• (k1)2 (k24k4)/4

• (k1)2(k2)2 /4. n2(n1)2 /4
• Hence p(k1) is also true.
• This completes the proof of basis step and
inductive step of MI, and hence by MI, p(n) holds
for all positive integers n.

5
Examples
• 2 Si1,n 2i-1 n2
• 3. n lt 2n
• 4. 3 n3 - n if n gt 0
• 5. Si1,n 2i 2(n1) -1
• 6. Sj1,n arj arn1 - a / (r -1)
• 7. Let Hk 1 1/2 ... 1/k gt H2n ³ 1 n/2
• 8. S n gt 2S 2n.
• 9. If n gt 3 gt 2n lt n!
• 10. (S1Ç ...ÇSn) S1 U ... U Sn.

6
• 7. Let Hk 1 1/2 ... 1/k. Then
• H2n ³ 1 n/2 for all non-negative integers
n.
• pf By induction on n.
• Let p(n) be the predicate H2n ³ 1 n/2
• Basis Step n 0.
• Then H20 H1 1 ³ 1 0/2. Hence p(0)
is true.
• Ind. Step Assume p(n) holds for any n ³ 0,
• I.e., H2n ³ 1 n/2 holds
for any n ³ 0.
• Then H2n1 1 1/2n 1/(2n1)
1/(2n2n)
• ³ H2n 2n x 1/(2n2n)
• ³ 1 n/2 ½ 1
(n1)/2.
• This establishes the ind. step of MI.
• As a result p(n), i.e., H2n ³ 1 n/2 , holds
for all nonnegative integers n.

7
More examples
• For every k ?12, there are m,n ? 0 s.t. k 4m
5n.
• pf By induction on k where k k-12.
• Basis k 0 (i.e., k k'12). Then
• k 12 4 x 3 5 x 0.
• Inductive step k t 1 gt 0 (i.e., k t 1
gt 12 )
• By Ind. Hyp., t 4m 5n.
• Then k t 1 4m 5n 1.
• if m gt 0 gt k 4(m-1) 5 (n1)
• if m 0 gt t 5n gt 11 gt n ? 3.
• hence t1 5(n-3) 15 1 4 x 4 5
(n-3).
• Q.E.D.

8
Correctness of MI.
• Correctness of MI Let p(.) be a property about
positive integers. If p(1) holds and p(n) implies
p(n1) for all n, then it is true that p(n) holds
for all positive integer n.
• Pf Assume MI is incorrect. Then
• the set NP k gt 0 p(k) does not hold
is not empty.
• Let m be the least number of NP
• -- existence implied by
well-order theorem
• Since p(1) holds, 1 Ï NP and m gt1.
• gt m-1 gt0 is a positive integer and p(m-1) holds
(o/w m would not be the least in NP)
• gt P(m) holds by the inductive step of MI
• gt m Ï NP, a contradiction.
• Hence the assumption that MI is incorrect
is wrong. Q.E.D.

9
Strong Induction and Well-Orering
• A problem MI is hard to prove
• If n is a positive integer gt 1, then n can be
written as a product of primes.
• To prove this theorem using induction, we needs a
stronger form of MI.

10
Strong Induction
• The 2nd form of MI(Strong Induction Complete
Induction) To prove that p(n) holds for all
non-negative integers n, where p(n) is a
propositional function of n, It suffices to show
that
• Basis step P(0) holds
• Inductive step P(0) /\ P(1) /\ ...,/\p(k-1) ?
P(k) holds for all k ? 0.
• I.e., Assume P(0),P(k-1) hold for arbitrary k,
and then show that p(k) is true as well.
• Notes
• P(0) /\ P(1) /\ ...,/\p(k-1) (or "t tltk ?P(k))
is called the induction hypothesis of the proof.
• If our intended domain is positive integers, then
the basis step is P(1) holds, and Ind.Hyp. is
P(1) /\ P(1) /\ ...,/\P(k-1)

11
Example
• Ex2 If n is a positive integer gt 1, then n can
be written as a product of primes.
• Pf Let p(n) be the proposition
• if ngt 1 then it can be written as a product of
primes.
• Basis step p(1) holds since ( n gt 1 ).
• Ind. step Let k be arbitrary positive
number and assume p(t) holds for all t lt k.
• There are two cases to consider
• case 1 k is a prime number, then p(k) holds
since k k is the product of itself.
• case 2 k is a composite number. Then by
definition, there are two numbers 1lt a, b lt k
such that k ab.
• By ind. hyp., p(a) and p(b) hold and since a,
b gt 1,
• a and b can be written as a product of
primes. Let a a1,,ai and b b1,bj, then k
a1ai x b1bj is a product of primes.

12
Correctness of complete Induction(CI) and
well-ordering .
• Correctness of CI Let p(.) be a property about
positive integers. If p(1) holds and p(1) /\ p(n)
/\p(n) implies p(n1) for all n, then it is true
that p(n) holds for all n.
• Pf Assume CI is incorrect. i.e.
• the set NP k p(k) is false is not
empty.
• Let m be the least number of NP
• ---existence by well-ordered property of
positive integers
• Since p(1), 1 Ï NP and m gt1.
• gt m-1 exists and p(m-1) is true
• gt P(m) holds by the inductive step of SI
• gt m Ï NP gt a contradiction.
• Q.E.D.

13
Well-ordered Property
• Well-ordered property of natural numbersEvery
non-empty subset of non-negative (or positive
integers) integers has a least element.
(???????(????)????????????)
• The property can be used directly in the proof
(in place of MI or SI).
• Ex In round-robin tournament, every player plays
every other exactly once and each match has a
winner and a looser. We say p1,p2,,pm form a
cycle of length m if p1 beats p2, p2 beats
p3,,pm beats p1. Show that if there is a cycle
of length m ³ 3, then there must exist a cycle of
3.

14
Ex 6
• pf Let C be the set n there is a cycle of
length n in the tournament. Obviously, m ?C and
C is a subset of non-negative integers. So by
well-ordering property, C has a least element,
say k.
• Let p1,p2,pk be such cycle. Since there is no
cycle of 1 or 2, k must ³ 3.
• If k 3, then we are done.
• O/w, k gt 3 and consider p1 and p3.
• If p3 beats p1, then p1,p2 , p3 is a cycle of
length 3 lt k. a contradiction.
• If p1 beats p3, then p1, p3,,pk form a cycle of
length lt k. This violates the fact that k is the
least element of C.
• As a result, k must 3.

15
Equivalence of MI,CI and well-ordered property
• We have used well-ordered properties (WO) of
natural numbers to show the correctness of MI and
CI.
• CI ? WOP ? MI
• The reverse direction also holds
• CI ? WOP ? MI
• Lemma Use CI to show WO (i.e., every nonempty
subset of nonnegative integers has a least
element).

16
CI implies WO
• Pf Let P(n) ?S?N, n?S ? S has a least
element.
• (every subset of N containing n has a least
number)
• Obviously if ?n P(n) holds, then since every
nonempty subset S of N contain a number, say k,
by p(k) S must have a least number.
• We now show that ?n P(n) by CI.
• Basis P(0) holds since every subset of N
containing 0 has 0 as its least element.
• Ind. case If S is a subset of N containing n gt0
, then either S contains a value k lt n or S
contains no value lt n. In the former case, by
I.H. P(k), S has a least element in the latter
case, then n is the least element in S.
• Exercise Use MI to show CI WO.

17
4.3 Recursive definitions and structural
Induction
• Different ways of defining a functions
• Explicit listing
• Suitable for finite functions only.
• Define by giving an explicit expression
• Ex F(n) 2n
• recursive (or inductive ) definition
• Define value of objects (sequences, functions,
sets, ...) in terms of values of smaller similar
ones.
• Ex the sequence 1,2,4,... (an 2n) can be
defined recursively as follows
• 1. a0 1
• 2. an1 2 x an for n gt 0.

18
Recursively defined functions
• To define a function f N ? D with the set of
nonnegative integers N as its domain
• specify the value of f at 0 (i.e., f(0))
--(eq1)
• Given a rule for finding f(n) from f(n-1),...,
f(0) for n gt0 .
• i.e., f(n) an expression in terms of n and
f(n), ..., f(0). --- (eq2)
• The rule is guaranteed to find exactly one value
from its arguments
• Such a definition is called a recursive or
inductive definition.
• Ex1
• f(n) 3 if n 0
• 2 f(n-1) 3 if n gt0
• gt f(0) 3, f(1) 2f(0) 3 9 f(2)
2f(1)3 21,...
• This guarantees that f is defined for all numbers.

19
More examples functions
• Ex2 The factorial function f(n) n!
• f(0) 1
• f(n) n f(n-1) for all n gt 0.
• Recursively defined functions (over N) are well
defined
• Pf Let P(n) "there is at least one value
assigned to f(n)".
• Q(n) "there are at most one value
assigned to f(n)".
• We show ( P(n) /\ Q(n) ) hold for all n by CI.
Hence f is well-defined
• Basis Obviously P(0) holds by eq1, and Q(0)
hold by eq12.
• Ind. case Assume p(k) /\ Q(k) holds for all
k lt n gt 0
• gt since f(n) can be assigned value
only by evaluating the expression
expr(n,f(0),..,f(n-1)), where by ind. hyp. all
f(i)s (iltn) have exactly one value. Hence the
rules can return only one value and P(n) /\ Q(n)
thus holds.

20
More examples
• Ex5 The Fibonacci number
• f(0) 0 f(1) 1
• f(n) f(n-1) f(n-2) for n gt 1.
• gt 0,1,1,2,3,5,8,...

21
• Ex6 Show that f(n) gt a n-2 whenever n 3,
• where a (1 sqrt(5))/2 1.618 is the golden
ratio
• Properties of a a2 (1 a).
• Pf (by MI). Let P(n) "f(n) gt a n-2 ".
• Basis P(3) holds since f(3) 2 gta3-2 .
• Ind.step (for n 4)
• If n 4 gtf(4) 3 gt a4-2 1.6182.
• If n gt 4 gt by ind. hyp., f(n-1) gtan-3,
f(n-2) gtan-4
• Hence f(n) f(n)f(n-1) gt an-3 an-4
• (1 a) an-4 a n-2. QED

22
Lame's theorem
• a,b positive integer with a ? b.
• divisions used by the Euclidean algorithm to
find gcd(a,b) 5 x decimal digits in b.
• Ex gcd(17200, 23456789) uses no more than (5 x
8) divisions
• Pf seq of equations used for finding gcd(a,b),
where
• r0 a, r1 b.
• r2 ro mod r1 ¹ 0, r3 r1 mod r2
¹ 0
• ...
• rn rn-2 mod rn-1 ¹ 0, rn1 rn-1 mod
rn 0, i.e.,until rnrn-1
• Then gcd(a,b) rn and division used n. WLOG,
suppose n gt 1.
• Note rn ³ 1 f2 rn-1 ³ 2 (if rn-1 1 gt
rn-2 mod rn-1 0) 2f2 f3
• rn-2 ³ rnrn-1 f2 f3 f4 ...
• r2 ³ r3 r4 ³fn-1fn-2fn
• b r1 ³ r2 r3³ fnfn-1 fn1.gt
an-1.
• logb gt (n-1) log a 0.208 (n-1) gt (n-1)/5
• n -1 lt 5 log b ? 5 ?(log b)1? 5 digit(b).
gt n 5digit(b).

23
Recursively defined sets and structures
• Given a universal set U, a subset V of U and a
set of operations (or rules) OP on U, we often
define a subset D of U as follows
• 1. Init (Basis Step) Every element of V is an
element of D.
• 2. Closure(Recursive Step) For each operation f
in OP, if fUn-gtU and t1,..,tn are objects
already known to be in the set D, then
f(t1,..,tn) is also an object of D.
• Example 9 The set S 3n n gt0 ? N can be
defined recursively as follows
• 1. Init 3 ? S (i.e., V 3 )
• 2. closure S is closed under .
• i.e., If a,b ? S then so is ab . (OP )

24
well-formed arithmetic expressions
• Ex 11 (2 x), (x (y/3)),... (ok)
• x2, xy/3 ... (no)
• Let Vr x,y,.., be the set of variables,
• M numerals finite representations of
numbers
• OP ,-,,/,
• U the set of all finite strings over Vr U
M U OP U (,).
• The set of all well-formed arithmetic expressions
(wfe) can be defined inductively as follows
• 1. Init every variable x in Vr and every
numeral n in M is a wfe.
• 2. closure If A, B are wfe, then so are (AB),
(A-B), (A B),
• (A / B) and (A By).
• Note "1 x " is not a wfe. Why ?

25
More examples
• Ex10 wff (well-formed propositional formulas)
• PV p1,p2,.. a set of propositional symbols.
• OP /\, \/, , -gt
• U the set of all finite strings over PV U OP U
(,)
• Init every pi ? PV is a wff
• closure If A and B are wffs, then so are
• (A/\B), (A \/B), (A-gtB) and A.
• Examples
• positive instances p1, (p1 /\ p2), (p1 -gt (p2
\/ p3)),
• negative instances (/\ p1 p2), p1 \/ p2, p1
/\ p2

26
• 1. The definition of D is not complete (in the
sense that there are multiple subsets of U
satisfying both conditions.
• Ex the universe U satisfies (1) and (2), but it
is not
• Our intended D.
• 2. In fact the intended defined set
• 3' D is the least of all subsets of U
satisfying 1 2, or
• 3'' D is the intersection of all subsets of U
satisfying 1 2
• or
• 3''' Only objects obtained by a finite number
of applications of rule 1 2 are elements of D.
• 3. It can be proven that 3',3'',and 3''' are
equivalent.
• 4. Hence, to be complete, one of 3',3'' or 3'''
should be appended to condition 1 2, though it
can always be omitted(or replaced by the adv.
inductively, recursively) with such understanding
in mind.

27
Proof of the equivalence of 3',3'' and 3'''
• D1 the set obtained by 1,2,3'
• D1 satisfies 12 and any S satisfies 12 is a
superset of D1.
• D2 the set obtained by 1,2,3''.
• D2 the intersection of all subsets Sk of U
satisfying 12.
• ? S S satisfies 1 2
• D3 the set obtained by 1,2,3'''.
• For any x ? U, x ? D3 iff there is a (proof)
sequence
• x1,...,xm x, such that for each xi (i 1..m)
either
• (init ) xi ? V or
• (closure) there are f in OP and t1,...tn in
x1,..,xi-1 s.t.
• xi f(t1,..,tn).
• Ex 12?S in EX9 due to the sequence 3, 336 ,
66 12.
• ((x2)-3) is a wfe by the seq x, 2, 3,
(x2), ((x2)-3)

28
the proof
• D2 satisfies 121.1 and is the least1.2 of all
sets satisfying 12 , Hence D1 exists and equals
to D2.
• 2 (2.1) D3 satisfies 1 2. (2.2) D3 is
contained in all sets satisfying 1 2. Hence
D3 D2.
• pf 1.1 Let C T1,,Tm, be the collection
of all sets satisfying 12, and D2, by
definition, is n C.
• Hence V ? Tk for all Tk ? C and as a result V ?
D2.--- (1)
• Suppose t1,,tn ? D2, then t1,,tn ? Tk for
each Tk in C,
• Hence f(t1,,tn) ? Tk and as a result
f(t1,..,tn) ? D2. ---(2).
• 1.2 Since D2 nC, D2 is a subset of all Tks,
and by 1.1, D2?C, D2 thus is the least among
these sets.
• Hence D1 exists and equals to D2.

29
• 2.1 D3 satisfies 1 2. by ind.
• 2.2 D3 is contained in all sets satisfying 1 2
by ind.
• Hence D3 D2.
• pf 2.1 two propositions need to be proved
• V ? D3 ---(1) and
• t1,..,tn? D3 gt f(t1,,tn) ? D3 for f ?
OP---(2).
• (1) is easy to show, since for each x in V, the
singleton sequence x is a proof. Hence x ? D3.
• As to (2), since t1,..,tn? D3, by definition,
there exist proof sequences S1,S2,,Sn for
t1,,tn, respectively.
• We can thus join them together to form a new
sequence S S1,S2,,Sn.
• We can then safely append f(t1,,tn) to the end
of S to form a new sequence for f(t1,,tn), since
all t1,,tn have appeared in S.
• As a result f(t1,,tn) ? D3. (2) thus is proved.

30
• 2.2 D3 is contained in all sets satisfying 1 2
by ind.
• pf Let D be any set satisfying 12.
• We need to show that for all x, x ? D3 gtx ?
D.
• The proof is by ind. on the length m of the
minimum proof sequence for x x1,,xm x
• If m 1 then xx1 ? V, and hence x ? D.
• If m k1 gt 1, then either xm ? V (and xm ? D)
or
• ? j1,j2,jn lt m and xm f(xj1,,xjn) for
some f ? OP.
• For the latter case, by ind. hyp., xj1,xjn ? D.
• Since D satisfies closure rule, f(xj1,,xjn)
xm ? D.
• Q.E.D

31
Example
• Def 2 The set Sof strings over an alphabet S
can be defined recursively as follows
• Basis step e ? S.
• Recursive step if a ? S and x ? S, then ax ?
S.
• Ex8' if S 0,1,2, then 1201 ? S since
• e ? 1 e 1 ? 01 ? 201 ?1201.
• Ex 8'' The set of natural numbers can be defined
as a subset of 1 inductively as follows
• Init e in N.
• closure If x in N, then 1x in N.
• ? e ,1, 11,111,1111,... are natural numbers
• (unary representation of natural numbers)

32
Induction principles III (structural induction)
• D a recursively defined set
• P D?true,false a property about objects of
D.
• To show that P(t) holds for all t ? D, it
suffices to show that
• 1. Basis step P(t) holds for all t ? V.
• 2. Ind. step For each f in OP and t1,..,tn ? D,
if P(t1),...,P(tn) holds, then P(f(t1,..,tn))
holds, too.

33
Correctness of SI
• Show the correctness of structural induction.
• Pf Assume not correct.
• gt NP t ? D P(t) does not hold is not
empty.
• gtlet x be any member of NP with a minimum length
n of proof sequence, say x1,..xn x.
• Since x has minimum length in NP, all x1,..xn-1
? NP.
• gt If n 1, then x1 x ? V (impossible)
• Else either n gt 1 and x ? V (impossible, like
n1)
• or n gt 1, and xf(t1,.,tn) for some
t1,..,tn in x1,..xn-1 and P holds for all
tks
• gt P(x) holds too gt x ? NP, a contradiction.
QED.

34
MI is a specialization of SI
• Rephrase the SI to the domain N, we have
• To show P(t) holds for all t ? N, it suffices to
show that
• Init P(e ) holds
• Ind. step OP 1
• for any x in N, If P(x) holds than P(1x) holds.
• Notes
• 1. The above is just MI.
• 2. MI is only suitable for proving properties of
natural numbers whereas SI is suitable for
proving properties of all recursively defined
sets.
• 3. The common variant of MI starting from a value
c ? 0 ,1 is also a special case of SI with the
domain
• D c, c1, c 2,

35
well-formed arithmetic expressions
• Ex 11 (2 x), (x (y/3)),... (ok)
• x2, xy/3 ... (no)
• Let Vr x,y,.., be the set of variables,
• M numerals finite representations of
numbers
• OP ,-,x,/,
• U the set of all finite strings over Vr U
M U OP U (,).
• The set of all well-formed arithmetic expressions
(wfe) can be defined inductively as follows
• 1. Init every variable x in Vr and every
numeral n in M is a wfe.
• 2. closure If A, B are wfe, then so are (AB),
(A-B), (A B),
• (A / B) and (A B).
• Note "1 x " is not a wfe. Why ?

36
More examples
• Ex10 wff (well-formed propositional formulas)
• PV p1,p2,.. a set of propositional symbols.
• OP /\, \/, , -gt
• U the set of all finite strings over PV U OP U
(,)
• Init every pi ? PV is a wff
• closure If A and B are wffs, then so are
• (A/\B), (A \/B), (A-gtB) and A.

37
• Ex9 Recursively define two functions on S.
• len S -gt N s.t. len(x) the length of the
string x.
• basis len(e) 0
• Ind. step for any x ? S and a ?S, len(ax)
len(x) 1.
• SxS? S s.t. (x,y) xy the concatenation
of x and y.
• Basis e y y for all strings y.
• Recursive step (az) y a(zy) for all symbols
a and strings z, y.

38
• Prove properties of len(-) on S
• Ex14 show that len(xy) len(x)len(y) for any
x,y ? S.
• By SI on x. Let P(x) "len(xy) len(x)
len(y)".
• Basis x e. gt
• x y y gt len(x y) len(y) len(e)
len(y).
• Ind. step x az
• len(xy) len((az) y) len((a(zy))
• 1 len(zy)
• 1 len(z) len(y) -- SI
• len(az) len(y)
• len(x) len(y).

39
Where do we use Recursion ?
• Define a domain
• numbers, lists, trees, formulas, strings,...
• Define functions on recursively defined domains
• Prove properties of functions or domains by
structural induction.
• compute recursive functions
• --gt recursive algorithm
• Ex
• len(x) // x a string
• match s with
• case e gt 0
• case a y gt 1 len(y)

40
Define lp, rp wff ? N
• Define two functions lp, rp wff ? N s.t.,
• lp(A) and rp(A) are the number of '(' and ')'
occurring in A, respectively.
• Define lp and rp recursively by cases of input A
• Basis Case A p is a logical variable.
• Then lp(A) rp(A) 0.
• Recursive cases
• 1. A (B _at_C ) where _at_ is either /\ or \/ or ?
.
• Then lp((B_at_C)) 1 lp(B) lp(C) and
• rp((B_at_C)) 1 rp(B) rp(C).
• 2. A B. Then lp(B) lp(B) and rp(B)
rp(B).
• EX lp( (p1 /\ (p2?p1)) ) rp( (p1 /\ (p2?p1))
) 2.

41
• Ex13 Show that for every wff A, lp(A) rp(A).
Namely, every wff has an equal number of left
and right parentheses.
• pf By S.I. on A.
• Basis Step A p is a logic variable.
• Then lp(p) 0 rp(p).
• Recursive step
• case 1 A (B_at_C), where _at_is any binary
connective.
• Then lp(A)
• 1 lp(B) lp(C) ---
Definition of lp
• 1 rp(B) rp(C) --- Ind. Hyp.
• rp(A) ---
Def. of rp.
• case2 AB. Then lp(A) lp(B) rp(B) rp(A).

42
Full Binary Tree
• Def 6 The set of full binary trees can be
defined inductively as follows
• Basis Step A single vertex is a full binary
tree.
• Recursive step If T1 and T2 are disjoint full
binary trees, and r is a vertex not in T1 and T2,
then (r,T1,T2) is full binary tree with root r,
left subtree T1 and right subtree T2.
• Ex r0, (r2, r0, r1), (r3, r0, (r2, r1, r4))
• Counter Ex (r0,r1), (r3, r2, r2)

43
Internal nodes and leaves
• Def1. A vetex r in a full binary tree T is an
internal node if it has two subtrees.
• 2. A vetex r in a full binary tree T is a
leaf if it has no subtrees.
• Def Define two functions Int, leaf the set
of full bianry tree ? N recursively as follows
• Basis Case t r is a single node tree. Then
• Int(r) 0 and leaf(r) 1.
• Recursive Case t (r, T1,T2) is a non-single
node tree. Then
• Int(t) 1 Int(T1) Int(T2) and
• leaf(t) leaf(T1) leaf(T2).

44
• Show that for all full binary trees T, the set of
leaves is 1 more than the set of internal
vertices.
• I.e., leaf(T) 1 Int(T).
• Pf By S.I. on T.
• Basis Case T is a sigle-vertex tree.
• Then leaf(T) 1 10 1 Int(T).
• Ind. Case T (r, T1,T2). Then
• leaf(T) leaf(T1) leaf(T2) --- Def of
leaf
• 1Int(T1) 1 Int(T2) ---
Ind.Hyp.
• 1 Int( T )
--- Def. of Int.

45
4.4 Recursive algorithm
• Definition an algorithm is recursive if it solve
a problem by reducing it to an instance of the
same problem with smaller inputs.
• Ex1 compute an where a ? R and n ? N.
• Ex2 gcd(a,b) a, b ? N, a gt b
• gcd(a,b) def if b 0 then a
• else gcd(b, a mod b).
• Ex show that gcd(a,b) will always terminate.
• Comparison b/t recursion and iteration
• Recursion easy to read, understand and devise.
• Iteration use much less computation time.
• Result programmer --gt recursive program --gt
• compiler --gt iterative program --gt machine.

46
4.5 Program correctness
• After designing a program to solve a problem, how
can we assure that the program always produce
correct output?
• Types of errors in a program
• syntax error ?easy to detect by the help of
compilers
• semantic error ? test or verify
• Program testing can only increase our confidence
about the correctness of a program it can never
guarantee that the program passing test always
produce correct output.
• A program is said to be correct if it produces
the correct output for every possible input.
• Correctness proof generally consists of two
steps
• Termination proof
• Partial correctness whenever the program
terminates, it will produce the correct output.

47
Program verification
• Problem
• what does it mean that a program produce the
correct output (or results)?
• By specifying assertions (or descriptions) about
the expected outcome of the program.
• Input to program verifications
• Pr the program to be verified.
• Q final assertions (postconditions), giving
the properties that the output of the program
should have
• P initial assertions(preconditions) , giving
the properties that the initial input values are
required to have.

48
Hoare triple
• P,Q assertions
• S a program or program segment.
• P S Q is called a Hoare triple, meaning that
• S is partially correct (p.c.) w.r.t P,Q,i.e.,
whenever P is true for I/P value of S and
terminates, then Q is true for the O/P values of
S.
• Ex1 x1 y 2 z x y z 3 is true. Why
?
• Ex 2 x 1 while x gt 0 x x 0 is true.
why?

49
Typical program constructs
• 1. assignment x expr
• x xy-3
• 2. composition S1S2
• Execute S1 first, after termination, then execute
S2.
• 3. Conditional
• 3.1 If ltcondgt then S
• 3.2 If ltcondgt then S1 else S2.
• 4. Loop
• 4.1 while ltcondgt do S
• 4.2 repeat S until ltcondgt // 4.3 do S while
ltcondgt
• Other constructs possible, But it can be shown
that any program can be converted into an
equivalent one using only 1,2,3.1 and 4.1

50
Assignment rule
• Px/expr x expr P
• Px/expr is the result of replacing every x in P
by the expression expr.
• ex P "y lt x /\ x z 5" gt Px/3 y lt 3
/\ 3z 5".
• Why correct?
• consider the variable spaces
• (...,x,...) x expr gt (..., expr,...)
P
• Hence if Px/expr holds before execution, P will
hold after execution.
• Example Q y xy x gt 2y 1 gt Q ?
• (xb,yb) gtya xbyb gt(xb,xbyb) (xa,ya)
P(xa,ya) def xa gt 2ya 1
• gt (xb,yb) Q P(xa,ya)xa/xbya/xbyb
• P(xb,xbyb) ? xb gt 2(xbyb) 1

51
Composition rules
• Splitting programs into subprograms and then show
that each subprogram is correct.
• The composition rule
• P S1 Q x 0 x
x2 ?
• Q S2 R ? x
x-1 x gt 0
• -------------------
---------------------------------------
• P S1S2 R x0 x x2
x x -1 x gt 0
• Meaning
• Backward reading to prove PS1S2Q, it suffices
to find an assertion Q s.t. PS1Q and Q S2R.
• Problem How to find Q ?

52
Example
• Show that x 1 y 2 z x y z 3
• x 1 y 2 z xy z 3
• --------------------------------------------------
------
• x1 y 2 ? ? z xy z 3

53
Classical rules
• Classical rules
• P gt P1 P S Q1 P gt P1
• P1 S Q Q1 gt Q P1 S Q1
• ---------------------- -----------------------
Q1 gt Q
• P S Q PS
Q -------------------------
• P S Q
• Examples
• x 1 gt x1gt1 x1gt0 x
x 1 x gt 0
• x1gt1 x x 1 x gt 1 x gt 0 gt x
? 0
• -----------------------------------
-----------------------------------
• x 1 x x 1 x gt 1 x1 gt 0 x
x1 x ? 0

54
Conditional rules
• P /\ ltcondgt S1 Q
• P /\ ltcondgt S2 Q
• ------------------------------------------------
• P if ltcondgt then S1 else S2 Q
• T /\ x gt
y gt x ³ x x³ x yx y ³ x

• ------------------------------------------------
• P /\ ltcondgt S Q T /\
xgty y x y ³ x
• P /\ltcondgt gt Q x gt
y gt y ³ x
• ---------------------------
--------------------------------------
• P if ltcondgt then S Q T if x gt y
then y x y ³ x

55
While-loop rules
• Loop invariant
• A statement P is said to be a loop invariant of
a while program While ltcondgt do S, if it remains
true after each iteration of the loop body S.
• I.e., P /\ ltcondgt S P is true.
• While rule
• P /\ ltcondgt S P
• --------------------------------------------------
---
• P while ltcondgt do S P /\ ltcondgt
• Issues
• How to find loop invariant P?
• Most difficulty of program verification lies in
the finding of appropriate loop invariants.

56
While loop example
• Show that
• ngt0 i 1 f 1
• while i lt n do (i i1 f f x i )
f n!
• To prove the program terminates with f n!, a
loop invariant is needed.
• Let p "i n /\ f i!"
• First show that p is a loop invariant of the
while program
• i.e., i n /\ f i! /\ i lt n i i1 f f
x i i n /\ fi!

57
while loop example(cont'd)
• n gt 0 --- i 1 ------ i n
• f 1 ------ p "i n /\
f i!
• while i lt n do (i i1 f f x
i )
• ------ p /\ i lt
n gt in /\ f i!

• gt f n!

58
Another example
• Ex5Show that the following program is correct
• Procedure prod(m,n integer) integer
• 1. If n lt 0 then a -n
• else a n ------ a n
• 2. k 0 x 0
• 3. while k lt a do --- p "x mk /\ k
a" is a loop
• x x m
invariant.
• k k1
• enddo --- x mk /\ k a /\ klta
gt ka /\ xma

• gt x m n
• 4. If n lt 0 then prod -x gt prod - m n
mn
• else prod x gt Prod m
n mn
• ---- prod mn. Hence the program is partially
correct !
• Note to be really correct, we need to show that
the program will eventually terminates.