Mth1114 Induction Andrecursion MINE

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

MTH1114

Computer
Mathematics
Week10
Mathematical Induction &
Recursion
Mathematical Induction

2
Induction
The principle of mathematical induction is a
useful tool for proving that a certain predicate is
true for all natural numbers.

It cannot be used to discover theorems, but only to


prove them.

3
Induction
If we have a propositional function P(n), and we
want to prove that P(n) is true for any natural
number n, we do the following:

• Show that P(0) is true.


(basis step)
• Show that if P(n) then P(n + 1) for any nN.
(inductive step)
• Then P(n) must be true for any nN.
(conclusion)

4
Induction
Example:

Show that n < 2n for all positive integers n.

Let P(n) be the proposition “n < 2n.”

1. Show that P(1) is true.


(basis step)

P(1) is true, because 1 < 21 = 2.

5
Induction
2. Show that if P(n) is true, then P(n + 1) is true.
(inductive step)

Assume that n < 2n is true.


We need to show that P(n + 1) is true, i.e.
n + 1 < 2n+1

We start from n < 2n:


n + 1 < 2n + 1  2n + 2n = 2n+1
Therefore, if n < 2n then n + 1 < 2n+1

6
Induction
3. Then P(n) must be true for any positive
integer.
(conclusion)

n < 2n is true for any positive integer.

End of proof.

7
Induction
Another Example (“Gauss”):

1 + 2 + … + n = n (n + 1)/2

1. Show that P(0) is true.


(basis step)

For n = 0 we get 0 = 0. True.

8
Induction
2. Show that if P(n) then P(n + 1) for any nN.
(inductive step)

1 + 2 + … + n = n (n + 1)/2
1 + 2 + … + n + (n + 1) = n (n + 1)/2 + (n + 1)
= (n + 1) (n/2 + 1)
= (n + 1) (n + 2)/2
= (n + 1) ((n + 1) + 1)/2

9
Induction

3. Then P(n) must be true for any nN.


(conclusion)

1 + 2 + … + n = n (n + 1)/2 is true for all nN.

End of proof.

10
Induction
There is another proof technique that is very similar
to the principle of mathematical induction.

It is called the second principle of mathematical


induction.

It can be used to prove that a propositional function


P(n) is true for any natural number n.

11
Induction
The second principle of mathematical induction:

• Show that P(0) is true.


(basis step)
• Show that if P(0) and P(1) and … and P(n),
then P(n + 1) for any nN.
(inductive step)
• Then P(n) must be true for any nN.
(conclusion)

12
Induction
Example:
Show that every integer greater than 1 can be
written as the product of primes.

• Show that P(2) is true.


(basis step)

2 is the product of one prime: itself.

13
Induction
• Show that if P(2) and P(3) and … and P(n),
then P(n + 1) for any nN. (inductive step)
Two possible cases:
• If (n + 1) is prime, then obviously P(n + 1) is true.
• If (n + 1) is composite, it can be written as the
product of two integers a and b such that
2  a  b < n + 1.
By the induction hypothesis, both a and b can be
written as the product of primes.
Therefore, n + 1 = ab can be written as the product
of primes.
14
Induction

• Then P(n) must be true for any nN.


(conclusion)

End of proof.

We have shown that every integer greater than 1


can be written as the product of primes.

15
Another Induction Example
Show that the sum of the first n odd integers is n2
– Example: If n = 5, 1+3+5+7+9 = 25 = 52
– Formally, Show  n P(n) where P(n) = ni=1(2i -1) = n2

Basis step : Show that P(1) is true


– P(1) = 1
Inductive hypothesis : assume true for k
– Note: we don’t yet know if this is true or not!
– Thus, we assume that P(k) is true, or that P(k) = ki=1(2i-1)= k2
Inductive step: show true for k+1
–We want to show that : P(k+1) = k+1i=1(2i-1)= (k+1)2
16
Another Induction Example
Induction Hypothesis
– P(k) = ki=1(2i-1)= k2
Inductive Steps
– P(k+1) = k+1i=1(2i-1)
= ki=1 (2i-1) + (2(k+1) - 1)
= k2 + (2k +1)
= k2 + 2k +1
= (k+1)2
17
What did we show
Base case: P(1)
If P(k) was true, then P(k+1) is true
– i.e., P(k) → P(k+1)

We know it’s true for P(1)


Because of P(k) → P(k+1), if it’s true for P(1), then it’s true for P(2)
Because of P(k) → P(k+1), if it’s true for P(2), then it’s true for P(3)
Because of P(k) → P(k+1), if it’s true for P(3), then it’s true for P(4)
Because of P(k) → P(k+1), if it’s true for P(4), then it’s true for P(5)
And onwards to infinity

Thus, it is true for all possible values of n

In other words, we showed that:


– [P(1)   k (P(k) → P(k+1))] →  n P(n)
18
More examples
Prove : 3 divides n 3 + 2n for n  0

Basis step : 3 divides 03 + 2  0 = 0, so the


required property is true when n = 0.

Inductive step : We want to prove


3 divides k 3 + k
 3 divides (k + 1) 3 + 2(k + 1)
Well,
(k + 1) 3 + 2(k + 1) = k 3 + 3k 2 + 3k + 1 + 2k + 2
= k 3 + 2k + 3k 2 + 3k + 3
= k 3 + 2k + 3(k 2 + k + 1) 19
Therefore,
3 divides k + 2k
3

 3 divides k 3 + 2k + 3(k 2 + k + 1)
 3 divides (k + 1) + 2(k + 1)
3

as required. This completes the inductive step,


and hence completes the proof.

20
Recursive Definitions
Recursion is a principle closely related to
mathematical induction.

In a recursive definition, an object is defined in


terms of itself.

We can recursively define sequences, functions


and sets.

21
Recursively Defined Sequences
Example:

The sequence {an} of powers of 2 is given by


an = 2n for n = 0, 1, 2, … .

The same sequence can also be defined


recursively:

a0 = 1
an+1 = 2an for n = 0, 1, 2, …

Obviously, induction and recursion are similar


principles.
22
Recursively Defined Functions
We can use the following method to define a
function with the natural numbers as its domain:

1. Specify the value of the function at zero.


2. Give a rule for finding its value at any integer
from its values at smaller integers.

Such a definition is called recursive or inductive


definition.

23
Recursively Defined Functions
Example:

f(0) = 3
f(n + 1) = 2f(n) + 3
f(0) = 3
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

24
Recursively Defined Functions
How can we recursively define the factorial
function f(n) = n! ?

f(0) = 1
f(n + 1) = (n + 1)f(n)
f(0) = 1
f(1) = 1f(0) = 11 = 1
f(2) = 2f(1) = 21 = 2
f(3) = 3f(2) = 32 = 6
f(4) = 4f(3) = 46 = 24

25
Recursively Defined Functions
A famous example: The Fibonacci numbers
f(0) = 0, f(1) = 1
f(n) = f(n – 1) + f(n - 2)
f(0) = 0
f(1) = 1
f(2) = f(1) + f(0) = 1 + 0 = 1
f(3) = f(2) + f(1) = 1 + 1 = 2
f(4) = f(3) + f(2) = 2 + 1 = 3
f(5) = f(4) + f(3) = 3 + 2 = 5
f(6) = f(5) + f(4) = 5 + 3 = 8
26
Recursive Algorithms
An algorithm is called recursive if it solves a problem
by reducing it to an instance of the same problem with
smaller input.

Example I: Recursive Euclidean Algorithm

procedure gcd(a, b: nonnegative integers with a < b)


if a = 0 then gcd(a, b) := b
else gcd(a, b) := gcd(b mod a, a)

27
Recursive Algorithms
Example II: Recursive Fibonacci Algorithm

procedure fibo(n: nonnegative integer)


if n = 0 then fibo(0) := 0
else if n = 1 then fibo(1) := 1
else fibo(n) := fibo(n – 1) + fibo(n – 2)

March 2, 2010 Applied Discrete Mathematics 28


Week 6: Counting
Recursive Algorithms
procedure iterative_fibo(n: nonnegative integer)
if n = 0 then y := 0
else
begin
x := 0
y := 1
for i := 1 to n-1
begin
z := x + y
x:=y
y := z
end
end {y is the n-th Fibonacci number}

29
Recursive Algorithms

For every recursive algorithm, there is an equivalent


iterative algorithm.

Recursive algorithms are often shorter, more


elegant, and easier to understand than their
iterative counterparts.

However, iterative algorithms are usually more


efficient in their use of space and time.

30

You might also like