Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Chapter 5: Induction and Recursion

Trần Hòa Phú

Ngày 29 tháng 4 năm 2024


5.1 Mathematical Induction

Trần Hòa Phú Chapter 5: Induction and Recursion 2 / 45


Theorem
To prove that P(n) is true for all positive integers n, where
P(n) is a propositional function, we complete two steps:
Basic Step we verify that P(1) is true.
Inductive Step Assume that P(k) is true for k is an
arbitrary positive integer. We prove that P(k + 1) is also
true.

Trần Hòa Phú Chapter 5: Induction and Recursion 3 / 45


Example Show that if n is a positive integer, then
n(n + 1)
1 + 2 + ... + n =
2
Solution
n(n + 1)
P(n) : ”1 + 2 + ... + n = ”.
2
1.2
Basic step P(1) : ”1 = ”. So P(1) is true.
2
Inductive step Assume that P(k) is true for k arbitrary positive integer:
k(k + 1)
1 + 2 + ... + k =
2
And we need to prove that P(k + 1) is also true.
(k + 1)(k + 2)
1 + 2 + ... + k + k + 1 =
2
In fact,
k(k + 1) (k + 1)(k + 2)
1 + 2 + ... + k + k + 1 = +k +1=
2 2
Therefore, P(k + 1) is also true.
In conclusion, by mathematical induction, P(n) is true for all positive integers n.
Trần Hòa Phú Chapter 5: Induction and Recursion 4 / 45
Exercise
1.Use mathematical induction to show that

1 + 2 + 22 + ... + 2n = 2n+1 − 1
for all nonnegative integers n.
2. Suppose {an } satisfies

a n

= 2an−1 , ∀n ≥ 2
a = 1

1
Prove that an = 2n−1 ∀n ≥ 1.

Trần Hòa Phú Chapter 5: Induction and Recursion 5 / 45


Strong Induction
Theorem
To prove that P(n) is true for all positive integers n, we complete
two steps:
Basic step verify that P(1) is true.
Inductive step Assume that P(1), P(2), .., P(k) are true for k is an
arbitrary positive integer. Then we need to prove P(k + 1) is also
true.

Trần Hòa Phú Chapter 5: Induction and Recursion 6 / 45


Example
Prove that every amount of postage of 12 cents or more can be
formed using just 4-cent and 5-cent stamps.

Trần Hòa Phú Chapter 5: Induction and Recursion 7 / 45


Exercise

Trần Hòa Phú Chapter 5: Induction and Recursion 8 / 45


5.3 Recursive Definitions and Structural
Induction
Definition
Recursion is a way to define an object in terms of itself.
Example Define a sequence an = 2n n = 1, 2, ... by
recursion.
Solution
an = 2.2n−1 = 2.an−1 n = 1, 2, 3, ...
a0 = 1

Trần Hòa Phú Chapter 5: Induction and Recursion 9 / 45


Exercise
Give a recursive definition of the sequence
{an }, n = 1, 2, 3, ... if
a) an = 6n
b) an = 2n + 1
c) an = 10n
d) an = n(n + 1)
f) an = 1 + (−1)n
Trần Hòa Phú Chapter 5: Induction and Recursion 10 / 45
Exercise
1) Let F (n) be the sum of the first n positive integer.
Give a recursive definition of F (n).
2) Let G(n) be the product of the first n positive integer.
Give a recursive definition of G(n).

Trần Hòa Phú Chapter 5: Induction and Recursion 11 / 45


Recursively Defined Sets and Structures
Example
Consider the subset S of the set of integers recursively
defined
Basic step: 3 ∈ S
Recursive step: If x ∈ S, y ∈ S, then x + y ∈ S

Trần Hòa Phú Chapter 5: Induction and Recursion 12 / 45


Exercise
Give a recursive definition of each of these sets
a) A = {2, 5, 8, 11, 14, ...}
b) B = {..., −5, −1, 3, 7, 11, ...}
c) C = {3, 12, 48, 192, 768, ...}
d) D = {1, 2, 4, 7, 11, 16, ...}

Trần Hòa Phú Chapter 5: Induction and Recursion 13 / 45


P∗
The set of string
Definition
The set ∗ of strings over the finite set (alphabet) is
P P

defined recursively by
Basis step λ ∈ ∗ (λ is the empty string containing no
P

symbols)
Inductive step If w ∈ ∗ and x ∈ , then wx ∈ ∗.
P P P

Example
= {1} ⇒ ∗ = {λ, 1, 11, 111, ...}: is set of string made
P P

by 1 with arbitrary length.


Trần Hòa Phú Chapter 5: Induction and Recursion 14 / 45
Exercise
P∗
Find if = {0, 1}.
P

Trần Hòa Phú Chapter 5: Induction and Recursion 15 / 45


Exercise
Let S be the subset of the set of ordered pairs of integers
defined recursively by
Basis step: (0, 0) ∈ S
Recursive step: If (a, b) ∈ S, then
(a + 2, b + 3) ∈ S and (a + 3, b + 2) ∈ S
A.(8,15) B.(9,15) C.(10,15) D.(12,15)

Trần Hòa Phú Chapter 5: Induction and Recursion 16 / 45


Exercise
Let S be the set defined recursively as follows:
Basis step: 2 ∈ S
Recursive step: If x ∈ S, then 2x ∈ S
What is S?
A. S = {2n |n = 1, 2, ...}
B. S = {22n |n = 1, 2, ...}
C. S = {2n|n = 1, 2, ...}

Trần Hòa Phú Chapter 5: Induction and Recursion 17 / 45


Recursive Algorithms
Definition
An algorithm is called recursive if it solves a problem by
reducing it to an instance of the same problem with smaller
input.

Trần Hòa Phú Chapter 5: Induction and Recursion 18 / 45


Algorithm 1: A Recursive Algorithm for Computing n!
procedure factorial(n: nonnegative integer)
if n = 0 then return 1
else return n. factorial(n − 1)
{ output is n!}

Trần Hòa Phú Chapter 5: Induction and Recursion 19 / 45


Algorithm 2: A Recursive Algorithm for Computing an
procedure power (a : nonzero real number, n: nonnegative
integer)
if n = 0 then return 1
else return a. power(a, n − 1)
{ output is an }

Trần Hòa Phú Chapter 5: Induction and Recursion 20 / 45


Algorithm 3: A Recursive algorithm for computing gcd(a, b)
procedure gcd(a, b : nonnegative integers with a < b)
if a = 0 then return b
else return gcd(b mod a, a)
{ output is gcd(a, b)}

Trần Hòa Phú Chapter 5: Induction and Recursion 21 / 45


Algorithm 4: A Recursive Linear Search
Algorithm
search(i,j,x) is the procedure that search for first occurrence of x in
the sequence ai , ai+1 , ..., aj .
procedure search(i, j, x : i, j, x , 1 ≤ i ≤ j ≤ n)
if ai = x then
return i
else if i = j then
return 0
else
return search(i + 1, j, x )
{ output is the location of x in a1 , a2 , ..., an if it appears, otherwise
it is 0}
Trần Hòa Phú Chapter 5: Induction and Recursion 22 / 45
Algorithm 5: A Recursive Binary Search Algorithm

Example To search for 8 in the list


1, 2, 3, 5, 6, 7, 8, 10
Trần Hòa Phú Chapter 5: Induction and Recursion 23 / 45
Proving Recursive Algorithms Correct
Example
Show that the following algorithm is correct by using
mathematical induction.
procedure factorial(n: nonnegative integer)
if n = 0 then return 1
else return n. factorial(n − 1)
{ output is n!}

Trần Hòa Phú Chapter 5: Induction and Recursion 24 / 45


Recursion and Iteration
A Fibonacci sequence is
fn = fn−1 + fn−2 ∀n ≥ 2, f0 = 0, f1 = 1 (1)

Trần Hòa Phú Chapter 5: Induction and Recursion 25 / 45


Trần Hòa Phú Chapter 5: Induction and Recursion 26 / 45
Merge Sort

Trần Hòa Phú Chapter 5: Induction and Recursion 27 / 45


Trần Hòa Phú Chapter 5: Induction and Recursion 28 / 45
Trần Hòa Phú Chapter 5: Induction and Recursion 29 / 45
Trần Hòa Phú Chapter 5: Induction and Recursion 30 / 45
Trần Hòa Phú Chapter 5: Induction and Recursion 31 / 45
Exercise
Consider the following algorithm
procedure F(a1, a2, ..., an : integers)
if n = 0 then return 0
else return an + F (a1, a2, ..., an−1)
Find
a) F (1, 3, 5) b) F (1, 2, 3, 5, 6)

Trần Hòa Phú Chapter 5: Induction and Recursion 32 / 45


Exercise
Let P(n) be the statement that n! < nn , where n is an
integer greater than 1. What do you need to prove in the
basis step if using induction method?
A. Show that P(2) is true
B. Show that P(3) is true
C. Show that P(4) is true
D. Show that P(1) is true

Trần Hòa Phú Chapter 5: Induction and Recursion 33 / 45


Exercise

Trần Hòa Phú Chapter 5: Induction and Recursion 34 / 45


Exercise
Find a recursive definition for the set of all integers divisible
by 3.
A. 3, −3 ∈ S and if a ∈ S then 3a ∈ S
B. 3 ∈ S and if a, b ∈ S then a − b ∈ S
C. 3 ∈ S and if a, b ∈ S then a + b ∈ S
D. 3 ∈ S and if a ∈ S then 3a ∈ S.

Trần Hòa Phú Chapter 5: Induction and Recursion 35 / 45


Exercise
Give a recursive definition of the sequence
an = 5n, n = 1, 2, ...
i. a0 = 0, an = an−1 + 5 for n = 1, 2, 3, ...
ii. a1 = 1, an = an−1 + 5 for n = 2, 3, ...
iii. a1 = 5, an−1 = an + 5 for n = 2, 3, ...
iv. a1 = 5, an = an−1 + 5 for n = 2, 3, ..

Trần Hòa Phú Chapter 5: Induction and Recursion 36 / 45


Exercise
Find a recursive definition for the set of all positive integers
NOT divisible by 4
i. If a ∈ S then a + 4 ∈ S
ii. 1, 2, 3 ∈ S. If a ∈ S, then a + 4, a − 4 ∈ S
iii. If a ∈ S, then a − 4, a + 4 ∈ S
iv. 1, 2, 3 ∈ S. If a ∈ S, then a + 4 ∈ S
A. i B. ii C. iii D. iv

Trần Hòa Phú Chapter 5: Induction and Recursion 37 / 45


Exercise
Given the recursive algorithm that computes the n-th
Fibonacci numbers
Procedure F(n: natural numbers)
If n = 0 then F (n) := 0
else
If n = 1 then F (n) := 1
else Fn) := F (n − 1) + F (n − 2)
How many additions are used if n = 6?
A. 7 B. 8 C. 9 D. 12

Trần Hòa Phú Chapter 5: Induction and Recursion 38 / 45


Exercise
Let S be the set defined recursively by:
5∈S
If x ∈ S then x + 5 ∈ S. What is S?
A. S = {5, 10, 15, 20, ...}
B. S = {0, 5, 10, 15, ...}
C . S = {0, 1, 2, 3, 4, ...}
D. S = {..., −10, −5, 0, 5, 10, ...}

Trần Hòa Phú Chapter 5: Induction and Recursion 39 / 45


Exercise
Find a recursive definition for the sequence
an = 1 + (−1)n n = 0, 1, 2, ...
i) a0 = 0, a1 = 2, an = an−2 for n > 1
ii) a0 = 2, a1 = 0, an = an−2 for n > 1
iii) a0 = 2, an = an−1 − 2 for n > 0
A.i B. ii C. iii D. None

Trần Hòa Phú Chapter 5: Induction and Recursion 40 / 45


Exercise
Given the recursive algorithm
Procedure AL(x integer, n positive integer)
If n =1 then AL(x,n) = x
else AL(x,n)= AL(x,n-1)+x
Find the value of AL(3,6)
A.18 B.12 C.6 D.9 E. None

Trần Hòa Phú Chapter 5: Induction and Recursion 41 / 45


Exercise
Find the output of the recursive algorithm if input n=5
procedure TT(n: integer)
If n=1 then f(1) = 0
else f(n) = f(n-1)*n
A.-120 B. -240 C.0 D. 120 E.240

Trần Hòa Phú Chapter 5: Induction and Recursion 42 / 45


Exercise
Given the recurrence relation
fn = 2fn−3 − 5fn−4 + 2n
How many initial conditions are needed to determine all
further terms?
A.2 B.4 C. None D.3 E.1

Trần Hòa Phú Chapter 5: Induction and Recursion 43 / 45


Exercise
In the strong induction proof of the following problem
Problem: Prove that P(n) is True for all n ≥ 24 with
P(n) : ”∃a, b ∈ Z+ : n = 5a + 7b”.
In order to prove P(k + 1) is True, we should .............................
A. use P(k − 4) : ”k − 4 = 5x + 7y , x , y ∈ N” is True and k + 1 = k − 4 + 5
B. use P(k − 2) : ”k − 2 = 5x + 7y , x , y ∈ N” is True and k + 1 = k − 2 + 3
C. use P(k − 1) : ”k − 1 = 5x + 7y , x , y ∈ N” is True and k + 1 = k − 1 + 2
D. use P(k − 3) : ”k − 3 = 5x + 7y , x , y ∈ N” is True and k + 1 = k − 3 + 4

Trần Hòa Phú Chapter 5: Induction and Recursion 44 / 45


Exercise
Given a recursive definition of the set of strings S
0∈S
x ∈ S → x 00 ∈ S
Which one is true?
i) 0000 ∈ S
ii) 00000 ∈ S
A. i B. ii C. Both D. None

Trần Hòa Phú Chapter 5: Induction and Recursion 45 / 45

You might also like