Study guide: Functions
Overview
A function f: A → B assigns to every element of the domain A exactly one element of the codomain B. Three properties, injective, surjective and bijective, describe whether information is lost, whether everything in B is reachable, and whether an inverse exists.
Many mistakes come from mixing up the codomain (where outputs are allowed to land) with the range (where they actually land).
Key ideas explained
Well-defined functions
Every input needs exactly one output. A rule sending 1 to both a and b is a relation but not a function, and a rule that leaves some input without an output fails too.
Injective (one-to-one)
Different inputs give different outputs: f(x₁) = f(x₂) implies x₁ = x₂. Between finite sets this needs |A| ≤ |B|.
Surjective (onto)
Every element of the codomain is hit, so the range equals the codomain. Between finite sets this needs |A| ≥ |B|.
Bijections, inverses and composition
A function that is both injective and surjective is a bijection and has an inverse f⁻¹. For finite sets of equal size, injective, surjective and bijective are all equivalent. Composition (g ∘ f)(x) = g(f(x)) applies f first, and generally g ∘ f ≠ f ∘ g.
Worked examples
Classifying a function between finite sets
Problem. f: {1,2,3,4} → {a,b,c} with f(1)=a, f(2)=b, f(3)=c, f(4)=a.
- Each input has exactly one output, so f is a function.
- f(1) = f(4) = a, so it is not injective (and the pigeonhole principle forces this, since 4 > 3).
- a, b and c are all hit, so it is surjective.
- Not injective means not bijective, and there is no inverse: a would have to map back to both 1 and 4.
Answer. A surjective function that is not injective, hence not bijective.
Computing a composition
Problem. f: {1,2,3} → {x,y,z} with f(1)=y, f(2)=z, f(3)=x, and g: {x,y,z} → {p,q} with g(x)=p, g(y)=q, g(z)=p. Describe g ∘ f.
- (g ∘ f)(1) = g(y) = q.
- (g ∘ f)(2) = g(z) = p.
- (g ∘ f)(3) = g(x) = p.
- Both p and q are hit, but 2 and 3 share the output p.
Answer. g ∘ f is surjective but not injective. f is a bijection, yet the composition is not injective because g is not.
Enter these same values in the calculator above and compare each step with the working shown here.
Common mistakes to avoid
- Believing a function must hit every codomain element. That is only required for surjectivity.
- Calling f(x) = x² injective on the integers: f(2) = f(−2). It is injective on the non-negative integers.
- Reading g ∘ f left to right. f is applied first.
- Assuming any reversible-looking rule has an inverse. Only bijections do, on their whole codomain.
Where this shows up in computer science
- Hash functions map a huge domain to a small codomain, so by the pigeonhole principle collisions are unavoidable.
- A block cipher must be a bijection on fixed-size blocks so that decryption exists.
- In programming, a type signature is a domain and codomain, and pipelines are compositions.
Test yourself
Try each question before opening the answer.
Is f(x) = 2x + 1 from ℤ to ℤ injective? Surjective?
Injective: 2x₁ + 1 = 2x₂ + 1 gives x₁ = x₂. Not surjective: even integers are never produced.
How many functions go from a 3-element set to a 4-element set, and how many are injective?
4³ = 64 functions; 4·3·2 = 24 are injective.
Is f(x) = x³ from ℝ to ℝ a bijection?
Yes. Its inverse is the cube root.