Counting 'n' objects

Community Article Published December 20, 2023

CS109 taught me that counting matters. To really appreciate probability and its use cases, one needs to count (the sample space and the event space). Chris Piech, the instructor of the course really dives deep into the intuitions of counting and comes up with some foundational blocks. This blog post would be my way of teaching what I understood. For a more detailed analysis please refer to the course (freely available in YouTube).

Counting 'n' objects

Two rules for counting

Step rule of counting

Imagine an experiment with two options, flipping a coin (heads or tails) and rolling a die (six possibilities). If the options don't influence each other (flipping the coin doesn't affect the die), the total number of possibilities for the whole experiment is simply the number of ways we can flip the coin (2) multiplied with the number of ways we can roll the die (6).

In other words, if there are n different things that can happen ( A1A_1, A2A_2, \dots, AnA_n), and they don't affect each other, the total number of ways the experiment can go is like multiplying the number of possibilities for each thing (n options multiplied together).

number of outcomes=A1A2An \text{number of outcomes} = |A_{1}| |A_{2}| \dots |A_{n}|

Let's illustrate this concept further with an example.

We have a 2x2 grid (shown below). What are the number of uniques images that can be formed by filling up the 4 grids?

We assume that each grid can take a combination of Red, Green and, Blue. We also assume that it takes 1 byte (8 bits) of memory to represent a color.

2x2 image

Now that the problem and the assumptions are clear, let's solve this problem step-by-step.

The total number of colors that can be represented by 8 bits

1 byte

There are 8 steps -- each with 2 outcomes (0 or 1). Each outcome is independent of the other, hence we can use the Step rule of counting here. The total number of colors would be 28 2^8

Total number of color combination in 1 pixel

Now that we have 28 2^8 ways to represent a single color, there are 3 colors which can be combined to come with a distinct color in one pixel.

Color in 1 pixel

This can be contered with the step rule of counting again. 28×28×28=224 2^8 \times 2^8 \times 2^8 = 2^{24}

Total number of unique images

4 pixel image

You can possible guess what I am going to do.

224×224×224×224 2^{24} \times 2^{24} \times 2^{24} \times 2^{24}

OR rule of counting

This one is simple. If you have n sets of outcomes and you want to know all the possible outcomes from either of the sets, you would have to add the number of outcomes from all the sets.

Imagine you're at a vending machine, and you can either:

Choose a cold drink: 5 options (Coke, Sprite, Fanta, Orange Juice, Water)

OR

Choose a hot drink: 3 options (Coffee, Tea, Hot Chocolate)

How many total choices do you have?

With the OR rule, we simply add the possibilities:

Total choices = Cold drink options + Hot drink options

Total choices = 5 + 3 = 8

We have covered the foundations of counting. In the following sections, we build some abstractions on top of them. Specifically, we delve into different ways of counting when we are given nn objects.

Count the arrangements (Permutations)

Given n n distinct objects, how many ways can we arrange them?

Let's consider a set of alphabets {a, b, c, d} and 4 positions each of which can take an alphabet.

all options in 4 positions

We can solve this problem with our friendly step rule of counting. There are 4 choices for the 1st position, once chosen, there are 3 choices for the 2nd position and so on.

Applying the step rule of counting, the total number of arrangements are 4×3×2×1=4!4 \times 3 \times 2 \times 1 = 4!

Given n n indistinct objects, how many ways can we arrange them?

Modifying the previous example we now have the following collection of alphabets [a, a, b, c]. Notice we still have 4 positions to fill but all the alphabets are not distinct.

A 🔑 insight shared by Chris Piech was to consider all the elements distinct and then proceed.

all options in 4 positions

With this modification we now know that there are 4! ways to arrange the alphabets. In reality we have overcounted. Now, we just need to remove the number of times we have overcounted.

Before removing the number of overcounts let's frame this thought a little better. There is either a static number of times we can overcount (which needs to be subtracted) OR there is a multiplicative factor by which we can overcount (which needs to be divided). In this case we have overcounted in multiples.

Consider a, a, b, c as an arrangement. Here there are 2 ways we can place the 'a's (if they were distinct).

duplicates

Both the arrangements are different if we consider two distinct 'a's, but that is not the case here. Continuing this thought we understand that for all the distinct arrangements with a, b, and c there are 2 duplicates (the 'a's swapping places).

This means that we would need to divide the 4! (all distinct) by 2 (two 'a's) and we get our desired answer 4!2! \frac{4!}{2!} .

What if we have the following alphabets [a, a, a, c] and the same 4 positions. The number of times we overcount is actually the number of arrangements of 'a's if they were distinct (which is 3!). Hence the total number of ways we can arrange the alphabets are 4!3!\frac{4!}{3!}

We can formulate it all by stating that if we have nn objects and out of them n1,n2,nrn_{1}, n_{2}, \dots n_{r} are indistinct, the total number of arrangements would be: n!n1!n2!nr! \frac{n!}{n_{1}! n_{2}! \dots n_{r}!}

Count the ways to choose k objects (Combinations)

Given n n distinct objects, in how many ways can we choose kk objects from them?

You have thrown a birthday party for nn people, but you have miscalculated and can only choose k people (k < n) to have cake. How many ways could you do that?

If you are thinking step-by-step you are on the right track.

  1. The first part of the problem is to line the n distinct people. We can do that in n! ways.
  2. The next part is to draw a partition between the first k people. We can do that only 1 way.
  3. We do not care about the arrangements of the first k people. There are a total of k! arrangements.
  4. We do not care about the arrangements of the rest of the (n-k) people. There are a total of (n-k)! arrangements.

number of ways to choose k from n objects=n!×1×1k!×1(nk)!=n!k!(nk)!\text{number of ways to choose k from n objects} = n! \times 1 \times \frac{1}{k!} \times \frac{1}{(n-k)!} = \frac{n!}{k!(n-k)!}

❤️ I really like this problem, because this was the first time I derived the equation of combination, that too with counting.

Note: Choosing k objects from n distinct objects is also denoted as CrnC_{r}^{n}

Given n n indistinct objects, in how many ways can we choose kk objects from them?

This is quite easy, there is only 1 way in which you can choose k objects from n indistinct objects.

Count the ways of putting objects in r buckets

Given n n distinct objects, in how many ways can we put them in rr buckets?

We have 3 distinct shapes that we need to put in 3 buckets. This problem is the same as arranging the 3 distinct shapes in 3 positions but where duplication is allowed. 3 shapes

This means that there are 3 ways for the first bucket, 3 ways for the second, and 3 ways for the third. With the step rule of counting, there are 333^3 ways in total.

Formulating the concept, if there are n distinct objects and r buckets, there are nrn^r ways to bucket.

Given n n indistinct objects, in how many ways can we put them in rr buckets?

Now we have n indistinct objects that we need to put in r buckets. The idea here is to add (r-1) dividers to the mix. Let's consider 3 objects and 2 buckets, the diagram below shows the objects and the dividers.

3 same shapes with dividers

Simply counting the number of arrangements would give us the desired result.

3 shapes with dividers arranged

This now becomes a problem of arranging indistinct objects. We can formulate this as (n+r1)!n!(r+1)!\frac{(n+r-1)!}{n!(r+1)!}

Summary

Thank you for your time. I hope I could do some justice to the topic on counting. I have been floored with the way the intuitions were hidden all this time. Happy learning.

Acknowledgement

I would like to thank Udbhas Mitra, Sayak Paul for their review on the blog post.