Combinatorics Coincidence

By Susam Pal on 14 Sep 2008

Combinatorics for Fun

At my current workplace, there are several engineers who have an affinity for combinatorics. As a result, discussions about combinatorics problems often occur at the cafeteria. Probability theory is another popular topic of discussion. Of course, often combinatorics and probability theory go hand in hand.

Recurrence Relation

At the cafeteria one day, I joined in on a conversation about combinatorics problems. During the conversation, I happened to share the following problem:

For integers n1 and k1, fk(n)={nif k=1,i=1nfk1(i)if k2. Find a closed-form expression for fk(n).

Nested Loops

Soon after I shared the above problem, a colleague of mine shared this problem with me:

Consider the following pseudocode with k nested loops:

x = 0
for c1 in 0 to (n - 1):
    for c2 in 0 to (c1 - 1):
        ...
            for ck in 0 to (ck-1 - 1):
                x = x + 1

What is the final value of x after the outermost loop terminates?

Coincidence

With one problem each, we went back to our desks. As I began solving the nested loops problem shared by my colleague, I realised that the solution to his problem led me to the recurrence relation in the problem I shared with him.

In the nested loops problem, if k=1, the final value of x after the loop terminates is x=n. This is also the value of f1(n).

If k=2, the inner loop with counter c2 runs once when c1=0, twice when c1=1, and so on. When the loop terminates, x=1+2++n. Note that this series is same as f2(n)=f1(1)+f1(2)++f1(n).

Extending this argument, we now see that for any k1, the final value of x is fk(n)=fk1(1)+fk1(2)++fk1(n). In other words, the solution to his nested loops problem is the solution to my recurrence relation problem. It was an interesting coincidence that the problems we shared with each other had the same solution.

Closed-Form Expression

The closed form expression for the recurrence relation is fk(n)=(n+k1k). It is quite easy to prove this using the principle of mathematical induction. Since we know that this is also the result of the nested loops problem, we can also arrive at this result by another way.

In the nested loops problem, the following inequalities are always met due to the loop conditions: n1c1c2ck0. The variables c1,c2,,ck take all possible arrangements of integer values that satisfy the above inequalities. If we find out how many such arrangements are there, we will know how many times the variable x is incremented.

Let us consider n1 similar balls and k similar sticks. For every possible permutation of these balls and sticks, if we count the number of balls to the right of the ith stick where 1ik, we get a number that the variable ci holds in some iteration of the ith loop. Therefore the variable ci is represented as the number of balls lying on the right side of the ith stick.

The above argument holds good because the number of balls on the right side of the first stick does not exceed n1, the number of balls on the right side of the second stick does not exceed the number of balls on the right side of the first stick, and so on. Thus the inequalities mentioned earlier are always satisfied. Also, any set of valid values for c1,c2,,ck can be represented as an arrangement of these sticks and balls.

The number of permutations of n1 similar balls and k similar sticks is (n+k1)!(n1)!k!=(n+k1k). This closed-form expression is the solution to both the recurrence relation problem and the nested loops problem.

Comments | #mathematics | #combinatorics | #puzzle