Combinatorial Counting

Notes on the combinatorics part of CS2304: Mathematical Foundations of Computer Science.

Review

Textbook:
An invitation to discrete mathematic

stars

Inclusion-Exclusion Principle

General formula:

proved by induction.

Derangements

Let D(n) denote the number of derangements of n elements.

Recurrence

D(n) = (n-1)[D(n-1) + D(n-2)]

initial condition: D(1) = 0, D(2) = 1

Closed form

or written as:

Derivation via the inclusion-exclusion principle

Let A_i denote the set of permutations in which “the i-th element is in its original position”. Then the number of derangements is , i.e. the number of permutations in which no element is in its original position.

Converting to the complement

  • $|A{i_1} \cap A{i2} \cap … \cap A{i_k}|$ denotes the number of permutations in which exactly k elements are in their original positions
  • There are ways to choose these k positions
  • The remaining n-k positions can be permuted arbitrarily, giving ways
  • Therefore

As n → ∞, converges to , which explains why

Euler’s totient function

Euler’s totient function is the number of positive integers less than n that are coprime to n.

How to compute it
  1. Multiplicativity: if a and b are coprime, then

  2. Totient of a prime: if p is prime, then

  3. Totient of a prime power: if p is prime and k ≥ 1, then

  4. General formula: for any positive integer n, if the prime factorization of n is:

    then by the inclusion-exclusion principle:

Generating Function

A generating function is a powerful tool that turns a sequence into a function, usually expressed as a power series:

where is the -th term of the sequence and is a formal variable.

Used for permutation and combination problems:

Used for the probability distribution of a discrete random variable:

3. Applications of generating functions

3.1 Solving recurrence relations

1
2
3
4
5
6
7
8
9
10
11
12
# 示例:使用生成函数求解斐波那契数列
def fibonacci_generating_function(n):
# 斐波那契数列的生成函数:G(x) = x/(1-x-x^2)
# 展开为幂级数可求第n项
import numpy as np

# 使用递推关系直接计算
fib = [0, 1]
for i in range(2, n+1):
fib.append(fib[i-1] + fib[i-2])

return fib[n]

3.2 Counting problems

Example: how many ways are there to place identical objects into distinct boxes?

  • Generating function:
  • The coefficient of in the expansion is the answer

3.3 Applications in probability

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 使用生成函数计算两个骰子和的概率分布
def dice_sum_probability(sum_value):
# 单个骰子的生成函数:P(x) = (x^1 + x^2 + ... + x^6)/6
# 两个骰子和的生成函数:P(x)^2

# 初始化概率计数
counts = [0] * 13 # 索引0不使用,可能的和为2-12

# 暴力计算所有可能结果
for i in range(1, 7):
for j in range(1, 7):
counts[i + j] += 1

# 计算概率
return counts[sum_value] / 36

4. Properties of generating functions

  • Addition: adding sequences corresponds to adding generating functions
  • Multiplication: convolving sequences corresponds to multiplying generating functions
  • Differentiation:
  • Integration:

5. Common generating functions

SequenceGenerating function
Fibonacci sequence

6. Example: computing binomial coefficients

1
2
3
4
5
6
7
8
9
10
11
12
# 使用生成函数计算组合数 C(n,k)
def binomial_coefficient(n, k):
# 使用二项式定理: (1+x)^n = Σ(k=0 to n) C(n,k) * x^k
if k < 0 or k > n:
return 0

# 计算组合数
result = 1
for i in range(1, k+1):
result = result * (n - i + 1) // i

return result

Translated from the Chinese original.

Welcome to my other publishing channels

中文