Combinatorics

Making counting fun.

Created by John Robison

Combinations

Creating a subset from a collection where the order of selection does not matter.


This is also known as the Binomial Coefficient.

Governing Equation

\[ {n\choose k} = \frac{n!}{k!(n - k)!} \]

Let's look at an example

While playing poker, you wonder how many possible hands you could possibly be dealt. You know that a standard deck contains 52 cards and your hand holds 5 cards. Each card is distinct and your hand can be rearranged, so the order is unimportant.

\[ {52\choose 5} = \frac{52!}{5!(52 - 5)!} \]


Plugging this into your favorite calculator or REPL shows that there are 2,598,960 possible hands

Permutations

Creating a subset from a collection where the order of selection does matter.

Governing Equation

\[ _{n}P_{k} = \frac{n!}{(n - k)!} \]

Let's look at an example

Buff, being the studmuffin he is, has many adoring fans. One such fan wants to make a shrine to Buff with a 3 picture frame using photos from their Buff folder containing 200 pictures. How many ways can these pictures be placed in this picture frame?

Shitty portraits of Buffycakes

\[ _{200}P_{3} = \frac{200!}{(200 - 3)!} \]

Plugging this into your favorite calculator or REPL shows that there are 7,880,400 possible ways to worship everyone's favorite boy

Off the Wall Number Sets

Really obscure number sets that always seem to creep up in competitons as the 'easy' solution to a problem.

Catalan Numbers

\[ C_{n} = \frac{1}{n + 1} {2n\choose n} = \frac{(2n)!}{(n + 1)!n!} = \prod_{k = 2}^n\frac{n + k}{k} for \ n \ge 0 \]


1
1
2
5
14
42
132
429
1430
4862
16798
...
                            

Why Are The Catalan Numbers Important?

The people who make the problems said so.

An Example Problem That We've Seen At Competiton

A Dyck Word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's. The count of the Dyck words for a string of length 2n is Cn.


XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY.

Additional examples of Catalan Number usages

Balls and Buckets

In a system where there are n balls and m buckets, this problem consists of determining the load (i.e. how many balls are in a given bucket).

Random Allocation

\[ Maximum\ Load = \frac{log\ n}{log\ log\ n} (1 + o(1)) \]

Example Applications

  • Dynamic Resource Allocation
  • Hashing

Itertools

Or how Python is once again the superior language.

Basics of Itertools


itertools.combinations("ABCDEFG", 3)
itertools.combinations_with_replacement("ABCDEFG", 3)
itertools.permutations("ABCDEFG", 3)
itertools.count(10, 2)
itertools.cycle("ABCD")
itertools.repeat(10, 3)