Unit 3 Part 2
Unit 3 Part 2
Chapter 5
With Question/Answer
Animations
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education.
Chapter Summary
Mathematical Induction
Strong Induction
Well-Ordering
Recursive Definitions
Structural Induction
Recursive Algorithms
Program Correctness (not yet included in
overheads)
Mathematical Induction
Section 5.1
Section Summary
Mathematical Induction
Examples of Proof by Mathematical Induction
Mistaken Proofs by Mathematical Induction
Guidelines for Proofs by Mathematical
Induction
Climbing an
Infinite Ladder
Suppose we have an infinite ladder:
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.
has a least element (see Section 5.2 and Appendix 1). Here is the proof:
which states that every nonempty subset of the set of positive integers
Suppose that P(1) holds and P(k) → P(k + 1) is true for all positive
integers k.
Assume there is at least one positive integer n for which P(n) is false.
Then the set S of positive integers for which P(n) is false is nonempty.
+ 9 = 25.
1 + the
We can conjecture that 3 +sum
5 +of
∙∙∙+
the (2n −positive
first n 1) + (2n + integers
odd 1) =n2 .is n2,
= k2 + 2k + 1
= (k + 1) 2
Hence, we have shown that P(k + 1) follows from P(k). Therefore the sum of the first n
positive odd integers is n2.
Proving Inequalities
n < 2n for all positive integers
Example: Use mathematical induction to
prove that
n.
< 2n.
Solution: Let P(n) be the proposition that n
2k+1 = 2∙2k
holds:
< 2∙ k!
< (k + 1)k!
(by the inductive hypothesis)
= (k + 1)!
Note that here2the
Therefore, n
<basis step is for
n! holds, P(4),every
since P(0), ≥ 4. and
P(1),nP(2),
integer
P(3) are all false.
Proving Divisibility Results
Example: Use mathematical induction to prove that n3 − n is
divisible by 3, for every positive integer n.
Solution: Let P(n) be the proposition that n3 − n is divisible by
3.
BASIS STEP: P(1) is true since 13 − 1 = 0, which is divisible by 3.
INDUCTIVE STEP: Assume P(k) holds, i.e., k3 − k is divisible by 3,
for an arbitrary positive integer k. To show that P(k + 1) follows:
(k + 1)3 − (k + 1) = (k3 + 3k2 + 3k + 1) − (k + 1)
= (k3 − k) + 3(k2 + k)
By the inductive hypothesis, the first term (k3 − k) is divisible by 3
and the second term is divisible by 3 since it is an integer
multiplied by 3. So by part (i) of Theorem 1 in Section 4.1 , (k + 1)3
− (k + 1) is divisible by 3.
Therefore, n3 − n is divisible by 3, for every integer positive integer
n.
Number of Subsets of a Finite Set
Example: Use mathematical induction to
show that if S is a finite set with n elements,
2n subsets.
where n is a nonnegative integer, then S has
X ∪ {a}.
For each subset X of S, there are exactly two subsets of T, i.e., X
and
2 ∙2k = 2k+1 .
two subsets of T for each subset of S, the number of subsets of T
is
Tiling Checkerboards
Example: Show that every 2n ×2n checkerboard with one square removed
can be tiled using right triominoes.
INDUCTIVE STEP: Assume that P(k) is true for every 2k ×2k checkerboard, for
some positive integer k.
→
continued
Tiling Checkerboards
Inductive Hypothesis: Every 2k ×2k checkerboard,
for some positive integer k, with one square removed
can be tiled using right triominoes.
Consider a 2k+1 ×2k+1 checkerboard with one square removed. Split this checkerboard into
four checkerboards of size 2k ×2k,by dividing it in half in both directions.
Remove a square from one of the four 2k ×2k checkerboards. By the inductive hypothesis, this
board can be tiled. Also by the inductive hypothesis, the other three boards can be tiled with
the square from the corner of the center of the original board removed. We can then cover
Hence, the entire 2k+1 ×2k+1 checkerboard with one square removed can be tiled using right
the three adjacent squares with a triominoe.
triominoes.
An Incorrect “Proof” by Mathematical
Induction
Example: Let P(n) be the statement that every set of n
lines in the plane, no two of which are parallel, meet in a
positive integers n ≥ 2.
common point. Here is a “proof” that P(n) is true for all
BASIS STEP: The statement P(2) is true because any two lines
in the plane that are not parallel meet in a common point.
that P(k) is true for the positive integer k ≥ 2, i.e., every set
INDUCTIVE STEP: The inductive hypothesis is the statement
that the lines are distinct. Hence, p1 = p2 lies on all k + 1 distinct lines, and
same line since two points determine a line. This contradicts the assumption
∧ P(k)
holds for an arbitrary integer k, and show
that
P(k + 1) must also hold.
We will have then shown by strong induction
that for every positive integer n, P(n) holds,
i.e., we can
reach the nth rung of the ladder.
Completion of the proof of the
Fundamental Theorem of Arithmetic
Example: Show that if n is an integer greater than 1, then n can
be written as the product of primes.
Solution: Let P(n) be the proposition that n can be written as a
product of primes.
BASIS STEP: P(2) is true since 2 itself is prime.
Otherwise, no 4-cent stamp have been used and at least three 5-cent
stamps were used. Three 5-cent stamps can be replaced by four 4-cent
f(n + 1) = 2f(n) + 3
Find f(1), f(2), f(3), f(4)
Solution:
2f(0) + 3 = 2∙3 + 3 = 9
2f(1)+ 3 = 2∙9 + 3 = 21
f(1) =
2f(2) + 3 = 2∙21 + 3 = 45
f(2) =
2f(3) + 3 = 2∙45 + 3 = 93
f(3) =
f(4) =
1250)
(1170-
Fibonacci Numbers
Example : The Fibonacci numbers are
defined as follows:
f0 = 0
f1 = 1
fn = fn−1 + fn−2
Find f2, f3 , f4 , f5 .
f2 = f 1 + f 0 = 1 + 0 = 1
f3 = f 2 + f 1 = 1 + 1 = 2
f4 = f 3 + f 2 = 2 + 1 = 3
f5 = f 4 + f 3 = 3 + 2 = 5
Fibonacci Numbers
Example 4: Show that whenever n ≥ 3, fn > αn − 2, where α = (1 + √5)/2.
Solution: Let P(n) be the statement fn > αn−2 . Use strong induction to
show that P(n) is true whenever n ≥ 3.
BASIS STEP: P(3) holds since α < 2 = f3
+ αk−3
fk−1 > αk−3, fk > αk−2.
By the inductive hypothesis, because k ≥ 4 we have
etc.
Example: The natural numbers N.
BASIS STEP: 0 ∊ N.
RECURSIVE STEP: If n is in N, then n + 1 is in N.
Initially 0 is in S, then 0 + 1 = 1, then 1 + 1 = 2,
etc.
Strings
Definition: The set Σ* of strings over the
alphabet Σ:
BASIS STEP: λ ∊ Σ* (λ is the empty string)
RECURSIVE STEP: If w is in Σ* and x is in Σ,
then wx Σ*.
l(λ) = 0;
recursively defined by:
(1795-
Lamé
1870)
Lames Theorem
Lamé’s Theorem: Let a and b be positive integers with a ≥ b.
Then the number of divisions used by the Euclidian algorithm to
find gcd(a,b) is less than or equal to five times the number of
decimal digits in b.
n b,
a• ≥
Proof: When we use the Euclidian algorithm to find gcd(a,b) with
divisions are used to
obtain (with a = r0,b =r1 ): • Since each quotient q1, q2 ,
0≤r …,qn-1 is at least 1 and qn ≥ 2:
r n ≥ 1 = f2 ,
0 1
r =rq +r 1 2 2
rn-1 ≥ 2 rn ≥ 2 f2 = f3,
< r1,
0 ≤ r3
rn-2 ≥ rn-1 + rn ≥ f3 + f2 = f4,
r1 = r2q2 + r3
< r2,
⋮ ⋮
rn-2 = rn-1qn-1 + rn 0 ≤ rn < r2 ≥ r3 + r4 ≥ fn-1 + fn-2 = fn,
rn-1, b = r1 ≥ r2 + r3 ≥ fn + fn-1
rn-1 = rnqn. continued
= fn+1.
Lamé’s Theorem
gcd(a,b) with a ≥ b, then b ≥ fn+1. By Example 4, fn+1 > αn − 1, for n >
It follows that if n divisions are used by the Euclidian algorithm to find
Suppose that b has k decimal digits. Then b < 10k and log10 b < k. It
.
follows that n − 1 < 5k and since k is an integer, n ≤ 5k.
5 (logresult
10 b + 1)
By Lamé’s Theorem, the number of divisions needed to find gcd(a,b) with
S ⊂ A:
true.
both x and y are in S. If x and y are both in A, then both x and y are divisible by 3.
INDUCTIVE STEP: The second part of the recursive definition adds x +y to S, if
a root r is h(T) = 0.
BASIS STEP: The height of a full binary tree T consisting of only
RECURSIVE STEP: If T1 and T2 are full binary trees, then the full
binary tree T = T1∙T2 has height
1 + max(h(T1),h(T2)).
h(T) =
n(T) = 1 ≤ 20+1 – 1 = 1.
RECURSIVE STEP: Assume n(T1) ≤ 2h(T1)+1 – 1 and also
n(T) = 1 + n(T1) + n(T2)
n(T)) n(T2) ≤ 2 – 1 whenever T1 and T2 are full
h(T2)+1
(by recursive formula of
= 2h(t)+1 – 1
h(T))
2
Generalized Induction
Generalized induction is used to prove results about
sets other than the integers that have the well-
9)
ordering property. (explored in more detail in Chapter
Show that am,n = m + n(n + 1)/2 is defined for all (m,n)∊N ×N.
Solution: Use generalized induction.
BASIS STEP: a0,0 = 0 = 0 + (0∙1)/2
INDUCTIVE STEP: Assume that am̍,n̍ = m̍+ n̍(n̍ + 1)/2 whenever(m̍,n̍)
is less than (m,n) in the lexicographic ordering of N ×N .
If n = 0, by the inductive hypothesis we can conclude
am,n = am−1,n + 1 = m − 1+ n(n + 1)/2 + 1 = m + n(n + 1)/2 .
2
Recursive Algorithms
Section 5.4
Section Summary
Recursive Algorithms
Proving Recursive Algorithms Correct
Recursion and Iteration (not yet included in
overheads)
Merge Sort
Recursive Algorithms
Definition: An algorithm is called recursive
if it solves a problem by reducing it to an
instance of the same problem with smaller
input.
For the algorithm to terminate, the instance
of the problem must eventually be reduced to
some initial case for which the solution is
known.
Recursive Factorial Algorithm
Example: Give a recursive algorithm for
computing n!, where n is a nonnegative
integer.
Solution: Use the recursive definition of the
factorial function.
if n = 0 then return 1
procedure factorial(n: nonnegative integer)
if n = 0 then return 1
integer)
if a = 0 then return b
with a < b)
{output is bn mod m}
Recursive Binary Search Algorithm
Example: Construct a recursive version of a
binary search algorithm.
Solution:
sequence of integers. Initially i is 1 and j is n. We are
Assume we have a1,a2,…, an, an increasing
searching
procedure for x.search(i, j, x : integers, 1≤ i ≤ j ≤n)
m := ⌊(i + j)/2⌋
binary
if x = am then
return m
else if (x < am and i < m) then
return binary search(i,m−1,x)
else if (x > am and j >m) then
return binary search(m+1,j,x)
else return 0
{output is location of x in a1, a2,…,an if it appears, otherwise 0}
Proving Recursive Algorithms Correct
Both mathematical and str0ng induction are useful techniques to show that
recursive algorithms always produce the correct output.
Example: Prove that the algorithm for computing the powers of real numbers is
correct. procedure power(a: nonzero real number, n: nonnegative
if n = 0 then return 1
integer)
≠0. Assuming the inductive hypothesis, the algorithm correctly computes a , since
INDUCTIVE STEP: The inductive hypothesis is that power(a,k) = ak, for all a
.
Merge Sort
Merge Sort works by iteratively splitting a
list (with an even number of elements) into
two sublists of equal length until each sublist
has one element.
Each sublist is represented by a balanced
binary tree.
At each step a pair of sublists is successively
merged into a list with the elements in
increasing order. The process ends when all
the sublists have been merged.
The succession of merged lists is represented
Merge Sort
8,2,4,6,9,7,10, 1, 5, 3
Example: Use merge sort to put the list
if n > 1 thenL.
elements
n
m := ⌊n/2⌋
L1 := a1, a2,…,am
L2 := am+1, am+2,…,an
L := merge(mergesort(L1), mergesort(L2 ))
{L is now sorted into elements in increasing order}
continued
Recursive Merge Sort
Subroutine merge, which merges two sorted lists.
procedure merge(L1, L2 :sorted lists)
L := empty list
while L1 and L2 are both nonempty
remove smaller of first elements of L1 and L2 from its list;
put at the right end of L
if this removal makes one list empty
then remove all elements from the other list and append them to L
return L {L is the merged list with the elements in increasing order}
comparisons.
Merging Two Lists
Example: Merge the two lists 2,3,5,6 and
1,4.
Solution:
Complexity of Merge Sort
Complexity of Merge Sort: The number of comparisons
comparisons.
continued
Complexity of Merge Sort
Summing over the number of comparisons at each level,
shows that