Induction and Recursion: Lucia Moura
Induction and Recursion: Lucia Moura
Inductive Step: Let k 1. Assume P(1), P(2), . . . , P(k) are all true,
and prove that P(k + 1) is true.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Use strong induction to prove:
Theorem (The Fundamental Theorem of Arithmetic)
Every positive integer greater than 1 can be written uniquely as a prime or
as the product of two or more primes where the prime factors are written
in order of nondecreasing size.
Proof:
Part 1: Every positive integer greater than 1 can be written as a prime
or as the product of two or more primes.
Part 2: Show uniqueness, when the primes are written in nondecreasing
order.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Proof of Part 1: Consider P(n) the statement n can be written as a
prime or as the product of two or more primes.. We will use strong
induction to show that P(n) is true for every integer n 1.
Basis Step: P(2) is true, since 2 can be written as a prime, itself.
Induction Step: Let k 2. Assume P(1), P(2), . . . , P(k) are true. We
will prove that P(k + 1) is true, i.e. that k + 1 can be written as a prime
or the product of two or more primes.
Case 1: k + 1 is prime.
If k + 1 is prime, then the statement is true as k + 1 can be written as
itself, a prime.
Case 2: k + 1 is composite.
By denition, there exist two positive integers a and b with
2 a b < k + 1, such that k + 1 = ab. Since a, b < k + 1, we know by
induction hypothesis that a and b can each be written as a prime or the
product of two or more primes. Thus, k + 1 = ab can be written as a
product of two or more primes, namely those primes in the prime
factorization of a and those in the prime factorization of b.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
We want to prove Part 2. The following Lemma has been proven.
Lemma (A)
If a, b, and c are positive integers such that gcd(a, b) = 1 and a[bc, then
a[c.
We prove the following lemma using induction.
Lemma (B)
If p is a prime and p[a
1
a
2
a
n
, where each a
i
is an integer and n 1,
then p[a
i
for some i, 1 i n.
Proof: Let P(n) be the statement If a
1
, a
2
, . . . , a
n
are integers and p is
a prime number such that p[a
1
a
2
a
n
, then p[a
i
for some i, 1 i n.
We will prove P(n) is true for all n 1.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Let P(n) be the statement If a
1
, a
2
, . . . , a
n
are integers and p is a prime
number such that p[a
1
a
2
a
n
, then p[a
i
for some i, 1 i n.
We will prove P(n) is true for all n 1.
Basis: Prove that P(1) is true.
The statement is trivially true, since for n = 1, p[a
1
already gives that p[a
i
for some i, 1 i 1.
Induction step: Let n 2. Assume P(n 1) is true. Prove P(n) is true.
Let p be a prime such that p[a
1
a
2
a
n
. In the case that p[a
n
, we are
done. So, consider the case p ,[ a
n
. Since p is prime, we have
gcd(p, a
n
) = 1, thus, by Lemma A, p[a
1
. . . a
n1
. By induction hypothesis,
we have that p[a
i
for some i, 1 i n 1. Combining both cases, we
get that p[a
i
for some i, 1 i n.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Proof of Part 2: (uniqueness of the prime
factorization of a positive integer).
Suppose by contradiction that n can be written as a product of primes in
two dierent ways, say n = p
1
p
2
. . . p
s
and n = q
1
q
2
. . . q
t
, where each p
i
and q
j
are primes such that p
1
p
2
p
s
and q
1
q
2
q
t
.
When we remove all common primes from the two factorizations, we have:
p
i
1
p
i
2
p
i
u
= q
j
1
q
j
2
q
j
v
, where no primes occur on both sides of this
equations and u and v are positive integers.
By Lemma B, p
i
1
must divide q
j
k
for some k, 1 k v. Since p
i
1
and
q
j
k
are primes we must have p
i
1
= q
j
k
, which contradicts the fact that no
primes appear on both sides of the given equation.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Examples of statements that can be proven by strong
induction
1
Consider a game with 2 players that take turns removing any positive
number of matches they want from one of two piles of matches. The
player that removes the last match wins the game. Prove that if both
piles contains the same number of matches initially, the second player
can always guarantee a win.
2
Prove that every amount of 12 cents or more can be formed with
4-cent and 5-cent stamps. (also try to prove it in a dierent way
using mathematical induction)
3
Prove that algorithm gcd(a, b) (given in page 313 of the textbook)
returns the gcd(a, b) for all integers a, b, a < b.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Strong Induction or Complete Induction
Prove that procedure gcd(a, b) returns the gcd(a, b) for all integers a, b,
a < b.
procedure gcd(a, b: nonnegative integers with a < b)
if (a = 0) then return b
else return gcd(b mod a, a)
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Recursive Denitions
Recursive Denitions
We can use recursion to dene:
functions,
sequences,
sets.
Mathematical induction and strong induction can be used to prove results
about recursively dened sequences and functions.
Structural induction is used to prove results about recursively dened sets.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Recursive Denitions
Recursively Dened Functions
Examples:
Dening the factorial function recursively:
F(0) = 1,
F(n) = n F(n 1), for n 1.
Dening the maximum number of comparisons for the Mergesort
algorithm (given in page 318):
T(1) = 0,
T(n) = T(n/2|) + T(n/2|) + n 1, for n 2.
Number of moves needed to solve the Hanoi tower problem:
H(1) = 1,
H(n) = 2H(n 1) + 1, for n 2.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Recursive Denitions
Recursively Dened Sequences
Consider the Fibonacci numbers, recursively dened by:
f
0
= 0,
f
1
= 1,
f
n
= f
n1
+ f
n2
, for n 2.
Prove that whenever n 3, f
n
>
n2
where = (1 +
5)/2.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Recursive Denitions
Let P(n) be the statement f
n
>
n2
. We will show that P(n) is true
for n 3 using strong induction.
Basis: We show that P(3) and P(4) are true:
= (1 +
2
= ((1+
5)/2)
2
= (1
2
+2
5+5)/4 = (3+
5)/2 + 1) =
k3
((3 +
5)/2)
=
k3
2
=
k1
.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Recursive Denitions
Recursively Dened Sets and Structures
Denition (Set of strings over an alphabet)
The set
and x , then wx
.
Example: If = 0, 1, then
and x , then wx
.
We now give a denition of concatenation of two strings.
Note how this denition is built on the denition of string.
Denition (Concatenation of two strings)
Basis Step: If w
, then w = w.
Recursive Step: If w
1
, w
2
and x , then
w
1
(w
2
x) = (w
1
w
2
)x.
CSI2101 Discrete Structures Winter 2010: Induction and Recursion Lucia Moura
Induction Strong Induction Recursive Defs and Structural Induction Program Correctness
Structural Induction
We now give a recursive denition of the reversal of a string.
Denition (Reversal of a string)
Basis Step:
R
=
Recursive Step: If w
The initial assertion p is the condition that the programs input (its
initial state) is guaranteed (by its user) to satisfy.
The nal assertion q is the condition that the output produced by the
program (its nal state) is required to satisfy.
Hoare triple notation:
The notation pSq means that, for all inputs I such that p(I) is true,
if program S (given input I) halts and produces output O = S(I),
then q(O) is true.