Lecture 16: Functions and Relations

We have already encountered functions many times and we often use functions when we program. Let us formalize what functions are in mathematical terms:


A function f from a set A to a set B is associates (or maps) every element of the set A to some element of the set B. We express the fact that a function f maps A to B as f: A rightarrow B.

Example -1

Consider A = { 1,3,5,7,11 } and B = { 2,4,6,8,12 }. Consider function f: A rightarrow B.


The function above can be represented explicitly by the mapping

 1 mapsto 2, 3 mapsto 4, 5 mapsto 6, ldots

or implicitly by the rule/formula f(x) = x+1.


Functions can map many elements of A to the same element of B. But the rule is that each element of A can only be mapped to one element of B. Which of the following are functions?


Example-3: Power Set Signature

While discussing power sets, we mentioned a correspondence between power-sets of a set and binary strings. We can indeed write it as a function. Let us take A to be {1,2,3} and B_3 = { 000,001,010,011,101,110,111 }. We write the function s: P(A) mapsto B_3 by using the mapping from every element of the power set S in P(A) and every element of B_3.

The table below shows the elements of the power set and the corresponding value of s(x).

Power set elt. (x in P(A)) s(x)
{} 000
{a} 100
{b} 010
{c} 001
{a,b} 110
{a,c} 101
{b,c} 011
{a,b,c} 111

Example-4: Mathematical functions

You may have seen many examples of functions from your mathematics classes, thus far.

  • k(x): x+1, a function from mathbb{N} rightarrow mathbb{N} takes an input natural number and yields an output natural number.

  • f(x): sin(x) is a function from mathbb{R} rightarrow mathbb{R}, takes an input real number and yields a real number output.

  • g(x): e^x is a function from mathbb{R} rightarrow mathbb{R}, takes an input real number and yields a real number output.

  • h(z): lfloor z rfloor is a function from mathbb{R} rightarrow mathbb{Z}, , takes an input real number and yields an integer output.

Non-Examples of Functions.

It remains to clarify what is not a function. In general, a mapping from set A to set B can fail to be a function for either of the two reasons below:

  1. It leaves an element of set A unmapped.

  2. It maps an element of A to multiple elements in B.

If either case occurs then, the mapping fails to be a function.

Consider the two mappings shown below. The mapping on the left fails to be a function because it does not map the elements 1,2 from the domain, while the mapping on the right fails to be a function since 2 is mapped to multiple elements.


Mathematical non functions

Strictly speaking, many functions that we saw in calculus are not quite functions.

  • The function frac{1}{x} is not a function from mathbb{R} rightarrow mathbb{R}.

    • It is a function however from (mathbb{R} - {0}) rightarrow mathbb{R}. Here, we have removed the input x=0 from the domain.

  • The function log(x) is not a function from mathbb{R} rightarrow mathbb{R} since it leaves all values x leq 0 unmapped.

    • If we wish to be precise, we will define the type of log(x) to be mathbb{R}_{geq 0} rightarrow mathbb{R}.

  • The function sqrt{x} is not defined for x < 0 and can be defined to both positive and negative square roots when x geq 0. For instance sqrt{4} = pm 2.

    • If we wish to be precise, we define | sqrt{x}| to be a function from mathbb{R}_{geq 0} rightarrow mathbb{R}, with the domain restricted to non-negative reals.


Formally, a relation R between sets A and B is defined as a subset of A times B, i.e, R subseteq A times B.


Let us take A = { 1,2,3,4,5,6} and B = {1,2,3,4,5,6}. Consider the relation R subseteq A times B by

 R = { (1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (6,2), (6,3) }

Like functions, we may view the relation as a mapping. However, unlike functions, it is possible that

  • x in A is not related to any element in B.

  • x in A could be related to multiple elements in B.

The relation R is visualized below:


Functions are a special case of relations, wherein

  • xin A is mapped to exactly one element in B.


Here is another example of a relation over numbers: R subseteq mathbb{N} times mathbb{N} defined as

R = { (m,n) | m mbox{divides} n }

Write down some examples of elements of R.


Consider sets A = {1,3,5} and B = {2,4,6} write the function f: A rightarrow B defined by f(n) = n+1 as a relation R_f subseteq A times B.

Counting Relations

If A is a set with m elements and B with n elements then how many relations can exist between A and B? How many functions?

Answer in class.


Let f be a function f: A rightarrow B. We say that A is the domain of f and B is the co-domain.

Similarly, let R subseteq A times B be a relation. We say that A is the domain of the relation and B is the co-domain.

Relations On a Set

A relation R from set A to itself is called a relation on A . We can represent relations from a set to itself by a special diagram called a graph.

Eg., Let A = { 1,3,5,7,9 }. Consider a relation R = { (a,b) | a=b lor b = a+2 }.

The relation has the tuples R = { (1,1), (1,3), (3,3), (3,5), (5,5), (5,7), (7,7), (7,9), (9,9) }. The graph looks as follows:


The rule is we have nodes or vertices for each element in the set A. If (a,b) in R, we draw an arrow from a to b. Graphs are very useful as visualizations of relations. We will spend 2-3 weeks at the end of this course talking about properties of graphs.