MADChap5 InductionandRecursion
MADChap5 InductionandRecursion
Ly Anh Duong
duongla3@fe.edu.vn
Table of Contents
1 Mathematical Induction
▶ Mathematical Induction
▶ Recursive
▶ Recursive Algorithms
▶ Problems
Introduction
1 Mathematical Induction
1. Prove that:
n(n + 1)
1 + 2 + 3 + .... + n − 1 + n = .
2
2. Conjecture a formula for the sum of the first n positive odd integers. Then
prove your conjecture using the induction method.
3. Prove that:
20 + 21 + .... + 2n = 2n+1 − 1.
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 ≥ 1.
Example 1. Prove that if n is an integer greater than 1, then n can be written as
the product of primes.
Example 2. Prove that every amount of postage of 12 cents or more can be
formed using just 4-cents and 5-cents stamps.
▶ Mathematical Induction
▶ Recursive
▶ Recursive Algorithms
▶ Problems
Introduction
2 Recursive
Fibonacci Numbers:
n 1 2 3 4 5 6 7
Fn 1 1 2 3 5 8 13
1. Find F8 .
2. Find F16 if F18 = 2584, F19 = 4181.
Fibonacci Numbers:
n 1 2 3 4 5 6 7
Fn 1 1 2 3 5 8 13
1. Find F8 .
2. Find F16 if F18 = 2584, F19 = 4181.
Solution. F8 = 21; F16 = 987.
Fibonacci Numbers:
n 1 2 3 4 5 6 7
Fn 1 1 2 3 5 8 13
1. Find F8 .
2. Find F16 if F18 = 2584, F19 = 4181.
Solution. F8 = 21; F16 = 987.
In general, this infinite sequence can be formulated by: F1 = F2 = 1 and
Fn = Fn−1 + Fn−2 ∀ n ≥ 3.
We use two steps to define a function with the set of non negative integers as its
domain:
• Basis step: Specify the value of the function at zero.
• Recursive step: Give a rule for finding its value at an integer from its values
at smaller assessment integers.
1. an = 5n − 2, ∀n ≥ 1.
2. an = n, ∀n ≥ 1.
3. f (n) = 1 + 2 + 3 + ... + n, ∀n ≥ 1.
4. f (n) = 2022, ∀n ≥ 1.
1. Give the recursive definition of an (where a is a non zero real number and n is
a non negative integer).
2. Suppose that f is defined recursively by f (0) = 3, f (n + 1) = 2f (n) + 3. Find
f (1), f (2), f (3) and f (4).
n
3. Give a recursive definition of ak .
P
k=0
2.
k=0
k=0
Examples.
Consider the subset S of the set of integers recursively defined by:
• Basis step: 3 ∈ S
• Recursive step: If x ∈ S and y ∈ S, then x + y ∈ S.
Examples.
Consider the subset S of the set of integers recursively defined by:
• Basis step: 3 ∈ S
• Recursive step: If x ∈ S and y ∈ S, then x + y ∈ S.
→ S = {3, 6, 9, 12, 15, 18, 21, ...}
Find S if
a. 1 is in S, if x is in S then x + 1 and x + 2 are in S;
b. 1 is in S, if x is in S then x + 3 is in S;
c. 1, 2 are in S, if x is in S then x + 3 is in S.
Find S if
a. 1 is in S, if x is in S then x + 1 and x + 2 are in S;
b. 1 is in S, if x is in S then x + 3 is in S;
c. 1, 2 are in S, if x is in S then x + 3 is in S.
Solution.
a. S = {1, 2, 3, ...};
Find S if
a. 1 is in S, if x is in S then x + 1 and x + 2 are in S;
b. 1 is in S, if x is in S then x + 3 is in S;
c. 1, 2 are in S, if x is in S then x + 3 is in S.
Solution.
a. S = {1, 2, 3, ...};
b. S = {1, 4, 7, 10, ...};
Find S if
a. 1 is in S, if x is in S then x + 1 and x + 2 are in S;
b. 1 is in S, if x is in S then x + 3 is in S;
c. 1, 2 are in S, if x is in S then x + 3 is in S.
Solution.
a. S = {1, 2, 3, ...};
b. S = {1, 4, 7, 10, ...};
c. S = {1, 2, 4, 5, 7, 8, ...}.
P∗
The set of strings over the fintite set (alphabet) is defined recursively by
P
P∗
• BASIS STEP: λ ∈ (where λ is the empty string containing no symbols).
P∗ P∗
• RECURSIVE STEP: If w ∈ and x ∈ , then wx ∈ .
P
Examples.
1. = {1}
P
P∗
• P∗0 = {λ}
• P∗1 = {λ, 1}
• P2∗ = {λ, 1, 11}
• P∗3 = {λ, 1, 11, 111}
• 4 = {λ, 1, 11, 111, 1111}
........................
2. = {0, 1}
P
P∗
• P0 = {λ}
∗
• P1 = {λ, 0, 1}
∗
• P2 = {λ, 0, 1, 00, 01, 10, 11}
∗
• 3 = {λ, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111}
........................
▶ Mathematical Induction
▶ Recursive
▶ Recursive Algorithms
▶ Problems
Definition
3 Recursive Algorithms
• 5! = 5.4!
• 4! = 4.3!
• 3! = 3.2!
• 2! = 2.1!
• 1! = 1.0!
• 0! = 1(Basis step)
• Recursive steps
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
Ly Anh Duong Chapter 5 Induction and Recursion duongla3@fe.edu.vn 36 / 65
Recursive Algorithm for Computing an
3 Recursive Algorithms
• 34 = 3.33
• 33 = 3.32
• 32 = 3.31
• 31 = 3.30
• 30 = 1 (Basis step)
• Recursive step
31 =3
32 =9
33 = 27
34 = 81
b = 2, n = 5, m = 3
n = 5 odd: mpower(2, 5, 3) = (mpower(2, 2, 3)2 mod m.2 mod m) mod m
n = 2 even: mpower(2, 2, 3) = (mpower(2, 1, 3)2 ) mod 3
n = 1 odd: mpower(2, 1, 3) = (mpower(2, 0, 3)2 mod 3.2 mod 3) mod 3
mpower(2, 0, 3) = 1 (Basis step)
Recursive steps
• mpower(2, 1, 3) = 2
• mpower(2, 2, 3) = 1
• mpower(2, 5, 3) = 2
Hence, 25 mod 3 = 2
Example. List all the steps used to search for 9 in the sequence 2, 3, 4, 5, 6, 8, 9, 11
2 3 4 5 6 8 9 11
a1 a2 a3 a4 a5 a6 a7 a8
→ i = 1, j = 8, x = 9
• a1 = 9(!) → Search(2, 8, 9)
• a2 = 9(!) → Search(3, 8, 9)
• a3 = 9(!) → Search(4, 8, 9)
• a4 = 9(!) → Search(5, 8, 9)
• a5 = 9(!) → Search(6, 8, 9)
• a6 = 9(!) → Search(7, 8, 9)
• a7 = 9(ok)
• return 7
Example. To search for 19 in the list 1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16
→ i = 1, j = 16, x = 19
• m := ⌊(1 + 16)/2⌋ = 8 : 19 > 10 ∧ 16 > 8 → binary search(9, 16, 19)
• m := ⌊(9 + 16)/2⌋ = 12 : 19 > 16 ∧ 16 > 12 → binary search(13, 16, 19)
• m := ⌊(13 + 16)/2⌋ = 14 : 19 = 19
• return 14
Example. Use the merge sort to put the terms of the list 8, 2, 4, 6, 9, 7, 10, 1, 5, 3 in
increasing order.
Theorem. The number of comparisons needed to merge sort a list with n elements
is O(nlogn).
▶ Mathematical Induction
▶ Recursive
▶ Recursive Algorithms
▶ Problems
Mathematical Induction
4 Problems