DISCRETE MATH • SETS, RELATIONS, AND FUNCTIONS

Function Composition and Inverses

How combining and reversing functions underpins algorithm design, cryptography, and algebraic reasoning.

Historical Context & Motivation

The idea of applying one process after another—what we now call function composition—is so fundamental that it predates the modern notion of a function itself. Ancient astronomers chained geometric transformations to predict planetary positions, and medieval algebraists composed arithmetic operations to solve equations. Yet it was not until the seventeenth and eighteenth centuries that mathematicians began to treat functions as first-class objects that could be combined, inverted, and studied in their own right.

The formal framework for inverse functions crystallized alongside the development of group theory and abstract algebra in the nineteenth century, where the notion of an 'undoing' operation became central to understanding symmetry and structure. Today, composition and inversion are indispensable in discrete mathematics: they appear in database query pipelines, compiler passes, encryption–decryption pairs, and the algebraic structures that unify all of these applications.

1694
Leibniz Introduces 'Function'
Gottfried Wilhelm Leibniz coins the term functio to describe quantities that depend on a variable, laying the linguistic groundwork for treating mappings as objects.
1748
Euler Formalizes Notation
Leonhard Euler's Introductio in Analysin Infinitorum popularizes the f(x) notation, making it natural to write composed expressions like f(g(x)).
1854
Cayley and Group Theory
Arthur Cayley publishes foundational work on groups of permutations, where composition of bijections and their inverses become the central operations.
1888
Dedekind's Set-Theoretic Functions
Richard Dedekind defines functions as mappings between sets, giving composition and inversion their modern set-theoretic formulations used throughout discrete mathematics.
1936
Church and Lambda Calculus
Alonzo Church develops the lambda calculus, where function composition is the primary computational mechanism, directly influencing functional programming languages.

The central questions these developments address are deceptively simple: given two functions f and g, when and how can we form a new function by chaining them? And under what conditions can we 'undo' a function—that is, find a function f−1 such that composing f with f−1 returns us to where we started? Answering these questions rigorously requires the language of sets, domains, codomains, and the classification of functions as injective, surjective, or bijective.

Core Principles & Definitions

Before diving into composition and inverses, recall that a function f : A → B is a relation from set A to set B in which every element of A is paired with exactly one element of B. The set A is the domain, B is the codomain, and the subset of B actually hit by f is the range (or image). This precise setup—distinguishing domain, codomain, and range—matters enormously when we compose functions or attempt to invert them.

1

Function Composition (g ∘ f)

Given f : A → B and g : B → C, the composition g ∘ f : A → C is defined by (g ∘ f)(x) = g(f(x)). The codomain of f must match (or be a subset of) the domain of g.
2

Injective (One-to-One)

A function f : A → B is injective if f(a₁) = f(a₂) implies a₁ = a₂. No two distinct elements in A map to the same element in B. Injectivity is necessary for a left inverse to exist.
3

Surjective (Onto)

A function f : A → B is surjective if for every b ∈ B there exists at least one a ∈ A with f(a) = b. The range equals the entire codomain. Surjectivity is necessary for a right inverse to exist.
4

Bijective (One-to-One and Onto)

A function that is both injective and surjective is bijective. Bijections are precisely the functions that possess a two-sided inverse f⁻¹ : B → A satisfying f⁻¹ ∘ f = id_A and f ∘ f⁻¹ = id_B.
5

Identity Function (id)

The identity function id_A : A → A maps every element to itself: id_A(a) = a. It serves as the neutral element for composition: f ∘ id_A = f and id_B ∘ f = f.
KEY TAKEAWAY
Think of function composition like an assembly line in a factory: raw material enters machine f, which produces a semi-finished product, and that product immediately enters machine g, which outputs the final result. The order matters—reversing the machines changes the output. An inverse function is like running the assembly line in reverse: if every product is unique (injective) and every possible product actually gets made (surjective), you can trace any final output back to exactly one piece of raw material.

Visual Explanation — Composition as a Pipeline

The diagram shows three sets A, B, and C with elements mapped by f (cyan arrows) and g (violet arrows). The dashed pink arc represents the composite g ∘ f, which sends each element of A directly to C by first applying f, then g. For example, 1 maps to a under f, then a maps to y under g, so (g ∘ f)(1) = y.

The arrow diagram above illustrates the essence of composition. Each element of A has exactly one arrow under f leading to B, and each element of B has exactly one arrow under g leading to C. The composite g ∘ f is obtained by following both arrows in sequence. Observe that the intermediate set B is 'hidden' in the composite—an external observer sees only the net mapping from A to C. This perspective is critical in software engineering, where intermediate data structures may be optimized away, and in algebra, where composition lets us build complex transformations from simple, well-understood pieces.

Notation Warning
In g ∘ f, the function on the right (f) is applied first. This right-to-left convention matches how we evaluate g(f(x))—start with the innermost function. Some computer science texts use left-to-right (pipeline) notation: f ; g or f >> g. Always check which convention a given text uses.

Mathematical Framework

Composition — Formal Definition

COMPOSITION DEFINITION
(g ∘ f)(x) = g(f(x)), for all x ∈ A
where f : A → B and g : B → C. The resulting function g ∘ f has domain A and codomain C.

Composition is associative but generally not commutative. Associativity means that for f : A → B, g : B → C, and h : C → D, we have h ∘ (g ∘ f) = (h ∘ g) ∘ f. This can be verified element-wise: both sides evaluate to h(g(f(x))) for every x ∈ A. Non-commutativity means that g ∘ f and f ∘ g are generally not equal—and in fact, f ∘ g may not even be defined when the domains and codomains do not align.

ASSOCIATIVITY OF COMPOSITION
h ∘ (g ∘ f) = (h ∘ g) ∘ f
for f : A → B, g : B → C, h : C → D. Both sides equal the function x ↦ h(g(f(x))).

Inverse Functions — Formal Definition

INVERSE FUNCTION
f⁻¹ ∘ f = id_A and f ∘ f⁻¹ = id_B
A function f : A → B has a (two-sided) inverse f⁻¹ : B → A if and only if f is bijective. Here id_A and id_B are the identity functions on A and B respectively.

The requirement of bijectivity is not merely a technicality—it is structurally necessary. If f fails to be injective, then two distinct elements a₁ ≠ a₂ satisfy f(a₁) = f(a₂) = b, and there is no well-defined way to assign f⁻¹(b) to a unique preimage. If f fails to be surjective, then some b ∈ B has no preimage, and f⁻¹(b) would be undefined. The left inverse (a function g with g ∘ f = id_A) exists if and only if f is injective, and the right inverse (a function h with f ∘ h = id_B) exists if and only if f is surjective. When both exist, they coincide and equal the unique two-sided inverse.

INVERSE OF A COMPOSITION
(g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹
When both f and g are bijections, the inverse of their composition reverses the order—akin to removing layers in the opposite order they were applied (the 'socks and shoes' rule).
🧦 Socks-and-Shoes Principle
To undo putting on socks then shoes, you remove the shoes first, then the socks. Similarly, to invert g ∘ f, you first invert g (undo the last operation), then invert f (undo the first). This principle generalizes: (h ∘ g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹ ∘ h⁻¹.

Injectivity, Surjectivity, and Invertibility

Understanding when a function can be inverted requires classifying it by how it relates its domain and codomain. The following diagram contrasts the three key properties—injective, surjective, and bijective—using concrete finite-set examples. These properties also interact with composition: composing two injections yields an injection, composing two surjections yields a surjection, and composing two bijections yields a bijection.

Three panels compare an injective function (left, cyan), a surjective function (center, pink), and a bijective function (right, amber). Only the bijective function possesses a two-sided inverse. Note how injectivity guarantees a left inverse and surjectivity guarantees a right inverse; bijectivity combines both properties.

Composition Preserves Injectivity and Surjectivity

How composition interacts with injectivity and surjectivity
Property of f and gProperty of g ∘ fProof Sketch
Both injectiveInjectiveg(f(a₁)) = g(f(a₂)) ⇒ f(a₁) = f(a₂) (g inj.) ⇒ a₁ = a₂ (f inj.)
Both surjectiveSurjectiveFor c ∈ C, ∃ b with g(b)=c (g surj.), ∃ a with f(a)=b (f surj.), so (g∘f)(a)=c.
Both bijectiveBijectiveCombines both results above; (g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹.
g ∘ f injectivef must be injectivef(a₁)=f(a₂) ⇒ g(f(a₁))=g(f(a₂)) ⇒ a₁=a₂ by injectivity of g∘f.
g ∘ f surjectiveg must be surjectiverange(g∘f) ⊆ range(g), so if range(g∘f) = C then range(g) = C.

Worked Example — Composition and Inversion on Finite Sets

Consider the sets A = {1, 2, 3}, B = {a, b, c}, and C = {x, y, z}. Define f : A → B by f(1) = b, f(2) = c, f(3) = a, and define g : B → C by g(a) = z, g(b) = x, g(c) = y. We will compute g ∘ f, verify that both f and g are bijections, find their inverses, and confirm that (g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹.

Composition and Inverse on {1,2,3} → {a,b,c} → {x,y,z}
1
Step 1 — Compute g ∘ fApply f first, then g to each element of A. (g ∘ f)(1) = g(f(1)) = g(b) = x. (g ∘ f)(2) = g(f(2)) = g(c) = y. (g ∘ f)(3) = g(f(3)) = g(a) = z.
g ∘ f : 1 ↦ x, 2 ↦ y, 3 ↦ z
2
Step 2 — Verify f is bijectiveInjective: f(1) = b, f(2) = c, f(3) = a — all images are distinct, so f is one-to-one. Surjective: the range {a, b, c} equals the codomain B, so f is onto. Hence f is bijective.
f is bijective ✓
3
Step 3 — Find f⁻¹Reverse each pair: f(1) = b ⇒ f⁻¹(b) = 1, f(2) = c ⇒ f⁻¹(c) = 2, f(3) = a ⇒ f⁻¹(a) = 3.
f⁻¹ : a ↦ 3, b ↦ 1, c ↦ 2
4
Step 4 — Find g⁻¹Similarly: g(a) = z ⇒ g⁻¹(z) = a, g(b) = x ⇒ g⁻¹(x) = b, g(c) = y ⇒ g⁻¹(y) = c.
g⁻¹ : x ↦ b, y ↦ c, z ↦ a
5
Step 5 — Verify (g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹Compute f⁻¹ ∘ g⁻¹ on each element of C. (f⁻¹ ∘ g⁻¹)(x) = f⁻¹(g⁻¹(x)) = f⁻¹(b) = 1. (f⁻¹ ∘ g⁻¹)(y) = f⁻¹(g⁻¹(y)) = f⁻¹(c) = 2. (f⁻¹ ∘ g⁻¹)(z) = f⁻¹(g⁻¹(z)) = f⁻¹(a) = 3. Now check (g ∘ f)⁻¹ by reversing g ∘ f: 1↦x ⇒ x↦1, 2↦y ⇒ y↦2, 3↦z ⇒ z↦3. These match exactly.
(g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹ : x ↦ 1, y ↦ 2, z ↦ 3 ✓

Strengths, Limitations, and Common Pitfalls

Comparison of composition and inverse properties
Property / FeatureComposition (g ∘ f)Inverse (f⁻¹)
When definedWhenever codomain of f ⊆ domain of gOnly when f is bijective (for two-sided inverse)
AssociativityAlways associative: h ∘ (g ∘ f) = (h ∘ g) ∘ fN/A (single operation, not chained in the same sense)
CommutativityGenerally NOT commutativeInvolution: (f⁻¹)⁻¹ = f
Preserves structurePreserves injectivity, surjectivity, bijectivityInverse of a bijection is a bijection
Common pitfallConfusing g ∘ f with f ∘ g (order matters!)Attempting to invert non-injective or non-surjective functions
Finite-set computationO(|A|) — evaluate f then g for each elementO(|A|) — reverse all pairs in the function table
KEY TAKEAWAY
Composition and inversion together give functions an algebraic structure analogous to multiplication and division in arithmetic. Composition builds complexity by chaining; inversion deconstructs it. The identity function plays the role of '1' (the multiplicative identity), and a bijection's inverse plays the role of a reciprocal. This analogy is not merely informal—it is made precise in group theory, where the set of all bijections on a set forms a group under composition, with the identity function as the neutral element and function inverses as group inverses.

Connections to Advanced Theory

Function composition and inverses serve as the gateway to several advanced mathematical structures. In group theory, the set of all bijections from a set to itself forms the symmetric group Sₙ, where the group operation is composition and every element has an inverse. In category theory, composition of morphisms is the fundamental operation, and isomorphisms are precisely the morphisms with two-sided inverses. In cryptography, encryption and decryption are modeled as a function and its inverse—the security of RSA depends on the computational difficulty of inverting certain one-way functions.

From discrete functions to advanced structures
Concept in This LessonAdvanced GeneralizationWhere It Appears
Composition g ∘ fMorphism composition in categoriesCategory theory, algebraic topology
Identity function id_AIdentity morphism, neutral element of a monoid/groupAbstract algebra, monoid theory
Bijection with inverseIsomorphism / group elementGroup theory (Sₙ), ring isomorphisms
Socks-and-shoes rule(AB)⁻¹ = B⁻¹A⁻¹ in matrix groupsLinear algebra, Lie groups
Left / right inverseSection / retraction in category theoryHomotopy theory, homological algebra

As you continue in discrete mathematics and theoretical computer science, you will encounter these ideas repeatedly. Permutation groups underpin combinatorics and coding theory. The lambda calculus—built entirely on function abstraction and application (composition)—is the theoretical foundation of functional programming languages like Haskell and ML. Understanding composition and inversion at the set-theoretic level gives you the vocabulary and intuition needed to move fluently through these more advanced settings.

Practice Problems

PROBLEM 1CONCEPTUAL
Let f : A → B and g : B → C. If g ∘ f is injective, must f be injective? Must g be injective? Justify your answers.
PROBLEM 2BASIC CALCULATION
Let A = {1, 2, 3, 4}, and define f : A → A by f(1) = 3, f(2) = 1, f(3) = 4, f(4) = 2. Compute f ∘ f (i.e., f²), and determine f⁻¹.
PROBLEM 3INTERMEDIATE
Let f : ℤ → ℤ be defined by f(n) = 2n + 1. Show that f is injective but not surjective. Find a left inverse for f and explain why no right inverse exists.
PROBLEM 4APPLIED
In a simple substitution cipher, each letter of the alphabet is replaced by another via a bijection σ : {A,…,Z} → {A,…,Z}. Encrypting a message is applying σ; decrypting is applying σ⁻¹. Suppose you encrypt a message with σ and then encrypt the result again with another bijection τ. Write the combined encryption as a single function, and express the decryption function. Which letter-substitution do you undo first when decrypting?
PROBLEM 5CRITICAL THINKING
Prove that if f : A → B has both a left inverse g (so g ∘ f = id_A) and a right inverse h (so f ∘ h = id_B), then g = h, and this common function is the unique two-sided inverse of f.

Lesson Summary

Function composition (g ∘ f)(x) = g(f(x)) chains two functions by feeding the output of f into g, producing a new function whose domain matches f's and whose codomain matches g's. Composition is associative but generally not commutative, and it preserves injectivity, surjectivity, and bijectivity. The identity function id serves as the neutral element for composition.

An inverse function f⁻¹ satisfies f⁻¹ ∘ f = id and f ∘ f⁻¹ = id, and exists if and only if f is a bijection. The socks-and-shoes rule states (g ∘ f)⁻¹ = f⁻¹ ∘ g⁻¹—invert in reverse order. Left inverses require injectivity, right inverses require surjectivity, and when both exist they are equal and unique. These concepts form the algebraic bedrock of group theory, category theory, and cryptography.

Varsity Tutors • Discrete Math • Function composition and inverses