02-Berhitunglanjut Slides
02-Berhitunglanjut Slides
Adila A. Krisnadhi
Faculty of Computer Science, Universitas Indonesia
Recurrence Relation
Generating Functions
2/40
Outline
Recurrence Relation
Generating Functions
3/40
Sum rule and product rule
How many possible license plates can the DKI Jakarta’s police department issue?
4/40
Inclusion-exclusion principle
How many bit strings of length 8 begins with a 1 or ends with 00?
5/40
Pigeonhole principle
How many students must register to the Discrete Math 2 class at least so that at
least two students can get the same final grade?
6/40
Permutation (without repetition)
How many ways to give out one gold, one silver, and one bronze medal to 100
marathoners?
7/40
Combination (without repetition)
8/40
Outline
Recurrence Relation
Generating Functions
9/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
• a1 =
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
• a1 = 2
• a2 =
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
• a1 = 2
• a2 = 3
• a3 =
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
• a1 = 2
• a2 = 3
• a3 = 5
• an =
10/40
Motivation
An example of rather complex problems: “How many bit strings of length n do
not contain two consecutive 0s?”
• Suppose an is the number of bit strings of length n that do not contain two
consecutive 0s.
• a1 = 2
• a2 = 3
• a3 = 5
• an = an−1 + an−2 untuk n > 2.
The last equation is called a recurrence relation representing the sequence 2, 3,
5, 8, 13, . . . .
Recurrence relation can be solved by finding an explicit formula for an as a
function of n.
10/40
Recurrence relation: Example 1
A pair of very young rabbits (one of each sex) is left on an island. Every pair of rabbits
does not breed until they reach the age of 2 months old. After reaching 2 months old,
each pair produces another pair (of opposite sex) each month. How many pairs of
rabbits are there in that island after n months if the rabbits live forever and every
rabbit is paired exactly to another rabbit?
11/40
Recurrence relation: Example 2
How many steps are needed to move all disks from peg 1 to peg 3 such that a
disk is never stacked on top of a smaller disk throughout the process of moving?
12/40
Recurrence relation: Example 3
A special PIN is made of a string containing n decimal digits. The PIN is valid if
it contains an even number of 0s. How many valid PINs of length n are there?
13/40
Solving recurrence relation
14/40
Outline
Recurrence Relation
Generating Functions
15/40
Generating functions
Definition 1
An (ordinary) generating function for the sequence of (possibly infinitely
many) real numbers a0 , a1 , . . . , ak , . . . is an infinite series of the form
∞
X
2 k
G(x) = a0 + a1 x + a2 x + . . . + ak x + . . . = ak x k
k=0
• In the context of counting, generating functions are also called formal power
series (FPS)
• Value of x is usually ignored, but some operations on generating functions can
only be well-defined if the series converges. So, if needed, x is assumed to be
close to 0.
16/40
Examples
17/40
Binomial is a polynomial containing two terms, e.g., x + y, 1 + 2x, 2x + 3yz
Theorem 2
Binomial theorem For every binomial x + y and n ∈ N, it holds that
n n n 0 n n−1 1 n 1 n−1 n 0 n
(x + y) = x y + x y + ... + xy + xy
0 1 n−1 n
n
X n n−k k
= x y
k=0
k
n
X n k n−k
= x y
k=0
k
19/40
Example
2 2 2 0 2 1 1 2 0 2
(x + y) = xy + xy + x y = x2 + 2xy + y 2
0 1 2
3 3 3 0 3 2 1 3 1 2 3 0 3
(x + y) = xy + xy + xy + x y = x3 + 3x2 y + 3xy 2 + y 3
0 1 2 3
(x + y)4 = x4 + 4x3 y + 6x2 y 2 + 4xy 3 + y 3
Given m∈ Z+ , what is the generating function of the sequence {ak } where
ak = mk
and k = 0, 1, . . . , m?
19/40
If |x| < 1, is f (x) = 1/(1 − x) the generating function of the infinite sequence
1, 1, 1, 1, . . . ?
20/40
Addition and multiplication of generating functions
Theorem 3
P∞ P∞
Let f (x) = k=0 ak xk and g(x) = k=0 bk x
k
. Then,
∞
X ∞ X
X k
f (x) + g(x) = (ak + bk )xk and f (x)g(x) = aj bk−j xk
k=0 k=0 j=0
21/40
SupposePf (x) = 1/(1 − x)2 . Compute a0 , a1 , a2 , . . . in the expansion of
f (x) = ∞ k
k=0 ak x .
22/40
Extended binomial coefficient
23/40
−2 1/2
Compute 3
and 3
24/40
−n n+r−1
Show that for n > 0, r
= (−1)r r
25/40
Theorem 5 (Extended binomial theorem)
Suppose u, x ∈ R and |x| < 1. Then,
∞
u
X u k
(1 + x) = x
k=0
k
26/40
Find the generating function for (1 + x)−n and (1 − x)−n untuk n ∈ Z+ .
27/40
Examples of generating function G(x) for the recurrence relation {ak } I
Assume: n ∈ Z+ , k = 0, 1, 2, . . .
n
k Pn n k k
G(x) = (1 + cx)n =
ak = k
c k=0 k c x
+ n1 cx + n2 c2 x2 + . . . + cn x n
=1
(
n
jika r | k Pn n
G(x) = (1 + xr )n =
k/r
rk
ak = k=0 k
x
0 jika r6 | k
n n
xr + x2r + . . . + xrn
=1+ 1 2
28/40
Examples of generating function G(x) for the recurrence relation {ak } II
(
1 jika k ≤ n 1 − xn+1 Pn
ak = G(x) = = k=0 xk = 1 + x + x2 + . . . + xn
0 jika k > n 1−x
1
= ∞ k 2
P
ak = 1 G(x) = k=0 x = 1 + x + x + . . .
1−x
1
ak = c k = ∞ k k 2 2
P
G(x) = k=0 c x = 1 + cx + c x + . . .
1 − cx
(
1 jika r | k 1 P∞ rk
ak = G(x) = r
= k=0 x = 1 + xr + x2r + . . .
0 jika r6 | k 1−x
29/40
Examples of generating function G(x) for the recurrence relation {ak } III
1
= ∞ k
P
ak = k + 1 G(x) = 2 k=0 (k + 1)x
(1 − x)
= 1 + 2x + 3x2 + 4x3 + . . .
1
n+k−1
= ∞ n+k−1
xk
P
ak = k G(x) = n k=0 k
(1 − x)
n+k−1 n n+1 n+2
x2 + x3 + . . .
= n−1 =1+ 1 x+ 2 3
1
ak = (−1)k n+k−1
= ∞ n+k−1
(−1)k xk
P
k G(x) = n k=0 k
(1 + x)
30/40
Examples of generating function G(x) for the recurrence relation {ak } IV
1
n+k−1
ak = ∞ n+k−1
ak xk
P
ak = k G(x) = n k=0 k
(1 + ax)
n+k−1
ak = 1 + n1 ax + n+1 n+2
2 2
a3 x3 + . . .
= n−1 2 a x + 3
1 P∞ xk x2 x3
ak = G(x) = ex = k=0 k! =1+x+ 2! + 3! + ...
k!
(−1)k+1 P∞ (−1)k+1 k x2 x3 x4
ak = G(x) = ln(1 + x) = k=1 k x =x− 2 + 3 − 4 + ...
k
31/40
Outline
Recurrence Relation
Generating Functions
32/40
Advanced counting using generating functions
33/40
Count the number of possible solutions of e1 + e2 + e3 = 17 with e1 , e2 , e3 ∈ N
and 2 ≤ e1 ≤ 5, 3 ≤ e2 ≤ 6, and 4 ≤ e3 ≤ 7.
34/40
How many ways are there to distribute 8 identical cookies to three children if
every child receives at least 2 and at most 4 cookies?
35/40
How many ways to insert 1 dollar tokens, 2 dollar tokens and 5 dollar tokens to a
vending machine to pay stuff priced at r dollars if the order of insertion is (a) not
important, and (b) important.
36/40
Outline
Recurrence Relation
Generating Functions
37/40
Solve the recurrence relation ak = 3ak−1 with a0 = 2 using generating functions.
38/40
In the valid PIN problem (slide 13), we obtain the recurrence relation
an = 8an−1 + 10n−1 with a1 = 9. Solve it using generating functions.
39/40
Pn n 2 2n
Show that k=0 k = n
.
40/40