Combinatorics and Enumerative Analysis
Combinatorics is the study of discrete structures. It determines the existence, count, and optimization of arrangements according to specific rules.
Fundamental Principles
- Addition Principle: If events are mutually exclusive, sum the ways.
- Multiplication Principle: If events occur in sequence, multiply the ways.
Permutations and Combinations
Permutations (): Choosing and ordering elements from .
Combinations (): Choosing elements without regard to order.
python
1import math
2
3def nCr(n, r):
4 return math.comb(n, r)
5
6def nPr(n, r):
7 return math.perm(n, r)
8
9print(f"Combinations (10 choose 3): {nCr(10, 3)}")
10print(f"Permutations (10 permute 3): {nPr(10, 3)}")
Pascal’s Triangle and Binomial Expansion
The identity allows for recursive construction.
python
1def pascal_triangle(rows):
2 triangle = [[1]]
3 for i in range(1, rows):
4 row = [1]
5 for j in range(1, i):
6 row.append(triangle[i-1][j-1] + triangle[i-1][j])
7 row.append(1)
8 triangle.append(row)
9 return triangle
10
11for row in pascal_triangle(6):
12 print(str(row).center(30))
Advanced Principles
- Pigeonhole Principle: If items are put in boxes, one box has item.
- Inclusion-Exclusion: Corrects for overcounting in unions ().
Which principle is best suited for counting the number of permutations where no element stays in its original position?
Stars and Bars: Multisets
Choosing items from types with repetition: .
python
1from itertools import combinations_with_replacement
2
3# Choosing 2 items from {A, B, C} with repetition
4types = ['A', 'B', 'C']
5k = 2
6combos = list(combinations_with_replacement(types, k))
7print(f"Multisets of size {k} from {types}:")
8print(combos)
9print(f"Formula count: {nCr(len(types) + k - 1, k)}")