Bytebook Compiler

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.

3 min read2 runnable examples4-question quiz

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
Python
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)))
Output
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:

Python
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
Output
(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. 1

    f : ℤ → ℤ, f(x) = 3x. Is f onto?

  2. 2

    Which functions have inverses?

  3. 3

    If f(x) = x² and g(x) = x + 1, what is (f ∘ g)(2)?

  4. 4

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