Combinatorics - CSE

advertisement
Combinatorics (or How to Count)
Version 1.0
1
Counting
Combinatorics involves counting objects or arrangements specified by some constraints. For example, suppose you
invite 10 friends to a party and each person as he/she arrives shakes the hands of all those already present, how many
handshakes are there in all? Suppose of the 10, six were boys and four were girls and they dance in boy-girl pairs then
how many pairs can be formed? The means to answer similar and much harder to count questions forms the subject
matter of combinatorics.
Combinatorics unlike some other branches of mathematics does not have many grand unifying theorems. One has to
apply a few basic principles in clever ways to the problem at hand to get an answer. Often, one solves the problem in
two different ways to check if we get the same answer to convince ourselves that we have counted correctly.
2
The sum and product principles
Principle 2.1 (Sum rule). If object O1 can be chosen in m ways and object O2 can be chosen in n ways the choice
of “either O1 or O2 (O1 ⊕ O2 )” can be done in (m + n) ways.
Principle 2.2 (Product rule). If object O1 can be chosen in m ways and object O2 can be chosen in n ways the choice
of “O1 and O2 (O1 ∧ O2 )” can happen in mn ways.
The above rules can be generalized to more than two objects.
While these rules appear very simple and obvious they are at the heart of most combinatorial arguments. We start by
discussing well known results relating to permutations and combinations both with and without replacements. One
way to differentiate a permutation and a combination is to think of a permutation as an ordered set and a combination
as a normal set which is unordered. This means that when treated as an ordered set or sequence {1, 2} is distinct
and different from {2, 1}. But they are counted as the same set when treated as a normal set where element order is
unimportant.
Theorem 2.1. Given n distinct objects the number of ordered sets of size k, where k ≤ n, without repetition is
(n × (n − 1) . . . × (n − k + 1) and with repetition1 is nk .
Proof. The proof is a straight forward application of the general product rule. Imagine we have k positions. The first
can be filled in n ways the second in (n − 1) ways and so on till the k th in (n − k + 1) ways. From the general product
rule the number of ways we can choose k objects is the product of the above (n × (n − 1) . . . × (n − k + 1). We will
n!
denote this by n Pk . This is often written as (n−k)!
.
1 Note that a set must have distinct elements. So, when we want repetition we are really talking about multi-sets. But we will abuse
terminology and continue to call them sets.
1
When repetition is allowed each of the k positions can be filled up in n ways giving us nk by the product rule. When
repetition is allowed we will write n P k .
The above calculates number of k-arrangements given n distinct objects. This corresponds to ordered sets. To
calculate the number of sets of size k that are possible without repetition given n distinct objects where we count all
arrangements with the same choice of objects only once we have to divide it by k Pk (which is k!) since every k-sized
set is counted k! times. So, we have the theorem:
Theorem 2.2. The number of k sized sets of n distinct objects without repetition where k ≤ n is n Pk /k! or
(We write this number as n Ck .)
n!
k!(n−k)! .
Notice that every time we have a k-sized set we are also creating an (n − k)-sized set of the elements that are not in
the set. So, the number of such sets must be the same as the k-sized sets giving us an important property of n Ck .
Corollary 2.1.
n
Ck =n Cn−k .
Before looking at the number of sets we can get with repetition let us discuss a simpler problem. Suppose we have m
different types of objects with ni , i = 1, m of each type. Let n = n1 + n2 + . . . nm . How many ordered sets can we
get of size n. We cannot directly use theorem 2.1 since for a particular arrangement changing the places of objects of
the same type among themselves does not give a new distinguishable arrangement. This means that for each type i of
objects we have an arrangement being counted ni ! times. This means we should divide n Pn by each ni ! for i = 1, m.
This gives us our next result.
Theorem 2.3. The number of n-sized ordered sets of objects of m distinct types with ni , i = 1, m objects of each type
and n = n1 + n2 + . . . + nm is n Pn /n1 !n2 !. . . nm ! or n1 !n2n!
!...nm ! . (We write this number as P (n1 , n2 , . . . , nm ).
We observe that n Ck = P (k, n − k). So, the number of k-combinations of n distinct elements is the same as the
number of arrangements of n elements of two distinct types with n1 = k and n2 = n − k.
3
Principle of bijection
For any sets A and B if we can find a bijection f : A B then we know that card(A)=card(B). For counting problems
where A, B are finite this is very useful. Often it is easier to count the cardinality of a set, say A, by showing a
bijection between A and another set B whose cardinality we already know or which is more easy to count.
Principle 3.1. If there is a bijection f : A B then card(A)=card(B). So, to count the number of elements in A it
is enough to count the number of elements in B.
We apply the bijection principle to find the number of sets of size k given n distinct objects with repetition allowed.
Imagine we have n separate bins created by (n − 1) markers (let us say the marker is the digit 1). These n bins
correspond to the n distinct types of objects. Assume each bin has a null string at the start. We append a 0 to the
string in the j th bin when we encounter an object of type j in the set. We will have exactly k 0s distributed across
the n bins for a set of size k. Concatenate all the strings of 0s as well as the 1s that separate each bin. We then have
a binary string of size (n − 1 + k) with exactly k 0s and (n − 1) 1s. This is clearly a bijection between each k-sized set
and the corresponding binary string of length (n − 1 + k). But we already know how to count such binary strings by
theorem 2.3. We have two types, 0 and 1 and n1 = k, n2 = n − 1 and by theorem 2.3 the number of such strings is
(n−1+k)
P (k, n − 1) = (n−1+k)!
Ck = (n−1+k) C(n−1) . So, we have the theorem:
k!(n−1)! =
Theorem 3.1. The number of sets (or combinations) of size k given n distinct objects with repetition allowed is
Ck . (We denote this by n C k .)
(n−1+k)
2
4
Principle of inclusion-exclusion
Often we know the counts of certain individual sets and overlapping sets and we need to count the number of elements
of either an individual set or an overlapping set. Suppose we have n objects where nA objects have property A, nB have
property B and nAB have both properties A and B. We have to find the number of objects that have neither property
A nor B. The number that have only property A is nAonly = nA − nAB. Similarly, with only property B, nBonly =
nB − nAB . So, the number that have neither property A nor B is (n − nAonly − nBonly − nAB = n − nA − nB + nAB .
This formula can be generalized.
Theorem 4.1. Let S be a set of objects. Let Si ⊆ S, Ti = 1, n be subsets that have properties p1 , p2 , . . . , pn respectively.
Let I ⊆ {1, 2 . . . , n} be an index set and define SI = i∈I Si . If I = {i} then SI = Si . S∅ = S. SI contains all objects
that have all the properties pi , i ∈ I. The principle of inclusion and exclusion states that the number of objects that
have none of the properties p1 , p2 , . . . , pn (denoted by |Snone |) is given by:
X
|Snone | =
(−1)|I| |SI |
I⊆{1,2,...,n}
. We write |A| for the cardinality of any set A.
Proof. The proof is by induction on the number of properties. It is true for n = 1 since:
X
|Snone | = |S| − |S1 | = |S∅ | − |S1 | =
(−1)|I| |SI |
I⊆{1}
P
Assume it holds upto (n − 1) that is |Snone | = I⊆{1,...,n−1} (−1)|I| |SI |. Now suppose there are n properties. For
simplicity let us write |SI | = NI and let S{1,...,n} be the set of objects that do not have properties p1 to pn . Note that
Nnone = N{1,...,n−1} − N{1,...,n−1,n} . That is the number of objects that do not have any of the properties p1 , . . . , pn
is equal to the number of objects that do not have properties p1 , . . . , pn−1 and possibly have property pn minus the
number of objects that have only property pn .
We can use the induction principle to write:
N{1,...,n−1} = N − N{1} − . . . − N{n−1} + N{1,2} + . . . + (−1)n−1 N{1,...,n−1}
(1)
Similarly, we can write:
N{1,...,n−1,n} = N{n} − N{1,n} − N{2,n} − . . . − N{n−1,n} + N{1,2,n} + . . . + (−1)n−1 N{1,...,n−1,n}
(2)
Subtracting equation (2) from equation (1) gives:
Nnone = N − N{1} − . . . − N{n−1} + N{1,2} . . . + (−1)n−1 N{1,...,n−1} − (−1)n−1 N{1,...,n−1,n}
= N − N{1} − . . . − N{n−1} + N{1,2} . . . + (−1)n−1 N{1,...,n−1} + (−1)n N{1,...,n−1,n}
This is indeed the formula for the number of objects that do not have any of the n properties p1 to pn .
5
Pigeonhole principle
This very simple and obvious principle has surprisingly deep applications. See the solved examples section for some
example applications.
Principle 5.1. If n objects are distributed in m bins where n > m then at least one bin contains more than one object.
3
6
Binomial coeffcients and their properties
We assume the binomial theorem as given:
Theorem 6.1.
(a + b)n =
n
X
n
Ck an−k bk
k=0
Binomial coefficients are a way use functions, polynomials in this case, for counting - a technique called generating
functions that we study in a later section.
The binomial coefficients n Ck , n ≥ k have some useful and important properties:
Theorem 6.2 (Properties of binomial coefficients).
1.
n
Ck = n Cn−k .
2.
n
Ck+1 = n−1 Ck+1 + n−1 Ck .
3.
n
C0 < n C1 < n C2 < . . . < n Cd n−1 e = n Cb n2 c .
2
n
4. k. Ck = n.
n−1
Ck−1 .
5. k.n Ck = (n − k + 1).n Ck−1 .
6.
n
C0 + n+1 C1 + n+2 C2 + . . . + n+k Ck = n+k+1 Ck .
7.
n
Cn + n+1 Cn + n+2 Cn + . . . + n+k Cn = n+k+1 Cn+1 .
8.
n
C0 + n C1 + n C2 + . . . + n Cn = 2n .
9.
n
C0 − n C1 + n C2 − n C3 + . . . + (−1)nn Cn = 0.
10. 1.n C1 + 2.n C2 + . . . + n.n Cn = n.2n−1 .
11.
n
Ck is divisible by n if n is prime and 1 ≤ k ≤ n − 1
Proof.
n!
1. This follows directly from the equation n Ck = k!(n−k)!
. A combinatorial argument is to see that choosing k
items from n is the same as leaving out n − k items from n items. So the number of ways one can do this should
be same.
2. Designate one of the n objects as a special object. Then all (k + 1)-combinations can be partitioned into those
that contain the special object and those that dont. The sum of these should give us all (k+1)-combinations (sum
principle). The number of combinations that include the special object is n−1 Ck . We choose a k-set from (n − 1)
objects not containing the special object and add the special object to this set to give a (k + 1)-combination.
The number of combinations that exclude it is simply n−1 Ck+1 . This proves the result.
3. We have n Ck /n Ck+1 =
k+1
n−k .
The ratio will be 1 when k =
n−1
2 .
For k < (n − 1)/2, k ∈ N0 the ratio is < 1 so
Ck < Ck+1 . k is a non-negative integer so when it is odd n = 2m + 1 we get d (2m+1)−1
e = m = b 2m+1
2
2 c. And
2m−1
2m
n
n
when n is even (n = 2m) again we have d 2 e = m = b 2 c. So, Cd n−1 e = Cb n2 c . Hence proved.
n
n
2
4
4.
k.n Ck =
n.(n − 1)!
k.n!
=
= n.n−1 Ck−1
k!(n − k)!
(k − 1)!((n − 1) − (k − 1))!
k.n Ck =
k.n!
(n − k + 1)n!
=
= (n − k + 1).n Ck−1
k!(n − k)!
(k − 1)!(n − k + 1)!
.
5.
6. Since n C0 = n+1 C0 we have by repeated use of property 2:
n
C0 + n+1 C1 + . . . + n+k Ck = n+1 C0 + n+1 C1 + n+2 C2 + . . . + n+k Ck
= n+2 C1 + n+2 C2 + . . . + n+k Ck
=
n+3
C2 +
n+3
C3 + . . . +
n+k
using prop 2.
Ck
...
...
= n+k Ck−1 + n+k Ck
= n+k+1 Ck
7. This follows directly by using property 1 on all terms in property 6.
8. Set a = b = 1 in the binomial theorem (6.1).
9. Set a = 1 and b = −1 in the binomial theorem.
10.
1.n C1 + 2.n C2 + . . . + n.n Cn =
n
X
k n Ck
k=1
= n.
n−1
X
n−1
Ck−1
n−1
Ck
using property 4.
k=1
= n.
=
n−1
X
k=0
n.(n C0
= 2.2n−1
11.
7
+ n C1 + . . . + n Cn−1 )
using property 8.
n!
. Now n divides n! and n is relatively prime to both k! and (n − k)! when 1 ≤ k ≤ n − 1. So, n
Ck = k!(n−k)!)
n
divides Ck when n is prime.
n
Recurrence relations, generating functions and counting
A recurrence relation (or just recurrence) is a formula that gives the value of a functin F (n) in terms of the values of
the function on earlier values of the argument i.e. in terms of F (n − 1), F (n − 2) etc. and the value of the function
on base values of the argument (like F (0) F (1) etc.); usually n ∈ N0 . The following are examples of recurrences:
5
Example 7.1. The Fibonacci sequence: f (n) = f (n − 1) + f (n − 2) with f (0) = 1, f (1) = 1.
Example 7.2. Let S(n) be the number of subsets of a set with n elements that is the size of P(A) where card(A) = n.
One can create a recurrence for S(n) by arguing as follows: the number of subsets is S(n − 1) plus the number of
subsets obtained by adding the nth element to each of the earlier subsets. That is S(n) = 2.S(n − 1), with S(0) = 1.
Such recurrences are very common when we try calculate the complexity of algorithms. Ideally, solving the recurrence
will give us a closed form formula for F (n) which is usually the count of something - for example the size of the power
set of a set with n elements in the example above.
One simple way to solve a recurrence is by unfolding and substitution. Consider the recurrence in example 7.2 and let
us unfold it by substitution. We get:
S(n) = 2.S(n − 1)
= 2.2.S(n − 2)
...
= 2k .S(n − k)
...
= 2n .S(0)
= 2n
since S(0) = 1.
Consider another example of unfolding and subsitution. Let the recurrence be a(n) = ca(n − 1) + f (n), n ≥ 1; c, a(0)
are defined constants and f is a known function. Unfolding gives us:
a(n) = ca(n − 1) + f (n)
= c(ca(n − 2) + f (n − 1)) + f (n) = c2 a(n − 2) + cf (n − 1) + f (n)
= c2 (ca(n − 3) + f (n − 2)) + cf (n − 1) + f (n) = c3 a(n − 3) + c2 f (n − 2) + cf (n − 1) + f (n)
...
= ck a(n − k) +
k−1
X
ci f (n − i)
i=0
...
n
= c a(0) +
n−1
X
ci f (n − i)
i=0
= cn a(0) +
n
X
cn−i f (i)
i=1
This is a recurrence that can be used to find the complexity of numerous recursive programs. For example, the tower
of Hanoi problem has the recurrence a(n) = 2.a(n − 1) + 1, with a(0) = 0 where a(n) is the number of disk moves
required to solve it. Using the above equation with c = 2 and f as the constant function 1 we get
a(n) = 2n a(0) +
n
X
2n−i
i=1
=
n
X
2n−i
since a(0) = 0.
i=1
= 1 + 2 + 22 + . . . + 2n−1
= 2n − 1
6
7.1
Generating functions
P∞
A power series is a series defined as: A(x) = n=0 an xn . The coefficients of such a series, the ai s, can often be used
for counting. For this reason such functions are called generating functions. They can be used to solve recurrences
because the sequence of values a(0), a(1), . . . , of the recurrence can be identified with the coefficients of the power
series, namely a0 , a1 , . . . ,.
We deal with powers series as formal objects and not as analytic ones. So, in particular, we will not be concerned
with whether or not the series converges. To see how generating functions are used to solve recurrences consider the
tower of Hanoi recurrence discussed earlier:
n ≥ 0, a(0) = 0
P∞
. We use the power series A(x) = n=0 an xn . Multiply both sides of the recurrence by xn and sum over all valid
values i.e. n ≥ 0. This gives us:
a(n + 1) = 2a(n) + 1,
∞
X
an+1 xn = 2
n=0
∞
X
an xn + xn
n=0
lhs = a1 + a2 x + a3 x2 + . . .
1
= [(a0 + a1 x + a2 x2 + . . .) − a0 ]
x
A(x)
=
since a0 = 0.
x
∞
X
rhs = 2A(x) +
xn
n=0
= 2A(x) +
1
1−x
2nd term sum of a geometric series.
x
(1 − x)(1 − 2x)
2
1
= x[
−
]
1 − 2x 1 − x
= x[2(1 + 2x + 22 x2 + . . .) − (1 + x + x2 + x3 + . . .)]
A(x) =
= x[(2 − 1)1 + (22 − 1)x + (23 − 1)x2 + (24 − 1)x3 + . . .]
= x + (22 − 1)x2 + (23 − 1)x3 + . . .
A(x) = a0 + a1 x + a2 x2 + . . . = x + (22 − 1)x2 + (23 − 1)x3 + . . .
a1 x + a2 x2 + a3 x3 + . . . = (2 − 1)x + (22 − 1)x2 + (23 − 1)x3 + . . .
since a0 = 0.
Comparing coeffiecients for powers of x we get an = 2n − 1. This is a very general method for solving recurrences and
can often handle cases that cannot be solved by unfolding and substitution.
Steps for solving recurrences using generating functions:
1. Define the range of legal values of the recurrence variable in the recurrence relation.
P∞
2. Choose the generating function e.g. A(x) = n=0 an xn .
3. Multiply both sides of the recurrence with xn and sum over all legal values of the recurrence variable.
4. Write the lhs and rhs in terms of A(x).
5. Solve for A(x).
7
6. To get a formula for an expand the solution of A(x) as a series and match terms for xn . You may have to use
partial fractions here. This may not always be possible.
Since we are treating power series as formal objects we can define operationsPon them and try and construct
an algebra
P∞
∞
of power series. Consider the following operations on power series A(x) = n=0 an xn and B(x) = n=0 bn xn :
a) Equality: A(x) = B(x) iff an = bn for n ≥ 0.
P∞
b) Multiply by scalar: cA(x) = n=0 (can )xn .
P∞
c) Addition: A(x) + B(x) = n=0 (an + bn )xn .
P∞
P∞
d) Multiplication: A(x)B(x) = n=0 cn xn , cn = i+j=n ai bj
Multiplication is the most useful operation here. Consider A(x) = a0 + a3x + a4 x4 + a8 x8 other ai s are 0, similarly let
B(x) = b0 + b4 x4 + b5 x5 + b8 x8 other bi s are 0. In A(x)B(x) the coefficient of x8 is (a0 b8 + a3 b5 + a4 b4 + a8 b0 ), similarly
x5 ’s coefficient is a0 b5 . If all non-zero ai s and bi s are 1 then x8 ’s coefficient is the number of non-negative integer
solutions of ea +eb = 8 where ea , eb stand for possible exponents of x in A(x) and B(x) respectively i.e. ea ∈ {0, 3, 4, 8}
and eb ∈ {0, 4, 5, 8}. More generally, this allows us to count the number of non-negative integer solutions of ea + eb = k
where the allowed exponents describe constraints on ea , eb .
This can be generalized. If Ai (x), i = 1..m are m power series where coefficients are only 0 or 1 then the coefficient
of xk in product A1 (x) . . . Am (x) can be interpreted as the non-negative integer solutions of
e1 + e2 + . . . + em = k
where constraints on ei are determined by which coefficients of Ai (x) are non-zero.
Consider an application of the above: find the coefficient of x25 in (1 + x3 + x8 )10 . This translates to the following
counting problem: find the number of non-negative integer solutions to e1 + e2 . . . + e10 = 25 where ei ∈ {0, 3, 8}, i =
1..10. We see that the only way to get 25 is as 3 × 3 + 2 × 8 + 5 × 0. So, the number of ways of doing this will be:
10!
25
3!2!5! and this will the coefficient of x .
P∞
P∞
Given the power series A(x) = n=0 an xn and B(x) = n=0 bn xn we say B(x) is a multiplicative inverse of A(x) if
1
.
A(x)B(x) = 1. We write B(x) = A(x)
We have
A(x)B(x) = a0 b0 + (a0 b1 + a1 b0 )x + (a0 b2 + a1 b1 + a2 b0 )x2 + . . .
=1
By matching coefficients and assuming that A(x) is a known series we get:
1
a0
a1
b1 = − 2
a0
2
a − a2 a0
b2 = 1 3
a0
b0 =
...
Note that A(x) has a multiplicative inverse only if a0 6= 0.
More generally B(x) divides A(x) if there exists a D(x) such that A(x) = B(x)D(x) or
A(x) has a multiplicative inverse, say B(x) and A(x) divides any C(x) since
8
C(x)
A(x)
A(x)
B(x)
= D(x). If a0 6= 0 then
is C(x)B(x).
If a0 = 0 and k is the smallest positive integer such that ak 6= 0 then write A1 (x) = A(x)
making ak the constant term of
xk
C1 (x)
C(x)
k
A1 (x). Then A(x) will divide C(x) only if x is a factor of C(x). Let C1 (x) = xk . Then C(x)
A(x) = A1 (x) = C1 (x)B1 (x)
where B1 (x) is the multiplicative inverse of A1 (x).
Example 7.3. Let A(x) = 1 − x, B(x) =
and get:
1
1−x .
Then we can calculate b0 , b1 , . . . using the earlier derived formulae
1
=1
a0
a1
b1 = − 2 = 1
a0
2
(−1)2
a − a2 a0
=
=1
b2 = 1 3
a0
13
b0 =
...
bn = 1
P∞ n
1
So we get B(x) = 1−x
=
can get several more power series. Replace x by ax to get:
n=0 x . From here we
P
P
P∞
∞
∞
1
1
1
n n
n n
n n n
=
a
x
,
=
(−1)
x
,
=
n=0
n=0
n=0 (−1) a x .
1−ax
1+x
1+ax
Let B1 (x), . . . , Bm (x) be respective multiplicative inverses of A1 (x), . . . , Am (x). Then B1 (x).B2 (x). . . . .Bm (x) is a
multiplicative inverse of A1 (x).A2 (x). . . . .Am (x). So, for n > 0:
!n
∞
∞
X
X
1
k
n−1+k
=
x
=
C k xk
(1 − x)n
k=0
k=0
We have a counting situation similar to the one earlier. We need the coefficient of xk which we have written as
n−1+k
Ck . How did we get this? Notice that we need to find the number of non-negative solutions of e1 + . . . + en = k,
ei = 1. Note that this is the same as k-combinations of n distinguishable objects with repetition. By theorem 3.1 we
get n−1+k Ck . By suitable substitutions we get the following:
∞
X
1
n−1+k
Ck (−1)k xk
=
n
(1 + x)
1
=
(1 − ax)n
k=0
∞
X
n−1+k
Ck ak xk
k=0
∞
X
1
n−1+k
=
Ck (−1)k ak xk
n
(1 + ax)
1
=
(1 − xn )
1
=
(1 + xn )
k=0
∞
X
xnk
k=0
∞
X
(−1)k xnk
k=0
1
1
=
(a − x)
a(1 −
∞
x)=
a
1 X xk
a
ak
k=0
∞
1 X xk
1
=−
(x − a)
a
ak
k=0
We briefly discuss how partial fractions are used when we use power series to solve recurrences. Let A(x) = (x −
α1 )m1 .(x − α2 )m2 . . . . .(x − αk )mk . Then C(x)
A(x) where C(x) is a polynomial with degree less than that of A(x) can be
9
written as:
a1m1
akmk
a11
ak1
+ ... +
+ ... +
+ ... +
(x − α1 )m1
(x − α1 )
(x − αk )mk
(x − αk )
The values of a11 to akmk can be calculated by equating the terms from the lhs and rhs for powers of x after multiplying
both sides by (x − α1 )m1 . . . (x − αk )mk .
8
Solved examples
10
Download