Lecture 7 - Induction-And-Recursion PDF
Lecture 7 - Induction-And-Recursion PDF
RECURSION
Lecture 7 - Ch. 4
4. Introduction
2
n(n 1)
Show that if n is a positive integer, then 1 2 ... n
Solution: 2
1(1 1)
BASIS STEP: because P(1)=1 (LHS), =1 (RHS). Thus, P(1) is true
INDUCTIVE STEP: 2
a) Assume P(k) is true for an arbitrary positive integer k. That is, we assume that
k (k 1)
P(k)= 1 2 ... k
2
b) Under assumption (a), it must be shown that P(k+1) is true, i.e.:
(k 1)[(k 1) 1] (k 1)(k 2)
P(k+1)= 1 2 ... k (k 1)
2 2
c) Comparing LHS in (a) and (b), we add k+1 to both sides of the equation in P(k)
k (k 1) k (k 1) 2(k 1) (k 1)(k 2)
P(k)+k+1=1 2 ... k (k 1) (k 1)
=P(k+1) 2 2 2
This shows that P(k+1) is true under the assumption that P(k) is true. 5
4.1 Mathematical Induction
Example 2:
6
c) Comparing LHS in (a) and (b), we add 2k+1 to both sides of the equation in P(k)
P(k)+ 2k+1 = 1 + 2 + 22 +· · ·+2k + 2k+1 = (2k+1 − 1) + 2k+1
= 2 · 2k+1 − 1 = 2k+2 − 1 =P(k+1)
This shows that P(k+1) is true under the assumption that P(k) is true.
4.2 Strong Induction
7
Note:
When we use strong induction to prove that P (n) is true for all positive integers n ,
our inductive hypothesis is the assumption that P(j) is true for j = 1,2, ... , k. That
is, the inductive hypothesis includes all k statements P(1), P(2), ... , P(k). Because
we can use all k statements P(1), P(2), ... , P(k) to prove P(k + 1), rather than just
the statement P(k) as in a proof by mathematical induction, strong induction is a
more flexible proof technique.
4.2 Strong Induction
Example 1:
8
Use strong induction to show that if you can run one mile or two miles, and if you can always run
two more miles once you have run a specified number of miles, then you can run any number of
miles.
Solution:
BASIS STEP: because we are told we can run one mile, so P (1) is true
INDUCTIVE STEP:
a) The inductive hypothesis is the assumption that P(j) is true; which is we can run any number of
miles from 1 to k. [P(j) is true for all positive integers 1 ≤j ≤ k]
b) We show that P(k+1) is true under assumption (a), that is, we must show that we can run
k + 1 miles:
If k = 1 , then we are already told that we can run two miles.
If k > 1 , then the inductive hypothesis tells us that we can run k-1 miles, so we can run
(k-1) + 2 = k + 1 miles.
4.3 Recursive Definitions and Structural Induction
Introduction
10
4.3 Recursive Definitions and Structural Induction
11
for n = 0, 1, 2, …
Or, using recursion:
1. Find first term of the sequence, namely, a0 = 1, then
2. find a term of the sequence from the previous one, namely,
an = 2an -1 , for n = 1, 2, ….
4.3 Recursive Definitions and Structural Induction
Recursively Defined Functions
12
Use two steps to define a function with the set of nonnegative integers as its
domain:
BASIS STEP: Specify that value of the function at zero. (f(0))
RECURSIVE STEP: Give a rule for finding its value at an integer from its
values at smaller integers. (f(n)=g(n-1))
Such a definition is called a recursive or inductive definition.
Examples:
Suppose that f is defined recursively by
f(0) = 3,
f(n) = 2f(n-1) + 3
Find f(1), f(2), f(3), and f(4).
Solution:
f(1) = 2f(0) + 3 = 2*3 + 3 = 9
f(2) = 2f(1) + 3 = 2*9 + 3 = 21
f(3) = 2f(2) + 3 = 2*21 + 3 = 45
f(4) = 2f(3) + 3 = 2*45 + 3 = 93
4.3 Recursive Definitions and Structural Induction
13
Example1:
Give an recursive definition of the factorial function f(n) = n! . Then find f(5)
Solution:
The recursive definition is:
Basis Step: f(0) = 1
Recursive Step: f(n) = (n)*f(n-1)
F(5) =5 F(4)
=5*4F(3)
=5*4*3 F(2)
=5 * 4 * 3 * 2F(1)
=5 * 4 * 3 * 2 * 1F(0)
=5*4*3*2*1*1
= 120
4.3 Recursive Definitions and Structural Induction
14
Example2: n
Give a recursive definition of . a
k 0
k
Solution:
n
a
k 0
k = a0 + a1 + a2 + a3 + … an
0
Basis Step: a
k 0
k a0
Inductive Step:
𝑛 𝑛−1
𝑎𝑘 = 𝑎𝑘 + 𝑎𝑛
𝑘=0 𝑘=0
4.3 Recursive Definitions and Structural Induction
15
DEFINITION 1
The Fibonacci numbers, f0, f1, f2, …, are defined by the equations
Basis step: f0= 0, f1 =1
Recursive step: fn = fn-1 + fn-2
for n = 2, 3, 4, ….
Example: Find the Fibonacci numbers f2, f3, f4, f5, and f6.
Solution:
f2 = f1 + f0 = 1 + 0 = 1,
f3 = f2 + f1 = 1 + 1 = 2,
f4 = f3 + f2 = 2 + 1 = 3,
f5 = f4 + f3 = 3 + 2 = 5,
f6 = f5 + f4 = 5 + 3 = 8.
Example: Find the Fibonacci numbers f7.
Solution ?
4.3 Recursive Definitions and Structural Induction
16
f4
f3
f2 fn+1 -1 addition to find fn
f2
f1 f f0
1