# 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:

Functions

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

### Example -1

Consider and . Consider function .

The function above can be represented explicitly by the mapping

or implicitly by the rule/formula .

### Example-2

Functions can map many elements of to the same element of . But the rule is that each element of can only be mapped to one element of . 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 to be and . We write the function by using the mapping from every element of the power set and every element of .

The table below shows the elements of the power set and the corresponding value of .

 Power set elt. () 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.

• , a function from takes an input natural number and yields an output natural number.

• is a function from , takes an input real number and yields a real number output.

• is a function from , takes an input real number and yields a real number output.

• is a function from , , 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 to set can fail to be a function for either of the two reasons below:

1. It leaves an element of set unmapped.

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

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 from the domain, while the mapping on the right fails to be a function since is mapped to multiple elements.

#### Mathematical non functions

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

• The function is not a function from .

• It is a function however from . Here, we have removed the input from the domain.

• The function is not a function from since it leaves all values unmapped.

• If we wish to be precise, we will define the type of to be .

• The function is not defined for and can be defined to both positive and negative square roots when . For instance .

• If we wish to be precise, we define to be a function from , with the domain restricted to non-negative reals.

## Relations

Formally, a relation between sets and is defined as a subset of , i.e, .

### Example-1

Let us take and . Consider the relation by

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

• is not related to any element in .

• could be related to multiple elements in .

The relation is visualized below:

Functions are a special case of relations, wherein

• is mapped to exactly one element in .

### Example-2

Here is another example of a relation over numbers: defined as

Write down some examples of elements of .

### Example-3

Consider sets and write the function defined by as a relation .

### Counting Relations

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

## Domains/Co-Domains

Let be a function . We say that is the domain of and is the co-domain.

Similarly, let be a relation. We say that is the domain of the relation and is the co-domain.

### Relations On a Set

A relation 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 . Consider a relation .

The relation has the tuples . The graph looks as follows:

The rule is we have nodes or vertices for each element in the set . If , we draw an arrow from to . 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.