Counting, Permutations & Combinations
A derivation-first toolkit for finite counting: sum, product, quotient and bijection principles; sequences, injections, permutations, cycles, combinations, multisets, and stars-and-bars edge cases.
Counting is rarely about arithmetic first. It is about defining the set of outcomes so precisely that two descriptions cannot denote the same outcome by accident. Once the object is clear—function, injection, subset, bijection, multiset, or orbit—the formula follows from a small number of proof principles.
Count objects, not stories #
For a finite set , its cardinality is the number of elements. The safest way to show two finite families have the same size is a bijection: pair every object in with exactly one object in and vice versa.
This is why the following translations matter:
| Informal outcome | Precise object |
|---|---|
| length- code over alphabet | function |
| ordered choice of distinct items from | injection |
| unordered choice of distinct items | -element subset of |
| rearrangement of all items of | bijection |
| unordered choice allowing repetition | multiset of cardinality over |
Here , with . This typed view also exposes a common error: when , an ordered selection of objects from an -element set is an injection, not a bijection onto the whole -element set.
Four principles do most of the work #
Disjoint sum rule #
If , then
The disjointness condition is load-bearing. If outcomes can appear in both cases, adding counts double-counts the intersection; that is the territory of inclusion–exclusion.
For a partition ,
Product rule #
If an outcome is uniquely a pair , then
For a sequence of stages, multiply the number of legal choices at each stage provided that the stage counts are valid for every preceding partial choice. If later choices depend on earlier ones, the factors may change, but the product rule still applies along each branch.
Complement and subtraction #
If , then
This is often easier than describing the allowed outcomes directly: count all candidates, then subtract the forbidden set. The subset condition prevents meaningless negative or overlapping subtraction.
Uniform-fiber quotient rule #
Suppose a surjection maps exactly objects of to every . The fibers partition into blocks of size , so
“Divide by the number of reorderings” is valid only when every abstract outcome has the same number of labelled representations. Nonuniform symmetry groups are the point where naïve division fails.
The two questions that select the formula #
flowchart TD
start["Choose r items<br/>from n types"]
order{"Does order matter?"}
repY{"May a type repeat?"}
repN{"May a type repeat?"}
seq["functions<br/>n^r"]
inj["injections<br/>(n)_r"]
multi["multisets<br/>C(n+r-1, r)"]
subset["r-subsets<br/>C(n, r)"]
start --> order
order -->|"yes"| repY
order -->|"no"| repN
repY -->|"yes"| seq
repY -->|"no"| inj
repN -->|"yes"| multi
repN -->|"no"| subset
The diagram is not a mnemonic detached from meaning. Its four leaves are four different sets: all functions, injections, multisets, and subsets. If a problem mixes labelled roles, indistinguishable items, or equivalence under rotation, first refine the object; do not force it into the nearest leaf.
Ordered choices: powers and falling factorials #
Let have elements.
Repetition allowed: #
A length- sequence is a function . Every one of the positions independently has choices, hence
This includes the combinatorial convention : there is exactly one empty sequence, or equivalently one function . In particular, combinatorics uses for the number of empty functions from the empty domain. Other analytic contexts may leave undefined; the counted object resolves the convention here.
No repetition: the falling factorial #
An ordered selection of distinct items is an injection . The first position has choices, the second , and so on:
If , the count is : an injection from a larger finite set into a smaller one cannot exist. The factorial quotient should therefore not be applied outside its stated range.
Permutations are bijections #
If , a permutation is an ordered selection of all elements:
The recursion says: choose the first image in ways, then permute the remaining elements. The base case counts the unique empty bijection . Mathlib's factorial documentation points to the formal result that factorial counts permutations, while the NIST DLMF combinatorics chapter records standard permutation and cycle notation.
Cycles expose the mechanism #
A cycle
maps and fixes all unlisted elements. Repeatedly following must eventually return to because is finite and is injective. That orbit is one cycle. Starting from an unvisited element and repeating partitions into disjoint cycles.
Therefore every finite permutation decomposes into disjoint cycles. The decomposition is unique up to:
- rotating the notation inside a cycle, such as ;
- reordering disjoint cycles;
- including or omitting fixed-point -cycles.
Disjoint cycles commute because each fixes every element moved by the other. Overlapping cycles generally do not.
The number of -cycles that can be formed from an -element set, for , is
Choose the participants, then arrange them around a directed cycle. There are linear listings but each cycle has rotational listings, leaving .
Unordered distinct choices: binomial coefficients #
An -combination from an -element set is an -element subset. Start with the ordered selections. Every subset appears in exactly orders, so the uniform-fiber rule gives
For , define . Formal verification that the -element subsets of an -element finite set have cardinality appears as Finset.card_powersetCard in Mathlib's finite-powerset API.
Three identities, three bijections #
Complementing a chosen subset swaps its size from to :
Fix a distinguished element . Every -subset either omits or contains it:
This is Pascal's recurrence, with the two cases disjoint. Finally, partition the whole power set by subset size:
The binomial theorem has the same classification. In expanding , choose the factors that contribute ; the other contribute :
Repeated objects: multinomial coefficients #
Suppose a word of length contains copies of type 1, …, copies of type , where . If all copies were temporarily labelled, there would be arrangements. Erasing labels within type identifies exactly labelled arrangements, independently across types. Thus
Equivalently, choose positions successively:
Unordered choices with repetition: stars and bars #
Choosing a multiset of size from labelled types is equivalent to solving
where is the multiplicity of type . For , encode a solution by stars separated into bins by bars. Every encoding contains symbols, and choosing the star positions gives
The left notation denotes multichoose, not an ordinary binomial coefficient. Mathlib formalizes the same identity as Nat.multichoose_eq in its binomial-coefficient library.
If every must be positive, set :
Hence the count is when , and when .
For , the stars-and-bars formula has a negative top index and must not be used. There is one empty multiset when , and none when .
Circular arrangements and the symmetry trap #
For distinct objects around an oriented circle, if rotations count as the same but reflections remain different, there are
arrangements. Every circular arrangement has exactly linear representations, one for each choice of starting point.
Mixed constraints: decompose one outcome exactly once #
When a count is uncertain, derive it in two structurally different ways. A mismatch usually reveals either a hidden order, a repeated representation, or overlapping cases.
Computing coefficients without giant factorials #
The factorial formula explains , but it is a poor numerical algorithm. Use symmetry and the exact recurrence
Each step is an integer even though it is written as a product followed by division. In fixed-width code, reduce factors by greatest common divisors or use a big-integer type; computing first can overflow long before the final coefficient does.
Pascal's recurrence is preferable when many coefficients for the same are needed, and dynamic programming over the relevant state definition generalizes naturally to constrained counts. Recurrences and generating functions develop that next layer.
Common counting failures #
| Symptom | Hidden mistake | Repair |
|---|---|---|
| answer differs by a factor of | ordered sequences confused with subsets | state whether positions/roles are labelled |
| divide-by-symmetry gives a fraction | orbit/fiber sizes are not uniform | inspect stabilizers; use a group-action method |
| product rule overcounts | one final object has several construction histories | make the decomposition unique or quotient the histories |
| sum rule overcounts | cases overlap | make cases disjoint or use inclusion–exclusion |
| factorial has a negative argument | formula used for | count injections as zero outside |
| stars and bars handles distinct items | indistinguishable stars were assumed | use functions for labelled items |
| circular count divides by automatically | reflection equivalence or symmetry was not specified | define the acting symmetries and fixed arrangements |
| sample rows determine a count | unstated constraints were inferred from data | count from declared rules, not accidental sample uniqueness |
A reliable workflow #
- Define the outcome set, including labels, order, replacement, and equivalence.
- Check boundary cases: , , , and impossible constraints.
- Choose a bijection, disjoint partition, Cartesian product, complement, or uniform quotient.
- Prove that every legal outcome is generated and generated exactly once.
- Only then simplify the arithmetic.
- Cross-check with a second decomposition or a small exhaustive enumeration.
The formulas are consequences of this workflow. Memorizing them without their object models is what makes small wording changes so dangerous.
Sources
- bookSpecial Sections of Mathematics 1: Discrete Mathematics, Section 1, Topic 1pdfnotes
- docsNIST Digital Library of Mathematical Functions — Combinatorial Analysis
- docsMathlib — Binomial Coefficients
- docsMathlib — Finite Powersets and Combinations
- docsMathlib — Factorials and Falling Factorials
- docsMathlib — Cardinality of Finite Products