March 3, Applied Discrete Mathematics 1
InductionInduction
TheThe principle of mathematical inductionprinciple of mathematical induction is ais a
useful tool for proving that a certain predicate isuseful tool for proving that a certain predicate is
true fortrue for all natural numbersall natural numbers..
It cannot be used to discover theorems, but only toIt cannot be used to discover theorems, but only to
prove them.prove them.
March 3, Applied Discrete Mathematics 2
InductionInduction
If we have a propositional function P(n), and weIf we have a propositional function P(n), and we
want to prove that P(n) is true for any naturalwant to prove that P(n) is true for any natural
number n, we do the following:number n, we do the following:
• Show that P(0) is true.Show that P(0) is true.
(basis step)(basis step)
• Show that if P(n) then P(n + 1) for any nShow that if P(n) then P(n + 1) for any n∈∈N.N.
(inductive step)(inductive step)
• Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N.
(conclusion)(conclusion)
March 3, Applied Discrete Mathematics 3
InductionInduction
Example:Example:
Show that n < 2Show that n < 2nn
for all positive integers n.for all positive integers n.
Let P(n) be the proposition “n < 2Let P(n) be the proposition “n < 2nn
.”.”
1. Show that P(1) is true.1. Show that P(1) is true.
(basis step)(basis step)
P(1) is true, because 1 < 2P(1) is true, because 1 < 211
= 2.= 2.
March 3, Applied Discrete Mathematics 4
InductionInduction
2. Show that if P(n) is true, then P(n + 1) is true.2. Show that if P(n) is true, then P(n + 1) is true.
(inductive step)(inductive step)
Assume that n < 2Assume that n < 2nn
is true.is true.
We need to show that P(n + 1) is true, i.e.We need to show that P(n + 1) is true, i.e.
n + 1 < 2n + 1 < 2n+1n+1
We start from n < 2We start from n < 2nn
::
n + 1 < 2n + 1 < 2nn
+ 1+ 1 ≤≤ 22nn
+ 2+ 2nn
= 2= 2n+1n+1
Therefore, if n < 2Therefore, if n < 2nn
then n + 1 < 2then n + 1 < 2n+1n+1
March 3, Applied Discrete Mathematics 5
InductionInduction
3.3. Then P(n) must be true for any positiveThen P(n) must be true for any positive
integer.integer.
(conclusion)(conclusion)
n < 2n < 2nn
is true for any positive integer.is true for any positive integer.
End of proof.End of proof.
March 3, Applied Discrete Mathematics 6
InductionInduction
Another Example (“Gauss”):Another Example (“Gauss”):
1 + 2 + … + n = n (n + 1)/21 + 2 + … + n = n (n + 1)/2
1.1. Show that P(0) is true.Show that P(0) is true.
(basis step)(basis step)
For n = 0 we get 0 = 0.For n = 0 we get 0 = 0. True.True.
March 3, Applied Discrete Mathematics 7
InductionInduction
2.2. Show that if P(n) then P(n + 1) for any nShow that if P(n) then P(n + 1) for any n∈∈NN..
(inductive step)(inductive step)
1 + 2 + … + n = n (n + 1)/21 + 2 + … + n = n (n + 1)/2
1 + 2 + … + n1 + 2 + … + n + (n + 1)+ (n + 1) = n (n + 1)/2= n (n + 1)/2 + (n + 1)+ (n + 1)
= (2n + 2 + n (n + 1))/2= (2n + 2 + n (n + 1))/2
= (2n + 2 + n= (2n + 2 + n22
+ n)/2+ n)/2
= (2 + 3n + n= (2 + 3n + n22
)/2)/2
= (n + 1) (n + 2)/2= (n + 1) (n + 2)/2
== (n + 1)(n + 1) (((n + 1)(n + 1) + 1)/2+ 1)/2
March 3, Applied Discrete Mathematics 8
InductionInduction
3.3. Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N.
(conclusion)(conclusion)
1 + 2 + … + n = n (n + 1)/2 is true for all n1 + 2 + … + n = n (n + 1)/2 is true for all n∈∈N.N.
End of proof.End of proof.
March 3, Applied Discrete Mathematics 9
InductionInduction
There is another proof technique that is very similarThere is another proof technique that is very similar
to the principle of mathematical induction.to the principle of mathematical induction.
It is calledIt is called the second principle of mathematicalthe second principle of mathematical
inductioninduction..
It can be used to prove that a propositional functionIt can be used to prove that a propositional function
P(n) is true for any natural number n.P(n) is true for any natural number n.
March 3, Applied Discrete Mathematics 10
InductionInduction
The second principle of mathematical induction:The second principle of mathematical induction:
• Show that P(0) is true.Show that P(0) is true.
(basis step)(basis step)
• Show that if P(0) and P(1) and … and P(n),Show that if P(0) and P(1) and … and P(n),
then P(n + 1) for any nthen P(n + 1) for any n∈∈N.N.
(inductive step)(inductive step)
• Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N.
(conclusion)(conclusion)
March 3, Applied Discrete Mathematics 11
InductionInduction
Example:Example:
Show that every integer greater than 1 can beShow that every integer greater than 1 can be
written as the product of primes.written as the product of primes.
• Show that P(2) is true.Show that P(2) is true.
(basis step)(basis step)
2 is the product of one prime: itself.2 is the product of one prime: itself.
March 3, Applied Discrete Mathematics 12
InductionInduction
• Show that if P(2) and P(3) and … and P(n),Show that if P(2) and P(3) and … and P(n),
then P(n + 1) for any nthen P(n + 1) for any n∈∈N.N. (inductive step)(inductive step)
Two possible cases:Two possible cases:
• If (n + 1) isIf (n + 1) is primeprime, then obviously P(n + 1) is true., then obviously P(n + 1) is true.
• If (n + 1) isIf (n + 1) is compositecomposite, it can be written as the, it can be written as the
product of two integers a and b such thatproduct of two integers a and b such that
22 ≤≤ aa ≤≤ b < n + 1.b < n + 1.
By theBy the induction hypothesisinduction hypothesis, both a and b can be, both a and b can be
written as the product of primes.written as the product of primes.
Therefore, n + 1 = aTherefore, n + 1 = a⋅⋅b can be written as the productb can be written as the product
of primes.of primes.
March 3, Applied Discrete Mathematics 13
InductionInduction
• Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N.
(conclusion)(conclusion)
End of proof.End of proof.
We have shown thatWe have shown that every integer greater than 1every integer greater than 1
can be written as the product of primes.can be written as the product of primes.
March 3, Applied Discrete Mathematics 14
Recursive DefinitionsRecursive Definitions
RecursionRecursion is a principle closely related tois a principle closely related to
mathematical induction.mathematical induction.
In aIn a recursive definitionrecursive definition, an object is defined in, an object is defined in
terms of itself.terms of itself.
We can recursively defineWe can recursively define sequencessequences,, functionsfunctions
andand setssets..
March 3, Applied Discrete Mathematics 15
Recursively Defined SequencesRecursively Defined Sequences
Example:Example:
The sequence {aThe sequence {ann} of powers of 2 is given by} of powers of 2 is given by
aann = 2= 2nn
for n = 0, 1, 2, … .for n = 0, 1, 2, … .
The same sequence can also be definedThe same sequence can also be defined
recursivelyrecursively::
aa00 = 1= 1
aan+1n+1 = 2a= 2ann for n = 0, 1, 2, …for n = 0, 1, 2, …
Obviously, induction and recursion are similarObviously, induction and recursion are similar
principles.principles.
March 3, Applied Discrete Mathematics 16
Recursively Defined FunctionsRecursively Defined Functions
We can use the following method to define aWe can use the following method to define a
function with thefunction with the natural numbersnatural numbers as its domain:as its domain:
1.1. Specify the value of the function at zero.Specify the value of the function at zero.
2.2. Give a rule for finding its value at any integerGive a rule for finding its value at any integer
from its values at smaller integers.from its values at smaller integers.
Such a definition is calledSuch a definition is called recursiverecursive oror inductiveinductive
definitiondefinition..
March 3, Applied Discrete Mathematics 17
Recursively Defined FunctionsRecursively Defined Functions
Example:Example:
f(0) = 3f(0) = 3
f(n + 1) = 2f(n) + 3f(n + 1) = 2f(n) + 3
f(0) = 3f(0) = 3
f(1) = 2f(0) + 3 = 2f(1) = 2f(0) + 3 = 2⋅⋅3 + 3 = 93 + 3 = 9
f(2) = 2f(1) + 3 = 2f(2) = 2f(1) + 3 = 2⋅⋅9 + 3 = 219 + 3 = 21
f(3) = 2f(2) + 3 = 2f(3) = 2f(2) + 3 = 2⋅⋅21 + 3 = 4521 + 3 = 45
f(4) = 2f(3) + 3 = 2f(4) = 2f(3) + 3 = 2⋅⋅45 + 3 = 9345 + 3 = 93
March 3, Applied Discrete Mathematics 18
Recursively Defined FunctionsRecursively Defined Functions
How can we recursively define the factorialHow can we recursively define the factorial
function f(n) = n! ?function f(n) = n! ?
f(0) = 1f(0) = 1
f(n + 1) = (n + 1)f(n)f(n + 1) = (n + 1)f(n)
f(0) = 1f(0) = 1
f(1) = 1f(0) = 1f(1) = 1f(0) = 1⋅⋅1 = 11 = 1
f(2) = 2f(1) = 2f(2) = 2f(1) = 2⋅⋅1 = 21 = 2
f(3) = 3f(2) = 3f(3) = 3f(2) = 3⋅⋅2 = 62 = 6
f(4) = 4f(3) = 4f(4) = 4f(3) = 4⋅⋅6 = 246 = 24
March 3, Applied Discrete Mathematics 19
Recursively Defined FunctionsRecursively Defined Functions
A famous example: The Fibonacci numbersA famous example: The Fibonacci numbers
f(0) = 0, f(1) = 1f(0) = 0, f(1) = 1
f(n) = f(n – 1) + f(n - 2)f(n) = f(n – 1) + f(n - 2)
f(0) = 0f(0) = 0
f(1) = 1f(1) = 1
f(2) = f(1) + f(0) = 1 + 0 = 1f(2) = f(1) + f(0) = 1 + 0 = 1
f(3) = f(2) + f(1) = 1 + 1 = 2f(3) = f(2) + f(1) = 1 + 1 = 2
f(4) = f(3) + f(2) = 2 + 1 = 3f(4) = f(3) + f(2) = 2 + 1 = 3
f(5) = f(4) + f(3) = 3 + 2 = 5f(5) = f(4) + f(3) = 3 + 2 = 5
f(6) = f(5) + f(4) = 5 + 3 = 8f(6) = f(5) + f(4) = 5 + 3 = 8
March 3, Applied Discrete Mathematics 20
Recursively Defined SetsRecursively Defined Sets
If we want to recursively define a set, we need toIf we want to recursively define a set, we need to
provide two things:provide two things:
• anan initial setinitial set of elements,of elements,
• rulesrules for the construction offor the construction of additionaladditional
elements from elements in the set.elements from elements in the set.
Example:Example: Let S be recursively defined by:Let S be recursively defined by:
33 ∈∈ SS
(x + y)(x + y) ∈∈ S if (xS if (x ∈∈ S) and (yS) and (y ∈∈ S)S)
S is the set of positive integers divisible by 3.S is the set of positive integers divisible by 3.
March 3, Applied Discrete Mathematics 21
Recursively Defined SetsRecursively Defined Sets
Proof:Proof:
Let A be the set of all positive integers divisible by 3.Let A be the set of all positive integers divisible by 3.
To show that A = S, we must show thatTo show that A = S, we must show that
AA ⊆⊆ S and SS and S ⊆⊆ A.A.
Part I:Part I: To prove that ATo prove that A ⊆⊆ S, we must show thatS, we must show that
every positive integer divisible by 3 is in S.every positive integer divisible by 3 is in S.
We will use mathematical induction to show this.We will use mathematical induction to show this.
March 3, Applied Discrete Mathematics 22
Recursively Defined SetsRecursively Defined Sets
Let P(n) be the statement “3n belongs to S”.Let P(n) be the statement “3n belongs to S”.
Basis step:Basis step: P(1) is true, because 3 is in S.P(1) is true, because 3 is in S.
Inductive step:Inductive step: To show:To show:
If P(n) is true, then P(n + 1) is true.If P(n) is true, then P(n + 1) is true.
Assume 3n is in S. Since 3n is in S and 3 is in S, itAssume 3n is in S. Since 3n is in S and 3 is in S, it
follows from the recursive definition of S thatfollows from the recursive definition of S that
3n + 3 = 3(n + 1) is also in S.3n + 3 = 3(n + 1) is also in S.
Conclusion of Part I:Conclusion of Part I: AA ⊆⊆ S.S.
March 3, Applied Discrete Mathematics 23
Recursively Defined SetsRecursively Defined Sets
Part II:Part II: To show: STo show: S ⊆⊆ A.A.
Basis step:Basis step: To show:To show:
All initial elements of S are in A. 3 is in A. True.All initial elements of S are in A. 3 is in A. True.
Inductive step:Inductive step: To show:To show:
(x + y) is in A whenever x and y are in S.(x + y) is in A whenever x and y are in S.
If x and y are both in A, it follows that 3 | x and 3 | y.If x and y are both in A, it follows that 3 | x and 3 | y.
As we already know, it follows that 3 | (x + y).As we already know, it follows that 3 | (x + y).
Conclusion of Part II:Conclusion of Part II: SS ⊆⊆ A.A.
Overall conclusion:Overall conclusion: A = S.A = S.
March 3, Applied Discrete Mathematics 24
Recursively Defined SetsRecursively Defined Sets
Another example:Another example:
The well-formed formulas of variables, numeralsThe well-formed formulas of variables, numerals
and operators from {+, -, *, /, ^} are defined by:and operators from {+, -, *, /, ^} are defined by:
x is a well-formed formula if x is a numeral orx is a well-formed formula if x is a numeral or
variable.variable.
(f + g), (f – g), (f * g), (f / g), (f ^ g) are well-formed(f + g), (f – g), (f * g), (f / g), (f ^ g) are well-formed
formulas if f and g are.formulas if f and g are.
March 3, Applied Discrete Mathematics 25
Recursively Defined SetsRecursively Defined Sets
With this definition, we can construct formulas suchWith this definition, we can construct formulas such
as:as:
(x – y)(x – y)
((z / 3) – y)((z / 3) – y)
((z / 3) – (6 + 5))((z / 3) – (6 + 5))
((z / (2 * 4)) – (6 + 5))((z / (2 * 4)) – (6 + 5))
March 3, Applied Discrete Mathematics 26
Recursive AlgorithmsRecursive Algorithms
An algorithm is calledAn algorithm is called recursiverecursive if it solves a problemif it solves a problem
by reducing it to an instance of the same problem withby reducing it to an instance of the same problem with
smaller input.smaller input.
Example I:Example I: Recursive Euclidean AlgorithmRecursive Euclidean Algorithm
procedureprocedure gcd(a, b: nonnegative integers with a < b)gcd(a, b: nonnegative integers with a < b)
ifif a = 0a = 0 thenthen gcd(a, b) := bgcd(a, b) := b
elseelse gcd(a, b) := gcd(bgcd(a, b) := gcd(b modmod a, a)a, a)
March 3, Applied Discrete Mathematics 27
Recursive AlgorithmsRecursive Algorithms
Example II:Example II: Recursive Fibonacci AlgorithmRecursive Fibonacci Algorithm
procedureprocedure fibo(n: nonnegative integer)fibo(n: nonnegative integer)
ifif n = 0n = 0 thenthen fibo(0) := 0fibo(0) := 0
else ifelse if n = 1n = 1 thenthen fibo(1) := 1fibo(1) := 1
elseelse fibo(n) := fibo(n – 1) + fibo(n – 2)fibo(n) := fibo(n – 1) + fibo(n – 2)
March 3, Applied Discrete Mathematics 28
Recursive AlgorithmsRecursive Algorithms
Recursive Fibonacci Evaluation:Recursive Fibonacci Evaluation:
f(4)f(4)
f(3)f(3)
f(2)f(2)
f(1)f(1) f(0)f(0)
f(1)f(1)
f(2)f(2)
f(1)f(1) f(0)f(0)
March 3, Applied Discrete Mathematics 29
Recursive AlgorithmsRecursive Algorithms
procedureprocedure iterative_fibo(n: nonnegative integer)iterative_fibo(n: nonnegative integer)
ifif n = 0n = 0 thenthen y := 0y := 0
elseelse
beginbegin
x := 0x := 0
y := 1y := 1
forfor i := 1i := 1 toto n-1n-1
beginbegin
z := x + yz := x + y
x : = yx : = y
y := zy := z
endend
endend {y is the n-th Fibonacci number}{y is the n-th Fibonacci number}
March 3, Applied Discrete Mathematics 30
Recursive AlgorithmsRecursive Algorithms
For every recursive algorithm, there is anFor every recursive algorithm, there is an equivalentequivalent
iterative algorithm.iterative algorithm.
Recursive algorithms are oftenRecursive algorithms are often shortershorter,, moremore
elegantelegant, and, and easier to understandeasier to understand than theirthan their
iterative counterparts.iterative counterparts.
However, iterative algorithms are usuallyHowever, iterative algorithms are usually moremore
efficientefficient in their use of space and time.in their use of space and time.

Per4 induction

  • 1.
    March 3, AppliedDiscrete Mathematics 1 InductionInduction TheThe principle of mathematical inductionprinciple of mathematical induction is ais a useful tool for proving that a certain predicate isuseful tool for proving that a certain predicate is true fortrue for all natural numbersall natural numbers.. It cannot be used to discover theorems, but only toIt cannot be used to discover theorems, but only to prove them.prove them.
  • 2.
    March 3, AppliedDiscrete Mathematics 2 InductionInduction If we have a propositional function P(n), and weIf we have a propositional function P(n), and we want to prove that P(n) is true for any naturalwant to prove that P(n) is true for any natural number n, we do the following:number n, we do the following: • Show that P(0) is true.Show that P(0) is true. (basis step)(basis step) • Show that if P(n) then P(n + 1) for any nShow that if P(n) then P(n + 1) for any n∈∈N.N. (inductive step)(inductive step) • Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N. (conclusion)(conclusion)
  • 3.
    March 3, AppliedDiscrete Mathematics 3 InductionInduction Example:Example: Show that n < 2Show that n < 2nn for all positive integers n.for all positive integers n. Let P(n) be the proposition “n < 2Let P(n) be the proposition “n < 2nn .”.” 1. Show that P(1) is true.1. Show that P(1) is true. (basis step)(basis step) P(1) is true, because 1 < 2P(1) is true, because 1 < 211 = 2.= 2.
  • 4.
    March 3, AppliedDiscrete Mathematics 4 InductionInduction 2. Show that if P(n) is true, then P(n + 1) is true.2. Show that if P(n) is true, then P(n + 1) is true. (inductive step)(inductive step) Assume that n < 2Assume that n < 2nn is true.is true. We need to show that P(n + 1) is true, i.e.We need to show that P(n + 1) is true, i.e. n + 1 < 2n + 1 < 2n+1n+1 We start from n < 2We start from n < 2nn :: n + 1 < 2n + 1 < 2nn + 1+ 1 ≤≤ 22nn + 2+ 2nn = 2= 2n+1n+1 Therefore, if n < 2Therefore, if n < 2nn then n + 1 < 2then n + 1 < 2n+1n+1
  • 5.
    March 3, AppliedDiscrete Mathematics 5 InductionInduction 3.3. Then P(n) must be true for any positiveThen P(n) must be true for any positive integer.integer. (conclusion)(conclusion) n < 2n < 2nn is true for any positive integer.is true for any positive integer. End of proof.End of proof.
  • 6.
    March 3, AppliedDiscrete Mathematics 6 InductionInduction Another Example (“Gauss”):Another Example (“Gauss”): 1 + 2 + … + n = n (n + 1)/21 + 2 + … + n = n (n + 1)/2 1.1. Show that P(0) is true.Show that P(0) is true. (basis step)(basis step) For n = 0 we get 0 = 0.For n = 0 we get 0 = 0. True.True.
  • 7.
    March 3, AppliedDiscrete Mathematics 7 InductionInduction 2.2. Show that if P(n) then P(n + 1) for any nShow that if P(n) then P(n + 1) for any n∈∈NN.. (inductive step)(inductive step) 1 + 2 + … + n = n (n + 1)/21 + 2 + … + n = n (n + 1)/2 1 + 2 + … + n1 + 2 + … + n + (n + 1)+ (n + 1) = n (n + 1)/2= n (n + 1)/2 + (n + 1)+ (n + 1) = (2n + 2 + n (n + 1))/2= (2n + 2 + n (n + 1))/2 = (2n + 2 + n= (2n + 2 + n22 + n)/2+ n)/2 = (2 + 3n + n= (2 + 3n + n22 )/2)/2 = (n + 1) (n + 2)/2= (n + 1) (n + 2)/2 == (n + 1)(n + 1) (((n + 1)(n + 1) + 1)/2+ 1)/2
  • 8.
    March 3, AppliedDiscrete Mathematics 8 InductionInduction 3.3. Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N. (conclusion)(conclusion) 1 + 2 + … + n = n (n + 1)/2 is true for all n1 + 2 + … + n = n (n + 1)/2 is true for all n∈∈N.N. End of proof.End of proof.
  • 9.
    March 3, AppliedDiscrete Mathematics 9 InductionInduction There is another proof technique that is very similarThere is another proof technique that is very similar to the principle of mathematical induction.to the principle of mathematical induction. It is calledIt is called the second principle of mathematicalthe second principle of mathematical inductioninduction.. It can be used to prove that a propositional functionIt can be used to prove that a propositional function P(n) is true for any natural number n.P(n) is true for any natural number n.
  • 10.
    March 3, AppliedDiscrete Mathematics 10 InductionInduction The second principle of mathematical induction:The second principle of mathematical induction: • Show that P(0) is true.Show that P(0) is true. (basis step)(basis step) • Show that if P(0) and P(1) and … and P(n),Show that if P(0) and P(1) and … and P(n), then P(n + 1) for any nthen P(n + 1) for any n∈∈N.N. (inductive step)(inductive step) • Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N. (conclusion)(conclusion)
  • 11.
    March 3, AppliedDiscrete Mathematics 11 InductionInduction Example:Example: Show that every integer greater than 1 can beShow that every integer greater than 1 can be written as the product of primes.written as the product of primes. • Show that P(2) is true.Show that P(2) is true. (basis step)(basis step) 2 is the product of one prime: itself.2 is the product of one prime: itself.
  • 12.
    March 3, AppliedDiscrete Mathematics 12 InductionInduction • Show that if P(2) and P(3) and … and P(n),Show that if P(2) and P(3) and … and P(n), then P(n + 1) for any nthen P(n + 1) for any n∈∈N.N. (inductive step)(inductive step) Two possible cases:Two possible cases: • If (n + 1) isIf (n + 1) is primeprime, then obviously P(n + 1) is true., then obviously P(n + 1) is true. • If (n + 1) isIf (n + 1) is compositecomposite, it can be written as the, it can be written as the product of two integers a and b such thatproduct of two integers a and b such that 22 ≤≤ aa ≤≤ b < n + 1.b < n + 1. By theBy the induction hypothesisinduction hypothesis, both a and b can be, both a and b can be written as the product of primes.written as the product of primes. Therefore, n + 1 = aTherefore, n + 1 = a⋅⋅b can be written as the productb can be written as the product of primes.of primes.
  • 13.
    March 3, AppliedDiscrete Mathematics 13 InductionInduction • Then P(n) must be true for any nThen P(n) must be true for any n∈∈N.N. (conclusion)(conclusion) End of proof.End of proof. We have shown thatWe have shown that every integer greater than 1every integer greater than 1 can be written as the product of primes.can be written as the product of primes.
  • 14.
    March 3, AppliedDiscrete Mathematics 14 Recursive DefinitionsRecursive Definitions RecursionRecursion is a principle closely related tois a principle closely related to mathematical induction.mathematical induction. In aIn a recursive definitionrecursive definition, an object is defined in, an object is defined in terms of itself.terms of itself. We can recursively defineWe can recursively define sequencessequences,, functionsfunctions andand setssets..
  • 15.
    March 3, AppliedDiscrete Mathematics 15 Recursively Defined SequencesRecursively Defined Sequences Example:Example: The sequence {aThe sequence {ann} of powers of 2 is given by} of powers of 2 is given by aann = 2= 2nn for n = 0, 1, 2, … .for n = 0, 1, 2, … . The same sequence can also be definedThe same sequence can also be defined recursivelyrecursively:: aa00 = 1= 1 aan+1n+1 = 2a= 2ann for n = 0, 1, 2, …for n = 0, 1, 2, … Obviously, induction and recursion are similarObviously, induction and recursion are similar principles.principles.
  • 16.
    March 3, AppliedDiscrete Mathematics 16 Recursively Defined FunctionsRecursively Defined Functions We can use the following method to define aWe can use the following method to define a function with thefunction with the natural numbersnatural numbers as its domain:as its domain: 1.1. Specify the value of the function at zero.Specify the value of the function at zero. 2.2. Give a rule for finding its value at any integerGive a rule for finding its value at any integer from its values at smaller integers.from its values at smaller integers. Such a definition is calledSuch a definition is called recursiverecursive oror inductiveinductive definitiondefinition..
  • 17.
    March 3, AppliedDiscrete Mathematics 17 Recursively Defined FunctionsRecursively Defined Functions Example:Example: f(0) = 3f(0) = 3 f(n + 1) = 2f(n) + 3f(n + 1) = 2f(n) + 3 f(0) = 3f(0) = 3 f(1) = 2f(0) + 3 = 2f(1) = 2f(0) + 3 = 2⋅⋅3 + 3 = 93 + 3 = 9 f(2) = 2f(1) + 3 = 2f(2) = 2f(1) + 3 = 2⋅⋅9 + 3 = 219 + 3 = 21 f(3) = 2f(2) + 3 = 2f(3) = 2f(2) + 3 = 2⋅⋅21 + 3 = 4521 + 3 = 45 f(4) = 2f(3) + 3 = 2f(4) = 2f(3) + 3 = 2⋅⋅45 + 3 = 9345 + 3 = 93
  • 18.
    March 3, AppliedDiscrete Mathematics 18 Recursively Defined FunctionsRecursively Defined Functions How can we recursively define the factorialHow can we recursively define the factorial function f(n) = n! ?function f(n) = n! ? f(0) = 1f(0) = 1 f(n + 1) = (n + 1)f(n)f(n + 1) = (n + 1)f(n) f(0) = 1f(0) = 1 f(1) = 1f(0) = 1f(1) = 1f(0) = 1⋅⋅1 = 11 = 1 f(2) = 2f(1) = 2f(2) = 2f(1) = 2⋅⋅1 = 21 = 2 f(3) = 3f(2) = 3f(3) = 3f(2) = 3⋅⋅2 = 62 = 6 f(4) = 4f(3) = 4f(4) = 4f(3) = 4⋅⋅6 = 246 = 24
  • 19.
    March 3, AppliedDiscrete Mathematics 19 Recursively Defined FunctionsRecursively Defined Functions A famous example: The Fibonacci numbersA famous example: The Fibonacci numbers f(0) = 0, f(1) = 1f(0) = 0, f(1) = 1 f(n) = f(n – 1) + f(n - 2)f(n) = f(n – 1) + f(n - 2) f(0) = 0f(0) = 0 f(1) = 1f(1) = 1 f(2) = f(1) + f(0) = 1 + 0 = 1f(2) = f(1) + f(0) = 1 + 0 = 1 f(3) = f(2) + f(1) = 1 + 1 = 2f(3) = f(2) + f(1) = 1 + 1 = 2 f(4) = f(3) + f(2) = 2 + 1 = 3f(4) = f(3) + f(2) = 2 + 1 = 3 f(5) = f(4) + f(3) = 3 + 2 = 5f(5) = f(4) + f(3) = 3 + 2 = 5 f(6) = f(5) + f(4) = 5 + 3 = 8f(6) = f(5) + f(4) = 5 + 3 = 8
  • 20.
    March 3, AppliedDiscrete Mathematics 20 Recursively Defined SetsRecursively Defined Sets If we want to recursively define a set, we need toIf we want to recursively define a set, we need to provide two things:provide two things: • anan initial setinitial set of elements,of elements, • rulesrules for the construction offor the construction of additionaladditional elements from elements in the set.elements from elements in the set. Example:Example: Let S be recursively defined by:Let S be recursively defined by: 33 ∈∈ SS (x + y)(x + y) ∈∈ S if (xS if (x ∈∈ S) and (yS) and (y ∈∈ S)S) S is the set of positive integers divisible by 3.S is the set of positive integers divisible by 3.
  • 21.
    March 3, AppliedDiscrete Mathematics 21 Recursively Defined SetsRecursively Defined Sets Proof:Proof: Let A be the set of all positive integers divisible by 3.Let A be the set of all positive integers divisible by 3. To show that A = S, we must show thatTo show that A = S, we must show that AA ⊆⊆ S and SS and S ⊆⊆ A.A. Part I:Part I: To prove that ATo prove that A ⊆⊆ S, we must show thatS, we must show that every positive integer divisible by 3 is in S.every positive integer divisible by 3 is in S. We will use mathematical induction to show this.We will use mathematical induction to show this.
  • 22.
    March 3, AppliedDiscrete Mathematics 22 Recursively Defined SetsRecursively Defined Sets Let P(n) be the statement “3n belongs to S”.Let P(n) be the statement “3n belongs to S”. Basis step:Basis step: P(1) is true, because 3 is in S.P(1) is true, because 3 is in S. Inductive step:Inductive step: To show:To show: If P(n) is true, then P(n + 1) is true.If P(n) is true, then P(n + 1) is true. Assume 3n is in S. Since 3n is in S and 3 is in S, itAssume 3n is in S. Since 3n is in S and 3 is in S, it follows from the recursive definition of S thatfollows from the recursive definition of S that 3n + 3 = 3(n + 1) is also in S.3n + 3 = 3(n + 1) is also in S. Conclusion of Part I:Conclusion of Part I: AA ⊆⊆ S.S.
  • 23.
    March 3, AppliedDiscrete Mathematics 23 Recursively Defined SetsRecursively Defined Sets Part II:Part II: To show: STo show: S ⊆⊆ A.A. Basis step:Basis step: To show:To show: All initial elements of S are in A. 3 is in A. True.All initial elements of S are in A. 3 is in A. True. Inductive step:Inductive step: To show:To show: (x + y) is in A whenever x and y are in S.(x + y) is in A whenever x and y are in S. If x and y are both in A, it follows that 3 | x and 3 | y.If x and y are both in A, it follows that 3 | x and 3 | y. As we already know, it follows that 3 | (x + y).As we already know, it follows that 3 | (x + y). Conclusion of Part II:Conclusion of Part II: SS ⊆⊆ A.A. Overall conclusion:Overall conclusion: A = S.A = S.
  • 24.
    March 3, AppliedDiscrete Mathematics 24 Recursively Defined SetsRecursively Defined Sets Another example:Another example: The well-formed formulas of variables, numeralsThe well-formed formulas of variables, numerals and operators from {+, -, *, /, ^} are defined by:and operators from {+, -, *, /, ^} are defined by: x is a well-formed formula if x is a numeral orx is a well-formed formula if x is a numeral or variable.variable. (f + g), (f – g), (f * g), (f / g), (f ^ g) are well-formed(f + g), (f – g), (f * g), (f / g), (f ^ g) are well-formed formulas if f and g are.formulas if f and g are.
  • 25.
    March 3, AppliedDiscrete Mathematics 25 Recursively Defined SetsRecursively Defined Sets With this definition, we can construct formulas suchWith this definition, we can construct formulas such as:as: (x – y)(x – y) ((z / 3) – y)((z / 3) – y) ((z / 3) – (6 + 5))((z / 3) – (6 + 5)) ((z / (2 * 4)) – (6 + 5))((z / (2 * 4)) – (6 + 5))
  • 26.
    March 3, AppliedDiscrete Mathematics 26 Recursive AlgorithmsRecursive Algorithms An algorithm is calledAn algorithm is called recursiverecursive if it solves a problemif it solves a problem by reducing it to an instance of the same problem withby reducing it to an instance of the same problem with smaller input.smaller input. Example I:Example I: Recursive Euclidean AlgorithmRecursive Euclidean Algorithm procedureprocedure gcd(a, b: nonnegative integers with a < b)gcd(a, b: nonnegative integers with a < b) ifif a = 0a = 0 thenthen gcd(a, b) := bgcd(a, b) := b elseelse gcd(a, b) := gcd(bgcd(a, b) := gcd(b modmod a, a)a, a)
  • 27.
    March 3, AppliedDiscrete Mathematics 27 Recursive AlgorithmsRecursive Algorithms Example II:Example II: Recursive Fibonacci AlgorithmRecursive Fibonacci Algorithm procedureprocedure fibo(n: nonnegative integer)fibo(n: nonnegative integer) ifif n = 0n = 0 thenthen fibo(0) := 0fibo(0) := 0 else ifelse if n = 1n = 1 thenthen fibo(1) := 1fibo(1) := 1 elseelse fibo(n) := fibo(n – 1) + fibo(n – 2)fibo(n) := fibo(n – 1) + fibo(n – 2)
  • 28.
    March 3, AppliedDiscrete Mathematics 28 Recursive AlgorithmsRecursive Algorithms Recursive Fibonacci Evaluation:Recursive Fibonacci Evaluation: f(4)f(4) f(3)f(3) f(2)f(2) f(1)f(1) f(0)f(0) f(1)f(1) f(2)f(2) f(1)f(1) f(0)f(0)
  • 29.
    March 3, AppliedDiscrete Mathematics 29 Recursive AlgorithmsRecursive Algorithms procedureprocedure iterative_fibo(n: nonnegative integer)iterative_fibo(n: nonnegative integer) ifif n = 0n = 0 thenthen y := 0y := 0 elseelse beginbegin x := 0x := 0 y := 1y := 1 forfor i := 1i := 1 toto n-1n-1 beginbegin z := x + yz := x + y x : = yx : = y y := zy := z endend endend {y is the n-th Fibonacci number}{y is the n-th Fibonacci number}
  • 30.
    March 3, AppliedDiscrete Mathematics 30 Recursive AlgorithmsRecursive Algorithms For every recursive algorithm, there is anFor every recursive algorithm, there is an equivalentequivalent iterative algorithm.iterative algorithm. Recursive algorithms are oftenRecursive algorithms are often shortershorter,, moremore elegantelegant, and, and easier to understandeasier to understand than theirthan their iterative counterparts.iterative counterparts. However, iterative algorithms are usuallyHowever, iterative algorithms are usually moremore efficientefficient in their use of space and time.in their use of space and time.