Discrete Mathematics · Unit 1 · Topic 03

Functions: one input, exactly one output.

Short answer

A function \(f : A \to B\) gives every element of \(A\) exactly one image in \(B\). It is one-one if different inputs give different outputs, onto if every element of \(B\) is an output, and a bijection (with an inverse) if it is both.

From an \(m\)-element set to an \(n\)-element set there are \(n^m\) functions, for example \(3^4 = 81\), of which 36 are onto.

EngineeringComputer Science / ITBSc
01

What a function is

Before you start: you need sets and ordered pairs from sets and relations.

A function \(f : A \to B\) assigns to every element \(a\) of \(A\) exactly one element \(f(a)\) of \(B\). As a relation, it is a set of pairs \((a, f(a))\) in which each \(a \in A\) appears exactly once as a first entry.

  • \(A\) is the domain and \(B\) is the codomain.
  • The range (or image) is the set of outputs actually used: \(f(A) = \{f(a) : a \in A\}\). It is a subset of the codomain, and it can be smaller.

Picture an arrow diagram: dots for \(A\) on the left, dots for \(B\) on the right, one arrow leaving each dot of \(A\). Two arrows may land on the same dot of \(B\), and some dots of \(B\) may receive none. What is not allowed is a dot of \(A\) with no arrow, or with two.

02

One-one, onto and bijective

Definitions

One-one (injective): different inputs give different outputs. To prove it, show \(f(a_1) = f(a_2) \Rightarrow a_1 = a_2\).

Onto (surjective): every element of the codomain is an output. For each \(y \in B\) there is an \(x \in A\) with \(f(x) = y\). That is, range = codomain.

Bijective: both one-one and onto. Exactly one arrow lands on each dot of \(B\).

For finite sets this gives quick size tests. A one-one function \(A \to B\) needs \(|A| \le |B|\), an onto function needs \(|A| \ge |B|\), and a bijection needs \(|A| = |B|\).

FunctionOne-one?Onto?
\(f(x) = 2x + 3\), \(\mathbb{R} \to \mathbb{R}\)YesYes
\(f(x) = x^2\), \(\mathbb{R} \to \mathbb{R}\)No: \(f(-1) = f(1)\)No: \(-1\) is never an output
\(f(x) = x^2\), \([0, \infty) \to [0, \infty)\)YesYes
\(f(x) = e^x\), \(\mathbb{R} \to \mathbb{R}\)YesNo: outputs are all positive
\(f(x) = x^3 - x\), \(\mathbb{R} \to \mathbb{R}\)No: \(f(0) = f(1) = 0\)Yes

Notice rows 2 and 3. The same formula can be onto or not depending on the codomain, and one-one or not depending on the domain. A function is the formula together with its domain and codomain.

03

Composition and inverse

Composition and inverse

For \(f : A \to B\) and \(g : B \to C\), the composition \(g \circ f : A \to C\) is \((g \circ f)(x) = g\big(f(x)\big)\). Apply \(f\) first.

\(f\) has an inverse \(f^{-1} : B \to A\) if and only if \(f\) is a bijection. Then \(f^{-1}\big(f(x)\big) = x\) and \(f\big(f^{-1}(y)\big) = y\).

\[(g \circ f)^{-1} = f^{-1} \circ g^{-1}\]

Composition is usually not commutative: \(g \circ f\) and \(f \circ g\) are different functions in general. To find an inverse, write \(y = f(x)\), solve for \(x\) in terms of \(y\), and check that the answer lies in the domain for every \(y\) in the codomain. If the solving step fails for some \(y\), the function is not onto; if it gives two answers, it is not one-one.

The inverse of a composition reverses the order, for the same reason that you take off your shoes before your socks.

04

Counting functions between finite sets

Let \(|A| = m\) and \(|B| = n\). Each of the \(m\) elements of \(A\) chooses its image, and the choices are counted with the product rule from permutations and combinations.

Number of functions \(A \to B\), \(|A| = m\), \(|B| = n\)

All functions: \(n^m\).

One-one (for \(m \le n\)): \(n(n-1)(n-2)\cdots(n-m+1) = \dfrac{n!}{(n-m)!}\). Zero if \(m > n\).

Bijections (for \(m = n\)): \(n!\).

Onto: \[\sum_{k=0}^{n} (-1)^k \binom{n}{k} (n-k)^m\]

The onto formula is inclusion-exclusion. Start with all \(n^m\) functions, remove those that miss a chosen element of \(B\) (\(\binom{n}{1}(n-1)^m\)), add back those that miss two (\(\binom{n}{2}(n-2)^m\)), and so on. For \(n = 2\) it is just \(2^m - 2\): everything except the two constant functions.

Floor and ceiling

Two functions from \(\mathbb{R}\) to \(\mathbb{Z}\) appear all over discrete maths. The floor \(\lfloor x \rfloor\) is the greatest integer \(\le x\), and the ceiling \(\lceil x \rceil\) is the least integer \(\ge x\). So \(\lfloor 3.7 \rfloor = 3\), \(\lceil 3.2 \rceil = 4\), \(\lfloor -2.3 \rfloor = -3\) and \(\lceil -2.3 \rceil = -2\). For an integer, both give the number itself. The number of multiples of \(k\) in \(1, 2, \dots, N\) is \(\lfloor N/k \rfloor\), and ceilings appear in the pigeonhole principle. Both functions are onto \(\mathbb{Z}\) but not one-one.

05

Solved examples

Example 1: Reading a function

Easy

\(A = \{1, 2, 3\}\), \(B = \{p, q, r, s\}\) and \(f = \{(1, p), (2, r), (3, p)\}\). Find the domain, codomain and range. Is \(f\) one-one? Onto?

  1. Each of 1, 2, 3 has exactly one image, so \(f\) is a function. Domain \(A\), codomain \(B\).
  2. Range \(= \{p, r\}\).
  3. \(f(1) = f(3) = p\) with \(1 \ne 3\): not one-one.
  4. \(q\) and \(s\) are never outputs: not onto.

AnswerRange \(\{p, r\}\); \(f\) is neither one-one nor onto.

Example 2: A linear bijection

Easy

Show that \(f : \mathbb{R} \to \mathbb{R}\), \(f(x) = 3x - 5\) is a bijection and find \(f^{-1}\).

  1. One-one: \(3a - 5 = 3b - 5 \Rightarrow a = b\).
  2. Onto: given \(y\), solve \(3x - 5 = y\) to get \(x = \dfrac{y + 5}{3}\), a real number.
  3. Check: \(f\!\left(\dfrac{y + 5}{3}\right) = (y + 5) - 5 = y\).

AnswerBijection, with \(f^{-1}(y) = \dfrac{y + 5}{3}\).

Example 3: Doubling on the integers

Medium

Is \(f : \mathbb{Z} \to \mathbb{Z}\), \(f(n) = 2n\) one-one? Onto?

  1. \(2a = 2b \Rightarrow a = b\): one-one.
  2. Every output is even, so \(3\) has no preimage (\(2n = 3\) gives \(n = 1.5 \notin \mathbb{Z}\)): not onto.
  3. The same formula on \(\mathbb{R} \to \mathbb{R}\) would be onto, since \(x = y/2\) is always real.

AnswerOne-one but not onto.

Example 4: Order of composition

Medium

\(f(x) = x^2\) and \(g(x) = x + 3\) on \(\mathbb{R}\). Find \(g \circ f\) and \(f \circ g\). Are they equal?

  1. \((g \circ f)(x) = g(x^2) = x^2 + 3\).
  2. \((f \circ g)(x) = f(x + 3) = (x + 3)^2\).
  3. At \(x = 1\): \(g \circ f\) gives 4 but \(f \circ g\) gives 16.

Answer\(g \circ f = x^2 + 3\), \(f \circ g = (x + 3)^2\); they are not equal.

Example 5: A rational function

Medium

Show that \(f(x) = \dfrac{2x + 1}{x - 1}\) is a bijection from \(\mathbb{R} - \{1\}\) to \(\mathbb{R} - \{2\}\), and find its inverse.

  1. First, 2 is never an output: \(\dfrac{2x + 1}{x - 1} = 2\) would mean \(2x + 1 = 2x - 2\), which is impossible. So the codomain \(\mathbb{R} - \{2\}\) is right.
  2. Solve \(y = \dfrac{2x + 1}{x - 1}\): \(yx - y = 2x + 1\), so \(x(y - 2) = y + 1\).
  3. For \(y \ne 2\) this gives exactly one \(x = \dfrac{y + 1}{y - 2}\), and \(x \ne 1\) (else \(y + 1 = y - 2\)). One solution for each \(y\) means one-one and onto.

AnswerBijection, with \(f^{-1}(y) = \dfrac{y + 1}{y - 2}\).

Example 6: Counting functions

Medium

\(|A| = 3\) and \(|B| = 5\). How many functions \(A \to B\) are there? How many are one-one? How many are onto?

  1. Each of the 3 elements has 5 choices: \(5^3 = 125\).
  2. One-one: \(5 \times 4 \times 3 = 60\).
  3. Onto would need \(|A| \ge |B|\), and \(3 < 5\).

Answer125 functions, 60 one-one, 0 onto.

Example 7: Onto functions

Exam level

How many onto functions are there from a 4-element set to a 3-element set?

  1. All functions: \(3^4 = 81\).
  2. Missing a chosen element: \(\binom{3}{1} 2^4 = 48\). Missing two chosen elements: \(\binom{3}{2} 1^4 = 3\). Missing all three: 0.
  3. Onto \(= 81 - 48 + 3 - 0\).
  4. Check another way: two elements of \(A\) must share an image. Choose the pair (\(\binom{4}{2} = 6\) ways) and then match the 3 groups to the 3 images (\(3! = 6\) ways).

Answer36 onto functions

Example 8: Inverse of a composition

Exam level

\(f(x) = 2x + 1\) and \(g(x) = x^3\) on \(\mathbb{R}\). Find \((g \circ f)^{-1}\) and check that it equals \(f^{-1} \circ g^{-1}\).

  1. \((g \circ f)(x) = (2x + 1)^3\). Solve \(y = (2x + 1)^3\): \(2x + 1 = \sqrt[3]{y}\), so \((g \circ f)^{-1}(y) = \dfrac{\sqrt[3]{y} - 1}{2}\).
  2. Separately, \(f^{-1}(y) = \dfrac{y - 1}{2}\) and \(g^{-1}(y) = \sqrt[3]{y}\).
  3. \(f^{-1}\big(g^{-1}(y)\big) = \dfrac{\sqrt[3]{y} - 1}{2}\), the same.
  4. Spot check at \(y = 27\): \(\dfrac{3 - 1}{2} = 1\), and indeed \((2 \cdot 1 + 1)^3 = 27\).

Answer\((g \circ f)^{-1}(y) = \dfrac{\sqrt[3]{y} - 1}{2} = (f^{-1} \circ g^{-1})(y)\)

Vipul Sir's tip

One piece of algebra answers three questions. Set \(y = f(x)\) and solve for \(x\). If there is always exactly one solution in the domain, the function is one-one and onto, and the formula you just found is \(f^{-1}\). When students prove one-one and onto separately and then find the inverse, they do the same work three times.

06

Common mistakes

1. Mixing up codomain and range

Wrong: "\(f(x) = x^2\) from \(\mathbb{R}\) to \(\mathbb{R}\) is onto because it is a parabola." Right: its range is \([0, \infty)\), which is smaller than the codomain \(\mathbb{R}\), so it is not onto. With codomain \([0, \infty)\) it would be onto.

2. Reading \(g \circ f\) as "\(g\) first"

\((g \circ f)(x) = g\big(f(x)\big)\): \(f\) acts first. In Example 4, \(g \circ f\) is \(x^2 + 3\), not \((x + 3)^2\).

3. Stopping inclusion-exclusion too early

Wrong: onto functions from 4 elements to 3 \(= 3^4 - 3 \cdot 2^4 = 33\). The functions that miss two elements (the 3 constant functions) were subtracted twice. Right: \(81 - 48 + 3 = 36\).

07

Practice questions

Try each one on paper before you open the answer.

Q1How many functions are there from a 2-element set to a 4-element set? How many are one-one?

\(4^2 = 16\) functions; \(4 \times 3 = 12\) one-one.

Q2Is \(f : \mathbb{R} \to \mathbb{R}\), \(f(x) = |x|\) one-one? Onto?

Neither. \(f(-1) = f(1) = 1\), and negative numbers such as \(-1\) are never outputs.

Q3If \(f(x) = 4x - 7\) on \(\mathbb{R}\), find \(f^{-1}(x)\) and \(f^{-1}(5)\).

\(y = 4x - 7 \Rightarrow x = \dfrac{y + 7}{4}\), so \(f^{-1}(x) = \dfrac{x + 7}{4}\) and \(f^{-1}(5) = 3\). Check: \(f(3) = 5\).

Q4\(f(x) = x + 1\) and \(g(x) = 2x\). Find \((f \circ g)(3)\) and \((g \circ f)(3)\).

\((f \circ g)(3) = f(6) = 7\) and \((g \circ f)(3) = g(4) = 8\).

Q5How many onto functions are there from a 5-element set to a 2-element set?

\(2^5 - 2 = 30\): all functions except the two constant ones.

Q6Find \(\lfloor -3.5 \rfloor\), \(\lceil -3.5 \rceil\), and the number of integers from 1 to 500 that are multiples of 7.

\(\lfloor -3.5 \rfloor = -4\), \(\lceil -3.5 \rceil = -3\), and \(\lfloor 500/7 \rfloor = 71\) (since \(7 \times 71 = 497\)).

Stuck on a question?

Bring it to a free demo class and work through it with Vipul Sir. Book on WhatsApp.

08

Frequently asked questions

What is the difference between codomain and range?

The codomain is the set you declare the outputs belong to. The range is the set of outputs actually produced. A function is onto exactly when the two are equal.

How many functions are there from A to B?

If \(|A| = m\) and \(|B| = n\), there are \(n^m\) functions, because each of the \(m\) inputs independently picks one of \(n\) outputs.

When does a function have an inverse?

When it is a bijection, both one-one and onto. Then every element of the codomain comes from exactly one input, and \(f^{-1}\) sends it back.

How do you count onto functions?

Use inclusion-exclusion: \(\sum_{k=0}^{n} (-1)^k \binom{n}{k} (n-k)^m\). For example, from 4 elements onto 3 there are \(81 - 48 + 3 = 36\).

Related lessons

← All 10 Discrete Mathematics topics
Vipul Sir
Written by Vipul Sir

Vipul Sir has taught mathematics for over 15 years to HSC, CBSE, ICSE, IGCSE/IB, Diploma, Engineering and BSc students at VVS Classes in Goregaon and Vile Parle, Mumbai. He teaches every class himself, concepts first and exam technique second.

Discrete Mathematics

Get Discrete Mathematics right, with Vipul Sir beside you.

Concept-first classes in small batches in Goregaon and Vile Parle, or online. Your first class is free.

WhatsApp↗