Counting unordered lists (CSCI 2824, Spring 2015)

On this page, we consider the problem of counting the number of ordered and unordered lists.

The materials correspond to Sect 5.4 of the textbook.

Recap on counting ordered lists

Example 1

How many ordered lists of 10 letters from {a,b,c} have exactly 3 a's?

Solution
  • Each subset of {1,ldots,10} of size 3 corresponds to a possible placement of the letter a. For instance, {1,3,4} corresponds to putting a in the 1st, 3rd and 4th position.

  • For the remaining 7 spaces, we can put either b or c.

  • The number of possible ways to fill the 7 spaces with b or c is 2^7.

  • Hence the total number of ordered lists of 10 letters from {a,b,c} with exactly 3 a's equals (# ways to put 3 a's among 10 positions) times (# ways to fill 7 spaces with b or c), that is, C(10,3)times 2^7.

Example 2

Similarly, the number of ordered lists of 10 English letters with exactly 3 a's is C(10,3)times 26^7.

Counting unordered lists

Example 3

How many ways can I distribute 10 candies among Alice, Bob and Carlos, so that each person has at least 1 candy?

Using the stars and bars technique

We can reason use the stars and bars diagram.

  • Start with a list of 10 stars.

  • Note that each possible distribution can be expressed by putting 2 bars among the stars, so that no bars are at the beginning/end of the lists, and no bars are next to each other.

  • The insertion of the two bars split the stars into three groups. We can think of the first group representing the candies for Alice, second for Bob and third for Carlos.

  • For example, ***|***|**** denotes the distribution where Alice gets 3 candies, Bob gets 3 candies and Carlos gets 4 candies.

  • The number of possible distributions is then equal to the number of possible ways to put 2 bars in 9 spaces, so the answer is C(9,2).

Example 4

How many ways can I distribute 10 candies among Alice, Bob and Carlos, with the possibility that someone can have no candies?

Using the stars and bars technique

Consider the following scenario:

  • We distribute 10+3=13 candies among Alice, Bob and Carlos, so that each person has at least one candy.

  • Then we take away one candy from each person. This is equivalent to distributing 10 candies among Alice, Bob and Carlos, where someone can have no candies.

Using the reasoning from Example 3, we see that there are C(12,2) possible ways of distributions.

Example 5 (NEW!)

How many ways can I distribute 20 candies among Alice, Bob and Carlos, so that each person has at least 3 candies?

Answer

To ensure that everyone gets at least 3 candies, we can do the following:

  • Distribute 20 - 3times 3 = 11 candies among Alice, Bob and Carlos.

  • There are now 9 candies left!

  • Give each person 3 candies.

So the number of distinct distributions is equal to the number of ways to distribute 11 candies among 3 people, which is

    C(11+3-1, 3-1)=C(13,2) = 78.

Example 6 (NEW!)

What is the cardinality of the set

 {(w,x,y,z)inmathbb{Z}^4 ,|, w+x+y+z = 30 ,land, w,x,y,z>0}?
Answer

Counting the number of elements of the above set is the same as counting the number of ways to distribute 30 apples among 4 people so that everyone gets at least 1 apple, with:

  • w : # apples the 1st person gets

  • x : # apples the 2nd person gets

  • y : # apples the 3rd person gets

  • z : # apples the 4th person gets

So then we can use the stars and bars method as in Example 3, and the answer would be

C(20-1, 4-1)=C(19,3)=969.

Example 7 (NEW!)

What is the cardinality of the set

 {(w,x,y,z)inmathbb{Z}^4 ,|, w+x+y+z = 30 ,land, w,x,y,zgeq0}?
Answer

Counting the number of elements of the above set is the same as counting the number of ways to distribute 30 apples among 4 people (though someone could get none), with:

  • w : # apples the 1st person gets

  • x : # apples the 2nd person gets

  • y : # apples the 3rd person gets

  • z : # apples the 4th person gets

So then we can use the stars and bars method as in Example 4, and the answer would be

C(20+4-1, 4-1)=C(23,3)=1771.

Example 8 (NEW!)

I have a box of dollar bills. In this box, there are 10 $1 bills, 20 $10 bills and 30 $20 bills. From the box, I grab 5 bills all at onces. How many possible outcomes are there?

Note that I can record all the possible results in a table like this:

$1 $10 $20
2 1 1
1 0 3
vdots vdots vdots

For instance, the first row says I get two $1 bills, one $10 bill and one $20 bill; the second row says I get one $1 bill and three $20 bills. (I probably should list my results in a more systematic way than the above though!)

Note that in this table each row contains three numbers that sum to 5. The point is that we can think of this counting problem as finding all the possible ways to distribute 5 objects (in this case bills) among 3 different bins.

Answer

Hence the answer can be obtained using the stars and bars technique:

 C(5+3-1, 3-1) = C(7,2)=21.

Example 9 (NEW!)

From a big of 8 (identical) red marbles, 5 (identical) green marbles 3 (identical) blue marbles, we grab 4 marbles all at once. How many different outcomes (in terms of color combinations) are there?

Answer

Like Example 8, this counting problem is equivalent to counting the number of ways to distribute 4 objects among 3 different bins (for the three different colors), with the restriction that the last bin (for the color green) can hold at most 3 objects.

Without any restriction, there are C(4+3-1, 3-1)=C(6,2)=15 ways to distribute 4 objects in 3 different bins.

There is one outcome that we don't want: that all 4 objects go into the green bin. Hence the answer is 15-1=14.

Placing n objects in r bins

In general, suppose we have n objects and r bins, with ngeq0 and rgeq1. Let T(n,r) be the number of ways of distributing the n objects in the r bins. Then using the above stars-and-bars technique, we get that

T(n,r) = C(n+r-1,r-1).

Note that the number of ways of distributing n objects in r bins is also equal to the cardinality of the following set:

mathcal{T}(n,r)triangleq left{ ( x_1,x_2,ldots,x_r)in mathbb{Z}^r  |  left(,forall,i (x_igeq0),right ) land (  x_1+x_2+cdots+x_r= n ) right}.

Finding T(n,r) via recursive counting

We studied how to find T(n,r) using the stars-and-bar technique. Now we consider an alternative method: recursive counting.

Simple cases:

  • If n=0, then all the bins have 0 objects. Hence T(0,r)=0 for all rgeq1.

  • If r=1, then there is only one bin for all the objects to go to. Hence T(n,1)=1.

Now suppose that ngeq1 and rgeq2. We claim that T(n,r)=T(n-1,r)+T(n,r-1).

Consider the number of objects placed in the last bin.

  • Case 1: nothing is placed in the last bin.

    • In this case, all the objects are placed in the other r-1 bins.

    • There are T(n,r-1) ways to place n objects in the first r-1 bins.

  • Case 2: At least one object is placed in the last bin.

    • This case is equivalent to trying to place n-1 objects in the r bins. (After that we place 1 object in the last bin.)

    • There are T(n-1,r) ways to place n-1 objects in the r bins. Since the two cases are mutually exclusive and all the placements of objects must fall under exactly one of the two cases, we have that T(n,r)=T(n-1,r)+T(n,r-1).

This is an example of recursive counting. To compute T(n,r) using the recursive formula, we can use the following program:

Program for computing T

 int T(int n, int r){
    assert( n >= 0 );
    assert( r >= 1 );
    /* Base Case */
    if ( r == 1) return 1;
    if ( n == 0) return 1;
    /* Recurrence */
    return T(n-1,r) + T(n,r-1);
 }

Here are the comparison of run times (in seconds) on some instances:

n r Time used by recursion Time used by explicit formula
20 10 0.12 s 1.00e-06 s
30 10 2.10 s 1.00e-06 s
40 10 22.0 s 1.00e-06 s