MatheramaMatherama
← Combinatorics
Combinatorics·Section 10·13 min read

Counting on Sets, Relations, and Functions

Every counting tool we have built across Sections 01–09—the Multiplication Principle, Combinations, Stars and Bars, Inclusion–Exclusion, Stirling Numbers, and Bell Numbers—comes together naturally when counting the three core structures of discrete mathematics: subsets, relations, and functions on finite sets.


1. Counting Subsets§

Let AA be a finite set containing nn elements. Recall that the collection of all subsets of AA is called the power set of AA, denoted P(A)\mathcal{P}(A).

  • Total subsets of AA: ∣P(A)∣=(n0)+(n1)+⋯+(nn)=2n|\mathcal{P}(A)| = \binom{n}{0} + \binom{n}{1} + \dots + \binom{n}{n} = 2^n, because each of the nn elements independently has 22 choices (either included in the subset or excluded).
  • Proper subsets of AA: Excluding AA itself leaves 2n−12^n - 1 proper subsets (and excluding both ∅\emptyset and AA leaves 2n−22^n - 2 non-empty proper subsets).
  • Subsets of exact size rr: (nr)\binom{n}{r}.
  • Subsets always containing kk particular elements: Those kk fixed elements have only 11 choice (must be included), while the remaining n−kn - k elements have 22 choices each, giving 1k⋅2n−k=2n−k1^k \cdot 2^{n - k} = 2^{n - k} subsets. (For a single particular element, 2n−12^{n - 1}.)

Example 1 (Pairs of Subsets Under Set Conditions): Let ∣A∣=n|A| = n. Find the number of ordered pairs (P,Q)(P, Q) of subsets of AA such that: (a) P∩Q=∅P \cap Q = \emptyset, (b) P∪Q=AP \cup Q = A, (c) P⊆QP \subseteq Q.

Solution: Decide the fate of each element x∈Ax \in A independently! In general, any element x∈Ax \in A has 44 possibilities across (P,Q)(P, Q): (1) x∈P,x∈Qx \in P, x \in Q; (2) x∈P,x∉Qx \in P, x \notin Q; (3) x∉P,x∈Qx \notin P, x \in Q; (4) x∉P,x∉Qx \notin P, x \notin Q.

  • (a) P∩Q=∅P \cap Q = \emptyset: Possibility (1) is forbidden, leaving 33 independent choices for each of the nn elements   ⟹  3n\implies 3^n ordered pairs. (If we instead ask for unordered disjoint pairs {P,Q}\{P, Q\}, notice (∅,∅)(\emptyset, \emptyset) is the unique pair with P=QP = Q; the remaining 3n−13^n - 1 ordered pairs come in symmetric twins (P,Q)(P, Q) and (Q,P)(Q, P), giving 3n−12+1=3n+12\frac{3^n - 1}{2} + 1 = \frac{3^n + 1}{2} unordered pairs.)
  • (b) P∪Q=AP \cup Q = A: Possibility (4) (xx in neither set) is forbidden, leaving 33 choices per element   ⟹  3n\implies 3^n ordered pairs.
  • (c) P⊆QP \subseteq Q: Possibility (2) (x∈Px \in P but x∉Qx \notin Q) is forbidden, leaving 33 choices per element   ⟹  3n\implies 3^n ordered pairs.

2. Counting Relations on Finite Sets§

Let AA and BB be finite sets containing mm and nn elements respectively. Recall that the Cartesian product A×BA \times B is the set of all ordered pairs:

A×B={(a,b):a∈A,  b∈B}A \times B = \{(a, b) : a \in A,\; b \in B\}

Because the first entry can be chosen from AA in mm ways and the second entry from BB in nn ways, ∣A×B∣=mn|A \times B| = mn.

By definition, any relation RR from AA to BB is a subset of A×BA \times B. Since A×BA \times B contains mnmn elements:

Total number of relations from A to B=2mn\text{Total number of relations from } A \text{ to } B = 2^{mn}

Visualizing Relations on a Set via a Square Matrix Grid§

Now focus on relations defined on a single set A={a1,a2,…,an}A = \{a_1, a_2, \dots, a_n\}—that is, subsets of A×AA \times A. To count special classes of relations effortlessly, arrange all n2n^2 ordered pairs of A×AA \times A in an n×nn \times n matrix:

A×A=[(a1,a1)(a1,a2)⋯(a1,an)(a2,a1)(a2,a2)⋯(a2,an)⋮⋮⋱⋮(an,a1)(an,a2)⋯(an,an)]A \times A = \begin{bmatrix} (a_1, a_1) & (a_1, a_2) & \cdots & (a_1, a_n) \\ (a_2, a_1) & (a_2, a_2) & \cdots & (a_2, a_n) \\ \vdots & \vdots & \ddots & \vdots \\ (a_n, a_1) & (a_n, a_2) & \cdots & (a_n, a_n) \end{bmatrix}

Notice the exact geometry of these n2n^2 entries:

  • nn main-diagonal entries: (ai,ai)(a_i, a_i) for 1≤i≤n1 \le i \le n.
  • n2−nn^2 - n non-diagonal entries, split evenly into n2−n2=n(n−1)2\frac{n^2 - n}{2} = \frac{n(n - 1)}{2} upper-triangular entries (ai,aj)(a_i, a_j) (i<ji < j) and n(n−1)2\frac{n(n - 1)}{2} lower-triangular entries (aj,ai)(a_j, a_i) (i<ji < j), where each upper entry sits directly opposite its mirror pair across the diagonal.

Matrix Grid Visualization of A x A Highlighting Diagonal and Off-Diagonal Pairs

Now count every major type of relation on AA directly from this matrix:

1. Total Relations on AA: Each of the n2n^2 entries independently has 22 choices (in RR or not in RR):

2n22^{n^2}

2. Reflexive Relations: A relation RR on AA is reflexive if (a,a)∈R(a, a) \in R for all a∈Aa \in A (e.g., on A={1,2,3}A = \{1, 2, 3\}, R={(1,1),(2,2),(3,3),(1,2)}R = \{(1, 1), (2, 2), (3, 3), (1, 2)\} is reflexive).

In the matrix of A×AA \times A, all nn main-diagonal entries must be included (11 choice each), while each of the n2−nn^2 - n non-diagonal entries is free (22 choices each):

1×1×⋯×1⏟n times×2×2×⋯×2⏟n2−n times=2n2−n=2n(n−1)\underbrace{1 \times 1 \times \dots \times 1}_{n \text{ times}} \times \underbrace{2 \times 2 \times \dots \times 2}_{n^2 - n \text{ times}} = 2^{n^2 - n} = 2^{n(n - 1)}

3. Irreflexive Relations: A relation RR on AA is irreflexive if (a,a)∉R(a, a) \notin R for all a∈Aa \in A. All nn diagonal entries are forced to be excluded (11 choice each), while the n2−nn^2 - n off-diagonal entries remain free:

2n(n−1)2^{n(n - 1)}

4. Symmetric Relations: A relation RR on AA is symmetric if whenever (a,b)∈R(a, b) \in R, we also have (b,a)∈R(b, a) \in R. For example, on A={1,2,3,4}A = \{1, 2, 3, 4\}:

  • R1={(1,1),(2,2)}R_1 = \{(1, 1), (2, 2)\} and R2={(1,1),(1,2),(2,1)}R_2 = \{(1, 1), (1, 2), (2, 1)\} are symmetric.
  • The empty relation R3=∅R_3 = \emptyset is vacuously symmetric (there is no pair (a,b)∈∅(a, b) \in \emptyset that violates the rule!).
  • R4={(1,1),(2,3)}R_4 = \{(1, 1), (2, 3)\} and R5={(1,1),(1,2),(2,1),(2,3)}R_5 = \{(1, 1), (1, 2), (2, 1), (2, 3)\} are not symmetric because (3,2)(3, 2) is missing.

Look at the matrix of A×AA \times A:

  • Each of the nn diagonal entries (ai,ai)(a_i, a_i) is its own mirror, so it freely has 22 choices (included or excluded).
  • Each of the n(n−1)2\frac{n(n - 1)}{2} upper-triangular entries (ai,aj)(a_i, a_j) (i<ji < j) has 22 choices—and once that choice is made, its lower-triangular mirror (aj,ai)(a_j, a_i) has only 11 forced choice (it must match)!

By the Multiplication Principle:

Symmetric relations on A=2n⋅2n(n−1)/2=2n2+n2=2n(n+1)/2\text{Symmetric relations on } A = 2^n \cdot 2^{n(n - 1)/2} = 2^{\frac{n^2 + n}{2}} = 2^{n(n + 1)/2}

5. Relations That Are Both Reflexive and Symmetric: All nn diagonal entries are forced to be included (11 choice each), and the n(n−1)2\frac{n(n - 1)}{2} off-diagonal mirror pairs have 22 choices each:

2n(n−1)/22^{n(n - 1)/2}

6. Antisymmetric Relations: A relation RR on AA is antisymmetric if whenever (a,b)∈R(a, b) \in R and (b,a)∈R(b, a) \in R, we must have a=ba = b. (In other words, for distinct a≠ba \neq b, we can never include both (a,b)(a, b) and (b,a)(b, a) simultaneously!)

Look at the matrix of A×AA \times A:

  • Each of the nn diagonal entries (ai,ai)(a_i, a_i) has a=ba = b, so it never violates antisymmetry: 22 choices each.
  • For each of the n(n−1)2\frac{n(n - 1)}{2} off-diagonal pairs {(ai,aj),(aj,ai)}\{(a_i, a_j), (a_j, a_i)\} (i<ji < j), including both is forbidden, leaving exactly 33 valid choices: include neither, include only (ai,aj)(a_i, a_j), or include only (aj,ai)(a_j, a_i).

By the Multiplication Principle:

Antisymmetric relations on A=2n⋅3n(n−1)/2\text{Antisymmetric relations on } A = 2^n \cdot 3^{n(n - 1)/2}

7. Equivalence Relations (The Bell Number Connection): A relation RR on AA is an equivalence relation if it is simultaneously reflexive, symmetric, and transitive.

Why is counting equivalence relations not a simple power of 22 or 33? Because transitivity links triples of entries across the matrix! Fortunately, a fundamental theorem of Set Theory gives an exact bijection:

  • Every equivalence relation RR on AA partitions AA into pairwise disjoint, non-empty equivalence classes.
  • Conversely, every partition of AA into non-empty subsets uniquely defines an equivalence relation (x∼yx \sim y if and only if xx and yy lie in the same block of the partition).

By the Bijection Principle (Section 01) and Stirling / Bell Numbers (Section 09):

Equivalence relations on an n-element set=Bn=∑r=1nS(n,r)\text{Equivalence relations on an } n\text{-element set} = B_n = \sum_{r=1}^{n} S(n, r)

Bijection Between Set Partitions and Equivalence Relations on A = 1, 2, 3, 4

Example 2: On the set A={1,2,3,4}A = \{1, 2, 3, 4\} (n=4n = 4), find the number of: (a) total relations, (b) reflexive relations, (c) symmetric relations, (d) antisymmetric relations, (e) equivalence relations.

Solution:

  • (a) Total relations: 242=216=65,5362^{4^2} = 2^{16} = 65{,}536.
  • (b) Reflexive relations: 24×3=212=40962^{4 \times 3} = 2^{12} = 4096.
  • (c) Symmetric relations: 24×5/2=210=10242^{4 \times 5 / 2} = 2^{10} = 1024.
  • (d) Antisymmetric relations: 24⋅34×3/2=16×36=16×729=11,6642^4 \cdot 3^{4 \times 3 / 2} = 16 \times 3^6 = 16 \times 729 = 11{,}664.
  • (e) Equivalence relations: B4=S(4,1)+S(4,2)+S(4,3)+S(4,4)=1+7+6+1=15B_4 = S(4, 1) + S(4, 2) + S(4, 3) + S(4, 4) = 1 + 7 + 6 + 1 = 15.

3. Counting Functions Between Finite Sets§

Let AA and BB be finite sets with ∣A∣=m|A| = m (the domain) and ∣B∣=n|B| = n (the codomain). According to the definition of a function, a mapping f:A→Bf : A \to B assigns to every element a∈Aa \in A one and only one image f(a)∈Bf(a) \in B.

Look closely at what that definition means combinatorially: defining a function f:A→Bf : A \to B is 100% identical to distributing mm distinct objects (elements of AA) into nn distinct boxes (elements of BB)!

Injective, Surjective, and Bijective Mappings Between Finite Sets A and B

1. Total Number of Functions§

Each of the mm elements in AA independently chooses its image from the nn elements in BB. By the Multiplication Principle:

Total functions f:A→B=n×n×⋯×n⏟m times=nm\text{Total functions } f : A \to B = \underbrace{n \times n \times \dots \times n}_{m \text{ times}} = n^m

2. Injective (One-One) Functions§

A function f:A→Bf : A \to B is injective (one-one) if distinct inputs always produce distinct outputs: a1≠a2  ⟹  f(a1)≠f(a2)a_1 \neq a_2 \implies f(a_1) \neq f(a_2) (no two balls land in the same box).

  • If m>nm > n: By the Pigeonhole Principle (Section 01), placing mm domain elements into n<mn < m codomain elements guarantees a collision—so there are 00 injective functions.
  • If m≤nm \le n: The first element of AA has nn choices, the second has n−1n - 1, ..., and the mm-th has n−m+1n - m + 1 choices:
Injective functions=nPm=n!(n−m)!(m≤n)\text{Injective functions} = {}^n P_m = \frac{n!}{(n - m)!} \qquad (m \le n)

3. Bijective (One-One and Onto) Functions§

A function is bijective if it is both injective and surjective. This requires m≤nm \le n (for injectivity) and m≥nm \ge n (for surjectivity), so m=nm = n is mandatory! When m=nm = n, every injective function automatically covers all nn elements of BB:

Bijective functions={n!if m=n0if m≠n\text{Bijective functions} = \begin{cases} n! & \text{if } m = n \\ 0 & \text{if } m \neq n \end{cases}

4. Surjective (Onto) Functions§

A function f:A→Bf : A \to B is surjective (onto) if every element b∈Bb \in B is hit by at least one element of AA (its range equals the entire codomain BB).

  • If m<nm < n: Not enough domain elements exist to cover all nn elements of BB, so there are 00 surjective functions.
  • If m≥nm \ge n: Covering every element of BB is literally identical to distributing mm distinct objects into nn distinct non-empty boxes (Section 09)! By PIE and Stirling numbers of the second kind:
Surjective functions=n! S(m,n)=∑k=0n(−1)k(nk)(n−k)m(m≥n)\text{Surjective functions} = n!\,S(m, n) = \sum_{k=0}^{n} (-1)^k \binom{n}{k} (n - k)^m \qquad (m \ge n)

Example 3: Let A={1,2,3,4,5}A = \{1, 2, 3, 4, 5\} (m=5m = 5) and B={a,b,c}B = \{a, b, c\} (n=3n = 3). Find the number of functions f:A→Bf : A \to B that are: (a) unrestricted, (b) injective, (c) surjective, (d) into (not onto).

Solution:

(a) Unrestricted: 35=2433^5 = 243.

(b) Injective: Since m=5>3=nm = 5 > 3 = n, there are 00 injective functions.

(c) Surjective: Using PIE with m=5m = 5 and n=3n = 3:

35−(31)25+(32)15−(33)05=243−3(32)+3(1)−0=1503^5 - \binom{3}{1}2^5 + \binom{3}{2}1^5 - \binom{3}{3}0^5 = 243 - 3(32) + 3(1) - 0 = 150

(Notice also 3! S(5,3)=6×25=1503!\,S(5, 3) = 6 \times 25 = 150!)

(d) Into (not onto): By the Subtraction Principle, 243−150=93243 - 150 = 93.

5. Monotonic Functions§

Suppose both A={1,2,…,m}A = \{1, 2, \dots, m\} and B={1,2,…,n}B = \{1, 2, \dots, n\} are ordered sets of numbers, and we want to count functions that respect order.

Strictly Increasing Functions (i<j  ⟹  f(i)<f(j)i < j \implies f(i) < f(j)): Every strictly increasing function outputs mm distinct values from BB. Conversely, once you choose any unordered subset of mm distinct values from BB, there is only 11 way to assign them in strictly increasing order (smallest value to 11, next smallest to 22, ..., largest to mm)! By the Bijection Principle (for m≤nm \le n):

Strictly increasing functions=(nm)\text{Strictly increasing functions} = \binom{n}{m}

(By the exact same reasoning, the number of strictly decreasing functions is also (nm)\binom{n}{m}.)

Non-Decreasing (Weakly Increasing) Functions (i<j  ⟹  f(i)≤f(j)i < j \implies f(i) \le f(j)): Now output values are allowed to repeat! A non-decreasing function is completely determined by choosing mm values from BB with repetition allowed (unordered) and writing them in non-decreasing order! Equivalently, let xjx_j be the number of domain elements mapping to j∈Bj \in B (1≤j≤n1 \le j \le n); then x1+x2+⋯+xn=mx_1 + x_2 + \dots + x_n = m with xj≥0x_j \ge 0. By Stars and Bars (Section 07):

Non-decreasing functions=(n+m−1m)\text{Non-decreasing functions} = \binom{n + m - 1}{m}

(And by symmetry, the number of non-increasing functions is also (n+m−1m)\binom{n + m - 1}{m}.)

Example 4: How many functions f:{1,2,3,4}→{1,2,3,4,5,6}f : \{1, 2, 3, 4\} \to \{1, 2, 3, 4, 5, 6\} are: (a) strictly increasing, (b) non-decreasing, (c) monotonic (either non-decreasing or non-increasing)?

Solution: Here m=4m = 4 and n=6n = 6.

  • (a) Strictly increasing: (64)=15\binom{6}{4} = 15.
  • (b) Non-decreasing: (6+4−14)=(94)=126\binom{6 + 4 - 1}{4} = \binom{9}{4} = 126.
  • (c) Monotonic: There are 126126 non-decreasing functions and 126126 non-increasing functions. Watch out for overlap! Which functions are both non-decreasing and non-increasing? Precisely the 66 constant functions (f(x)≡cf(x) \equiv c for c∈{1,…,6}c \in \{1, \dots, 6\})! By PIE (avoiding overcounting):
126+126−6=246126 + 126 - 6 = 246

Master Summary Table§

Discrete Structure Count Condition
Subsets of an nn-element set 2n2^n n≥0n \ge 0
Relations from AA to BB 2mn2^{mn} ∣A∣=m,  ∣B∣=n\lvert A \rvert = m,\; \lvert B \rvert = n
Reflexive / Irreflexive relations on AA 2n(n−1)2^{n(n - 1)} ∣A∣=n\lvert A \rvert = n
Symmetric relations on AA 2n(n+1)/22^{n(n + 1)/2} ∣A∣=n\lvert A \rvert = n
Reflexive & Symmetric relations on AA 2n(n−1)/22^{n(n - 1)/2} ∣A∣=n\lvert A \rvert = n
Antisymmetric relations on AA 2n⋅3n(n−1)/22^n \cdot 3^{n(n - 1)/2} ∣A∣=n\lvert A \rvert = n
Equivalence relations on AA Bn=∑r=1nS(n,r)B_n = \sum_{r=1}^n S(n, r) ∣A∣=n≥1\lvert A \rvert = n \ge 1
Total functions f:A→Bf : A \to B nmn^m ∣A∣=m,  ∣B∣=n\lvert A \rvert = m,\; \lvert B \rvert = n
Injective (one-one) functions nPm=n!(n−m)!{}^n P_m = \frac{n!}{(n - m)!} m≤nm \le n (00 if m>nm > n)
Bijective functions n!n! m=nm = n (00 if m≠nm \neq n)
Surjective (onto) functions n! S(m,n)=∑k=0n(−1)k(nk)(n−k)mn!\,S(m, n) = \sum_{k=0}^n (-1)^k \binom{n}{k}(n - k)^m m≥nm \ge n (00 if m<nm < n)
Strictly increasing / decreasing functions (nm)\binom{n}{m} m≤nm \le n
Non-decreasing / non-increasing functions (n+m−1m)\binom{n + m - 1}{m} m,n≥1m, n \ge 1
Section 10 of 11 · Combinatorics

Mark this section complete when you have worked through its proofs.