Sets, Relations & Functions
Functions
Functions as special relations: domain, codomain and range, one-to-one, onto and bijective functions, composition, inverses, floor and ceiling, and counting functions.
A function f : A → B assigns to each element of A exactly one element of B.
- A is the domain; B is the codomain.
- f(a) = b: b is the image of a; a is a pre-image of b.
- The range is the set of all images: {f(a) : a ∈ A} ⊆ B.
A relation fails to be a function if some element of A has no image or two images.
Types of functions
| Type | Also called | Definition |
|---|---|---|
| One-to-one | injective | different inputs → different outputs: f(a₁) = f(a₂) ⇒ a₁ = a₂ |
| Onto | surjective | every element of B is hit: range = codomain |
| Bijection | one-to-one correspondence | both injective and surjective |
| Function on ℤ → ℤ | Injective? | Surjective? |
|---|---|---|
| f(x) = x + 1 | yes | yes — bijection |
| f(x) = 2x | yes | no (odd numbers are missed) |
| f(x) = x² | no (f(2) = f(−2)) | no |
| f(x) = ⌊x / 2⌋ | no | yes |
def classify(f, A, B):
images = [f(a) for a in A]
injective = len(set(images)) == len(images)
surjective = set(images) == set(B)
return f"injective={injective}, surjective={surjective}, bijective={injective and surjective}"
A = range(-3, 4)
print("x+1 :", classify(lambda x: x + 1, A, range(-2, 5)))
print("x^2 :", classify(lambda x: x * x, A, range(0, 10)))
print("2x :", classify(lambda x: 2 * x, A, range(-6, 7)))x+1 : injective=True, surjective=True, bijective=True x^2 : injective=False, surjective=False, bijective=False 2x : injective=True, surjective=False, bijective=False
Composition
(g ∘ f)(x) = g(f(x)) — apply f first, then g. Composition is associative but not commutative:
f = lambda x: x + 3
g = lambda x: 2 * x
print("(g∘f)(5) =", g(f(5))) # 2 * (5 + 3) = 16
print("(f∘g)(5) =", f(g(5))) # 2 * 5 + 3 = 13(g∘f)(5) = 16 (f∘g)(5) = 13
Inverse functions
A function has an inverse f⁻¹ if and only if it is a bijection. Then f⁻¹(b) = a exactly when f(a) = b, and f⁻¹(f(x)) = x. Example: f(x) = 3x + 2 on ℝ has f⁻¹(y) = (y − 2)/3.
Floor and ceiling
- ⌊x⌋ (floor): the largest integer ≤ x. ⌊3.7⌋ = 3, ⌊−3.7⌋ = −4.
- ⌈x⌉ (ceiling): the smallest integer ≥ x. ⌈3.2⌉ = 4, ⌈−3.2⌉ = −3.
They appear constantly in algorithm analysis: a binary tree with n nodes has height at least ⌈log₂(n + 1)⌉ − 1; splitting n items into pages of 10 needs ⌈n/10⌉ pages.
Counting functions
For finite sets with |A| = m and |B| = n:
| Count | Formula |
|---|---|
| all functions A → B | nᵐ |
| injective functions (m ≤ n) | n!/(n − m)! |
| bijections (m = n) | n! |
| surjective functions | Σₖ (−1)ᵏ C(n, k)(n − k)ᵐ (by inclusion–exclusion) |
Pigeonhole connection
If m > n, no function A → B can be injective: some two elements of A must share an image. That is the pigeonhole principle.
Test yourself
4 questions · pick an answer to check it instantly
- 1
f : ℤ → ℤ, f(x) = 3x. Is f onto?
Values like 1 and 2 have no pre-image. - 2
Which functions have inverses?
An inverse must be defined on the whole codomain and be single-valued. - 3
If f(x) = x² and g(x) = x + 1, what is (f ∘ g)(2)?
f(g(2)) = f(3) = 9. - 4
How many functions are there from a 3-element set to a 2-element set?
Each of the 3 elements independently chooses one of 2 images: 2³ = 8.