Advanced Counting Techniques
Advanced Counting Techniques
Suppose that A1 and A2 are sets. Then |A1 ∪ A2 | = |A1 | + |A2 | − |A1 ∩ A2 |
Example How many bit strings of length eight either start with a 1 bit or
end with the two bits 00?
Solution There are 27 bit strings that start with 1 and 26 bit strings that
end with 00. 25 bit strings that start with 1 and end with 00.
Hence, bit strings that start with a 1 bit or end with 00 are
|A1 ∪A2 | = |A1 |+|A2 |−|A1 ∩A2 | = 27 +26 −25 = 25 (22 +2−1) = 32(5) = 160
Example A company receives 350 applications. Suppose 220 of these ap-
plicants majored in CS, 147 majored in business, and 51 majored both in
CS and business. How many of the applicants majored neither in CS nor in
business?
Solution Let N stand for the total number of applicants. Let A1 be the set
of CS majors and A2 denote the set of business majors.
|Ac1 ∩ Ac2 | = |(A1 ∪ A2 )c | = N − |A1 ∪ A2 | = N − (|A1 | + |A2 | − |A1 ∩ A2 |)
= 350 − (220 + 147 − 51) = 350 − 316 = 34
Example Let S represent the set of 100 students enrolled in 3 courses c1 , c2 ,
and c3 .
If N (c1 ) = 35, N (c2 ) = 30, N (c3 ) = 30,
N (c1 ∩ c2 ) = 9, N (c1 ∩ c3 ) = 11, N (c2 ∩ c3 ) = 10, and N (c1 ∩ c2 ∩ c3 ) = 5.
What is the number of students enrolled in neither of the three courses?
1
Solution N (c¯1 ∩ c¯2 ∩ c¯3 ) = N ((c1 ∪ c2 ∪ c3 )c ) = N − [N (c1 ∪ c2 ∪ c3 )]
However, N (c1 ∪ c2 ∪ c3 ) = N (c1 ) + N (c2 ) + N (c3 ) − {N (c1 ∩ c2 ) + N (c1 ∩
c3 ) + N (c2 ∩ c3 )} + N (c1 ∩ c2 ∩ c3 )
Hence, N (c1 ∪ c2 ∪ c3 ) = 35 + 30 + 30 − (9 + 11 + 10) + 5 = 70
Thus, N (c¯1 ∩ c¯2 ∩ c¯3 ) = 100 − 70 = 30
Example Determine the number of positive integers n where 1 ≤ n ≤ 100
and n is not divisible by 2, 3, or 5.
Solution S = {1, 2, 3, ..., 100}, N = 100
c1 = {n : 2|n}
c2 = {n : 3|n}
c3 = {n : 5|n}
N (c¯1 ∩ c¯2 ∩ c¯3 ) =?
N (c1 ) = ⌊100/2⌋ = 50
N (c2 ) = ⌊100/3⌋ = 33
N (c3 ) = ⌊100/5⌋ = 20
c1 ∩ c2 = {n : 2|n ∧ 3|n} = {n : 6|n}
c1 ∩ c3 = {n : 2|n ∧ 5|n} = {n : 10|n}
c2 ∩ c3 = {n : 3|n ∧ 5|n} = {n : 15|n}
c1 ∩ c2 ∩ c3 = {n : 2|n ∧ 3|n ∧ 5|n} = {n : 30|n}
N (c1 ∩ c2 ) = ⌊100/6⌋ = 16
N (c1 ∩ c3 ) = ⌊100/10⌋ = 10
N (c2 ∩ c3 ) = ⌊100/15⌋ = 6
N (c1 ∩ c2 ∩ c3 ) = ⌊100/30⌋ = 3
2
N (c¯1 ∩ c¯2 ∩ c¯3 ) = N − N (c1 ∪ c2 ∪ c3 )
However, N (c1 ∪ c2 ∪ c3 ) = N (c1 ) + N (c2 ) + N (c3 ) − {N (c1 ∩ c2 ) + N (c1 ∩
c3 ) + N (c2 ∩ c3 )} + N (c1 ∩ c2 ∩ c3 )
= 50 + 33 + 20 − (16 + 10 + 6) + 3 = 74
∴ N (c¯1 ∩ c¯2 ∩ c¯3 ) = 100 − 74 = 26
Recurrence Relations
3
a1 = a0 + 1
a2 = a1 + 2 = a0 + 1 + 2
a3 = a2 + 3 = a0 + 1 + 2 + 3
..
.
n
X
an = k
k=0
n(n+1)
Hence, an = 2
.
1. Fibonacci Sequence
fn = fn−1 + fn−2 , f1 = 1, f2 = 1
2. The tower of Hanoi
Hn = 2Hn−1 + 1
We can use iterative approach to solve this recurrence relation:
4
Hn = 2Hn−1 + 1
= 2(2Hn−2 + 1) + 1 = 22 Hn−2 + 2 + 1
= 2(2(2Hn−3 + 1) + 1) + 1
= 23 Hn−3 + 22 + 2 + 1
..
.
Pn−1
Hence, Hn = i=0 2i = 2n − 1.
Example Find a recurrence relation for the number of bit strings of length
n that do not have two consecutive zeros. How many such bit strings are of
length 5?
Solution Clearly a1 = 2, a2 = 3. Assume n ≥ 3.
The number of bit strings of length n that do not have two consecutive 0’s
equals the number of such bit strings ending with a 0 plus the number of
such bit strings ending with a 1.
The bit strings of length n ending with 1 that do not have two consecutive
0’s are the bit strings of length n − 1 with no consecutive 0’s and a 1 added
at the end. Hence, an−1 such bit strings.
5
The bit strings of length n ending with 0 that do not have two consecutive
0’s are the bit strings of length n − 1 that end with 1. Hence, there are an−2
such bit strings.
∴ an = an−1 + an−2 , n ≥ 3
{an } satisfies the same recurrence relation as the Fibonacci sequence. Be-
cause a1 = f3 and a2 = f4 , it follows that an = fn+2 .
Definition A recurrence relation of the form
6
Solving linear homogeneous recurrence relations with constant co-
efficients
an = c1 an−1 + c2 an−2 , n ≥ 2
Substituting an = rn , we get
r 2 − c1 r − c2 = 0
7
The roots r1 and r2 of this equation can determine the following three cases:
A) r1 and r2 are distinct real numbers
B) r1 and r2 are real but r1 = r2
C) r1 and r2 form a complex conjugate pair
Case (A) (Distinct Real Roots)
Theorem Let c1 and c2 be real numbers. Suppose that r2 − c1 r − c2 = 0
has two distinct roots r1 and r2 . Then the sequence {an } is a solution of the
recurrence relation
an = c1 an−1 + c2 an−2
if and only if an = α1 r1n + α2 r2n for n = 0, 1, 2, ... where α1 and α2 are con-
stants.
Example What is the solution of the recurrence relation an = an−1 + 2an−2
with a0 = 2 and a1 = 7?
Solution The characteristic equation is r2 − r − 2 = 0. The characteristic
roots are r = 2 and r = −1.
Hence, the sequence {an } is a solution to the recurrence relation if and only
if an = α1 2n + α2 (−1)n for some constants α1 and α2 .
From the initial conditions
8
α0 = 2 = α1 + α2
α1 = 7 = 2α1 − α2
=⇒ α1 = 3, α2 = −1
Hence, an = 3 · 2n − (−1)n .
Example Find an explicit formula for the Fibonacci sequence
Example The recurrence relation is given by fn = fn−1 + fn−2 with f0 = 0
and f1 = 1.
The characteristic equation is r2 − r − 1 = 0
√ √
1+ 5 1− 5
=⇒ r1 = 2
and r2 = 2
.
√ √
Hence, fn = α1 ( 1+2 5 )n + α2 ( 1−2 5 )n
f0 = 0 = α1 + α2
√ √
1+ 5 1− 5
f1 = 1 = α1 ( ) + α2 ( )
2 2
Since α1 = −α2 ,
√ √
f1 = 1 = α1 ( 1+2 5 ) − α1 ( 1−2 5 )
√ √
=⇒ α1 (1 + 5) − α1 (1 − 5) = 2
√
=⇒ 2 5α1 = 2
9
√1 −1
=⇒ α1 = 5
and α2 = √
5
√ √
Hence, fn = √1 [( 1+ 5 )n − ( 1−2 5 )n ]
5 2
= α1 r0n + α2 nr0n
= an
10
(The reverse direction) To show every solution {an } of the recurrence rela-
tion an = c1 an−1 + c2 an−2 has an = α1 r0n + α2 nr0n , we will show that there
are constants α1 and α2 such that the sequence {an } with an = α1 r1n + α2 r2n
satisfies the same initial conditions. This requires that
a0 = C 0 = α 1
a1 = C1 = α1 r0 + α2 r0
α2 r0 = C1 − C0 r0
C1 − C0 r0
α2 = , r0 ̸= 0
r0
a0 = 1 = α 1
a1 = 3 + α2 3 = 6 =⇒ α2 = 1
Hence, the solution to the recurrence relation and the initial conditions is
an = 3n + n3n .
11
Case (C) Complex Roots
Recall De Moiver’s Theorem
(cosθ + isinθ)n = cosnθ + isinnθ, n ≥ 0
p
If z = x + iy ∈ C, z ̸= 0, z = r(cosθ + isinθ) where r = x2 + y 2 and
y
tanθ = x
an = 2(an−1 − an−2 ), n ≥ 1, a0 = 1, a1 = 2
√ π π
1+i= 2[cos( ) + isin( )]
4 4
and
√ −π −π
1−i= 2[cos(
) + isin( )]
4 4
√ π π
= 2[cos( ) − isin( )]
4 4
12
Hence,
an = α1 (1 + i)n + α2 (1 − i)n
√ π π √ −π −π n
= α1 [ 2(cos( ) + isin( ))]n + α2 [ 2(cos( ) + isin( ))]
4 4 4 4
√ nπ nπ √ nπ nπ
= α1 ( 2)n (cos( ) + isin( )) + α2 ( 2)n (cos( ) − isin( ))
4 4 4 4
√ n nπ nπ
= ( 2) ((α1 + α2 )cos( ) + (α1 − α2 )isin( ))
4 4
√ n nπ nπ
= ( 2) (k1 cos( ) + k2 sin( ))
4 4
a0 = 1 = k1
√ 1 1
a1 = 2 = 2(k1 √ + k2 √ )
2 2
√
=⇒ k2 = 1 ∴ an = ( 2)n [cos( nπ
4
) + sin( nπ
4
)], n ≥ 0
rk − c1 rk−1 − ...ck = 0
13
has k distinct roots r1 , r2 , ..., rk . Then a sequence an is a solution of the
recurrence relation
if and only if
an = α1 r1n + a2 r2n + ... + ak rkn
a0 = 2 = α1 + α2 + α3
a1 = 5 = α1 + 2α2 + 3α3
a2 = 15 = α1 + 4α2 + 9α3
14
equation
rk − c1 rk−1 − ... − ck = 0
if and only if
15
a0 = 1 = α1,0
where c1 , c2 , ..., ck are real numbers and F (n) ̸= 0 is called a linear nonhomo-
geneous recurrence relation with constant coefficients and
16
(p)
Theorem If {an } is a particular solution of the nonhomogeneous linear
recurrence relation with constant coefficients
(h)
and {an } is a solution of the associated homogeneous recurrence relation
(p) (h)
then every solution of (*) is of the form {an + an }.
Example Find all solutions of the recurrence relation an + 3an−1 + 2n. What
is the solution with a1 = 3?
Solution The associated linear homogeneous recurrence relation is an =
(h)
3an−1 . Hence, an = α3n , where α is a constant.
(p)
F (n) = 2n. A reasonable guess for a particular solution is an = cn + d.
Since an = 3an−1 + 2n, cn + d = 2(c(n − 1) + d) + 2n,
=⇒ (2c + 2)n + (2d − 3c) = 0.∀n
−3
=⇒ c = −1, d = 2
an = −n − 32 + α · 3n , where α is a constant.
If a1 = 3, an = −n − 32 + ( 11
6
)3n .
Example Find all solutions of the recurrence relation an = 5an−1 −6an−2 +7n
Solution The associated homogeneous equation is an = 5an−1 − 6an−2 and
r = 2 and r = 3 are the characteristic roots.
(h)
Hence, an = α1 · 3n + α2 · 2n where α1 and α2 are constants.
17
(p)
Because F (n) = 7n , a reasonable guess for the an = A7n , substituting we
get
A7n = 5A7n−1 − 6A7n−2 + 7n
=⇒ 49A = 35A − 6A + 49
49
=⇒ A = 20
(p)
Hence, an = ( 49
20
)7n
an = α1 3n + α2 2n + ( 49
20
)7n
Theorem Suppose that {an } satisfies the linear nonhomogeneous recurrence
relation
an = c1 an−1 + c2 an−2 + ... + ck an−k + F (n)
where b0 , b1 , ..., bt and s are real numbers. When s is not a root of the char-
acteristic equation of the associated linear homogeneous recurrence relation,
there is a particular solution of the form (pt nt + pt−1 nt−1 + ... + p1 n + p0 )sn
when s is a root of this characteristic equation and its multiplicity is m, there
is a particular solution of the form
18
Example What form does a particular solution of the linear nonhomoge-
neous recurrence relation an = 6an−1 − 9an−2 + F (n) have when F (n) = 3n ,
F (n) = n3n , F (n) = n2 2n , and F (n) = (n2 + 1)3n ?
Solution The associated linear homogeneous equation is an = 6an−1 −9an−2 .
The characteristic equation is r2 − 6r + 9 = (r − 3)2 = 0.
(p)
=⇒ r = 3 is a characteristic root of multiplicity 2. Hence, an = p0 n2 3n if
F (n) = 3n
(p)
an = (p1 n + p0 )n2 3n if F (n) = n3n
(p)
an = (p2 n2 + p1 n + p0 )n2 3n if F (n) = n2 3n
(p)
an = (p2 n2 + p1 n + p0 )2n if F (n) = (n2 + 1)2n
19