TExcelTAD
Home › Discrete Mathematics › Finite Cardinality and Mapping Rules

Finite Cardinality and Mapping Rules

How do you compare the sizes of two sets without counting them? Pair up their elements. If a function between them is injective, surjective or bijective, that tells you which set is at least as large. These mapping rules are the foundation of counting: to count a hard set, find a bijection with an easy one.

The mapping rules

For finite sets AA and BB:

If there is a… from AA to BB then
total injective function ∣A∣≤∣B∣\lvert A \rvert \le \lvert B \rvert
total surjective function ∣A∣≥∣B∣\lvert A \rvert \ge \lvert B \rvert
bijection ∣A∣=∣B∣\lvert A \rvert = \lvert B \rvert

Why the first rule holds: an injective total function sends the ∣A∣\lvert A \rvert elements of AA to ∣A∣\lvert A \rvert different elements of BB, so BB has at least that many. The surjective rule is the mirror image: every element of BB receives at least one arrow, and each element of AA sends exactly one, so there are at least as many arrows (elements of AA) as elements of BB.

The contrapositive of the injective rule is the pigeonhole principle: if ∣A∣>∣B∣\lvert A \rvert > \lvert B \rvert, every total function A→BA \to B maps two elements to the same place.

Counting subsets with a bijection

How many subsets does an nn-element set {a1,…,an}\{a_1, \ldots, a_n\} have? Map each subset SS to the bit string b1b2⋯bnb_1 b_2 \cdots b_n with bi=1b_i = 1 exactly when ai∈Sa_i \in S. This is a bijection (different subsets give different strings, and every string describes a subset), so

∣P(A)∣=∣{0,1}n∣=2n\lvert \mathcal{P}(A) \rvert = \lvert \{0, 1\}^n \rvert = 2^n
python · try it yourself
from itertools import combinations, product

A = ["a", "b", "c", "d"]
subsets = [frozenset(c) for k in range(len(A) + 1) for c in combinations(A, k)]
encode = lambda S: "".join("1" if x in S else "0" for x in A)
codes = [encode(S) for S in subsets]
all_strings = ["".join(p) for p in product("01", repeat=len(A))]
print("subsets:", len(subsets), " bit strings:", len(all_strings))
print("encoding is injective:", len(set(codes)) == len(codes))
print("encoding is surjective:", set(codes) == set(all_strings))
subsets: 16  bit strings: 16
encoding is injective: True
encoding is surjective: True

Counting functions

The number of total functions from AA to BB is ∣B∣∣A∣\lvert B \rvert^{\lvert A \rvert}: each element of AA independently picks one of the ∣B∣\lvert B \rvert values. That is why the set of all functions A→BA \to B is often written BAB^A. The number of injective functions is ∣B∣(∣B∣−1)⋯(∣B∣−∣A∣+1)\lvert B \rvert (\lvert B \rvert - 1) \cdots (\lvert B \rvert - \lvert A \rvert + 1): each next element must avoid the values already used.

python · try it yourself
from itertools import product

def count_functions(m, n):
    """All functions from an m-set to an n-set, and how many are injective / surjective."""
    total = inj = surj = 0
    for f in product(range(n), repeat=m):
        total += 1
        inj += len(set(f)) == m
        surj += len(set(f)) == n
    return total, inj, surj

for m, n in [(2, 3), (3, 3), (4, 3), (3, 4)]:
    t, i, s = count_functions(m, n)
    print(f"|A|={m}, |B|={n}: functions={t} (= {n}^{m}), injective={i}, surjective={s}")
|A|=2, |B|=3: functions=9 (= 3^2), injective=6, surjective=0
|A|=3, |B|=3: functions=27 (= 3^3), injective=6, surjective=6
|A|=4, |B|=3: functions=81 (= 3^4), injective=0, surjective=36
|A|=3, |B|=4: functions=64 (= 4^3), injective=24, surjective=0

The table illustrates the rules: injective functions exist only when ∣A∣≤∣B∣\lvert A \rvert \le \lvert B \rvert, surjective ones only when ∣A∣≥∣B∣\lvert A \rvert \ge \lvert B \rvert, and bijections only when the sizes are equal (3!=63! = 6 for two 3-element sets).

In software

The bijection trick is how programs enumerate combinatorial objects: loop over the integers 0,…,2n−10, \ldots, 2^n - 1 and decode each as a subset (bitmask dynamic programming in contest code), or decode an integer as a permutation (the factorial number system). Perfect hashing builds an injective function from a known key set into a table with no collisions.

Exercises

Exercise 1: Which rule?

There is an injective total function from the set of students into the set of seats. What can you conclude?

Answer

There are at least as many seats as students.

Solution

Injective means no two students share a seat, so ∣students∣≤∣seats∣\lvert \text{students} \rvert \le \lvert \text{seats} \rvert by the injective mapping rule.

Exercise 2: Counting functions

How many functions are there from {1,2,3,4}\{1, 2, 3, 4\} to {a,b}\{a, b\}? How many are injective?

Answer

16; none are injective.

Solution

Each of 4 inputs picks one of 2 values: 24=162^4 = 16. An injective function needs 4 different values, but only 2 exist (pigeonhole).

Exercise 3: A bijection for a count

Find a bijection that shows the number of subsets of {1,…,n}\{1, \ldots, n\} with an even number of elements equals the number with an odd number of elements, for n≥1n \ge 1.

Answer

Toggle the element 1: S↦S⊕{1}S \mapsto S \oplus \{1\}.

Solution

Define f(S)=S∪{1}f(S) = S \cup \{1\} if 1∉S1 \notin S, and f(S)=S−{1}f(S) = S - \{1\} if 1∈S1 \in S. It changes the size by exactly 1, so it maps even-sized subsets to odd-sized ones. Applying it twice gives back SS, so ff is its own inverse and therefore a bijection. Hence each parity class has 2n−12^{n - 1} subsets.

python · try it yourself
from itertools import combinations

n = 5
sizes = [k for k in range(n + 1) for _ in combinations(range(n), k)]
print("even:", sum(1 for k in sizes if k % 2 == 0), " odd:", sum(1 for k in sizes if k % 2 == 1))
even: 16  odd: 16

Test yourself

If there is a total surjective function from AA to BB, then…

  1. ∣A∣≤∣B∣\lvert A \rvert \le \lvert B \rvert
  2. ∣A∣≥∣B∣\lvert A \rvert \ge \lvert B \rvert
  3. ∣A∣=∣B∣\lvert A \rvert = \lvert B \rvert
  4. nothing follows

How many functions are there from a 3-element set to a 5-element set?

  1. 15
  2. 125
  3. 243
  4. 60

Which bijection proves that an nn-element set has 2n2^n subsets?

  1. subsets to integers 1..n1..n
  2. subsets to nn-bit strings
  3. subsets to permutations
  4. subsets to pairs