Small 12
Small 12
Part One
Everybody – do the wave!
The Wave
● If done properly, everyone will eventually
end up joining in.
● Why is that?
● Someone (me!) started everyone off.
● Once the person before you did the wave,
you did the wave.
Let P be some property. The principle of mathematical
induction states that if
∀k ∈ ℕ. (P(k) → P(k+1))
then
∀n ∈ ℕ. P(n)
…then it's
always true.
Induction, Intuitively
● It's true for 0.
● Since it's true for 0, it's true for 1.
● Since it's true for 1, it's true for 2.
● Since it's true for 2, it's true for 3.
● Since it's true for 3, it's true for 4.
● Since it's true for 4, it's true for 5.
● Since it's true for 5, it's true for 6.
● …
Proof by Induction
● A proof by induction is a way to use
mathematical induction to show that some result
is true for all natural numbers n.
● In a proof by induction, there are three steps:
● Prove that P(0) is true.
– This is called the basis or the base case.
● Prove that if P(k) is true, then P(k+1) is true.
– This is called the inductive step.
– The assumption that P(k) is true is called the inductive
hypothesis.
● Conclude, by induction, that P(n) is true for all n ∈ ℕ.
Some Summations
20 = 1 = 21 – 1
2 0 + 2 1 = 1 + 2 = 3 = 22 – 1
20 + 2 1 + 2 2 = 1 + 2 + 4 = 7 = 2 3 – 1
20 + 21 + 22 + 23 = 1 + 2 + 4 + 8 = 15 = 24 – 1
20 + 21 + 22 + 23 + 24 = 1 + 2 + 4 + 8 + 16 = 31 = 25 – 1
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
At
At the
For the inductive start
thestep,
start of
of the
assume proof,
thethat we
we tell
for some
proof, k ∈the
tell ℕ that
the
P(k) holds, meaning
reader that
reader what property
what property we're
we're going
going to
to show
show isis
true 2 0
+ 2 1
+ … + 2 k-1
= 2 k
– 1. (1)
true for all natural numbers n, then tell
for all natural numbers n, then tell
We need them
to show thatgoing
we're P(k +to1) holds,
prove meaning
it by that the sum
induction.
them we're going to prove it by induction.
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
20 + 21 + … + 2k-1 + 2k = (20 + 21 + … + 2k-1) + 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For theIn aa proof
Ininductive by
by induction,
proof step, we
assume that
induction, we need
for
need to
to prove
some k ∈ ℕthat
prove that
that
P(k) holds, meaning that
2 0
◻◻ P(0) + 2
is
1
+
true… + 2 k-1
= 2 k
– 1. (1)
P(0) is true
We need to◻show If that P(k
P(k) is + 1) holds,
true, then meaning
P(k+1) is that the sum
true.
◻ If P(k) is true, then P(k+1) is true.
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
20 + 21 + … + 2k-1 + 2k = (20 + 21 + … + 2k-1) + 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For the Here,
Here, we
we state
inductive step, what
what P(0)
state assume thatactually
P(0) for some
actually says.
k ∈Now,
says. ℕ that
Now,
P(k) holds,
can meaning that
can go 0prove1 this
go prove this using
using any
any proof
proof techniques
techniques
2 + 2 + …we'd + 2 like!
k-1
= 2 – 1.
k
(1)
we'd like!
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
20 + 21 + … + 2k-1 + 2k = (20 + 21 + … + 2k-1) + 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For the inductive step, assume that for some k ∈ ℕ that
P(k) holds, meaning that
20 + 21 + … + 2k-1 = 2k – 1. (1)
In
In aa proof
proof byby induction,
induction, wewe need
need toto prove
prove that that
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that ✓✓ P(0)
P(0) isis true
true
20 + 2 1 +◻◻…If+ P(k)
If 2P(k)
k-1
+isis
2ktrue,
= (20then
true, + 21 P(k+1)
then + … + is
P(k+1) 2isk-1true.
)true.
+ 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For the inductive step, assume that for some k ∈ ℕ that
P(k) holds, meaning that
20 + 21 + … + 2k-1 = 2k – 1. (1)
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that The The goal
goal of
of this
this step
step isis to
to prove
prove
20 + 21 +“If
… P(k)
+ 2k-1is+true,
2k =then
(20 +P(k+1)
21 + …is+true.”
2k-1) + 2k
“If P(k) is true, then P(k+1) is true.”
= 2k – 1 + 2 k (via (1))
To
To dodo this,
this, we'll
we'll choose
= 2(2kan
choose )an– arbitrary
1arbitrary k,
k, assume
assume
that is true, = 2
that P(k) is true, then try
P(k) then
k+1
– 1. to prove
try to prove P(k+1).
P(k+1).
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For the inductive step, assume that for some k ∈ ℕ that
P(k) holds, meaning that
20 + 21 + … + 2k-1 = 2k – 1. (1)
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
Here,
Here, we
we explicitly
explicitly stating
stating P(k+1),
P(k+1), which
which k
2 + 2 + … + 2 + 2 = (2 + 2 + … + 2 ) + 2
0 1 k-1 k 0 1 k-1
isis what
what we
we want
want to
to prove.
prove. Now,
Now, we can
=2 –1+2
k k
(viacan
we (1))
use
use any
any proof
proof technique
= 2(2k) –we
technique 1 want
we want to
to try
try
to
to prove –it.
prove
= 2 k+1
1.
it.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true forHere,
all n ∈we
Here, ℕ,use
we from
use our inductive
which
our hypothesis
the theorem
inductive follows.
hypothesis
(the
For our base assumption
(the case, we need
assumption thatto P(k)
that P(k) isis
show true)
P(0) to
is true,
true) to meaning
that thesimplify
sum of athe first zero
complex powers ofThis
expression. two is
is 2
a
0
– 1. Since
simplify a complex expression. This is a
the sum of the first zero powers of two is zero and 20 – 1
common theme in inductive proofs.
is zero as well, we see that P(0) is true.proofs.
common theme in inductive
For the inductive step, assume that for some k ∈ ℕ that
P(k) holds, meaning that
20 + 21 + … + 2k-1 = 2k – 1. (1)
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
20 + 21 + … + 2k-1 + 2k = (20 + 21 + … + 2k-1) + 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
Theorem: The sum of the first n powers of two is 2n – 1.
Proof: Let P(n) be the statement “the sum of the first n powers
of two is 2n – 1.” We will prove, by induction, that P(n) is
true for all n ∈ ℕ, from which the theorem follows.
For our base case, we need to show P(0) is true, meaning
that the sum of the first zero powers of two is 20 – 1. Since
the sum of the first zero powers of two is zero and 20 – 1
is zero as well, we see that P(0) is true.
For the inductive step, assume that for some k ∈ ℕ that
P(k) holds, meaning that
20 + 21 + … + 2k-1 = 2k – 1. (1)
We need to show that P(k + 1) holds, meaning that the sum
of the first k + 1 powers of two is 2k+1 – 1. To see this,
notice that
20 + 21 + … + 2k-1 + 2k = (20 + 21 + … + 2k-1) + 2k
= 2k – 1 + 2 k (via (1))
= 2(2k) – 1
= 2k+1 – 1.
Therefore, P(k + 1) is true, completing the induction. ■
A Quick Aside
● This result helps explain the range of
numbers that can be stored in an int.
● If you have an unsigned 32-bit integer,
the largest value you can store is given
by 1 + 2 + 4 + 8 + … + 231 = 232 – 1.
● This formula for sums of powers of two
has many other uses as well. If we have
time, we'll see one later today.
Structuring a Proof by Induction
● Define some property P that you'll show, by induction, is
true for all natural numbers.
● Prove the base case:
● State that you're going to prove the property holds for 0, then
go prove it.
● Prove the inductive step:
● Say that you're assuming P is true for some natural number k,
then write out exactly what that means.
● Say that you're going to prove P is true for k+1, then write out
exactly what that means.
● Prove that P is true for k+1 using any proof technique you'd
like.
● This is a rather verbose way of writing inductive proofs.
As we get more experience with induction, we'll start
leaving out some details from our proofs.
Induction, Intuitively
● You can imagine an “machine” that turns
proofs that the property holds for k into
proofs that the property holds for k + 1.
● Starting with a proof that the property holds
for 0, we can run the machine as many times
as we'd like to get proofs for 1, 2, 3, … .
● The principle of mathematical induction says
that this style of reasoning is a rigorous
argument.
Why Induction Works
P(k) → P(k + 1)
P(0)
The Counterfeit Coin Problem
Problem Statement
● You are given a set of three seemingly
identical coins, two of which are real and
one of which is counterfeit.
● The counterfeit coin weighs more than
the rest of the coins.
● You are given a balance. Using only one
weighing on the balance, find the
counterfeit coin.
Finding the Counterfeit Coin
3
Finding the Counterfeit Coin
1 2
3
Finding the Counterfeit Coin
3
Finding the Counterfeit Coin
3
Finding the Counterfeit Coin
1 2
3
A Harder Problem
● You are given a set of nine seemingly
identical coins, eight of which are real
and one of which is counterfeit.
● The counterfeit coin weighs more than
the rest of the coins.
● You are given a balance. Using only two
weighings on the balance, find the
counterfeit coin.
Finding the Counterfeit Coin
1 2 3 4 5 6
7 8 9
Finding the Counterfeit Coin
6
5
4
3
2
1
7 8 9
Now
Now wewe have
have one
one weighing
weighing
to
to find
find the
the counterfeit
counterfeit out
out
of
of these
these three
three coins.
coins.
Finding the Counterfeit Coin
1
2
3
4
5
7 8 9 6
Now
Now wewe have
have one
one weighing
weighing
to
to find
find the
the counterfeit
counterfeit out
out
of
of these
these three
three coins.
coins.
Finding the Counterfeit Coin
1 2 3 4 5 6
7 8 9
Now
Now wewe have
have one
one weighing
weighing
to
to find
find the
the counterfeit
counterfeit out
out
of
of these
these three
three coins.
coins.
If we have n weighings on the scale, what
is the largest number of coins out of which
we can find the counterfeit?
A Pattern
● Assume out of the coins that are given, exactly
one is counterfeit and weighs more than the
other coins.
● If we have no weighings, how many coins can
we have while still being able to find the
counterfeit?
● One coin, since that coin has to be the counterfeit!
● If we have one weighing, we can find the
counterfeit out of three coins.
● If we have two weighings, we can find the
counterfeit out of nine coins.
So far, we have
1, 3, 9 = 30, 31, 32
I think it's a mixed bag. On the one hand, I think that anyone who
I think it's a mixed bag. On the one hand, I think that anyone who
wants to study CS should have a fair shot at doing so, and inasmuch
wants to study CS should have a fair shot at doing so, and inasmuch
as this makes this easier, I'm all for it. It also means that we have a
as this makes this easier, I'm all for it. It also means that we have a
more interesting mix of CS students and a wider set of interests.
more interesting mix of CS students and a wider set of interests.
That makes me feel really optimistic about the future.
That makes me feel really optimistic about the future.
MII
(a)
MIIII
(a)
(a)Double
Doublethe
thestring
stringafter
afteran
anM.M. (c)
(b)
(b)Replace IIIwith
ReplaceIII withU.U. MIIIIU
(c)
(c)Append
AppendU,U,ififthe
thestring
stringends
endsin
inI.I. (b)
(d)
(d)Delete UUfrom
DeleteUU fromthe thestring.
string. MUIU
(a)
MUIUUIU
(d)
MUIIU
Try It!
(a)
(a)Double
Doublethe
thestring
stringafter
afteran
anM.M.
(b)
(b)Replace IIIwith
ReplaceIII withU.U.
(c)
(c)Append
AppendU,U,ififthe
thestring
stringends
endsin
inI.I.
(d)
(d)Delete UUfrom
DeleteUU fromthe thestring.
string.
Not a single person in this room
was able to solve this puzzle.
MII 2
MIIII 4
MIIIIU 4
MIIIIUIIIIU 8
MIIIIUUIU 5
MIIIIUUIUIIIIUUIU 10
MUIUUIUIIIIUUIU 7
The Key Insight
● Initially, the number of I's is not a
multiple of three.
● To make MU, the number of I's must end
up as a multiple of three.
● Can we ever make the number of I's a
multiple of three?
Lemma 1: If n is an integer that is not a multiple of three,
then n – 3 is not a multiple of three.
Proof: By contrapositive; we'll prove that if n – 3 is a multiple
of three, then n is also a multiple of three. Because n – 3 is
a multiple of three, we can write n – 3 = 3k for some
integer k. Then n = 3(k+1), so n is also a multiple of three,
as required. ■
Lemma 2: If n is an integer that is not a multiple of three,
then 2n is not a multiple of three.
Proof: Let n be a number that isn't a multiple of three. If n is
congruent to one modulo three, then n = 3k + 1 for some
integer k. This means 2n = 2(3k+1) = 6k + 2 = 3(3k) + 2,
so 2n is not a multiple of three. Otherwise, n must be
congruent to two modulo three, so n = 3k + 2 for some
integer k. Then 2n = 2(3k+2) = 6k+4 = 3(2k+1) + 1, and
so 2n is not a multiple of three. ■
Lemma: No matter which moves are made, the number of I's in the string
never becomes multiple of three.
Proof: Let P(n) be the statement “After any n moves, the number of I's in
the string will not be multiple of three.” We will prove, by induction, that
P(n) is true for all n ∈ ℕ, from which the theorem follows.
As a base case, we'll prove P(0), that the number of I's after 0 moves is
not a multiple of three. After no moves, the string is MI, which has one I in
it. Since one isn't a multiple of three, P(0) is true.
For our inductive step, suppose that P(k) is true for some k ∈ ℕ. We'll
prove P(k+1) is also true. Consider any sequence of k+1 moves. Let r be
the number of I's in the string after the kth move. By our inductive
hypothesis (that is, P(k)), we know that r is not a multiple of three. Now,
consider the four possible choices for the k+1st move:
Case 1: Double the string after the M. After this, we will have 2r I's
in the string, and from our lemma 2r isn't a multiple of three.
Case 2: Replace III with U. After this, we will have r – 3 I's in the string,
and by our lemma r – 3 is not a multiple of three.
Case 3: Either append U or delete UU. This preserves the number of
I's in the string, so we don't have a multiple of three I's at this point.
Therefore, no sequence of k+1 moves ends with a multiple of three I's.
Thus P(k+1) is true, completing the induction. ■
Theorem: The MU puzzle has no solution.
1111010
Transmit
1111010
Decompress
101010101010101010101010101010
Lossless Data Compression
● In order to losslessly compress data, we need two
functions:
● A compression function C, and
● A decompression function D.
● We need to have D(C(x)) = x.
● Otherwise, we can't uniquely encode or decode
some bitstring.
● This means that D must be a left inverse of C, so
(as you proved in PS3!) C must be injective.
A Perfect Compression Function
● Ideally, the compressed version of a bitstring
would always be shorter than the original
bitstring.
● Question: Can we find a lossless compression
algorithm that always compresses a string
into a shorter string?
● To handle the issue of the empty string (which
can't get any shorter), let's assume we only
care about strings of length at least 10.
A Counting Argument
● Let �n be the set of bitstrings of length n, and �<n be
the set of bitstrings of length less than n.
● How many bitstrings of length n are there?
● Answer: 2n
● How many bitstrings of length less than n are there?
● Answer: 20 + 21 + … + 2n – 1 = 2n – 1
● By the pigeonhole principle, no function from �n to �<n
can be injective – at least two elements must collide!
● Since a perfect compression function would have to be
an injection from �n to �<n, there is no perfect
compression function!
Why this Result is Interesting
● Our result says that no matter how hard we try,
it is impossible to compress every string into a
shorter string.
● No matter how clever you are, you cannot write
a lossless compression algorithm that always
makes strings shorter.
● In practice, only highly redundant data can be
compressed.
● The fields of information theory and
Kolmogorov complexity explore the limits of
compression; if you're interested, go explore!
Next Time
● Variations on Induction
● Starting induction later.
● Taking larger steps.
● Complete induction.