0% found this document useful (0 votes)
15 views37 pages

04-Induction and Recursion (SF)

Uploaded by

tuantmse180360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views37 pages

04-Induction and Recursion (SF)

Uploaded by

tuantmse180360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 4

Induction and Recursion


Objectives
 Mathematical Induction
 Strong Induction and Well-Ordering
 Recursive Definitions and Structural Induction
 Recursive Algorithms
4.1 Mathematical Induction
 Introduction
 Mathematical Induction
 Examples of Proofs by Mathematical Induction
Principle of Mathematical Induction

Principle of Mathematical Induction:


To prove P(n) is true for all possible
integers n, where P(n) is a propositional
function, we complete two step:
1) Basis step:
Verifying P(1) is true
2) Inductive step:
Show P(k) → P(k+1) is true for all k>0
Principle of Mathematical Induction
Induction: Example 1
n  n  1
Ex: Prove that 1  2  3   n  for all integers n>0
2
Solution:
n  n  1
Let P  n   "1  2  3  n  "
2
11  1
 Basis step: P 1  "1  "  true
2
 Inductive step: With arbitrary k>0,

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”

We have, 1+3+5+…+(2k-1)+(2k+1)=k2+2k+1 = (k+1)2.


So, P(k+1) is true. Proved.
4.2 Strong Induction and Well-Ordering

Principle of Strong Induction


To prove P(n) is true for all positive integers n, where P(n) is
a propositional function, two steps are performed:
Basis step:
Verifying P(1) is true
Inductive step:
Show  P 1  P  2     P  k   P  k  1 is true for all
k>0
Strong Induction: Example 1
Ex: Prove that if n is an integer greater than 1, then n can
be written as the product of primes.

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.

Ex: an  2 , for n  0,1,2,. . . .


n

 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.

Ex: Suppose that f is defined recursively by:


f (0)  3, f (n  1)  2 f ( n)  3
Find f (1), f (2), f (3), and f (4).
f (1)  2 f (0)  3  9 f (2)  2 f (1)  3  21
f (3)  2 f (2)  3  45 f (4)  2 f (3)  3  93
Recursively Defined Functions
n
F  n    ai
Ex1: Give the recursive definition of

i 0

Solution:
0
F  0    ai  a0
1) Basis step:

2) Inductive step: i 0

n
We have:  ai  a0  a1  a2    an1  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  2an1
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: 3S
Step 2: If xS then x+3S

Ex2: Give a recursive definition of the set of odd positive integers.


S  1, 3, 5, 7, 9, 11,

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 ∑*

Ex: ∑ = {0,1}  ∑* is the set of string made by 0 and 1 with


arbitrary length and arbitrary order of symbols 0 and 1.
Basis step:   *   0,1
Recursive step:  *  00,01,10,11
  * and 0    0  *
0  * and 1   01 * 000,010,100,110, 
*
  
01 * and 0    010  * 001,011,101,111 
 
Recursively Defined Sets and Structures
Two strings can be combined via the operation of concatenation.

Definition 3: String Concatenation ()


Basis step: If w  ∑* then wλ=w, λ is the empty string
Recursive step: If w1 ∑* and w2 ∑* and x ∑ then
w1(w2x) =(w1w2)x

Ex: ∑ ={0,1}  ∑* is the set of string made by 0 and 1 with


arbitrary length and arbitrary order of symbols 0 and 1
Recursively Defined Sets and Structures
Ex: When does a string belong to the set A of bit strings
defined recursively by
λ∈A
0x1 ∈ A if x ∈ A,
where λ is the empty string?
4.4 Recursive Algorithms
Definition 1: An algorithm is called recursive if it solves a
problem by reducing it to an instance of the same problem
with smaller input.

Ex1: Recursive algorithm for computing n!


n!  n   n  1! and 0!  1

procedure factorial (n: nonnegative integer)


if n=0 then factorial(n) :=1
else factorial(n) = n.factorial(n-1)

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

procedure power (a: nonzero real number


n: nonnegative integer)
if n=0 then power(a,n) :=1
else power(a,n) = a.power(a,n-1)

a n  1, n  0
a n  a.a.a a  a.a n1 , 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.

bn mod m = (b.(bn-1 mod m) mod m


b0 mod m = 1
Using division to improve performance: ( n steps backward to 0 faster)
If n is even  bn = bn/2.bn/2
 bn mod m = ((bn/2 mod m). (bn/2 mod m)) mod m
 bn mod m = (bn/2 mod m)2 mod m
If n is odd bn = b.b n/2.b n/2
 bn mod m = ([(b n/2 mod m)2 mod m].(b mod m))mod m
4.4 Recursive Algorithms
Ex4: Recursive algorithm for computing gcd(a,b), where a,b: non negative
integer, a < b

If a>b then swap a,b


gcd(a,b) = b , a = 0
gcd(a,b) = gcd(b mod a, a)
4.4 Recursive Algorithms
Ex5: Recursive algorithm for linear searching the value
x in the sequence ai , ai 1 ,, a j , sub-sequence of a n,
1  i  n, 1  j  n
i>j  location =0
ai = x  location = i
location (i, j, x) = location ( i+1, j, x)
4.4 Recursive Algorithms
Ex6: Recursive algorithm for binary searching the value x
in the increasingly ordered sequence, ai , ai 1 ,, a j ,
1  i  n, 1  j  n

Algorithm: page 314 – You should modify it.


Proving Recursive Algorithms Correct
 Using mathematical induction.
 Ex: prove the algorithm that computes n! is correct.
procedure f (n: nonnegative integer)
if n=0 then f(n) :=1
else f(n) = n.f(n-1) If n=0, first step of the algorithm tells us
f(0)=1  true
Assuming f(n) is true for all n≥0
f(n)= 1.2.3….(n)
(n+1).f(n)= 1.2.3…n.(n+1) = (n+1)!
f(n+1) = (n+1)!
Conclusion: f(n) is true for all integer n, n≥0

More examples: Page 315


Recursion and Iteration
procedure rFibo (n: nonnegative integer)
If n=0 then rFibo(0)=0
Else if n=1 then rFibo(1)=1
Else rFibo(n) := rFibo(n-2) + rFibo(n-1)

procedure iFibo (n: nonnegative integer)


If n=0 then y:=0
Else if n=1 then y:=1
Else Begin
x:=0 ; y:=1
for i:= 2 to n
Begin Recursive algorithm uses
z:= x+y; x:= y; y:=z
End
far more computation
End {iFibo(n) = z} than iterative one
Merge Sort
Ex: Merge two sorted lists L1, L2 to list L, an increasing ordered
list.

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.

procedure Merge (L1, L2: sorted list)


L:= empty list
While L1 and L2 are both no empty
Begin
remove smaller of first element of L1 and L2
and put it to the right end of L
if removal of this element makes one list empty
then remove all elements from the other list and
append them to L
End {L has increasing order} Theorem 1: The number of
comparisons needed to merge sort
a list with n elements is O(nlog n)
Merge Sort

L: 8 2 4 6 9 7 10 1 5 3

≤ L: 1 2 3 4 5 6 7 8 9 10

Procedure mergeSort (L=a1…an)


If n>1 then
Begin
m:= n/2
Merge?
L1 := a1…am
L2 := am+1…an
L:=merge(mergeSort(L1),mergeSort(L2))
End
{L is sorted}
Summary
 Mathematical Induction
 Strong Induction and Well-Ordering
 Recursive Definitions and Structural
Induction
 Recursive Algorithms
Thanks

You might also like