04-Induction and Recursion (SF)
04-Induction and Recursion (SF)
k k 1
Suppose: P k "1 2 3 k " is true.
2
Show that:
k 1 k 1 1
P k 1 "1 2 3 k k 1 " is true.
2
Induction: Example 1
k k 1
We have P k "1 2 3 k "
2
k k 1 k k 1 2 k 1
1 2 3 k k 1 k 1
2 2
k 1 k 2 k 1 k 1 1
2 2
k 1 k 1 1
P k 1 "1 2 3 k k 1 " is true.
2
P k P k 1 : true
Proved.
Induction: Example 2
Ex: Conjecture a formula for the sum of the first n positive
odd integers. Then prove your conjecture using mathematical
induction.
Solution:
The sum of the first n positive odd integers for n = 1, 2, 3, 4, 5
are: 1=1, 1+3=4, 1+3+5=9, 1+3+5+7=16, 1+3+5+7+9=25.
Conjecture: 1+3+5+…+(2n-1) = n2.
Proof. Let P(n)=“1+3+5+…+(2n-1)=n2.”
- Basis step. P(1)=“1=1” is true.
- Inductive step. (P(k) P(k+1)) is true.
Suppose P(k) is true for arbitrary k > 0.
That is “1+3+5+…+(2k-1)=k2”
Solution:
P(n): n can be written as the product of primes
Basis steps: P(2) = true (2=2), product of 1 prime
P(4) = true (4=2.2), product of 2 primes
Inductive step:
Assumption: P(j)=true for all positive j ≤ k
- Case k+1 is a prime P(k+1) =true
- Case k+1 is a composite k+1= ab, 2 ≤ a ≤ b<k+1
P(k) is true
Strong Induction: Example 2
Ex: Prove that every amount of postage of 12 cents or more
can be formed using just 4-cents and 5-cents stamps.
Solution:
P(n): “n cents can be formed using just 4-cent and 5-cent
stamps”, n > 11
P(12) is true: 12 cents = 3.4 cents
P(13) is true: 13= 2.4 + 1.5
P(14) is true: 14= 1.4 + 2.5
P(15) is true: 15= 3.5
Assumption: P(j) is true with 12 ≤ j ≤ k, k > 15 P(k-3) is true
k+1= (k-3) + 4 , k>12
P(k+1) is true because k+1 is the result of adding a 4-cent
stamp to the amount k-3
Proved
4.3 Recursive Definition
and Structural Induction
Introduction
Recursively Defined Functions
Recursively Defined Sets and Structures
Structural Induction
Generalized Induction
Recursive Algorithms
Recursion: Introduction
Objects/ functions may be difficultly defined.
Define an object/function in terms of itself.
1, n 0
2 n 1
n
2.2 , n 0
Recursion: Introduction
Recursively Defined Functions
Recursive (inductive) function:
Two steps to define a function with the set of nonnegative
integers as its domain:
1) Basis step: Specify the value of the function at zero.
2) Recursive step: Give a rule for finding its value at an
integer from its values at smaller integers.
i 0
Solution:
0
F 0 ai a0
1) Basis step:
2) Inductive step: i 0
n
We have: ai a0 a1 a2 an1 an
i 0
n n 1
ai ai an
i 0 i 0
F n F n 1 an
F is defined recursively by: F 0 a0 , F n F n 1 an
Recursively Defined Functions
Ex2: Give the recursive definition of
an 2n , for n 0,1,2,. . . .
Solution:
an 2 an : 1, 2, 4, 8, 16,
n
a0 1 an 2an1
n 1
a0 1, an 2.2 , n 0
1, n 0
2 n 1
n
2. 2 , n 0
Recursively Defined Functions
Definition 1: Fibonacci numbers
f 0 0, f 1 1, f n f n 1 f n 2
f 0 0, f 5 f 4 f 3 5
f 1 1, f 6 f 5 f 4 8
f 2 f 1 f 0 1
f 3 f 2 f 1 2
f 4 f 3 f 2 3
0, 1, 1, 2, 3, 5, 8,
Recursively Defined Functions
Theorem 1: Lamé’s theorem:
Let a, b be integers, a ≥ b. Then the number of divisions
used by the Euclidean algorithm to find gcd(a,b) is less than
or equal to five times the number of decimal digits in b.
Proof: page 298 procedure gcd(a,b)
Ex: gcd(25,7), b= 7 , 1 digit x:=a; y:=b
x y r while y 0
25 7 25 mod 7=4 begin
7 4 7 mod 4=3 r := x mod y
4 3 4 mod 3=1 x:=y
3 1 3 mod 1=0 (4 divisions) y:= r
1 0 Stop end { gcd(a,b) is x}
Recursively Defined Sets and Structures
Ex1: S= {3,6,9,12,15,18,21,…}
Step 1: 3S
Step 2: If xS then x+3S
1 S x S x 2 S
1 S , and if x S then x 2 S
Recursively Defined Sets and Structures
Definition 2: The set ∑* of string over alphabet ∑ can be
defined recursively by:
Basis step: λ ∑*, λ is the empty string with no symbols
Recursive step: If w ∑* and x ∑ then wx ∑*
n! = 1, n=0
n! = 1.2.3.4…n = n.(n-1)!, n>0
4.4 Recursive Algorithms
Ex2: Recursive algorithm for computing an
a n 1, n 0
a n a.a.a a a.a n1 , n 0
Modular Exponentiation mod m ?
n
b
Ex3: 3644 mod 645 ?
We have ab mod m a mod2 m b mod m mod m
3644 mod 645 3322 mod 645 mod 645 36
3 mod 645 3 mod 645 mod 645
322 161 2
264
3 mod 645 3 mod 645 3 mod 645
161 80 2
93
3 mod 645 3 mod 645 mod 645
80 40 2
576
3 mod 645 3 mod 645 mod 645
40 20 2
411
3 mod 645 3 mod 645 mod 645
20 10 2
186
3 mod 645 3 mod 645 mod 645
10 5 2
354
3 mod 645 3 mod 645 3 mod 645
5 2 2
243
32 mod 645 9
4.4 Recursive Algorithms
Ex3: Recursive algorithm for computing bn mod m
m ≥ 2, n ≥ 0, 1 ≤ b < m.
List1: 2, 3, 5, 6
List2: 1, 4
≤ L: 1, 2, 3, 4, 5, 6
Merge Sort
Ex: Merge two sorted lists L1, L2 to list L, an increasing ordered
list.
L: 8 2 4 6 9 7 10 1 5 3
≤ L: 1 2 3 4 5 6 7 8 9 10