Title: Chapter 4: Induction and Recursion
1Chapter 4Induction and Recursion
24.1 Mathematical Induction
- A powerful, rigorous technique for proving that a
predicate P(n) is true for every positive integer
n, no matter how large. - Essentially a domino effect principle.
- Based on a predicate-logic inference rule
P(1)?k?1 (P(k)?P(k1))??n?1 P(n)
The First Principleof MathematicalInduction
4.1 Mathematical Induction
3Outline of an Inductive Proof
- Want to prove ?n P(n)
- Base case (or basis step) Prove P(1).
- Inductive step Prove ?k P(k)?P(k1).
- E.g. use a direct proof
- Let k?N, assume P(k). (inductive hypothesis)
- Under this assumption, prove P(k1).
- Inductive inference rule then gives ?n P(n).
4.1 Mathematical Induction
4Induction Example (1st princ.)
- Prove that the sum of the first n odd positive
integers is n2. That is, prove - Proof by induction.
- Base case Let n1. The sum of the first 1 odd
positive integer is 1 which equals 12.(Cont)
P(n)
4.1 Mathematical Induction
5Example cont.
- Inductive step Prove ?k?1 P(k)?P(k1).
- Let k?1, assume P(k), and prove P(k1).
By inductivehypothesis P(k)
4.1 Mathematical Induction
6Another Induction Example
- Prove that ?ngt0, nlt2n. Let P(n)(nlt2n)
- Base case P(1)(1lt21)(1lt2)T.
- Inductive step For kgt0, prove P(k)?P(k1).
- Assuming klt2k, prove k1 lt 2k1.
- Note k 1 lt 2k 1 (by inductive hypothesis)
lt 2k 2k (because 1lt22?20?2?2k-1 2k)
2k1 - So k 1 lt 2k1, and were done.
4.1 Mathematical Induction
7Validity of Induction
- Proof that ?k?1 P(k) is a valid consequentGiven
any k?1, ?n?1 (P(n)?P(n1)) (antecedent 2)
trivially implies ?n?1 (nltk)?(P(n)?P(n1)), or
(P(1)?P(2)) ? (P(2)?P(3)) ? ? (P(k?1)?P(k)).
Repeatedly applying the hypothetical syllogism
rule to adjacent implications k-1 times then
gives P(1)?P(k) which with P(1) (antecedent 1)
and modus ponens gives P(k). Thus ?k?1 P(k).
4.1 Mathematical Induction
8The Well-Ordering Property
- The validity of the inductive inference rule can
also be proved using the well-ordering property,
which says - Every non-empty set of non-negative integers has
a minimum (smallest) element. - ? ??S?N ?m?S ?n?S m?n
- Implies n?P(n) has a min. element m, but then
P(m-1)?P((m-1)1) contradicted.
4.1 Mathematical Induction
9Generalizing Induction
- Can also be used to prove ?n?c P(n) for a given
constant c?Z, where maybe c?1. - In this circumstance, the base case is to prove
P(c) rather than P(1), and the inductive step is
to prove ?k?c (P(k)?P(k1)). - Induction can also be used to prove?n?c P(an)
for an arbitrary series an. - Can reduce these to the form already shown.
4.1 Mathematical Induction
104.2Strong Induction
- Characterized by another inference
ruleP(1)?k?1 (?1?i?k P(i)) ? P(k1)??n?1
P(n) - Difference with 1st principle is that the
inductive step uses the fact that P(i) is true
for all smaller iltk1, not just for ik.
4.2 Strong Induction
11Example of Second Principle
- Show that every ngt1 can be written as a product
p1p2ps of some series of s prime numbers. Let
P(n)n has that property - Base case
- Inductive step Let k?2. Assume ?2?i?k P(i).
Consider k1. If prime, - Else k1ab, where 1lta?k and 1ltb?k.
4.2 Strong Induction
12Another 2nd Principle Example
- Prove that every amount of postage of 12 cents or
more can be formed using just 4-cent and 5-cent
stamps. - Base case
- Inductive step Let k?15, assume ?12?i?k P(i).
4.2 Strong Induction
13Proofs By Well-Ordering Property
- Use the well-ordering property to prove the
division algorithm a dq r, 0 ? r lt d,
where q and r are unique. - S n n a dq is nonempty, so S has a
least element r a dq0 . If r ? 0, it is also
the case that r lt d . If it were not, - If a dq1 r1 dq2 r2, 0 ? r1 , r2 lt d, ,
then -
4.2 Strong Induction
14 4.3Recursive Definitions
- In induction, we prove all members of an infinite
set have some property P by proving the truth for
larger members in terms of that of smaller
members. - In recursive definitions, we similarly define a
function, a predicate or a set over an infinite
number of elements by defining the function or
predicate value or set-membership of larger
elements in terms of that of smaller ones.
4.3 Recursive Definitions
15Recursion
- Recursion is a general term for the practice of
defining an object in terms of itself (or of part
of itself). - An inductive proof establishes the truth of
P(n1) recursively in terms of P(n). - There are also recursive algorithms, definitions,
functions, sequences, and sets.
4.3 Recursive Definitions
16Recursively Defined Functions
- Simplest case One way to define a function fN?S
(for any set S) or series anf(n) is to - Define f(0).
- For ngt0, define f(n) in terms of f(0),,f(n-1).
- E.g. Define the series an 2n recursively
- Let a0 1.
- For ngt0, let an 2an-1.
4.3 Recursive Definitions
17Another Example
- Suppose we define f (n) for all n?N recursively
by - Let f (0)3
- For all n?N, let f (n1)2f (n)3
- What are the values of the following?
- f (1) , f (2) , f (3) ,
f (4)
4.3 Recursive Definitions
18Recursive definition of Factorial
- Give an inductive definition of the factorial
function F(n) n! 2?3??n. - Base case F(0) 1
- Recursive part F(n) n ? F(n-1).
- F(1)1
- F(2)2
- F(3)6
4.3 Recursive Definitions
19The Fibonacci Series
- The Fibonacci series fn0 is a famous series
defined by f0 0, f1 1, fn2 fn-1
fn-2
0
1
1
2
3
5
8
13
Leonardo Fibonacci1170-1250
4.3 Recursive Definitions
20Inductive Proof about Fib. series
- Theorem fn lt 2n.
- Proof By induction.
- Base cases
- Inductive step Use 2nd principle of induction
(strong induction). Assume ?iltk, fi lt 2i.
Then
Implicitly for all n?N
4.3 Recursive Definitions
21Recursively Defined Sets
- An infinite set S may be defined recursively, by
giving - A small finite set of base elements of S.
- A rule for constructing new elements of S from
previously-established elements. - Implicitly, S has no other elements but these.
- Example Let 3?S, and let xy?S if x,y?S. What
is S ?
4.3 Recursive Definitions
22The Set of All Strings
- Given an alphabet S, the set S of all strings
over S can be recursively defined as e ? S (e
, the empty string) - w ? S ? x ? S ? wx ? S
- Exercise Prove that this definition is
equivalent to our old one
Bookuses ?
4.3 Recursive Definitions
234.4Recursive Algorithms
- Recursive definitions can be used to describe
algorithms as well as functions and sets. - Example A procedure to compute an.
- procedure power(a?0 real, n?N)
- if n 0 then return 1 else return a
power(a, n-1)
4.4 Recursive Algorithms
24Efficiency of Recursive Algorithms
- The time complexity of a recursive algorithm may
depend critically on the number of recursive
calls it makes. - Example Modular exponentiation to a power n can
take log(n) time if done right, but linear time
if done slightly differently. - Task Compute bn mod m, where m2, n0, and
1bltm.
4.4 Recursive Algorithms
25Modular Exponentiation Alg. 1
- Uses the fact that bn bbn-1 and that xy mod
m x(y mod m) mod m.(Prove the latter theorem
at home.) - procedure mpower(b1,n0,mgtb ?N)
- Returns bn mod m.if n0 then return 1
elsereturn (bmpower(b,n-1,m)) mod m - Note this algorithm takes T(n) steps!
4.4 Recursive Algorithms
26Modular Exponentiation Alg. 2
- Uses the fact that b2k bk2 (bk)2.
- procedure mpower(b,n,m) same signature
- if n0 then return 1else if 2n then return
mpower(b,n/2,m)2 mod melse return
(mpower(b,n-1,m)b) mod m - What is its time complexity?
T(log n) steps
4.4 Recursive Algorithms
27A Slight Variation
- Nearly identical but takes T(n) time instead!
- procedure mpower(b,n,m) same signature
- if n0 then return 1else if 2n then return
(mpower(b,n/2,m) mpower(b,n/2,m)) mod
melse return (mpower(b,n-1,m)b) mod m
The number of recursive calls made is critical.
4.4 Recursive Algorithms
28Recursive Euclids Algorithm
- procedure gcd(a,b?N)if a 0 then return belse
return gcd(b mod a, a) - Note recursive algorithms are often simpler to
code than iterative ones - However, they can consume more stack space, if
your compiler is not smart enough.
4.4 Recursive Algorithms
29Merge Sort
- procedure sort(L ?1,, ?n)if ngt1 then m
?n/2? this is rough ½-way point L
merge(sort(?1,, ?m), sort(?m1,,
?n))return L - The merge takes T(n) steps, and merge-sort takes
T(n log n).
4.4 Recursive Algorithms
30- Example Sort the list 27, 10, 12, 20, 25, 13,
15, 22.
27 10 12 20 25 13 15 22
31Merge Routine
- procedure merge(A, B sorted lists)L empty
listi0, j0, k0while iltA ? jltB A
is length of A if iA then Lk Bj j j
1 else if jB then Lk Ai i i
1 else if Ai lt Bj then Lk Ai i i
1 else Lk Bj j j 1 k k1return L
Takes T(AB) time
4.4 Recursive Algorithms