DM 05 Functions and relations

Previous: DM 04 Sets | DM 00 Course Map | Next: DM 06 Induction and Recursion

One sentence

A relation is any set of pairs connecting two sets; a function is the special relation that gives every input exactly one output, and the questions “can outputs collide?” and “is every target hit?” classify functions.

Functions

A function assigns to each element of exactly one element of .

  • is the domain (allowed inputs).
  • is the codomain (declared type of the outputs).
  • The range or image is the set of outputs actually produced: .

Two requirements: every input has an output (nothing left out), and no input has two outputs (no ambiguity). Two different inputs sharing an output is allowed.

Example. , . Domain , codomain , range .

A function is its domain, its codomain and its rule. from to and from to are different functions with different properties.

Injective, surjective, bijective

Injective (one-to-one): different inputs give different outputs.

No collisions. You can recover the input from the output.

Surjective (onto): every element of the codomain is hit.

Range equals codomain.

Bijective: both. A perfect pairing between and .

Injective?Surjective?
, no ()no ( is never hit)
, yesno ( is never hit)
, yesno (odd numbers missed)
, yesyes: bijective
, noyes

How to prove each.

  • Injective: assume , deduce .
  • Surjective: take an arbitrary , construct an with .
  • Not injective: exhibit two inputs with equal outputs. Not surjective: exhibit a target that is never hit.

Worked example. , . Injective: gives . Surjective: given , take ; then . So is a bijection.

For finite sets, sizes constrain everything.

  • An injection requires .
  • A surjection requires .
  • A bijection requires . This is the principle behind counting by correspondence (DM 07 Counting).

The first line, read backwards, is the pigeonhole principle: if , no function is injective, so some two inputs collide. Hash functions map a huge set to a small one, so collisions are unavoidable.

Composition and inverse

Given and , the composition is

acts first. Composition is associative but not commutative: with and , while . The same rule governed matrix products in LA 03 Matrices as Linear Maps.

has an inverse , with and , exactly when is bijective. Injectivity makes the answer unambiguous; surjectivity makes it defined for every .

In linear algebra: an matrix is injective exactly when its null space is trivial, surjective exactly when its column space is everything, and for square matrices each implies the other (LA 05 Subspaces Basis and Rank).

Relations

A relation from to is any subset of . Write when . A function is a relation with the “exactly one output” property; a general relation may relate an element to none, one or many.

A relation on is a subset of . Examples on the integers: , , , “divides”, “has the same parity as”.

A relation on a finite set can be drawn as a directed graph (DM 10 Graphs): an arrow from to whenever .

Properties of a relation on

PropertyDefinitionExampleNon-example
reflexive for every
symmetric”is a sibling of”
antisymmetric and “is a sibling of”
transitive and “is a friend of”

Antisymmetric is not the opposite of symmetric. Equality is both; “is a friend of” on some groups is neither.

Equivalence relations

A relation that is reflexive, symmetric and transitive is an equivalence relation. It captures “the same in some respect”.

Examples. Equality. “Has the same birthday as”. “Has the same remainder when divided by 3” (congruence mod 3, DM 08 Number Theory and Modular Arithmetic). “Is similar to” for matrices (LA 11 Change of Basis).

The equivalence class of is everything equivalent to it:

Equivalence relations and partitions are the same thing

The equivalence classes of an equivalence relation partition : every element is in exactly one class. Conversely, every partition defines an equivalence relation (“is in the same block as”).

Worked example. Congruence mod 3 on has three classes:

They are disjoint and cover every integer.

Checking the three properties for ” when is even”: reflexive, since is even. Symmetric, since if is even so is . Transitive, since is a sum of two even numbers. The classes are the evens and the odds.

Equivalence is how mathematics builds new objects: a rational number is an equivalence class of fractions ().

Partial orders

A relation that is reflexive, antisymmetric and transitive is a partial order. It captures “comes before or equals”.

Examples. on numbers. on sets. “Divides” on positive integers. “Must be completed before” on tasks.

“Partial” because some pairs may be incomparable: neither nor ; neither nor . If every pair is comparable it is a total order, like on numbers.

A partial order on tasks (“put on socks before shoes”) can always be extended to a total order that respects it. Finding one is topological sorting, used by build systems, package managers and course prerequisite planning (DM 11 Trees and Graph Algorithms).

Common mistakes

  • Confusing codomain with range. Surjectivity depends on the declared codomain.
  • Reading “one-to-one” as “one-to-one correspondence”. The first means injective; the second means bijective.
  • Checking a property on a few pairs and concluding it holds for all.
  • Thinking symmetric and antisymmetric are opposites.

Learn by doing

  1. Is , injective? Surjective? What if the codomain is and the domain ?
  2. With and , find and .
  3. On , define when is even. Is an equivalence relation?
  4. Is “divides” on a total order? Name two incomparable elements.

Where this shows up

A policy is a function from observations to actions; if two different states produce the same observation, the observation map is not injective and the state cannot be recovered from a single frame. That is partial observability (04 Perception and State). Task dependencies in a robot’s plan form a partial order.