Login
📚 Discrete Mathematics
Chapters ▾
⇩ Download ▾

2.6 Advanced Counting Using PIE

Stars and bars allows us to count the number of ways to distribute 10 cookies to 3 kids and natural number solutions to x + y + z = 11 , for example. A relatively easy modification allows us to put a lower bound restriction on these problems: perhaps each kid must get at least two cookies or x , y , z 2 . This was done by first assigning each kid (or variable) 2 cookies (or units) and then distributing the rest using stars and bars.

What if we wanted an upper bound restriction? For example, we might insist that no kid gets more than 4 cookies or that x , y , z 4 . It turns out this is considerably harder, but still possible. The idea is to count all the distributions and then remove those that violate the condition. In other words, we must count the number of ways to distribute 11 cookies to 3 kids in which one or more of the kids gets more than 4 cookies. For any particular kid, this is not a problem; we do this using stars and bars. But how to combine the number of ways for kid A, or B or C? We must use the PIE.

The Principle of Inclusion/Exclusion (PIE) gives a method for finding the cardinality of the union of not necessarily disjoint sets. We saw in Additive and Multiplicative Principles how this works with three sets. To find how many things are in one or more of the sets A , B , and C , we should just add up the number of things in each of these sets. However, if there is any overlap among the sets, those elements are counted multiple times. So we subtract the things in each intersection of a pair of sets. But doing this removes elements which are in all three sets once too often, so we need to add it back in. In terms of cardinality of sets, we have

| A B C | = | A | + | B | + | C | | A B | | A C | | B C | + | A B C |

.

For four or more sets, we do not write down a formula for PIE. Instead, we just think of the principle: add up all the elements in single sets, then subtract out things you counted twice (elements in the intersection of a pair of sets), then add back in elements you removed too often (elements in the intersection of groups of three sets), then take back out elements you added back in too often (elements in the intersection of groups of four sets), then add back in, take back out, add back in, etc. This would be very difficult if it wasn't for the fact that in these problems, all the cardinalities of the single sets are equal, as are all the cardinalities of the intersections of two sets, and that of three sets, and so on. Thus we can group all of these together and multiply by how many different combinations of 1, 2, 3, … sets there are.

Just so you don't think that these problems always have easier solutions, consider the following example.

Counting Derangements

The advanced use of PIE has applications beyond stars and bars. A derangement of n elements { 1 , 2 , 3 , , n } is a permutation in which no element is fixed. For example, there are 6 permutations of the three elements { 1 , 2 , 3 } :

123     132     213     231     312     321

. but most of these have one or more elements fixed: 123 has all three elements fixed since all three elements are in their original positions, 132 has the first element fixed (1 is in its original first position), and so on. In fact, the only derangements of three elements are

231  and  312

.

If we go up to 4 elements, there are 24 permutations (because we have 4 choices for the first element, 3 choices for the second, 2 choices for the third leaving only 1 choice for the last). How many of these are derangements? If you list out all 24 permutations and eliminate those which are not derangements, you will be left with just 9 derangements. Let's see how we can get that number using PIE.

Of course we can use a similar formula to count the derangements of any number of elements. However, the more elements we have, the longer the formula gets. Here is another example:

Counting Functions

We have seen throughout this chapter that many counting questions can be rephrased as questions about counting functions with certain properties. This is reasonable since many counting questions can be thought of as counting the number of ways to assign elements from one set to elements of another.

In Counting functions we saw how to count all functions (using the multiplicative principle) and in Counting injective functions we learned how to count injective functions (using permutations). Surjective functions are not as easily counted (unless the size of the domain is smaller than the codomain, in which case there are none).

The idea is to count the functions which are not surjective, and then subtract that from the total number of functions. This works very well when the codomain has two elements in it:

When there are three elements in the codomain, there are now three choices for a single element to exclude from the range. Additionally, we could pick pairs of two elements to exclude from the range, and we must make sure we don't over count these. It's PIE time!

You might worry that to count surjective functions when the codomain is larger than 3 elements would be too tedious. We need to use PIE but with more than 3 sets the formula for PIE is very long. However, we have lucked out. As we saw in the example above, the number of functions which exclude a single element from the range is the same no matter which single element is excluded. Similarly, the number of functions which exclude a pair of elements will be the same for every pair. With larger codomains, we will see the same behavior with groups of 3, 4, and more elements excluded. So instead of adding/subtracting each of these, we can simply add or subtract all of them at once, if you know how many there are. This works just like it did in for the other types of counting questions in this section, only now the size of the various combinations of sets is a number raised to a power, as opposed to a binomial coefficient or factorial. Here's what happens with 4 and 5 elements in the codomain.

We have seen that counting surjective functions is another nice example of the advanced use of the Principle of Inclusion/Exclusion. Also, counting injective functions turns out to be equivalent to permutations, and counting all functions has a solution akin to those counting problems where order matters but repeats are allowed (like counting the number of words you can make from a given set of letters).

These are not just a few more examples of the techniques we have developed in this chapter. Quite the opposite: everything we have learned in this chapter are examples of counting functions!

So far we have not used a function as a model for binomial coefficients (combinations). Think for a moment about the relationship between combinations and permutations, say specifically ( 9 3 ) and P ( 9 , 3 ) . We do have a function model for P ( 9 , 3 ) . This is the number of injective functions from a set of size 3 (say { 1 , 2 , 3 } to a set of size 9 (say { 1 , 2 , , 9 } ) since there are 9 choices for where to send the first element of the domain, then only 8 choices for the second, and 7 choices for the third. For example, the function might look like this:

f ( 1 ) = 5 f ( 2 ) = 8 f ( 3 ) = 4

.

This is a different function from:

f ( 1 ) = 4 f ( 2 ) = 5 f ( 3 ) = 8

.

Now P ( 9 , 3 ) counts these as different outcomes correctly, but ( 9 3 ) will count these (among others) as just one outcome. In fact, in terms of functions ( 9 3 ) just counts the number of different ranges possible of injective functions. This should not be a surprise since binomial coefficients counts subsets, and the range is a possible subset of the codomain.1

While it is possible to interpret combinations as functions, perhaps the better advice is to instead use combinations (or stars and bars) when functions are not quite the right way to interpret the counting question.

After another gym class you are tasked with putting the 14 identical dodgeballs away into 5 bins. This time, no bin can hold more than 6 balls. How many ways can you clean up?

_____

( 18 4 ) [ ( 5 1 ) ( 11 4 ) ( 5 2 ) ( 4 4 ) ] .

Based on the previous question, give a combinatorial proof for the identity:

( n k ) = ( n + k 1 k ) j = 1 n ( 1 ) j + 1 ( n j ) ( n + k ( 2 j + 1 ) k 2 j )

.

Illustrate how the counting of derangements works by writing all permutations of { 1 , 2 , 3 , 4 } and the crossing out those which are not derangements. Keep track of the permutations you cross out more than once, using PIE.

The 9 derangements are: 2143, 2341, 2413, 3142, 3412, 3421, 4123, 4312, 4321.

Let d n be the number of derangements of n objects. For example, using the techniques of this section, we find

d 3 = 3 ! ( ( 3 1 ) 2 ! ( 3 2 ) 1 ! + ( 3 3 ) 0 ! )

. We can use the formula for ( n k ) to write this all in terms of factorials. After simplifying, for d 3 we would get

d 3 = 3 ! ( 1 1 1 + 1 2 1 6 )

. Generalize this to find a nicer formula for d n . Bonus: For large n , approximately what fraction of all permutations are derangements? Use your knowledge of Taylor series from calculus.

Discrete Mathematics: An Open Introduction, 3rd edition, by Oscar Levin (discrete.openmathbooks.org), licensed under CC BY-SA 4.0; this adaptation is distributed under the same license. License: CC-BY-SA-4.0.