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 and :
| If there is a… from to | then |
|---|---|
| total injective function | |
| total surjective function | |
| bijection |
Why the first rule holds: an injective total function sends the elements of to different elements of , so has at least that many. The surjective rule is the mirror image: every element of receives at least one arrow, and each element of sends exactly one, so there are at least as many arrows (elements of ) as elements of .
The contrapositive of the injective rule is the pigeonhole principle: if , every total function maps two elements to the same place.
Counting subsets with a bijection
How many subsets does an -element set have? Map each subset to the bit string with exactly when . This is a bijection (different subsets give different strings, and every string describes a subset), so
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 to is : each element of independently picks one of the values. That is why the set of all functions is often written . The number of injective functions is : each next element must avoid the values already used.
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 , surjective ones only when , and bijections only when the sizes are equal ( for two 3-element sets).
In software
The bijection trick is how programs enumerate combinatorial objects: loop over the integers 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 by the injective mapping rule.
Exercise 2: Counting functions
How many functions are there from to ? How many are injective?
Answer
16; none are injective.
Solution
Each of 4 inputs picks one of 2 values: . 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 with an even number of elements equals the number with an odd number of elements, for .
Answer
Toggle the element 1: .
Solution
Define if , and if . It changes the size by exactly 1, so it maps even-sized subsets to odd-sized ones. Applying it twice gives back , so is its own inverse and therefore a bijection. Hence each parity class has subsets.
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 to , then…
- nothing follows
How many functions are there from a 3-element set to a 5-element set?
- 15
- 125
- 243
- 60
Which bijection proves that an -element set has subsets?
- subsets to integers
- subsets to -bit strings
- subsets to permutations
- subsets to pairs