Recursive counting (CSCI 2824, Spring 2015)

Topic covered:

  • Recursive Counting: counting things by establishing recurrence relations (Section 5.5 of the book)

Recursive Counting

We already saw in the previous lecture an example of recursive counting, when we count the number of ways to distribute n indistinguishable objects among k different bins.

The idea behind recursive counting is to set up a recurrence that expresses what you wish to count. This is especially useful when what we wish to count does not neatly fit into any of the categories studied thus far.

Example 1

Question. How many matches need to be played between n teams in a round-robin tournament?

Answer

Let us look at it recursively. Let a_n be the number of matches for n teams. We know that

  • If n < 2 then a_n = 0. We need at least two teams to make it interesting!

  • Otherwise, a_2 = 1. Two teams will play exactly one match.

Can we now express a_n in terms of a_{n-1}?

Yes, we can say that with n teams, # of matches involving all n teams = # of matches involving teams 1… (n-1) + # of matches involving team n

Team n plays precisely n-1 matches (one with every other team). I.e, a_n = a_{n-1} + (n-1).

Therefore, the answer to the problem is governed by the recurrence relation:

  • a_n = a_{n-1} + (n-1) for n > 2

  • a_2 = 1.

The closed form solution is indeed C(n,2) = frac{n (n-1)}{2}.

Example 2

Question. What is the number of n permutations of n objects?

In other words, what is P(n,n)?

Answer

Let us write a recurrence.

To obtain a permutation of n objects (numbered 1…n), let us do the following:

  • Take away the last object and permute the remaining objects from 1 .. (n-1).

  • After this, we can decide where to insert the last object.

Step 1 yields P(n-1,n-1) possible permutations. (We pretend not to know what it may be :-) )

Once we have fixed a permutation of the first n-1 objects, there are n possible places where object #n can be inserted. Therefore the recurrence is

  • P(n,n) = n * P(n-1,n-1)

  • The base case for 1 object is very simple, P(1,1) = 1.

Once again, we know by eyeballing the recurrence that  P(n,n) = n!.

Example 3

We wish to roll a dice n times to obtain a sum of k. Each roll of the dice can give us a number from 1 to 6.

Question. For any integer ngeq1 and any integer k, let D(n,k) be the number of ways to obtain a sum of k from n rolls of a dice. Write a recurrence relation for D(n,k).

Answer

Let us get rid of the base cases where n=1 or kleq1.

  • For any integer k, D(1,k)=begin{cases} 1 & text{ if } 1leq kleq6  0,  & text{ otherwise}.end{cases}

  • For any integer n>1, D(n,k)=0 whenever kleq 1.

Now for the generic case (i.e., the non-base case where n>1 and k>1). Suppose we wish to roll the dice n times and arrive at a sum of exactly k. Let us split cases on the last roll:

  • The last roll showed up 1 and first n-1 rolls sum up to k-1.

  • The last roll shows up 2 and first n-1 rolls sum up to k-2

  • cdots

  • The last roll shows up to 6 and first n-1 rolls sum up to k-6.

Therefore, we can now write a recurrence to express the sum:

D(n,k) = D(n-1, k-1) + D(n-1,k-2) + cdots + D(n-1,k-6), n > 1, k >1 .

An alternative approach is to express D(n,k) as the cardinality of the set

mathcal{D}(n,k) triangleq { (x_1, x_2, ldots, x_n) inmathbb{Z}^n  |  (, x_1+x_2+cdots+x_n = k, ) land  |  (, x_1+x_2+cdots+x_n = k, ) land  (,forall, i= 1,ldots,n (, 1leq x_i leq 6,),)}.
It is not difficult to see that
 mathcal{D}(n,k) = bigcup_{j=1}^6 {(x_1, x_2, ldots, x_{n-1}, j) inmathbb{Z}^n  |  (x_1,x_2,cdots,x_{n-1})in mathcal{D}(n-1, k-j)}.

The above is a disjoint union, i.e., the sets whose union gives mathcal{D}(n,k) have empty intersection. Therefore by the sum rule,

 begin{aligned} |mathcal{D}(n,k) | =& sum_{j=1}^6 left|{(x_1, x_2, ldots, x_{n-1}, j) inmathbb{Z}^n  right|  (x_1,x_2,cdots,x_{n-1})in mathcal{D}(n-1, k-j)}left.right|  =& sum_{j=1}^6 left|mathcal{D}(n-1, k-j)right|  =& sum_{j=1}^6 D(n-1,k-j). end{aligned}