1 1 Counting
1 1 Counting
1 1 Counting
PART 1
COMBINATORIAL THEORY
(Lý thuyết tổ hợp)
PART 2
GRAPH THEORY
(Lý thuyết đồ thị)
2
Contents of Part 1
Chapter 0: Sets, Relations
Chapter 1: Counting problem
Chapter 2: Existence problem
Chapter 3: Enumeration problem
Chapter 4: Combinatorial optimization problem
3
Contents
1. Basic counting principles
2. Elementary combinatorial configuration
3. The inclusion-exclusion principle
4. Recurrence relation
5. Generating function
4
1. Basic counting principles
1.1. The sum rule
1.2. The product rule
5
1.1. The sum rule
Let us consider two tasks: Task 1
m1 is the number of ways to do task 1
m2 is the number of ways to do task 2
Tasks are independent of each other, i.e.,
Performing task 1 does not accomplish task 2 and vice versa. Task 2
Sum rule: the number of ways that “either task 1 or task 2 can be done,
but not both”, is m1 + m2.
➔The sum rule can also be phrased in terms of set theory: The
size of the union on k finite pair wise disjoint sets is the sum of
their sizes:
• Let A1, A2, …, Ak be disjoint sets. Then the number of ways
to choose any element from one of these sets is
|A1 A2 … Ak| = |A1| + |A2| + … + |Ak|.
7
1.1. The sum rule
Example 1: A student can choose a computer project from one of three
lists. The three lists contain 23, 15, and 19 possible projects respectively.
How many possible projects are there to choose from?
23+15+19 ☺
Example 2: How many strings of 4 decimal digits, have exactly three digits that are
9s?
The string can have:
• The non-9 as the first digit (x999)
• OR the non-9 as the second digit (9x99)
• OR the non-9 as the third digit (99x9)
• OR the non-9 as the fourth digit (999x)
Thus, we use the sum rule:
• For each of those cases, there are 9 possibilities for the non-9 digit (any
number other than 9)
• Thus, the answer is 9+9+9+9 = 36
8
1. Basic counting principles
1.1. The sum rule
1.2. The product rule
9
The product rule
• Consider two tasks:
– m1 is the number of ways to do task 1
– m2 is the number of ways to do task 2
– Tasks are independent of each other, i.e.,
• Performing task 1does not accomplish task 2 and vice versa.
Product rule: the number of ways that “both tasks 1 and 2 can be done”
in m1m2.
task 1 task 2
10
1.2. The product rule
Generalized product rule: If we have a procedure consisting of
sequential tasks T1, T2, …, Tk that can be done in m1, m2, …, mk ways,
respectively, then there are m1 * m2 * … * mk ways to carry out the
procedure.
➔The product rule can also be phrased in terms of set theory: Let A1, A2,
…, Ak be finite sets. Then the number of ways to choose one element
from each set in the order of A1, A2, …, Ak is
|A1 x A2 x … x Ak| = |A1| * |A2| * … * |Ak|.
11
1.2. The product rule
• If each element ai of k- tuple (a1, a2, ..., ak) has mi number of
ways to select (i = 1, 2, ..., k), then the number of tuples could be
generated is the product of these m1m2 ... mk
12
1.2. The product rule
In many counting problems, it is only after building the first element, we just know how
to build the second element; after building the first two elements we only know how to
build the third element,… In that case, we use the general product rules:
Suppose we construct a k-tuple (a1, a2, ..., ak) by building each element in turn and
• a1 can be chosen by m1 way;
• After a1 is selected, a2 can be chosen by m2 way;
• ...
• After a1, a2, ..., ak-1 are chosen, ak can be chosen by mk ways;
Then the number of k-tuples generated is m1m2 ... mk
Example 1: 6 persons are competing for 4 prizes. How many different outcomes are
A B C
possible? 1st prize 2nd prize 3rd prize 4th prize
D E F
A 4-tuple = (1st prize, 2nd prize, 3rd prize, 4th prize)
13
1.2. The product rule
Example 2: There are 18 math majors and 325 CS majors. How many
ways are there to pick one math major and one CS major?
Total is 18 * 325 = 5850
Example 3: The license plate: 2 letters-4 digits
(a) no letter or digit can be repeated
26 25 10 9 8 7
(b) with repetitions allowed
26 26 10 10 10 10
(c) same as (b), but only vowels and even digits
52x54
14
More complex counting problems
• Combining the product rule and the sum rule.
15
More complex counting problems
Example 1: How many BASIC variables: single letter
or single letter+single digit
26 + 26 * 10=286
16
More complex counting problems
Example 2: How many integers from 1 through 999 do not have any repeated
digits?
Let A = integers from 1 to 999 not having repeated digits.
Partition A into 3 sets:
• A1=one-digit integers not having repeated digits;
• A2=two-digit integers not having repeated digits;
• A3=three-digit integers not having repeated digits.
➔ |A| = |A1| + |A2| + |A3| (by the sum rule)
= 9 + 9×9 + 9×9×8 = 738 (by the product rule)
17
Contents
1. Basic counting principles
2. Elementary combinatorial configuration
3. The inclusion-exclusion principle
4. Recurrence relation
5. Generating function
18
2. Elementary combinatorial configuration
2.1. Permutation
2.2. Combination
19
2. Elementary combinatorial configuration
2.1. Permutation
2.1.1. Permutation
2.1.2. Circulation permutation
2.1.3. Permutation of multisets
2.2. Combination
20
2.1.1. Permutation
Example 1: A PIN is defined as a sequence of any 4 digits from the set {0, 1,
…, 9}.
Question 1. How many different PINs are possible if repetitions are allowed?
Solution. Choosing a PIN is a 4-step operation:
Step 1: Choose the 1st symbol (10 different ways).
Step 2: Choose the 2nd symbol (10 different ways).
Step 3: Choose the 3rd symbol (10 different ways).
Step 4: Choose the 4th symbol (10 different ways).
➔ Based on the product rule:
10×10×10×10 = 10,000 PINs are possible.
21
2.1.1. Permutation
Example 1: A PIN is defined as a sequence of any 4 digits from the set {0, 1,
…, 9}.
Question 2. How many different PINs are possible if repetitions are NOT
allowed?
Solution. Choosing a PIN is a 4-step operation:
Step 1: Choose the 1st symbol (10 different ways).
Step 2: Choose the 2nd symbol (9 different ways).
Step 3: Choose the 3rd symbol (8 different ways).
Step 4: Choose the 4th symbol (7 different ways).
➔ Based on the product rule:
10×9×8×7 = 5,040 PINs are possible.
22
2.1.1. Permutation
A permutation of a set A of objects is an ordered arrangement of
the elements of A where each element appears only once
Example: If A = {a, b, c}, then the permutations of A are
1. abc
2. acb
3. bac
4. bca
5. cab
6. cba
The number of permutations of any set with n elements is
P(n) = n! = n(n − 1) · · · 2 · 1
(Note that by definition 0! = 1)
23
2. Elementary combinatorial configuration
2.1. Permutation
2.1.1. Permutation
2.1.2. Circulation permutation
2.1.3. Permutation of multisets
2.2. Combination
24
2.1.2. Circulation permutation
Circulation permutation n!/n=(n-1)!
Example 3: 6 people A, B, C, D, E, F are seated around a round table, how
many different circular arrangements are possible, if arrangements are
considered the same when one can be obtained from the other by rotations?
ABCDEF, A
BCDEFA, F B
CDEFAB,
DEFABC,
E C
EFABCD,
FABCDE D
are the same arrangements circularly
➔ There are 6! ways to seat 6 people around the table
For each seating, there are 6 “rotations” of the seating
Thus, the final answer is 6!/6 = 5! = 120
25
2. Elementary combinatorial configuration
2.1. Permutation
2.1.1. Permutation
2.1.2. Circulation permutation
2.1.3. Permutation of multisets
2.2. Combination
26
2.1.4. Permutations of multisets
A multiset M is a collection whose members need not be distinct.
Example: The collection
M = (a, a, a, b, b, c, d, d, d, 1, 2, 2, 2, 3, 3, 3, 3)
is a multiset; and sometimes it is convenient to write
M = (3a, 2b, c, 3d, 1• 1, 3•2, 4•3).
A multiset M over a set S can be viewed as a function v : S → ℕ from S
to the set ℕ of nonnegative integers; each element x S is repeated t(x)
times in M; we write M = (S; t).
27
2.1.4. Permutation of multisets
Let M be a multiset and |M| = n.
28
2.1.4. Permutation of multisets
Proposition 2. Let M be a multiset of r different types with repetition numbers
n1, n2,…, nr respectively. Let n = n1+ n2 +… + nr . Then the number of
permutations of M equals
𝒏!
𝒏𝟏 ! 𝒏𝟐 ! … 𝒏𝒓 !
Proof.
nr
29
2.1.4. Permutation of multisets
Example: Find the number of 8-permutations of the multiset M = (a, a, a, b, b,
c, c, c, c) = (3a, 2b, 4c).
Solution:
How to select 8 elements from (3+2+4) = 9 elements ???
(2a,2b,4c)
(3a, b, 4c)
(3a, 2b, 3c)
The number of 8-permutations of (2a, 2b, 4c): 8!/(2!2!4!)
The number of 8-permutations of (3a, b, 4c): 8!/(3!1!4!)
The number of 8-permutations of (3a, 2b, 3c): 8!/(3!2!3!)
➔ Thus the answer is
8!/(2!2!4!) + 8!/(3!1!4!) + 8!/(3!2!3!) = 420 + 280 + 560 = 1260
30
2. Elementary combinatorial configuration
2.1. Permutation
2.2. Combination
31
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
32
2.2.1. Definitions
A k-combination of a set of n elements is a subset of size k of n
elements.
(Note: A permutation is a sequence while a combination is a set)
Example:
The 2-permutation (sequence) of SOHN is:
SO, SH, SN, OH, ON, OS, HN, HS, HO, NS, NO, NH
The 2-combination (set) of SOHN is:
{S,O},{S,H},{S,N},{O,H},{O,N},{H,N}
33
2.2.1. Definitions
The number of all k-combinations of a set of n elements denoted
n k
, Cn or C (n, k )
k
and read “n choose k”.
n n!
= Proof ???
k k ! (n − k )!
This number is also called a binomial coefficient because such numbers
occur as coefficients in the expansions of powers of binomial
expressions such as (a+b)n
34
k-combination
Example: X = {a, b, c}; n = 3, r = 2
k-permutation k-combination
select 2 from 3 elements of X: select 2 from 3 elements of X:
1. (a, b) 1. {a,b}
2. (a, c) 2. {a, c} Order is not the matter
3. {b, c}
3. (b, a)
Ordered sequence
4. (b, c)
5. (c, a)
6. (c, b) 2!
n! n!
P = n(n − 1)...(n − k + 1) =
k
Cnk =
(n − k )! k !(n − k )!
n
35
Difference between permutation and combination
Example: A club has 25 members.
a. How many ways are there to choose 4 members of the club to serve
on an executive committee?
These 4 members are unordered, so use combinations:
C(25,4) = 25!/4!(25-4)! = 25*23*22 = 12,650
b. How many ways are there to choose a president, vice president,
secretary, and treasurer of the club?
These selections are ordered, so use permutations:
P(25,4) = 25!/(25-4)! = 25*24*23*22 = 303,600
36
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
37
Binomial Coefficients
(a + b)4 = (a + b)(a + b)(a + b)(a + b)
4 4 4 4 4
=0 a4 + 1 a3b +2 a2b2 +3 ab3 +4 b4
Binomial Theorem:
Let x and
y be variables,
and let
n be any nonnegative integer.
Then
n n − j j
n
(x + y ) = x yn
j
j =0
38
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
39
2.2.3. Combinations of multisets
Let M be a multiset {a1, a2, …, an} (M has n distinct objects):
A k-combination of M is an unordered collection of k objects selected from n types
of objects of M.
A k-combination of M is also called an k-combination with repetition allowed.
The number of k-combination of M is C(n+ k-1, k) = C(n+ k-1, n-1)
(the number of selections, with repetitions, of k objects from n distinct objects)
Need to divide k candies for n kids B1, B2, …,Bn. How many different ways to divide?
Let tj be the number of candies for kid Bj, j=1,…,n. At this point, the above problem
leads to the problem:
Let k and n be non-negative integers. How many non-negative integers in the following
equation have?
t1 + t2 + t3 + + tn = k
t1 , t2 , , tn Z +
40
The number of r-combination of M is C(n+ r-1, r)
Proof: When k objects are selected from the multiset M, we put them into the following
n boxes
so that the ith type objects are contained in the ith box, 1 ≤ i ≤ n.
Since the objects of the same type are identical, we may use the symbol "O" to denote
any object in the boxes, and the objects in different boxes are separated by a stick "|".
For example, for n = 4 and k = 7:
Convert the symbol "O" to zero 0 and the stick "|" to one 1, any such placement is
converted into a 0-1 sequence of length k+n–1 with exactly k zeros and n–1 ones
➔Now the problem becomes counting the number of 0-1 words of length k+(n–1) with
exactly k zeros and n–1 ones
= C(n+ k-1, k) = C(n+ k-1, n-1)
41
2.3.3. Combinations of multisets
Example 1: How many ways to divide 10 candies for 4 kids
(there may be kids without any candies)
How many solutions are there to the equation
x1 + x2 + x3 + x 4 = 10
when the variables are nonnegative integers (xi ≥ 0)?
Answer:
Select with repetition from x1, x2, x3, x4 10 times.
For example if x1 is selected twice then x1= 2 in the final solution.
Therefore: k-combination with repetition allowed (n = 4, k =10):
C(4+10-1,10)
42
2.3.3. Combinations of multisets
Example 2: How many nonnegative integer solutions are there to the inequality
x1 + x 2 ++ x 6 10 ?
It is equivalent to
x1 + x2 + + x6 + x7 = 10, 0 xi , 1 i 6, 0 x7
which can be transformed to
y1 + y 2 ++ y 6 + y 7 = 9, yi 0 integer
where y i = x i for 1 i 6
and y7 = x7 − 1
➔ Answer: 7 types of objects to select (y1,..,y7) and 9 times of selections [k = 9, n = 7]
➔ k-combination with repetition C(7+9-1,9)= C(15, 9) = 5005
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
44
2.2.4. Multinomial coefficients
The number of ordered arrangements of n objects, in which there are k1
objects of type 1, k2 objects of type 2, ..., and km objects of type m and
where k1+k2+ ... +km = n, is
n n!
=
k1 , k2 ,..., km k1 !k2 !...km !
45
2.2.4. Multinomial coefficients
The Multinomial Theorem:
n k1 k2 km
( x1 + x2 + ...xm ) =
n
x1 x2 ...xm
k1 , k2 ,..., km
where the summation is over all sequences of non-negative integers
(k1, k2, ..., km) such that k1 + k2 + ... + km = n.
46
Summary: select or order k objects from n distinct objects
Order matters Order does not matter
1 𝑛! 2 𝑛!
Repetition 𝑃 𝑛, 𝑘 = 𝐶 𝑛, 𝑘 =
𝑛−𝑘 ! 𝑘! 𝑛 − 𝑘 !
not allowed
(k-permutation of n-set) (k-combination of n-set)
3 𝑛𝑘 4 𝑛+𝑘−1 !
Repetition 𝐶 𝑛 + 𝑘 − 1, 𝑘 =
allowed (k-permutation with repetition 𝑘! 𝑛 − 1 !
allowed) (k-combination with repetition allowed)
The set A = {S,O,H,N}
The 2-permutation (sequence) of set A is: P(4,2) = 12 The 2-combination (set) of set A is: C(4, 2) = 6
SO, SH, SN, OH, ON, OS, HN, HS, HO, NS, NO, NH {S,O},{S,H},{S,N},{O,H},{O,N},{H,N}
1 2
The multi set B = {S, O, H, N}
The 2-permutation with repetition allowed of set B is: 42 = 16 The 2-combination with repetition allowed of
SS, OO, HH, NN, set B is: C(4+2-1,2)=10
SO, SH, SN, OH, ON, OS, HN, HS, HO, NS, NO, NH
{S, S}, {O, O}, {H, H}, {N, N},
{S,O},{S,H},{S,N},{O,H},{O,N},{H,N}
3 4
Contents
1. Basic counting principles
2. Elementary combinatorial configuration
3. The inclusion-exclusion principle
4. Recurrence relation
5. Generating function
48
3. Inclusion-exclusion principle
3.1. Inclusion-exclusion principle
3.2. Derangement
49
3.1. Inclusion-exclusion principle
The inclusion–exclusion principle is an equation relating the sizes of two sets
and their union. It states that if A and B are two (finite) sets, then
|A B| = |A| + |B| - |A B|
50
3.1. Inclusion-exclusion principle
More generally, for finite sets A1, A2,…,Am
𝐴1 ∪ 𝐴2 ∪ ⋯ ∪ 𝐴𝑚 = 𝑁1 − 𝑁2 + ⋯ + (−1)𝑚+1 𝑁𝑚
where:
Nk =
1i1 i2 ...ik m
| Ai1 Ai2 ... Aik |, k = 1, 2,..., m
51
3.1. Inclusion-exclusion principle
Example: How many integers from 1 to 1000 are either multiples of 3
or multiples of 5?
A = set of all integers from 1 to 1000 that are multiples of 3.
B = set of all integers from 1 to 1000 that are multiples of 5.
AB=?
The set of all integers from 1 to 1000 that are multiples of either 3 or 5.
AB=?
The set of all integers that are both multiples of 3 and 5, which also is
the set of integers that are multiples of 15.
To use the inclusion-exclusion principle to obtain |A B|, we need
|A|
|B|
|A B|
52
3.1. Inclusion-exclusion principle
A = set of all integers from 1 to 1000 that are multiples of 3 ➔ |A| = ?
From 1 to 1000, every third integer is a multiple of 3, each multiple of 3 is of
the form 3p, for any integer p from 1 through [1000/3]=333, Hence |A| = 333.
B = set of all integers from 1 to 1000 that are multiples of 5➔ |B| = ?
Similarly for multiples of 5, each multiple of 5 is of the form 5q for some
integer q from 1 through [1000/5]=200. Hence, we have |B| = 200.
A B: set of all integers from 1 to 1000 that are multiples of 15➔ |A B|=?
To determine the number of multiples of 15 from 1 through 1000, each
multiple of 15 is of the form 15r for some integer r from 1 through
[1000/15]=66. ➔ |A B| = 66.
From the principle, we have the number of integers either multiples of 3 or
multiples of 5 from 1 to 1000 given by
|A B| = |A| + |B| - |A B|
=333 + 200 – 66 = 467
53
Bài 1: Đề thi Toán Rời Rạc thường gồm
ba bài: Tổ hợp, Đồ thị, Thuật toán. Dữ liệu
thống kê chothấy: 50% không làm được
bài nào, 35% làm được bài Tổ hợp, 31%
làm được bài Đồ thị, 29% làmđược bài
Thuật toán, 23% làm được cả bài Tổ hợp
và bài Đồ thị, 20% làm được cả bài Tổ
hợp và bàiThuật toán, 19% làm được cả
bài Đồ thị và bài Thuật toán. Hãy tính tỉ lệ
sinh viên làm được cả ba bài
54
Bài 2: Đề thi Toán Rời Rạc thường gồm
ba bài: Tổ hợp, Đồ thị, và Thuật toán. Dữ
liệu thống kêcho thấy: 20% làm được bài
Tổ hợp, 16% làm được bài Đồ thị, 14%
làm được bài Thuật toán, 8% làmđược cả
bài Tổ hợp và bài Đồ thị, 5% làm được cả
bài Tổ hợp và bài Thuật toán, 4% làm
được cả bàiĐồ thị và bài Thuật toán, 2%
làm được cả ba bài. Hãy tính tỉ lệ sinh viên
không làm được bài nào.
55
3. Inclusion-exclusion principle
3.1. Inclusion-exclusion principle
3.2. Derangement
56
3.2. Derangement
A derangement of {1, 2, …, n} is a permutation on the set such that none
of elements i is placed at position ith in the permutation.
(In other words, derangement is a permutation that has no fixed points)
Example: n = 5
Permutation (2,3,5,4,1) is not a derangement
Permutation (2,3,5,1,4) is a derangement
57
3.2. Derangement
Denote Dn the number of derangements of {1, 2, …, n}➔ Dn = ??
Let S = set of all permutations of {1, 2, …, n} ➔ |S| = n!
Let Ai = subset of permutations of {1, 2, …, n} such that the ith element = i in
the permutation. |𝐴1 ∪ 𝐴2 ∪ ⋯ ∪ 𝐴𝑛 | ? ? ?
➔|𝐴1 ∪ 𝐴2 ∪ ⋯ ∪ 𝐴𝑛 | counts the number of permutations in which at least one
object i of the n objects appears in the ith position (its original position).
➔Dn: number of permutations such that none of the n objects appears in their
original positions. Therefore:
Dn = n! - |𝐴1 ∪ 𝐴2 ∪ ⋯ ∪ 𝐴𝑛 |
Remember the inclusion-exclusion principle:
58
3.2. Derangement
An example of derangements arises in a very famous problem called the
hat-check problem:
“n people walk into a party and give their hats to hat-check girl.
Unfornately, she completely loses track of which of n hats belong to
which owners. Therefore, when the party finishes, their hats are returned
at random.”
Question: What is the probability that nobody receives their own hat
back?
➔This is equivalent to asking for the probability that a permutation of n
objects is a derangement.
𝐷𝑛
➔ =
𝑛! What is the value in number????
since there are Dn possible derangements and n! possible permutations.
59
3.2. Derangement
It is amusing to note that as n → ∞, the probability that a permutation of n
values is a derangement is given by
60
Contents
1. Basic counting principles
2. Elementary combinatorial configuration
3. The inclusion-exclusion principle
4. Recurrence relation
5. Generating function
61
4. Recurrence relations
4.1. Recurrence relations
4.2. Solve recurrence relations
62
4.1. Recurrence relations
Definition:
A recurrence relation for the sequence {an} is the equation that expresses an
in terms of one or more of the previous terms in the sequence namely, a0, a1,
…, an-1, for all integers n with n n0, where n0 is a nonnegative integer.
A sequence is called a solution to a recurrence relation if its terms satisfy the
recurrence relation.
Example: Consider the recurrence relation
an = 2an-1 – an-2 for n = 2, 3, 4, … The sequence an=n+1??
The sequence an=3n is a solution of this recurrence relation?
For n 2 we see that: 2an-1 – an-2 = 2(3(n – 1)) – 3(n – 2) = 3n = an
Therefore, the sequence an=3n is a solution of the recurrence relation
The sequence an=5 is also a solution of the this recurrence relation?
For n 2 we see that: 2an-1 – an-2 = 25 - 5 = 5 = an
Therefore, the sequence an=5 is also a solution of the recurrence relation
the same recurrence relation can have multiple solutions.
4.1. Recurrence relations
A recurrence without specifying any initial values (initial conditions).
➔ can have (and usually has) multiple solutions.
Example: an = 2an-1 – an-2 for n = 2, 3, 4. It has the following sequences an as solution:
an=5
an=3n
an=n+1
If both the initial conditions and the recurrence relation are specified, then the
sequence is uniquely determined.
Example: an = 2an-1 – an-2 for n = 2, 3, 4
where a0=0; a1 = 3
➔The sequence an=5 is not the solution
➔The sequence an=3n is the unique solution
4.1. Recurrence relations
Some applications of recurrence relations:
1. To solve many counting problems we can try construct recurrence
relations.
2. Complexity analysis of the recursive algorithm is a recurrence
relation on the number of operations.
Modeling with Recurrence Relations
Example 1: Someone deposits $10,000 in a savings account at a bank yielding 5% per year
with interest compounded annually. How much money will be in the account after 30 years?
Solution:
• Let Pn denote the amount in the account after n years.
How can we determine Pn on the basis of Pn-1?
• We can derive the following recurrence relation:
Pn = Pn-1 + 0.05Pn-1 = 1.05Pn-1.
• The initial condition is P0 = 10,000. P30 = (1.05)3010,000 = 43,219.42
Then we have:
• P1 = 1.05P0
• P2 = 1.05P1 = (1.05)2P0
• P3 = 1.05P2 = (1.05)3P0
•…
•Pn = 1.05Pn-1 = (1.05)nP0
➔ We now have a formula to calculate Pn for any natural number n and can avoid the iteration.
66
Example 2: Fibonacci
A pair of rabbits is placed on an island. After 2 months old, a pair of rabbits
produces another pair each month. How many rabbits are there after n months?
• n = 1: f1 = 1
• n = 2: f2 = 1 Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,etc.
• n > 2: fn = fn-1 + fn-2
The n > 2 formula is true since each new pair comes from a pair at least 2 months old.
New-born Month 1-month rabbits >=2-month rabbits Total fn
rabbits
1 1 0 1
2 0 1 1
3 1 1 2
4 1 2 3
5 2 3 5
6 3 5 8
7 5 8 13
8 8 13 21
4.1. Recurrence relations
Some applications of recurrence relations:
1. To solve many counting problems we can try construct recurrence
relations.
2. Complexity analysis of the recursive algorithm is a recurrence
relation on the number of operations.
4.1. Recurrence relations
Example 3. Recall the factorial function
How many times is function Factorial called when we execute the statement Factorial(n); ?
When n = 1, Factorial is called 1➔ T(1) = 1
Otherwise:
We perform 1.
Plus the number of calls in the recursive call in Factorial(n − 1) ➔ T(n-1)
This can be expressed as a formula (similar to the definition of n!):
T(1) = 1 T(n) = 1 + T(n-1)
T(n) = 1 + T(n − 1), n >1
This is known as a recurrence relation.
Find the formula to calculate T(n) for
any natural number n and can avoid the
iteration. explicit formula
4.1. Recurrence relations
Factorial(n);
To solve this problem, the divide and conquer technique is built based on the following
argument: The given key is either
equal to the element at the middle of array A
or equal to the element at the left (L) half of array A
or equal to the element at the right (R) half of array A.
or not in array
72
4.1. Recurrence relations
Input: An array A consists of n elements: A[0],…,A[n-1] in ascending order;
Value key with the same data type as array A.
Output: the index in array if key is found, -1 if key is not found
To solve this problem, the divide and conquer technique is built based on the following
argument: The given key is either
equal to the element at the middle of array A
or equal to the element at the left (L) half of array A
or equal to the element at the right (R) half of array A.
or not in array
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
int mid = (low + high) / 2;
if (A[mid]==key) return mid;
else if (key < A[mid])
return binsearch(low, mid-1, A, key);
else
return binsearch(mid+1, high, A, key);
}
else return -1; 73
}
4.1. Recurrence relations
Example 4. Performance of Recursive Binary Search
Input: An array A consists of n elements: A[0],…,A[n-1] in ascending order;
Value key with the same data type as array A.
Output: the index in array if key is found, -1 if key is not found
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
int mid = (low + high) / 2;
if (A[mid]==key) return mid;
else if (key < S[mid])
return binsearch(low, mid-1, A, key);
else
return binsearch(mid+1, high, A, key);
}
else return -1;
}
➔ binsearch(0, n-1, A, key);
How many times is binsearch called in the worst case ?
74
Backward substitution
Backward substitution: this works exactly as its name suggests. Starting from the
equation itself, work backwards, substituting values of the function for previous ones.
Example: T(n)= T(n-1) + 2n
where T(1) = 5
Answer: We begin by unfolding the recursion by a simple substitution of the
function values.
• We observe that:
T(n-1) = T((n-1) -1) + 2(n-1)
= T(n-2) + 2 (n-1)
• Substituting into the original equation:
T(n)=T(n-2)+2(n-1)+2n
Backward substitution
• If we continue to do that we get
T(n) = T(n-2) + 2(n-1) + 2n
= T(n-3) + 2(n-2) + 2(n-1) + 2n
= T(n-4) + 2(n-3) + 2(n-2) + 2(n-1) + 2n
…………
= T(n-i) + σ𝑖−1
𝑗=0 2(𝑛 − 𝑗) function’s value at the ith iteration
• Solving the sum we get
T(n) = T(n-i) + 2n(i-1) – 2(i-1)(i-1+1)/2 + 2n
= T(n-i) + 2n(i-1) – i2 + i + 2n
Backward substitution
• We want to get rid of the recursive term T(n-i). To do that, we need to know
at what iteration we reach our base case, i.e. for what value of i can we use
the initial condition T(1)=5?
We get the base case when n - i =1 or i = n - 1
• Substituting in the equation above we get
T(n) = T(1) + 2n(n-1-1) – (n-1)2 + (n-1) + 2n
= 5 + 2n(n-2) – (n2-2n+1) + (n-1) + 2n = n2 + n + 3
4. Recurrence relations
4.1. Recurrence relations
4.2. Solving recurrence relations
78
4.2. Solving Recurrence Relations
Solving the recurrence relation for a sequence a0, a1, a2, a3,….an,… is to give
the explicit formula to compute the value for the general term an, i.e., to find an
expression for an that does not involve any other ai
Example: Given the recurrence relation:
an = 2an-1 – an-2 where n = 2, 3, 4,…
a0=0; a1 = 3
➔The explicit formula for the above recurrence relation is an=3n
➔an=3n is the solution to the above recurrence relation
• Does not exist the method to solve all types of recurrence relation.
• Consider method to solve the recurrence relation with following types:
• A linear homogeneous recurrence relation of degree k with constant
coefficients
• A linear nonhomogeneous recurrence relation of degree k with constant
coefficients
79
4.2. Solving Recurrence Relations
Definition: A linear homogeneous recurrence relation of degree
k with constant coefficients is a recurrence relation of the form:
an = c1an-1 + c2an-2 + … + ckan-k
where c1, c2, …, ck are real number constants, and ck 0.
80
4.2. Solving Recurrence Relations
Definition: A linear homogeneous recurrence relation of degree
k with constant coefficients is a recurrence relation of the form:
an = c1an-1 + c2an-2 + … + ckan-k
where c1, c2, …, ck are real number constants, and ck 0.
Explain:
• Linear: the right-hand side is the sum of the terms before the term an
in the sequence where the coefficients (c1, c2, ..,ck) are constant (not
a function dependent on n)
• Homogeneous: the right-hand side has no additional terms other
than the terms ai of the sequence
• Degree k: the right hand side has the (n-k)th term of the sequence
81
A linear homogeneous recurrence relation of degree k
with constant coefficients
• We try to find solution of the form an = rn, where r is a constant.
• The sequence {an = rn } is a solution of the recurrence relation
an = c1an−1 + … + ckan−k
82
A linear homogeneous recurrence relation of degree k
with constant coefficients
• Theorem 1. Let c1 and c2 be real numbers.
Suppose that r2 - c1 r - c2 = 0 has 2 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(r1)n + 2(r2)n (1)
n = 0, 1, ..., where 1 and 2 are constants.
83
Example 1
Fibonacci sequence is given by the following recurrence relation:
Fn = Fn-1 + Fn-2, n 2,
F0 = 0, F1 = 1.
Find the explicit formula for Fn.
Solution: Solve the characteristic equation:
r2 - r - 1 = 0,
its characteristic roots are: Leonardo Fibonacci
1+ 5 1− 5 1170-1250
r1 = ; r2 =
2 2
84
Example 1
• The explicit formula:
Fn = 1.(r1)n + 2.(r2)n
where 1, 2 are constants and could be determined by using
initial conditions F0, F1. F0= 1+ 2 = 0
F1= 1r1+ 2r2 = 1
Solving these two equations, we have: 1 = 1 ; 2 = − 1
5 5
Muavre formula
Therefore
1
( ) ( )
n n
1+ 5 1− 5 , n 0.
Fn = −
5 2 2
85
The case: one characteristic root with multiplicities 2
But what happens if the characteristic equation has only one root?
Theorem 2: Let c1 and c2 be real numbers with c2 0. Suppose that r2 -
c1 r - c2 = 0 has only one root r0. Then a sequence {an } is a solution of
the recurrence relation an = c1 an-1 + c2 an-2
if and only if
an = 1r0n + 2 nr0n
n = 0, 1, ..., where 1 , 2 are constants.
86
Example 2
Solving the following recurrence relation
an = 6 an-1 - 9 an-2
with initial conditions a0 = 1 and a1 = 6.
Solution:
Characteristic equation:
r2 - 6 r + 9 = 0 has one root r = 3. The solution is:
an = 1 3n + 2 n 3n
To determine 1, 2 , using the initial conditions, we have:
a0 = 1 = 1 ,
a1 = 6 = 1 * 3 + 2 *1* 3
Solving these equations, we have 1 = 1 and 2 = 1.
Therefore, the solution of the recurrence relation is:
an = 3n + n 3n
87
General case
Theorem 3. Let c1, c2, ..., ck be real numbers. Assume the characteristic
equation
rk - c1 rk-1 - c2 rk-2 - . . . - ck = 0
has k distinct roots r1, r2, ..., rk . Then a sequence {an} is a solution of
the recurrence relation:
an = c1 an-1 + c2 an-2 +...+ ck an-k,
if and only if
an = 1 r1n + 2 r2n + . . . + k rkn
where n = 0, 1, 2,..., and 1, 2, ..., k are constants
88
Example 3
Solving the recurrence relation:
an = 6 an-1 - 11 an-2 + 6 an-3
where initial conditions
a0 = 2, a1 = 5, a2 = 15.
Solution: Characteristic equation
r3 - 6 r2 + 11 r - 6 = 0
has 3 distinct roots r1 = 1, r2 = 2, r3 = 3.
Therefore, the solution is
an = 1 1n + 2 2n + 3 3n for some constants 1, 2, 3
89
Example 3
Using the initial conditions, we have following equations to
determine the value for constants 1, 2, 3:
a0 = 2 = 1 + 2 + 3
a1 = 5 = 1 + 2.2 + 3.3
a2 = 15 = 1 + 2.4 + 3.9.
Solving above equations, we have
1 = 1, 2 = -1 and 3 = 2.
Therefore, the solution of the recurrence relation is
an = 1 - 2n + 2. 3n
90
General case
Given linear homogeneous recurrence relation of degree k with constant
coefficients k
i =1
i =1 j =0
where n≥0, and αij are constants.
91
Example 4
Solve the following recurrence relation:
cn = – 4cn-1 + 3cn-2 + 18cn-3 , n 3,
c0 = 1; c1 = 2; c2 = 13.
Solution: Characteristic equation
r3 + 4r2 – 3r – 18 = (r – 2)(r + 3)2 = 0
Hence, the solution of the recurrence relation:
cn = α10 2n + (α20 + α21 n)(– 3)n
where α10, α20, α21 are constants
92
Example 4
These constants are determined by using initial conditions:
0 = c0 = α10 20 + (α20 + α21.0) (-3)0 = α10 + α20
2 = c1 = α10 21 + (α20 + α21.1) (-3)1 = 2α10 - 3α20 - 3α21
13 = c2 = α10 22 + (α20 + α21.2) (-3)2 = 4α10 + 9α20 +18α21
Solving three equations above, we have: α10 =1 ; α20 = -1; α21=1
93
Linear nonhomogeneous recurrence relation with
constant coefficients
• Linear nonhomogeneous recurrence relation with constant
coefficients) has the term F(n) dependent on n (and
independent on any value of ai ) :
an = c1an−1 + … + ckan−k + F(n)
Solving:
an = 2an-1 + 1, n 1;
a1 = 1.
Solution:
(1) Linear homogeneous part: an = 2an-1
➔ Characteristic equation: r - 2 = 0 has one root r = 2
➔ Solution of linear homogeneous part is: h(n) = c12n
(2) Linear Nonhomogenenous part F(n) = 1, then the particular solution is
of the form
p(n) = C
Solving:
an = 2an-1 + 1, n 1;
a1 = 1.
n*4
• The last level log2n has the value = 4n, thus we have:
2.5. Recurrence relations and generating functions
2.5.1. Recurrence relations
2.5.2. Generating functions
104
2.5.2. Generating functions
Generating functions are a tool to solve a wide variety of counting
problems and recurrence relations.
Example 1:
How many ways to give 12 oranges for three children: A, B and C such that:
A gets at least four, and B and C gets at least two, but C gets no more than five
105
2.5.2. Generating functions
Example 1:
Find the number of integer solutions to
a + b + c = 12 where a ≥ 4, b ≥ 2, 2 c 5
Let
a: a(x) = x4 + x5 + x6 + x7 + x8 (since b + c 4 ➔ a 8)
b : b(x) = x2 + x3 + x4 + x5 + x6 (since a + c 6 ➔ b 6)
c : c(x) = x2 + x3 + x4 + x5 (since 2 c 5)
The coefficient of x12 in g(x) = a(x) b(x) c(x)
n=12
=(x4+ x5+ x6+ x7+ x8) (x2+ x3+x4+ x5+ x6)(x2+ x3+x4+ x5)
=???
is the solution to the problem
g(x) is called a generating function.
Why???
Assume xa, xb, xc are terms derived from (x4+ x5+ x6+ x7+ x8) , (x2+ x3+x4+ x5+ x6), (x2+ x3+x4+ x5)
respectively when developing the right-hand side ➔ 4 a 8, 2 b 6, 2 c 5
When developing the right-hand side, these three terms give us the term xn where n = a +b + c:
g(x) = (x4+ x5+ x6+ x7+ x8) (x2+ x3+x4+ x5+ x6)(x2+ x3+x4+ x5)
= …. + Kxn +…..
➔The coefficient of xn in f(x) is the number of positive integer solutions to
a + b + c = n where 4 a 8, 2 b 6, 2 c 5
106
2.5.2. Generating functions
Example 1:
Find the number of integer solutions to
a + b + c = 12 where a ≥ 4, b ≥ 2, 2 c 5
Let
a: a(x) = x4 + x5 + x6 + x7 + x8 (since b + c 4 ➔ a 8)
1. x4*x3*x5
b : b(x) = x2 + x3 + x4 + x5 + x6 (since a + c 6 ➔ b 6)
2. x4*x4*x4
c : c(x) = x2 + x3 + x4 + x5 (since 2 c 5)
3. x4*x5*x3
The coefficient of x12 in g(x) = a(x) b(x) c(x) 4. x4*x6*x2
=(x4+ x5+ x6+ x7+ x8) (x2+ x3+x4+ x5+ x6)(x2+ x3+x4+ x5) 5. x5*x2*x5
which is 14, is the solution 6. x5*x3*x4
g(x) is called a generating function. 7. x5*x4*x3
8. x5*x5*x2
9. x6*x2*x4
10. x6*x3*x3
11. x6*x4*x2
12. x7*x2*x3
13. x7*x3*x2
14. x8*x2*x2
107
2.5.2. Generating functions
Example 2: Four kinds of jelly beans: Red, Green, White, Black. In how many
ways can we select 24 jelly beans so that we have an even number of white
beans and at least six black ones?
White: 1+ x2+ x4+….+ x16 + x18
Black: x6+ x7+….+ x16+ x18
Red (green): 1+ x1+ x2+….+ x16+ x18
g(x)=(1+ x2+ x4+….+ x16+ x18)(x6+ x7+….+ x16+ x18) (1+ x1+ x2+….+ x16+ x18)2
The coefficient of x24 is the solution.
i =0
109
2.5.2. Generating functions
Example 4: For the binomial coefficients we already know that:
n n
( x + y ) = C (n, k ) x y
n k n−k
( x + 1) = C (n, k ) x k = g ( x)
n
k =0 k =0
110
2.5.2. Generating functions
Definition: Let a0, a1, a2, … be a sequence of real numbers. The function
g ( x) = a0 + a1 x + ... + ak x + ... = ai x i
k
i =0
111
2.5.2. Generating functions
If we have two generating functions F(x) and G(x), we define the sum and
product as follows:
F ( x) = ak x k
G ( x) = bk x k
k =0 k =0
F ( x) + G ( x) = ( ak + bk ) x k Match all terms with equal powers in x.
k =0
k
F ( x)G ( x) = a j bk − j x k
k =0 j =0
Example 3: Example why multiplying generating functions is useful:
1 1 1
= =(1+x+x2+x3+...)(1+x+x2+x3+...) where |x| < 1
(1−𝑥)2 1−𝑥 1−𝑥
k
= (1) x k * (1) x k = 1 x = ( k + 1) x
k k
k =0 k =0 k =0 j =0 k =0
112
2.5.2. Generating functions
Generating functions are a tool to solve a wide variety of counting problems and
recurrence relations.
Definition: Let a0, a1, a2, … be a sequence of real numbers. The function
g ( x) = a0 + a1 x + ... + ak x k + ... = ai x i
i =0
is called the generating function for the given sequence.
113
2.5.2. Generating functions
Example: Solve the recurrence relation an-3an-1 = n, n1, a0=1
𝑥
𝑔 𝑥 − 1 − 3𝑥𝑔(𝑥) =
(1 − 𝑥)2
1 𝑥
𝑔 𝑥 = +
1 − 3𝑥 (1 − 3𝑥)(1 − 𝑥)2
1 𝐴 𝐵 𝐶
𝑔 𝑥 = + + +
1 − 3𝑥 (1 − 3𝑥) (1 − 𝑥) (1 − 𝑥)2
3 1 1
⇒ 𝐴 = ;𝐵 = − ;𝐶 = − ;
4 4 2 g ( x) = a0 + a1 x + ... + ak x k + ... = ai x i
i =0
7/4 −1/4 −1/2
𝑔 𝑥 = + +
(1 − 3𝑥) (1 − 𝑥) (1 − 𝑥)2
114
2.5.2. Generating functions
Example: Solve the recurrence relation an-3an-1 = n, n1, a0=1
7/4 −1/4 −1/2
𝑔 𝑥 = + +
(1 − 3𝑥) (1 − 𝑥) (1 − 𝑥)2
We find an by determining the coefficient of xn in each of the three summands
1. (7/4)/(1-3x) = (7/4) [1/(1-3x)]
= (7/4) [1 + 3x + (3x)2 + (3x)3+….]
➔ The coefficient of xn is (7/4)3n
2. (-1/4)/(1-x) = (-1/4) [1/(1-x)]
= (-1/4) [1 + x + (x)2 + (x)3+….]
➔ The coefficient of xn is (-1/4)
3. (-1/2)/(1-x)2 = (-1/2) [1/(1-x)2]
= (-1/2) [1 + 2x + 3(x)2 + 4(x)3+….]
➔ The coefficient of xn is (-1/2)(n+1)
Therefore, an = (7/4) 3n – (1/2)n -3/4, n ≥ 0
115
Bài tập (1)
• Câu hỏi 1: Trong một hệ thống thông tin, mỗi mật khẩu
dài từ 6 tới 8 ký tự, trong mỗi ký tự là một chữ cái in
hoa hoặc chữ số. Mỗi mật khẩu phải chứa ít nhất 1 chữ
số. Hỏi có bao nhiêu mật khẩu khác nhau ?
• Câu hỏi 2: Một phiếu thi trắc nghiệm nhiều lựa chọn
gồm 10 câu hỏi. Mỗi câu hỏi có 4 phương án trả lời.
• Có bao nhiêu cách điền phiếu trắc nghiệm nếu tất cả các câu
hỏi đều được trả lời ?
• Có bao nhiêu cách điền phiếu trắc nghiệm nếu có thể bỏ qua
các câu hỏi không trả lời ?
Bài tập (2)
• Câu hỏi 3: Trong một đám cưới, một nhóm
bạn 10 người kể cả cô dâu và chú rể tổ chức
chụp ảnh. Để chụp ảnh người ta xếp 6 người
thành 1 hàng dọc. Hỏi có bao nhiêu cách xếp
để chụp ảnh nếu
• Ảnh phải có cô dâu
• Ảnh phải có cả cô dâu và chú rể
• Ảnh phải có cả cô dâu và chú rể đứng cạnh nhau
• Ảnh chỉ có cô dâu hoặc chú rể
• Ảnh có cô dâu đứng bên trái chú rể
Bài tập (3)
• Câu hỏi 4: Có bao nhiêu xâu nhị phân độ dài
10 ký tự và có 5 bit 0 liên tiếp hoặc 5 bit 1 liên
tiếp.