Combinatorics
Counting the ways…
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Combinatorics
Counting the ways…
Combinatorics is counting without listing. Grow a decision tree, fill slots, regroup orderings, build Pascal's triangle and slide bars through stars — and see where every formula comes from.
A decision tree that grows one stage at a time: every choice multiplies the leaves. Then the addition rule for either-or cases.
total = n₁ × n₂ × … × nₖMultiplicationMultiplication RuleFill k slots from n items and watch the choices shrink — n, n − 1, n − 2 — or stay at n when repeats are allowed.
P(n, k) = n! / (n − k)!Counting PrincipleOrdered SelectionList every ordered pick, then gather the k! orderings of each set into one row. What is left is nCr.
C(n, k) = n! / (k! (n − k)!)PermutationsUnordered SelectionBuild each row by adding the two numbers above. Find C(n, k), its mirror image, the hockey stick and the row sums of 2ⁿ.
C(n, k) = C(n − 1, k − 1) + C(n − 1, k)CombinationsRecurrenceExpand (a + b)ⁿ by picking a or b from each factor, and see why the coefficients are C(n, k) — with (a + b)³ as a cube cut into 8 blocks.
(a + b)ⁿ = ∑ C(n, k) aⁿ⁻ᵏ bᵏPascal's TriangleExpansionShare n identical items among k bins by dragging k − 1 bars through a row of stars. Every arrangement is one distribution.
C(n + k − 1, k − 1)CombinationsBijectionCount a union of overlapping sets by adding, subtracting and adding back — with a tally on every Venn region so nothing is counted twice.
|A ∪ B ∪ C| = ∑|A| − ∑|A ∩ B| + |A ∩ B ∩ C|Counting PrincipleOvercount CorrectionMore pigeons than holes forces a collision. Then a surprise: in any group, two people have shaken the same number of hands.
some hole holds ≥ ⌈n / m⌉Counting PrincipleExistence ProofThe code side of permutations: build every ordering with recursion and backtracking, one choice per level of the call tree.
n! orderingsPermutationsBacktrackingInclude or exclude each element in turn: the 2ⁿ leaves of the recursion tree are exactly the subsets that Pascal's rows count.
2ⁿ subsetsCombinationsInclude / Excluden! = n × (n − 1)! computed by recursion, with the call stack growing and unwinding — the number every page here divides by.
n! = n × (n − 1)!MultiplicationRecursionEvery "equally likely" probability is a count divided by a count. Poker hands, birthday collisions and lottery odds are combinatorics problems first.
How many subsets a brute-force search tries, how many paths a grid has, how many interleavings two threads allow — complexity analysis is counting.
nPr, nCr and stars and bars are not facts to memorise. Each comes from one picture, and once you have seen the picture you can rebuild the formula.