Chapter 3 — Combinatorics & Counting
This chapter explores combinatorial principles, counting techniques, and their applications in AI & ML. Combinatorics helps in feature selection, probability computations, and optimization tasks.
3.1 Counting Principles
Fundamental rules to count possibilities without listing all outcomes.
- Addition Rule: If there are m ways to do one thing and n ways to do another (mutually exclusive), total ways = m + n.
- Multiplication Rule: If a task has multiple steps, total ways = product of ways for each step.
AI/ML Context: Useful for counting possible feature combinations, dataset variations, and decision paths.
3.2 Permutations
Permutations are arrangements of objects where order matters.
- n objects arranged in r slots:
P(n, r) = n! / (n - r)! - Example: Arranging 3 features out of 5 → P(5,3) = 60
AI/ML Context: Useful for modeling ordered sequences, hyperparameter arrangements, and feature ordering.
3.3 Combinations
Combinations are selections where order does not matter.
- n objects choose r:
C(n, r) = n! / (r! * (n - r)!) - Example: Selecting 3 features from 5 → C(5,3) = 10
AI/ML Context: Used in feature subset selection, ensemble modeling, and sampling without order.
3.4 Factorials & Binomial Coefficients
Factorials n! are the product of all positive integers ≤ n.
import math
# Factorial example
print(math.factorial(5)) # Output: 120
# Binomial coefficient
def binomial(n, r):
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
print(binomial(5,3)) # Output: 10
AI/ML Context: Binomial coefficients are used in probabilistic models, combinatorial feature calculations, and binomial distributions.
3.5 Binomial Theorem
Expands powers of sums: (a + b)^n = Σ C(n,k) * a^(n-k) * b^k
AI/ML Context: Used in probability expansions, feature interaction modeling, and analyzing combinations in datasets.
3.6 Pigeonhole Principle
If n items are placed into m containers and n > m, at least one container holds more than one item.
AI/ML Context: Helps in reasoning about collisions in hashing, feature overlaps, and guarantees in combinatorial optimization.
3.7 Practical Python Examples
import itertools
import math
# Permutations
features = ['A','B','C','D']
perms = list(itertools.permutations(features, 2))
print("Permutations of 2 features:", perms)
# Combinations
combos = list(itertools.combinations(features, 2))
print("Combinations of 2 features:", combos)
# Factorials & Binomial coefficients
print("5! =", math.factorial(5))
def binomial(n,r):
return math.factorial(n)//(math.factorial(r)*math.factorial(n-r))
print("C(5,2) =", binomial(5,2))
3.8 Why Combinatorics Matters in AI/ML
Combinatorics is essential for:
- Feature selection and combination analysis
- Understanding dataset possibilities
- Combinatorial optimization in algorithms and hyperparameter tuning
3.9 Exercises
- Calculate permutations and combinations for selecting 3 features out of 6.
- Implement a Python function to generate all subsets of a given feature list.
- Use the pigeonhole principle to reason about collisions in a dataset of 15 items in 10 bins.
Hints / Solutions
- Use
itertools.permutations()anditertools.combinations(). - Use recursion or
itertools.combinations()for subsets. - Since 15 > 10, at least one bin must contain ≥2 items.
3.10 Further Reading & Videos
- Discrete Mathematics and Its Applications – Kenneth H. Rosen
- Khan Academy – Combinatorics & Permutations
- YouTube – "Combinatorics for Computer Science" by WilliamFiset
Next Chapter: Graph Theory — Graphs, trees, connectivity, and their use in network modeling, search algorithms, and ML data structures.