Combinatorics - Merging N Objects


I got this problem from The Algorithm Design Manual by Steven Skiena. It’s a fairly simple exercise in Chapter 2 that I felt had a neat symmetry, so I’ve decided to write a bit about it.

The problem is as states:

Suppose you have given N companies, and we want to eventually merge them into one big company. How many ways are there to merge them?

Let’s simplify the problem into its true state:

How many ways can you merge N items?

Let’s look at this from the ground up with N increasing as we go along:

#Two items

You can only merge $2$ items $1$ way so that $a + b = ab$

#Three items

The question here is, how many ways can we get three items merged into only two items, since we know exactly how many ways we can merge two items. Working this out reveals the following possibilities.

We stopped when we got down to two items because we know there is only one way to merge two items.

#Expanding to $N$

Let’s look at how many possibilities there are to reduce $N$ items down to $N-1$. We need to merge $2$ items from our list of $N$ items, to leave us with $N-1$ items. The number of different pairs of items we can merge from $N$ is $\binom{N}{2}$ or $\frac{N(N-1)}{2}$. For each $\binom{N}{2}$, we must merge the remaining $N-1$ items out to get our sum total. This gives us a neat little definition:

which can more formally be written:

Expanding it out gives us the following “tail recursive” pattern:

This can be iteratively written down as:

Alternatively, to get a $O(1)$ answer we could write it out as:

Written on June 15, 2016