Discrete Structures and Counting
Combinatorics is the branch of mathematics dealing with combinations of objects belonging to finite sets. It is foundational for probability and computer science.
Permutations and Combinations
- A permutation is an arrangement of objects in a specific order. The number of permutations of objects taken at a time is:
- A combination is a selection of objects where order does not matter:
python
1import math
2
3def permutations(n, r):
4 return math.factorial(n) // math.factorial(n - r)
5
6def combinations(n, r):
7 return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
8
9print(f"Permutations of 5 pick 3: {permutations(5, 3)}") # 60
10print(f"Combinations of 5 pick 3: {combinations(5, 3)}") # 10
In how many ways can you arrange 3 distinct books on a shelf?
The Pigeonhole Principle
The pigeonhole principle states that if items are put into containers, with , then at least one container must contain more than one item. While simple, it is a powerful tool for proving existence.
If there are 13 people in a room, what is the minimum number of people who must share the same birth month?
Generating Functions
A generating function is a way of encoding an infinite sequence of numbers by treating them as the coefficients of a formal power series:
python
1def get_fibonacci_coefficient(n):
2 """Sequence: 0, 1, 1, 2, 3, 5, ...
3 Generating function: x / (1 - x - x^2)"""
4 a, b = 0, 1
5 for _ in range(n):
6 a, b = b, a + b
7 return a
8
9print(f"10th Fibonacci number: {get_fibonacci_coefficient(10)}")
Principle of Inclusion-Exclusion
For two sets and : This generalizes to any number of sets, compensating for over-counting elements in intersections.