Induction and Recursion

Download Report

Transcript Induction and Recursion

Induction and Recursion
1
4.1 Mathematical Induction
Introduction
 Mathematical Induction is used to show that P(n) is true for every
positive integer n.
 Used only to prove results obtained in some other way.
 Example:
Suppose we have an infinite ladder, and we want to know whether
we can reach every step on the ladder. We know two things
1. We can reach the first rung of the ladder.
2. If we can reach a particular rung of the ladder, then we can reach
the next rung.
Can we conclude that we can reach every rung?
By 1, we can reach the first rung.
By 2, we can reach the second rung.
Apply 2 again, we can reach the third rung…
2
After 100 uses of 2, we can reach the 101st rung….
4.1 Mathematical Induction
Mathematical Induction
PRINCIPLE OF MATHEMATICAL INDUCTION
To prove that P(n) is true for all positive integers n, where P(n) is a
propositional function, we complete two steps.
BASIS STEP: We verify that P(1) is true.
INDUCTIVE STEP: We show that the conditional statement P(k) →P(k+1) is
true for all positive integers k.
 To complete the inductive step of a proof using the principle of
mathematical induction, we assume that P(k) is true for an arbitrary
positive integer k and show that under this assumption, P(k+1) must
also be true.
 The assumption that P(k) is true is called the inductive hypothesis.
 The proof technique is stated as
[P(1)Λ  k(P(k) →P(k+1) )] →  nP(n) where the domain is the set of positive
integers
3
4.1 Mathematical Induction
Examples of Proofs by Mathematical Induction
 Show that if n is a positive integer, then
n(n  1)
1  2  ...  n 
2
Solution:
1(1  1)
BASIS STEP: P(1) is true, because 1 = 2
INDUCTIVE STEP: For the inductive hypothesis we assume that
P(k) holds for an arbitrary positive integer k. That is, we assume that
k (k  1)
1  2  ...  k 
2
Under this assumption, it must be shown that P(k+1) is true, i.e.:
(k  1)[( k  1)  1] (k  1)( k  2)
1  2  ...  k  (k  1) 

2
2
When we add k+1 to both sides of the equation in P(k), we obtain
k (k  1)
k (k  1)  2(k  1) (k  1)( k  2)
1  2  ...  k  (k  1) 
 (k  1) 

2
2
2
This shows that P(k+1) is true under the assumption that P(k) is
4
true. This complete the inductive step.
4.1 Mathematical Induction
 Use mathematical induction to prove that n3 – n is divisible by 3
whenever n is a positive integer.
Solution:
BASIS STEP: The statement P(1) is true because 13 – 1 = 0 is
divisible by 3.
INDUCTIVE STEP: For the inductive hypothesis we assume that
P(k) is true; that is we assume that k3 – k is divisible by 3. To
complete the inductive step, we must show that when we assume
the inductive hypothesis, the statement (k+1)3 – (k+1) is also
divisible by 3.
(k+1)3 – (k+1) = (k3 + 3k2 + 3k + 1) – (k + 1)
= (k3 – k) + 3(k2 + k)
By inductive hypothesis, we know that (k3 – k) is divisible by 3; and
the second term is also divisible by 3. This completes the inductive
step.
5
4.3 Recursive Definitions and Structural
Induction
Introduction
6
4.3 Recursive Definitions and Structural
Induction
 Sometimes it’s easier to define an object in
terms of itself.
 This process is called Recursion.
 Example:
The sequence of powers of 2 is given by an = 2n
for n = 0, 1, 2, ….
This sequence can also be defined by giving the
first term of the sequence, namely, a0 = 1,
and a rule finding a term of the sequence from
the previous one, namely, an+1 = 2an, for n = 0,
1, 2, ….
7
4.3 Recursive Definitions and Structural
Induction
Recursively Defined Functions
 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.
RECURSIVE STEP: Give a rule for finding its value at an integer
from its values at smaller integers.
 Such a definition is called a recursive or inductive definition.
 Examples:
 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).
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
8
4.3 Recursive Definitions and Structural
Induction
 Examples:
 Give an inductive definition of the factorial function F(n) = n!.
Solution:
Basis Step: F(0) = 1
Inductive Step: F(n+1) = (n+1)F(n)
E.g. Find F(5).
F(5) = 5F(4) = 5*4F(3) = 5*4*3F(2) = 5 * 4 * 3 * 2F(1)
= 5 * 4 * 3 * 2 * 1F(0) = 5 * 4 * 3 * 2 * 1 * 1 = 120
n
 Give a recursive definition of
 a.
k 0
Solution:
Basis Step:
0
a
k 0
k
 a0
n 1
Inductive Step:
a
k 0
k
n
k
 ( ak )  an 1
k 0
9
4.3 Recursive Definitions and Structural
Induction
DEFINITION 1
The Fibonacci numbers, f0, f1, f2, …, are defined by the equations f0= 0, f1 =
1, and 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 ?
10
4.4 Recursive Algorithms
Introduction
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.
 Example: Give a recursive algorithm for computer n!, when n is a
nonnegative integer.
Review:
Basis Step: F(0) = 1
Inductive Step: F(n+1) = (n+1)F(n)
E.g. Find F(5).
F(5) = 5F(4) = 5*4F(3) = 5*4*3F(2) = 5 * 4 * 3 * 2F(1)
= 5 * 4 * 3 * 2 * 1F(0) = 5 * 4 * 3 * 2 * 1 * 1 = 120
ALGORITHM 1 A Recursive Algorithm for Computing n!.
procedure factorial(n: nonnegative integer)
if n = 0 then factorial(n):=1
else factorial(n):=n*factorial(n-1)
11
4.4 Recursive Algorithms
 Example: Give a recursive algorithm for computer an, where a is a
nonzero number and n is a nonnegative integer
ALGORITHM 2 A 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)
12
4.4 Recursive Algorithms
f4
f3
f2
f1
f2
f1
f0
f1
fn+1 -1 addition to find fn
f0
(f5 -1) addition to find f4
= (5-1) = 4 additions
13