Introduction

A function assigns to every element of one set exactly one element of another. Hash functions, encodings, permutations of an array, the transition function of an automaton and the indexing of an array are all functions. In discrete mathematics, functions are the tool for comparing the sizes of sets (bijections), for counting (how many maps of a given kind exist) and for building algebraic structures (monoids and groups are sets with a binary operation, which is a function).

Prerequisites

  • Sets: Cartesian product, power set, cardinality.
  • Relations: a function is a special relation; composition and inverse of relations.

Core concepts

Definition and terminology

A function f:A→Bf : A \to B is a relation f⊆A×Bf \subseteq A \times B such that every a∈Aa \in A is related to exactly one b∈Bb \in B, written f(a)=bf(a) = b.

  • AA is the domain, BB the codomain.
  • f(a)f(a) is the image of aa; aa is a preimage of f(a)f(a).
  • The range (image of ff) is f(A)={f(a)∣a∈A}⊆Bf(A) = \{f(a) \mid a \in A\} \subseteq B. The range can be smaller than the codomain.
  • For S⊆AS \subseteq A, f(S)={f(s)∣s∈S}f(S) = \{f(s) \mid s \in S\}. For T⊆BT \subseteq B, the preimage f−1(T)={a∈A∣f(a)∈T}f^{-1}(T) = \{a \in A \mid f(a) \in T\} is defined for every function, invertible or not.
  • Two functions are equal if they have the same domain, the same codomain and the same value at every point.

If every input has at most one image but some have none, the relation is a proper partial function, not a total function on the stated domain. If an input has two images, it is not even a partial function.

Injective, surjective, bijective

TypeDefinitionEquivalent testFinite-set consequence (∣A∣=m\lvert A \rvert = m, ∣B∣=n\lvert B \rvert = n)
Injective (one-to-one)f(a1)=f(a2)→a1=a2f(a_1) = f(a_2) \to a_1 = a_2distinct inputs give distinct outputsneeds m≤nm \le n
Surjective (onto)∀b∈B ∃a∈A: f(a)=b\forall b \in B\ \exists a \in A:\ f(a) = brange = codomainneeds m≥nm \ge n
Bijectiveinjective and surjectivehas an inverse functionneeds m=nm = n

For finite sets of the same size, injective, surjective and bijective are equivalent: a map f:A→Af : A \to A on a finite set is one-to-one if and only if it is onto. This fails for infinite sets: f(n)=2nf(n) = 2n on N\mathbb{N} is injective but not surjective.

Surjectivity depends on the codomain: f(x)=x2f(x) = x^2 is surjective as a map R→[0,∞)\mathbb{R} \to [0, \infty) but not as a map R→R\mathbb{R} \to \mathbb{R}. Injectivity depends on the domain: the same rule is injective on [0,∞)[0, \infty) but not on R\mathbb{R}.

Injective, surjective and bijective mappings
Figure 1. Arrow diagrams of four maps: injective but not surjective, surjective but not injective, bijective, and a relation that is not a function.

Composition

For f:A→Bf : A \to B and g:B→Cg : B \to C, the composition g∘f:A→Cg \circ f : A \to C is (g∘f)(a)=g(f(a))(g \circ f)(a) = g(f(a)): apply ff first. Composition is associative, h∘(g∘f)=(h∘g)∘fh \circ (g \circ f) = (h \circ g) \circ f, but not commutative in general.

IfThen
ff and gg injectiveg∘fg \circ f injective
ff and gg surjectiveg∘fg \circ f surjective
g∘fg \circ f injectiveff injective (gg need not be)
g∘fg \circ f surjectivegg surjective (ff need not be)

Example for the third row: A={1}A = \{1\}, B={1,2}B = \{1, 2\}, C={1}C = \{1\}, f(1)=1f(1) = 1, g(1)=g(2)=1g(1) = g(2) = 1. Then g∘fg \circ f is injective (one-element domain), ff is injective, but gg is not.

Inverse functions

f:A→Bf : A \to B has an inverse f−1:B→Af^{-1} : B \to A with f−1(f(a))=af^{-1}(f(a)) = a and f(f−1(b))=bf(f^{-1}(b)) = b if and only if ff is a bijection. Then f−1f^{-1} is also a bijection and (g∘f)−1=f−1∘g−1(g \circ f)^{-1} = f^{-1} \circ g^{-1}.

Example: f(x)=3x+2f(x) = 3x + 2 on R\mathbb{R} is a bijection with f−1(y)=(y−2)/3f^{-1}(y) = (y - 2)/3. On Z\mathbb{Z}, the same rule is injective but not surjective (1 has no preimage), so it has no inverse as a map Z→Z\mathbb{Z} \to \mathbb{Z}.

Images and preimages of unions and intersections

For f:A→Bf : A \to B, S1,S2⊆AS_1, S_2 \subseteq A and T1,T2⊆BT_1, T_2 \subseteq B:

  • f(S1∪S2)=f(S1)∪f(S2)f(S_1 \cup S_2) = f(S_1) \cup f(S_2), but only f(S1∩S2)⊆f(S1)∩f(S2)f(S_1 \cap S_2) \subseteq f(S_1) \cap f(S_2), with equality for all S1,S2S_1, S_2 if and only if ff is injective.
  • Preimages respect everything: f−1(T1∪T2)=f−1(T1)∪f−1(T2)f^{-1}(T_1 \cup T_2) = f^{-1}(T_1) \cup f^{-1}(T_2), f−1(T1∩T2)=f−1(T1)∩f−1(T2)f^{-1}(T_1 \cap T_2) = f^{-1}(T_1) \cap f^{-1}(T_2), f−1(T‾)=f−1(T)‾f^{-1}(\overline{T}) = \overline{f^{-1}(T)}.

Floor and ceiling

For real xx, ⌊x⌋\lfloor x \rfloor is the largest integer ≤x\le x and ⌈x⌉\lceil x \rceil the smallest integer ≥x\ge x. So ⌊−2.5⌋=−3\lfloor -2.5 \rfloor = -3 and ⌈−2.5⌉=−2\lceil -2.5 \rceil = -2. Useful facts for integer nn and real xx: ⌊x+n⌋=⌊x⌋+n\lfloor x + n \rfloor = \lfloor x \rfloor + n; ⌈x⌉=−⌊−x⌋\lceil x \rceil = -\lfloor -x \rfloor; ⌊n/2⌋+⌈n/2⌉=n\lfloor n/2 \rfloor + \lceil n/2 \rceil = n; x−1<⌊x⌋≤x≤⌈x⌉<x+1x - 1 < \lfloor x \rfloor \le x \le \lceil x \rceil < x + 1.

Counting functions

Let ∣A∣=m|A| = m and ∣B∣=n|B| = n.

Functions A→BA \to BCountReason
Allnmn^meach of the mm inputs picks one of nn outputs
Injectiven(n−1)⋯(n−m+1)=n!(n−m)!n(n-1)\cdots(n-m+1) = \dfrac{n!}{(n-m)!} if m≤nm \le n, else 0ordered choice of distinct images
Surjective∑k=0n(−1)k(nk)(n−k)m=n! S(m,n)\displaystyle \sum_{k=0}^{n} (-1)^k \binom{n}{k} (n-k)^m = n!\, S(m, n)inclusion-exclusion on missed outputs
Bijectiven!n! if m=nm = n, else 0permutations
Partial functions(n+1)m(n+1)^meach input maps to an output or is undefined
Relations A→BA \to B2mn2^{mn}for comparison

Here S(m,n)S(m, n) is the Stirling number of the second kind (partitions of an mm-set into nn non-empty blocks; see Relations). A surjection is a partition of AA into nn blocks, followed by an assignment of the nn blocks to the nn outputs.

Derivation of the onto formula. Let EiE_i be the set of functions that never take the value bib_i. A function is onto exactly when it lies in no EiE_i. Any kk of the EiE_i together leave n−kn - k allowed outputs, giving (n−k)m(n-k)^m functions, so inclusion-exclusion gives the alternating sum. The full principle is in Counting.

The pigeonhole principle

If m>nm > n, no function A→BA \to B is injective: some output has at least two preimages. Generalised form: some output has at least ⌈m/n⌉\lceil m/n \rceil preimages.

Worked examples

Example 1: counting maps

∣A∣=4|A| = 4, ∣B∣=3|B| = 3. All functions: 34=813^4 = 81. Injective: 0 (since 4>34 > 3). Onto:

34−(31)24+(32)14−(33)04=81−48+3−0=36=3!⋅S(4,3)=6⋅6.3^4 - \binom{3}{1} 2^4 + \binom{3}{2} 1^4 - \binom{3}{3} 0^4 = 81 - 48 + 3 - 0 = 36 = 3! \cdot S(4, 3) = 6 \cdot 6 .

Reverse direction, ∣A∣=3|A| = 3, ∣B∣=4|B| = 4: all 43=644^3 = 64, injective 4⋅3⋅2=244 \cdot 3 \cdot 2 = 24, onto 0.

Example 2: classify

MapInjectiveSurjective
f:Z→Zf : \mathbb{Z} \to \mathbb{Z}, f(n)=n+3f(n) = n + 3YesYes
f:Z→Zf : \mathbb{Z} \to \mathbb{Z}, f(n)=2nf(n) = 2nYesNo (odd numbers missed)
f:Z→Nf : \mathbb{Z} \to \mathbb{N}, f(n)=∣n∣f(n) = \lvert n \rvertNo (f(1)=f(−1)f(1) = f(-1))Yes (with N\mathbb{N} including 0)
f:R→Rf : \mathbb{R} \to \mathbb{R}, f(x)=x3f(x) = x^3YesYes
f:Z→Zf : \mathbb{Z} \to \mathbb{Z}, f(n)=⌊n/2⌋f(n) = \lfloor n/2 \rfloorNoYes
f:N×N→Nf : \mathbb{N} \times \mathbb{N} \to \mathbb{N}, f(a,b)=2a3bf(a, b) = 2^a 3^bYesNo

Example 3: functions on a set to itself

How many functions f:{1,2,3}→{1,2,3}f : \{1, 2, 3\} \to \{1, 2, 3\} are not bijections? 33−3!=27−6=213^3 - 3! = 27 - 6 = 21. How many satisfy f(f(x))=xf(f(x)) = x for every xx (involutions)? The identity, plus the three that swap one pair and fix the third element: 4.

Example 4: pigeonhole

Among any 13 people, two share a birth month (⌈13/12⌉=2\lceil 13/12 \rceil = 2). How many integers must be chosen from 1 to 100 to guarantee that two have the same remainder mod 7? 7+1=87 + 1 = 8.

Important properties and observations

  • A function f:A→Af : A \to A on a finite set is injective if and only if it is surjective. For infinite sets these are independent.
  • ∣A∣≤∣B∣|A| \le |B| for finite sets if and only if an injection A→BA \to B exists; ∣A∣=∣B∣|A| = |B| if and only if a bijection exists. This is how cardinality of infinite sets is defined.
  • The number of injective functions from an mm-set to an nn-set is the number of mm-permutations P(n,m)P(n, m).
  • A strictly increasing function R→R\mathbb{R} \to \mathbb{R} is injective. A continuous function on an interval is injective if and only if it is strictly monotone.
  • Characteristic functions: subsets of AA correspond bijectively to functions A→{0,1}A \to \{0, 1\}, which proves ∣P(A)∣=2∣A∣|\mathcal{P}(A)| = 2^{|A|}.

GATE examination perspective

  • Counting questions. Number of functions, injections, surjections and bijections between finite sets. Check the onto formula on small cases (for m=nm = n it gives n!n!).
  • Classification questions. Decide injective or surjective for a map on Z\mathbb{Z}, N\mathbb{N} or R\mathbb{R}. Always check the stated domain and codomain.
  • Composition questions. Use the table: g∘fg \circ f injective forces ff injective; g∘fg \circ f onto forces gg onto.
  • Trap: a function on an infinite set can be injective without being surjective.
  • Trap: onto count is 0 when m<nm < n; injective count is 0 when m>nm > n.

Summary

Sources

  1. K. H. Rosen, Discrete Mathematics and Its Applications, 7th ed., McGraw-Hill, 2012: Sections 2.3 (Functions), 2.5 (Cardinality of Sets), 6.2 (The Pigeonhole Principle) and 8.6 (Applications of Inclusion-Exclusion).

Next steps