What Counting Gets You

Every time you analyze a brute-force solution’s runtime, estimate hash collisions, or argue that a greedy choice is optimal, you’re doing combinatorics. The core question is “how many ways?” and precise answers to that question are what separate “I think this is exponential” from “I can prove the search space has exactly states.”

That precision drives algorithm design directly. If you know the exact count, you can decide whether brute force is feasible, estimate how much pruning buys you, or recognize when a polynomial-time approach must exist.

Fundamental Counting Principles

  • Product rule: if task has outcomes and task has outcomes, then followed by has outcomes.
  • Sum rule: if and are mutually exclusive, the total outcomes are .

These two rules are so basic they almost don’t feel worth stating, but every counting argument bottoms out at one of them.

Permutations and Combinations

Permutations. An arrangement of items chosen from distinct items, where order matters:

All items arranged: permutations.

Combinations. A selection of items from where order does not matter:

Key identity: (choosing what to include is the same as choosing what to leave out). The binomial theorem expands .

Multinomial coefficients. When distributing distinct items into groups of sizes (where ):

This generalizes combinations and is what you use for anagram counting (how many distinct rearrangements of MISSISSIPPI?).

The Pigeonhole Principle

If items are placed into containers and , at least one container holds more than one item. Generalized: some container holds at least items.

Tip

The pigeonhole principle sounds trivial but it’s surprisingly powerful in proofs. It gives you existence results for free: you don’t need to find the crowded container, you just know it’s there.

Classic applications:

  • In any group of 13 people, at least two share a birth month.
  • A hash table with slots and keys must have at least one collision. There is no injective hash function when the domain exceeds the range.

Inclusion-Exclusion

For counting elements in a union of sets:

This corrects for overcounting at each intersection level. It’s essential for counting derangements, surjections, and elements satisfying “at least one of several properties.”

Example

Derangements (permutations with no fixed point). For elements, let = “element is fixed.” By inclusion-exclusion: The probability of a random permutation being a derangement converges to remarkably fast. This result shows up in problems about random shuffling and matching.

Stars and Bars

The number of ways to distribute identical items into distinct bins is . This comes up whenever you’re counting solutions to equations like with non-negative integer constraints.

Recurrences and Catalan Numbers

Many combinatorial quantities satisfy recurrences, and recognizing the recurrence often leads to a closed form or a known sequence.

The Catalan numbers count an absurd number of things: valid parenthesizations, binary tree shapes, non-crossing partitions, paths that stay below the diagonal. They satisfy:

Note

The Catalan numbers show up in Dynamic Programming directly. The number of distinct binary trees with internal nodes is , which is the same as the number of ways to fully parenthesize a product of factors. This is exactly the structure behind matrix chain multiplication.

Counting in Algorithm Analysis

Subsets and brute force. The brute-force approach to subset-sum examines all subsets. That count comes from . Backtracking prunes this space, but is the baseline you’re pruning from.

Inversions and sorting. The number of permutations of elements with exactly inversions (pairs out of order) follows the Mahonian distribution. Sorting algorithms that do adjacent swaps (bubble sort, insertion sort) take exactly as many swaps as there are inversions.

Warning

Complement counting is one of the most useful tricks and it’s easy to forget about it. Instead of counting what you want directly, count the total minus what you don’t want. Binary strings of length with at least one 1? Total minus the one all-zero string gives . Always check whether the complement is easier before diving into a direct count.

Double counting. A proof technique where the same quantity is counted two different ways, yielding an identity. For instance, counting edges in by rows gives and by degree sum gives , confirming . This appears frequently in graph theory proofs.