Part1 1counting
Part1 1counting
Part1 1counting
Discrete Mathematics
PART 2
GRAPH THEORY
(Lý thuyết đồ thị) 2
Contents of Part 1: Combinatorial Theory
Chapter 1. Counting problem
• This is the problem aiming to answer the question: “How many ways are there that satisfy
given conditions?” The counting method is usually based on some basic principles and some
results to count simple configurations .
• Counting problems are effectively applied to evaluation tasks such as calculating the
probability of an event, calculating the complexity of an algorithm (how long the algorithm
will take to run), ....
Given N paintings in a row over a
distance of M centimeters.
Each painting i (1 ≤ i ≤ N) will be drawn
on a length of ti cm, so t1+t2+..+tn = M.
The K city's most famous artists have
been selected to do this work, each artist
will be assigned to draw at least one
painting. To facilitate the artist's work, if
someone is assigned to draw more than
one painting, the paintings must be
Street art adjacent to each other on the street art
3
Contents of Part 1: Combinatorial Theory
Chapter 1. Counting problem
• This is the problem aiming to answer the question: “How many ways are there that satisfy
given conditions?” The counting method is usually based on some basic principles and some
results to count simple configurations .
• Counting problems are effectively applied to evaluation tasks such as calculating the
probability of an event, calculating the complexity of an algorithm
Chapter 2. Existence problem
In the counting problem, configuration existence is obvious; in the existence problem, we need to
answer the question: "Is there a combinatorial configuration that satisfies given properties ?”
Chapter 3. Enumeration problem
This problem is interested in giving all the configurations that satisfy given conditions.
Chapter 4. Combinatorial optimization problem
• Unlike the enumeration problem, this problem only concerns the "best" configuration in a
certain sense.
• In the optimization problems, each configuration is assigned a numerical value (which is the
use value or the cost to construction the configuration), and the problem is that among the
configurations that satisfy the given conditions, find the configuration with the maximum or
minimum value assigned to it
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
Chapter 1
counting problem
8
1. Basic counting principles
1.1. The sum rule
1.2. The product rule
1.3. Tree diagram
9
1.1. The sum rule
Example:
• 40 textbooks on Mathematics; 50 textbooks on English
• to select 1 book: 40+50 choices
• What about selecting 2 books?
problem decompose combine
Task 1
Independent tasks
Let us 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 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.
Things 1 2 3 … k
ways m1 m2 m3 … mk
Tasks 1 2 3 … k
ways m1 m2 m3 … mk
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|.
14
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) 15
• Thus, the answer is 9+9+9+9 = 36
1.1. The sum rule
Example 3: What is the value of k once this program is executed?
n1=10; n2=20; n3=30;
k=0;
for (i1= 1; i1<=n1;i1++) k=k+1;
for (i2= 1; i2<=n2;i2++) k=k+1;
for (i3= 1; i3<=n3;i3++) k=k+1;
Answer: At the beginning, the value of k is assigned to 0. There are 3
independent “for” loops. After each for loop, the value of k is increased by one:
• The first “for” loop iterates 10 times,
• The second “for” loop iterates 20 times,
• The last “for” loop iterates 30 times
Therefore, at the end, the value of k = 10 + 20 + 30 = 60
16
1. Basic counting principles
1.1. The sum rule
1.2. The product rule
1.3. Tree diagram
17
The sum rule
Object type 1
Consider 2 types of objects:
m1 is number of object type 1
m2 is number of object type 2 Object type 2
The decision to choose object type 1 or 2 is independent of each other, that is,
• the decision to choose object type 1 does not affect the decision to choose
object type 2, and vice versa.
Sum rule: The number of ways to choose an object type 1 or an object type 2 is m1
+ m2
Object type 1 2 3 … k
Quality m1 m2 m3 … mk
The number of ways to choose one object from these k types of objects
= m1 +m2 +m3 +...+ mk 18
The product rule
• Consider 2 types of objects:
– m1 is number of object type 1
– m2 is number of object type 2
The decision to choose object type 1 or 2 is independent of each other, that is,
• the decision to choose object type 1 does not affect the decision to choose
object type 2, and vice versa.
Product rule: The number of ways to choose an object type 1 and an object type 2
is m1 m2
Object type1 Object type2
20
Analysis
# of possible plates = ?
1.2. The product rule
m1 * m2 * *mk
Analysis
# of possible plates = ?
Procedure:
Step 1: Step 3:
Step 2: Step 4:
Step 5:
Step 6:
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.
25
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 turnand
• 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: 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
27
1.2. The product rule
Example 3: How many functions are there from set A to set B?
2n
2
To define each function we have to make 3 choices, one for each element of A.
Each has 4 options (to select an element from B).
How many ways can each choice
4 x 4 x 4 be made?
30
More complex counting problems
31
More complex counting problems
Example 1: How many BASIC variables: single letter
or single letter+single digit
26 + 26 * 10=286
Comment: If your computer can try 200 million passwords per second, then how
long can the password be determined, so the intruder could penetrate this computer
system?
(2 684 483 063 360/200 000 000) / (60 * 60) hours
Nearly 4 hours!
34
IP Address Example (Internet Protocol v. 4)
35
How many valid IP addresses are there?
Wedding picture
Example 5: Consider a wedding picture of 6 people
– There are 10 people, including the bride and groom
(a) How many possibilities are there if the bride must be in the picture?
• Product rule: place the bride AND then place the rest of the party
• First place the bride:
She can be in one of 6 positions
• Next, place the other 5 people via the product rule:
There are 9 people to choose for the second person, 8 for the
third, etc. Total = 9*8*7*6*5 = 15120
Product rule yields 6 * 15120 = 90,720 possibilities
Wedding picture
Example 5: Consider a wedding picture of 6 people
– There are 10 people, including the bride and groom
b) How many possibilities are there if the bride and the groom must be
in the picture?
• Product rule: place the bride/groom AND then place the rest of the party
• First place the bride and groom:
She can be in one of 6 positions
He can be in one of 5 remaining positions
Total of 6*5 = 30 possibilities
• Next, place the other 4 people via the product rule:
There are 8 people to choose for the 3rd, 7 for the 4th, etc.
Total = 8*7*6*5 = 1680
Product rule yields 30 * 1680 = 50,400 possibilities
Wedding picture
Example 5: Consider a wedding picture of 6 people
– There are 10 people, including the bride and groom
c) How many possibilities are there if only one the bride or the groom must be in the
picture?
Sum rule: place only the bride
• Product rule: place the bride AND then place the rest of the party
– First place the bride
She can be in one of 6 positions
– Next, place the other 5 people via the product rule (We can’t choose the
groom!!!)
There are 8 people to choose for the second person, 7 for the third, etc.
Total = 8*7*6*5*4 = 6720
Product rule yields 6 * 6720 = 40,320 possibilities
OR place only the groom (hmm… quickly, how many?)
• Same possibilities as for bride: 40,320
Sum rule yields 40,320 + 40,320 = 80,640 possibilities
40
1.3. Tree diagram
In many cases, we can solve the counting problems by construction
of “Tree diagrams”.
Example 1: In a tennis match, the player wins the game if he is the first
player wins two sets. Question: What is the probability that player A
will win the game in 3 sets?
Solution: Construct tree diagram:
Thus, the answer is ???
2/6
1st set winner
44
2. Elementary combinatorial configuration
2.1. Permutation
2.2. Combination
45
2. Elementary combinatorial configuration
2.1. Permutation
2.2. Combination
The permutation that will open the lock is 942, we do care about the
order.
46
2. Elementary combinatorial configuration
2.1. Permutation
2.1.1. Permutation
2.1.2. k-permutation
2.1.3. Circulation permutation
2.1.4. Permutation of multisets
2.2. Combination
47
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.
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:
48
10×9×8×7 = 5,040 PINs are possible.
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)
Proof: 49
2.1.1. Permutation
• The number of permutations of any set with n elements is
P(n) = n! = n(n − 1) · · · 2 · 1
Proof: (by product rule)
Forming a permutation is an n-step operation:
• Step 1: Choose the 1st element ( n different ways).
• Step 2: Choose the 2nd element ( n-1 different ways).
• …
• Step n: Choose the nth element (1 way).
Based on the product rule, the number of permutations is
n (n-1) … 2 1 = n!
50
2.1.2. k-permutation
• A permutation of a set A of n objects x1, x2, ..,xn is an ordered
arrangement of the elements of A where each element appears
only once.
• A k-permutation of a set A of n objects x1, x2, ..,xn is an ordered
arrangement of k distinct elements of A.
1st 2nd 3rd k -th
x1 x2 x3 xn
x1
1st
x2
2nd
x3
3rd
x2
1st
x1
2nd
x3
3rd
51
2.1.2. k-permutation
• A permutation of a set A of n objects x1, x2, ..,xn is an ordered
arrangement of the elements of A where each element appears
only once.
• A k-permutation of a set A of n objects x1, x2, ..,xn is an ordered
arrangement of k distinct elements of A.
52
2.1.2. k-permutation
• A k-permutation of a set A of n objects x1, x2, ..,xn is an ordered
arrangement of k distinct elements of A.
The number of k-permutations of a set A with n=|A| elements is
P(n, k) = n(n−1)…(n−k+1) = n! / (n−k)!
D F C E B A
6!
P (6, 4) 6*5* 4 *3
2!
53
2.1.2. k-permutation
Example 2: Suppose you “have” time to listen to 10 songs on your daily
jog around campus. There are 6 A tunes, 8 B tunes, and 3 C tunes
to choose from.
Finally, suppose you still want 4 A, 4 B, and 2 C tunes, and the order
of the groups doesn’t matter, but you get dizzy and fall down if all the
songs by any one group aren’t played together.
How many playlists are there?
56
2.1.3. 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?
A
ABCDEF, B
BCDEFA, F
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 57
Hoán vị vòng tròn
• A circulation permutation of a set A of n objects is an ordered of all n
objects of A arranged as a circle; there is not the beginning object and
the ending object.
"The number of circulation permutations of an n-set equals n!/n = (n-1)!”
Proof ????
1 circulation
permutation of set
4 objects A, B, C, D
4 permutations of
set 4 objects A, B,
C, D
2.1.3. Circulation permutation
• A circulation permutation of a set A of n objects is an ordered of all n
objects of A arranged as a circle; there is not the beginning object and
the ending object.
"The number of circulation permutations of an n-set equals n!/n = (n-1)!”
59
2.1.3. Circulation permutation
Example 4: How many ways to arrange 3 couples in a round table with
alternating sex
60
2. Elementary combinatorial configuration
2.1. Permutation
2.1.1. Permutation
2.1.2. k-permutation
2.1.3. Circulation permutation
2.1.4. Permutation of multisets
2.2. Combination
61
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, 32, 43).
• 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).
• Let M be a multiset and |M| = n.
– A k-permutation of M is an ordered arrangement of k objects
selected from n objects of M.
– A n-permutation of M is called a permutation of M.
62
2.1.4. Permutation of multisets
• Let M be a multiset and |M| = n.
– A k-permutation of M is an ordered arrangement of k objects selected
from n objects of M.
– A n-permutation of M is called a permutation of M.
The total = rk
64
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.
• List the elements of M as
nr
Let X be the set of all permutations of S, and let Y be the set of all permutations of M.
There is a map f : X → Y, sending each permutation of S to a permutation of M by
removing the subscripts of the elements. Note that for each permutation of M there
are n1!, n2!,…,nr! ways to put the subscripts of the first, the second, ..., and the rth type
elements back, respectively.
Thus there are n1!n2! … nr! elements of X sent to Y by f. Therefore
|X| n!
| Y | 65
n1 ! n2 !...nr ! n1 ! n2 !...nr !
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
n! n
Cn .
m
68
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
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}
70
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
71
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} Order is not the matter
2. (a, c)
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)
n
k
C
k
(n k )! n
k !(n k )! 72
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} Order is not the matter
2. (a, c)
3. {b, c}
3. (b, a)
Ordered sequence
4. (b, c) n!
Pnk n(n 1)...( n k 1)
5. (c, a) (n k )!
6. (c, b)
Consider the set A of all k-permutation of n elements. Divide them into subset so that the two permutations
belonging to the same subset differ only in order. Obviously, these subsets are a partition on the set A, and each such
subset corresponds to a k-combination of n elements. There are the same number of k-permutation in each subset,
and is equal to k! (number of permutations). The number of subsets is the number of k-combination of n. According
to the sum rule, product of k! and the number of subsets is equal to the number of k-permutation of n, that is, equal
to n (n-1) ... (n - k + 1). Therefore, the number of k-combination of n is:
n(n 1)(n 2)...(n k 1) n! 73
or
k! k !(n k )!
k-combination of a set
Example: Consider the following nested loop
for a:=1 to n
for b:=1 to a-1
for c:=1 to b-1
[Statements]
endfor
endfor
endfor
Question: How many times the statements in the innermost loop will be
executed?
Answer: Each iteration corresponds to a triple of integers (a, b, c) where a > b >
c . The set of all this kind of triples corresponds to all 3-combinations of {1, …,
n}. Thus, the total number of iterations is C(n,3).
Difference between permutation and combination
• A combination is a set while a permutation is a sequence.
Let’s consider the difference between unordered and ordered selections:
1. Two ordered selections are the same if
– the elements chosen are the same;
– the elements chosen are in the same order.
• Ordered selections correspond to k-permutations.
• The number of all k-permutations is P(n, k) = n! / (n−k)!
2. Two unordered selections are the same if
– the elements chosen are the same (regardless of the order in which the
elements are chosen)
• Unordered selections correspond to k-combinations
• The number of all k-combinations is C(n, k) = n! / k! (n−k)!
75
Difference between permutation and combination
When dealing with any counting problem, we should ask
ourselves about the importance of order in the problem:
• When order is relevant, we think in terms of permutations
and arrangements and the rule of product.
• When order is not relevant, combinations could play a key
role in solving the problem.
76
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?
77
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
78
Binomial Coefficients
Binomial Theorem: Let x and y be variables, and let n be any nonnegative integer.
Then
n n j n
(x y ) x n
y j
j
j 0
79
Binomial Coefficients
n n j j
n
(x y ) x y
n
j
j 0
What is n? 17
17 17 8 9 8 9
What is j? 9 (3a ) 8
(2b ) 9
3 2 a b
9 9
What is x? 3a
What is y? 2b
80
Binomial Coefficients
n n n j j
(x y ) x y
n
j
j 0 What is coefficient
(a + b)2 = a2 + 2ab + b2 of a9b3 in (a + b)12?
(a + b)3 = a3 + 3a2b + 3ab2 + b3
(a + b)4 = a4 + 4a3b + 6a2b2 + 4ab3 + b4
A. 36
Pascal’s triangle B. 220
C. 15
D. 6
E. No clue
j
j 0
Powers of 2
Proof ???
Suppose you have a set of size
n. How many subsets does 2n
it have?
j
j 0
n n n j j
j 1 1 (1 1)n
j 0
n n n
j 2
j 0
Pascal Identity
A relationship between the entries in Pascal’s triangle: n n -1 n -1
j j -1 j
85
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 86
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) 87
2.3.3. Combinations of multisets
Example 1: 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 x 1 x 2 x 3 x 4 10
When the variables are nonnegative integers (xi ≥ 0)?
1 3 6 0 10
It is equivalent to
x1 x2 x6 x7 10, 0 xi , 1 i 6, 0 x7
which can be transformed to
y1 y2 y6 y7 9, y i 0 integer
where y i x i for 1 i 6
and y7 x7 1
Ans1: 10 locations for bars, pick 6 locations allowing repetitions [k = 6, n = 10]
k-combination with repetition C(6+10-1,6)=C(15,6) =5005
90
2.2. Combination
2.1.1. Definitions
2.2.2. Binomial coefficients
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
91
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 !
92
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.
93
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
Repetition
allowed (k-permutation with repetition
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
95
3. Inclusion-exclusion principle
3.1. Inclusion-exclusion principle
3.2. Derangement
96
3.1. Inclusion-exclusion principle
The inclusion–exclusion principle is an equation relating the sizes of two sets
and and their union. It states that if A and B are two (finite) sets, then
|A B| = |A| + |B| - |A B|
where:
Nk
1 i1 i2 ... ik m
| Ai1 Ai2 ... Aik |, k 1, 2,..., m
( 1) m 1 | Ai A j Ak ... Am | 98
3.1. Inclusion-exclusion principle
Example 1: 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|
99
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 of this
multiple can be represented as 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
100
3.1. Inclusion-exclusion principle
Example 2: There are 350 applicants to a job, and
• (i) 220 with major in CS
• (ii) 147 with major in Business
• (iii) 51 with major in both CS and Business
How many applicants have major neither in CS nor Business ?
Answer:
• A: set of applicants with major in CS
• B: set of applicants with major in Bussiness
AB:
Set of applicants with major both in CS and bussiness
AB:
Set of applicants with major in CS or business or both
|A B| = |A| + |B| - |A B| = 220 + 147 – 51 = 316
The desired answer : 350 – 316 = 34 101
3.1. Inclusion-exclusion principle
Example 3: In a class of students undergoing a computer course the following were
observed:
– Out of a total of 50 students: 30 know Java, 18 know C#, 26 know C, 9 know
both Java and C#, 16 know both Java and C, 8 know both C# and C, 47 know at
least one of the three languages.
From this we have to determine:
a. How many students know none of these languages?
b. How many students know all three languages?
a. 50 – 47 = 3
b.
A = All the students who know Java in class.
B = All the students who know C# in the class.
C = All the students who know C in class.
Students know all three languages, so we need to find |A B C|
We have to derive the inclusion-exclusion formula for three sets
|A B C| = (|A| + |B| + |C|) –(|B C| + |A B| + |A C|) + |A B C| 102
3.1. Inclusion-exclusion principle
– Out of a total of 50 students: 30 know Java, 18 know C#, 26 know C, 9 know
both Java and C#, 16 know both Java and C, 8 know both C# and C, 47 know at
least one of the three languages.
A = All the students who know Java in class.
B = All the students who know C# in the class.
C = All the students who know C in class.
Students know all three languages, so we need to find |A B C|.
|A B C| = 6
103
3.1. Inclusion-exclusion principle
Example 4: How many binary strings of length 10 such that either start
with 00 or end with 11?
Answer:
A: set of binary strings of length 10 that start with 00 00xxxxxxxx
B: set of binary strings of length 10 that end with 11 8 positions
Need to calculate |A B| Each position: 0/1
104
3. Inclusion-exclusion principle
3.1. Inclusion-exclusion principle
3.2. Derangement
105
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
107
3.2. Derangement
Let Ai = subset of permutations of {1, 2, …, n} such that the ith element = i in
the permutation.
• |Ai| = (n-1)! since if exactly one of the n objects is placed at its original
position, that leaves the other (n-1) objects to be freely permuted in (n-1)!
possible ways. Hence,
• |Ai Aj Ak | = (n-3)! since if exactly three of the n objects are placed at their
original positions, that leaves the other (n-3) objects to be freely permuted in
(n-3)! possible ways. Hence,
We have:
Dn = n! -
110
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.
113
4. Recurrence relations
4.1. Recurrence relations
4.2. Solve recurrence relations
114
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. Why???
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.
n = 1: f1 = 1
n = 2: f2 = 1
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.
Modeling with Recurrence Relations
Example 3: Let an denote the number of bit strings of length n that do
not have two consecutive 0s (“valid strings”). Find a recurrence relation
and give initial conditions for the sequence {an}.
Solution:
Idea: The number of valid strings equals the number of valid strings
starting with a 0 plus the number of valid strings starting with a 1.
• The sum rule: an = |A| + |B|.
• We have:
– A = {1??????} |A| = an-1
– B = {0??????} 2nd place is 1 |B| = an-2
• Therefore
an = an-1 +an-2, n > 2 fn = fn-1 + fn-2 , n > 2
a1 = 2; a2 = 3; f1=1, f2=1
This sequence satisfies the same recurrence relation as the Fibonacci sequence. 121
Since a1 = f3 and a2 = f4, we have an = fn+2
Modeling with Recurrence Relations
Example 4: The Tower of Hanoi, consists of three towers (a), (b), (c) together with
n disks of different sizes. Initially these disks are stacked on the tower (a) in an
ascending order, i.e. the smaller one sits over the larger one.
The objective of the game is to move all the disks from tower (a) to tower (c),
following 3 rules:
• Only one disk can be moved at a time.
• Only the top disk can be moved
• No large disk can be sit over a smaller disk.
Let hn denote the minimum number of moves needed to solve the Tower of Hanoi
problem with n disks. What is the recurrence relation for hn?
122
Tower a Tower c Tower b
Tower of Hanoi: n=5
The objective of the game is to move all the disks from tower (a) to tower (c), following
3 rules:
1. Only one disk can be moved at a time.
2. Only the top disk can be moved
3. No large disk can be sit over a smaller disk.
Tower a Tower c
Toán rời rạc
Tower b
Tower of Hanoi
• h1 = 1
• For n ≥ 2, we need to do 3 following steps to transfer all disks from tower (a) to tower (c):
(1) Move the top n - 1 disks (following the rules of the game) from tower (a) to tower (b)
(3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c), placing
them on top of the largest disk
Tower a Tower c
Toán rời rạc
Tower b
Tower of Hanoi
• h1 = 1
• For n ≥ 2, we need to do 3 following steps to transfer all disks from tower (a) to tower (c):
(1) Move the top n - 1 disks (following the rules of the game) from tower (a) to tower (b)
The problem of n-1 disks #moves = hn-1
(2) Move the largest disk to the tower (c)
#moves = 1
(3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c), placing
them on top of the largest disk
The problem of n-1 disks #moves = hn-1
hn = 2hn-1 + 1, n ≥ 2
h1 = 1
Tower a Tower c
Toán rời rạc
Tower b
Tower of Hanoi: n=5
(1) Move the top n - 1 disks (following the rules of the game) from tower (a) to tower (b)
The problem of n-1 disks #moves = hn-1
(2) Move the largest disk to the tower (c)
#moves = 1
(3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c) , placing
them on top of the largest disk
The problem of n-1 disks #moves = hn-1
Tower a Tower c
Toán rời rạc
Tower b
Tower of Hanoi
Find the formula to calculate hn for any natural number n and can avoid
the iteration.
hn = 2 hn−1 + 1
= 2 (2 hn−2 + 1) + 1 = 22 hn−2 + 2 + 1
= 22(2 hn−3 + 1) + 2 + 1 = 23 hn−3 + 22 + 2 + 1
…
= 2n−1 h1 + 2n−2 + … + 2 + 1
= 2n−1 + 2n−2 + … + 2 + 1 (because h1 = 1)
= 2n − 1
History “Tower of Hanoi”
Legend: a group of Eastern monks are the keepers of three towers on
which sit 64 golden rings. Originally all 64 rings were stacked on one
tower with each ring smaller than the one above. The monks are to move
the rings from this first tower to the third tower one at a time but never
moving a larger ring on top of a smaller one. Once the 64 rings have all
been moved, the world will come to an end.
Number of moves = 264-1 = 18 446 744 073 709 551 615
which would require more than five billion centuries!
Factorial(n);
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. T(n) = 1 + T(n-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 Find the formula to calculate T(n) for
T(n) = 1 + T(n − 1), n >1 any natural number n and can avoid the
This is known as a recurrence relation. 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
134
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)
{
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; 135
}
Example: Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
mid = (low + high) / 2;
if (A[mid]== key) return mid;
else if (key < A[mid])
key=33
return binsearch(low, mid-1, A, key);
else
return binsearch(mid+1, high, A, key);
}
else return -1;
}
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
136
Example: Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
mid = (low + high) / 2;
if (A[mid]== key) return mid;
else if (key < A[mid])
key=33
return binsearch(low, mid-1, A, key);
else
return binsearch(mid+1, high, A, key);
}
else return -1;
}
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
binsearch(0, 14, A, 33);
binsearch(0, 6, A, 33);
binsearch(4, 6, A, 33);
142
binsearch(4, 4, A, 33);
Example: Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
mid = (low + high) / 2;
if (A[mid]== key) return mid;
else if (key < A[mid])
key=33
return binsearch(low, mid-1, A, key);
else
return binsearch(mid+1, high, A, key);
}
else return -1;
}
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid binsearch(0, 14, A, 33);
binsearch(0, 6, A, 33);
binsearch(4, 6, A, 33);
143
binsearch(4, 4, A, 33);
Example: Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
mid = (low + high) / 2;
if (A[mid]== key) return mid;
else if (key < A[mid])
key=33
Value 33 is located @index=4
return binsearch(low, mid-1, A, key);
else
}
return binsearch(mid+1, high, A, key);
key=31??
else return -1;
}
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid binsearch(0, 14, A, 33);
binsearch(0, 6, A, 33);
binsearch(4, 6, A, 33);
144
binsearch(4, 4, A, 33);
4.1. Recurrence relations
Example 6. 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)
{
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 ?
145
4.1. Recurrence relations
Example 6. Performance of Recursive Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high) T(0) = ?
{
mid = (low + high) / 2;
binsearch(0, -1, A, key);
if (A[mid]==key) return mid; T(1) = ?
else if (key < S[mid])
return binsearch(low, mid-1, A, key); binsearch(0, 0, A, key);
else
return binsearch(mid+1, high, A, key);
binsearch(0, -1, A, key);
} binsearch(1, 0, A, key);
else return -1;
}
binsearch(0, n-1, A, key);
Let T(n): the number of times that binsearch is called in the worst case when array A has n elements
• T(0) = 1 Recurrence relation:
• T(1) = 2 T(n) = T(n/2) + 1
• T(2) = T(1) + 1 = 3 T(0) = 1
• T(4) = T(2) + 1 = 4 T(1) = 2
• T(8) = T(4) + 1 = 4 + 1 = 5 NGUYỄN KHÁNH PHƯƠNG
Bộ môn KHMT – ĐHBK HN 146
• T(n) = T(n/2) + 1
4. Recurrence relations
4.1. Recurrence relations
4.2. Solving recurrence relations
147
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
148
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.
149
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 (tuyến tính): 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 (thuần nhất): 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
150
A linear homogeneous recurrence relation of degree k with constant coefficients
151
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
if and only if r satisfying:
rn = c1rn−1 + … + ckrn−k, or (substract the right-hand side from the left
rk − c1rk−1 − … − ck = 0 and × by rk−n)
152
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.
153
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:
1 5 1 5
r1 ; r2
2 2
Leonardo Fibonacci
1170-1250
Muavre formula
Therefore
1
n n
1 5 1 5 , n 0.
Fn
5 2 2
155
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
a n 1r0n 2 nr0n
n = 0, 1, ..., where 1 , 2 are constants.
156
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
Recurrence relation: an = c1an−1 + … + ckan−k
Characteristic equation: rk − c1rk−1 − … − ck = 0
157
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
158
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
160
General case
Given linear homogeneous recurrence relation of degree k with constant
coefficients k
an ci ani
Its characteristic equation is: i 1
k
r k ci r k i 0
i 1
161
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
162
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
163
Example 5
Solving the recurrence relation
an = 7an-1 -16an-2 +12an-3, n 3;
a0 = 1, a1 = 5, a2 = 17.
Linear nonhomogeneous recurrence relation with constant coefficients
169
Tower a Tower c Tower b
Example 1: Tower of Hanoi
To solve the recurrence relation, we could use the substitution method:
an = 2 an−1 + 1
= 2 (2 an−2 + 1) + 1 = 22 an−2 + 2 + 1
= 22(2 an−3 + 1) + 2 + 1 = 23 an−3 + 22 + 2 + 1
…
= 2n−1 a1 + 2n−2 + … + 2 + 1
= 2n−1 + 2n−2 + … + 2 + 1 (as a1 = 1)
= 2n − 1
170
Example 1: Tower of Hanoi
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:
Example 2: Performance of Recursive Binary Search
int binsearch(int low, int high, int A[], int key)
{
if (low <= high)
{
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;
}
1 f(n/)
T(n/) T(n/) T(n/)
2 T(n/2) 2f(n/2)
T(n/2) T(n/2) T(n/2)
. .
. .
i i f(n/i)
. .
log n
• The value of T(n) is the sum of the cost of all level on the tree:
2.5. Recurrence relations and generating functions
185
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
186
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
187
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)
1. x4*x3*x5
c : c(x) = x2 + x3 + x4 + x5 (since 2 c 5) 2. x4*x4*x4
The coefficient of x12 in g(x) = a(x) b(x) c(x) 3. x4*x5*x3
=(x4+ x5+ x6+ x7+ x8) (x2+ x3+x4+ x5+ x6)(x2+ x3+x4+ x5) 4. x4*x6*x2
which is 14, is the solution 5. x5*x2*x5
g(x) is called a generating function. 6. x5*x3*x4
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 188
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.
189
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
is called the generating function for the given sequence.
• A finite sequence
a0, a1, a2, …, an
can be regarded as the infinite sequence
a0, a1, a2, …, an, 0, 0,… [set all terms higher than n to 0]
and its generating function
g(x) = a0 + a1x + a2x2 + … + anxn
is a polynomial.
Example 4: For the binomial coefficients we already know that:
n n
( x y ) C ( n, k ) x y
n k nk
( x 1) C ( n, k ) x k g ( x)
n
k 0 k 0
i 0
is called the generating function for the given sequence.
Some useful generating function:
Ex1: (1 xn+1) = (1 x)(1 + x + x2 + x3 + ...+ xn).
So (1-xn+1)/(1-x) = 1 + x + x2 + x3 + ...+ xn,
(1-xn+1)/(1-x) is the generating function for the sequence:
1, 1, 1, …, 1, 0, 0, 0, … . (where the first n+1 terms are 1)
Ex2: From Ex1, If n and |x| < 1, then 1 = (1 x)(1 + x + x2 + x3 + ...).
So, 1/(1-x) = 1 + x + x2 + x3 + ...
1/(1-x) where |x| < 1 is the generating function for the sequence:
1, 1, 1, …, 1, … .
Ex3: From Ex2, set x = ay |ay| < 1, then
1/(1-x) = 1/(1-ay) = 1 + ay + (ay)2 + (ay)3 + ….
191
1/(1-ay) where |ay| < 1 is the generating function for the sequence 1, a, a2, a3,…
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
Ex4: Example why multiplying generating functions is useful:
=(1+x+x2+x3+...)(1+x+x2+x3+...) where |x| < 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
192
2.5.2. Generating functions
Another way to show that 1/(1-x)2 is the generating function of the sequence 1,
2,3, 4…:
( )
1/(1-x)2 is the generating function for the sequence 1,2,3,4,….
Ex5: Show that x/(1-x)2 is the generating function for the sequence 0,1,2,3,….
We have
( )
= 0x0 + x1 + 2x2 + 3x3+….
193
2.5.2. Generating functions
Ex5: Show that x/(1-x)2 is the generating function for the sequence 0,1,2,3,….
We have
( )
= 0x0 + x1 + 2x2 + 3x3+….
Ex6: Show that (x+1)/(1-x)3 is the generating function for the sequence 12,22,32,42,….
From Ex5 we have:
( )
( )
194
2.5.2. Generating functions
Ex6: Show that (x+1)/(1-x)3 is the generating function for the sequence 12,22,32,42,….
From Ex5 we have:
( )
( )
Ex7: Show that x(x+1)/(1-x)3 is the generating function for the sequence 02, 12, 22, 32,
42, ….
195
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 ... ai xi
k
i 0
is called the generating function for the given sequence.
Example 1: Solve the recurrence relation an-3an-1 = n, n1, a0=1
Let 𝑔 𝑥 = 𝑎 𝑥 be the generating function for a0, a1,…, an
g ( x) a0 a1 x ... ak x k ... ai x i
i 0
xn
We find an by determining the coefficient of in g(x)
determining the coefficient of xn in each of the three summands 197
2.5.2. Generating functions
Example 1: Solve the recurrence relation an-3an-1 = n, n1, a0=1
)-1
( )-1
( )-1
( -1=
( (
( (
200
2.5.2. Generating functions
Example 2: Tower of Hanoi
Solve the recurrence relation an = 2 an−1 + 1, n1, a0=0
( (
201
2.5.2. Generating functions
Example 3: Solve the recursive relation an+2-5an+1+6an=2, n0, a0=3, a1=7
Let 𝑔 𝑥 = 𝑎 𝑥 be the generating function for a0, a1,…, an
202
2.5.2. Generating functions
Example 3: Solve the recursive relation an+2-5an+1+6an=2, n0, a0=3, a1=7
Let 𝑔 𝑥 = 𝑎 𝑥 be the generating function for a0, a1,…, an