Discrete MathSets and Functions

Functions Between Sets

A function assigns each element of its domain exactly one element of its codomain; the range is the set of outputs actually reached.

Not started Jump to practice

In algebra a function was usually a formula, and you found its domain and range. In discrete math a function can be any rule between sets: from people to birthdays, from strings to lengths, from a set to itself. The definition focuses on what makes something a function at all.

Key idea

A function $f : A \to B$ assigns to each element of the domain $A$ exactly one element $f(a)$ of the codomain $B$. The range (or image) is the set of outputs actually reached, $f(A) = \{f(a) : a \in A\}$; it is a subset of the codomain and can be much smaller.

Why it works

"Exactly one" has two parts: every element of $A$ gets an output (none is skipped), and no element gets two. In an arrow diagram, that means exactly one arrow leaves each dot of $A$; the dots of $B$ may receive any number of arrows, including none. The codomain is part of the definition, a promise about where outputs are allowed to land, while the range records where they actually land. Keeping the two apart is what makes the next lesson's idea of "onto" possible.
An arrow diagram. Each element of $A$ needs exactly one arrow out. Switch the codomain to "outputs only" and notice that the elements never hit disappear: those form the difference between codomain and range.

Worked examples

Worked example: Function or not?

Let $A = \{1, 2, 3\}$ and $B = \{a, b\}$. Which of these is a function from $A$ to $B$? (i) $\{(1, a), (2, a), (3, b)\}$; (ii) $\{(1, a), (2, b)\}$; (iii) $\{(1, a), (1, b), (2, a), (3, a)\}$.
  1. (i) Each of $1, 2, 3$ has exactly one output. Two inputs share the output $a$, which is allowed: a function.
  2. (ii) $3$ has no output: not a function from $A$.
  3. (iii) $1$ has two outputs: not a function.

Answer: Only (i)

Worked example: Range versus codomain

Let $f : \{-2, -1, 0, 1, 2\} \to \mathbb{Z}$ be $f(x) = x^{2}$. Find the range.
  1. Outputs: $f(-2) = 4$, $f(-1) = 1$, $f(0) = 0$, $f(1) = 1$, $f(2) = 4$.
  2. Range: $\{0, 1, 4\}$. The codomain is still $\mathbb{Z}$; most integers are simply never reached.

Answer: $\{0, 1, 4\}$

Worked example: Counting functions

How many functions are there from $A = \{1, 2, 3\}$ to $B = \{a, b\}$?
  1. Each of the $3$ elements of $A$ independently chooses one of $2$ outputs.
  2. Product rule: $2 \cdot 2 \cdot 2 = 2^{3} = 8$. In general there are $|B|^{|A|}$ functions from $A$ to $B$.

Answer: $8$

Common mistake

Treating codomain and range as the same set. For $f : \mathbb{Z} \to \mathbb{Z}$, $f(x) = 2x$, the codomain is $\mathbb{Z}$, but the range is only the even integers. Saying "the range is $\mathbb{Z}$" claims that $3$ is an output, and it is not. Check: the codomain is given in the arrow $f : A \to B$; the range you must work out by asking which outputs actually occur.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 1 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • Oscar Levin, Discrete Mathematics: An Open Introduction (3rd ed.), section "Functions"
  • MIT OpenCourseWare 6.042J Mathematics for Computer Science (Lehman, Leighton and Meyer), chapter "Mathematical Data Types" (functions)
@teddyrookbook GitHub