Search Knowledge

© 2026 LIBREUNI PROJECT

Discrete Structures and Counting

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 nn objects taken rr at a time is: P(n,r)=n!(nr)!P(n, r) = \frac{n!}{(n-r)!}
  • A combination is a selection of objects where order does not matter: C(n,r)=(nr)=n!r!(nr)!C(n, r) = \binom{n}{r} = \frac{n!}{r!(n-r)!}
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 nn items are put into mm containers, with n>mn > m, 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 (an)(a_n) by treating them as the coefficients of a formal power series: G(x)=n=0anxnG(x) = \sum_{n=0}^{\infty} a_n x^n

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 AA and BB: AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B| This generalizes to any number of sets, compensating for over-counting elements in intersections.