Functions and Relations
Previously, we explored the mathematical formalism of sets. Sets allow us to model collections of data. However, we frequently wish to capture relevant relationships in our data. Structured data is called as such because the data in the collection is related in some way. For example:
- An element precedes another element in a list.
- A person is friends with another person in a social network.
- One number is a divisor of another number.
To model structured data, we need some way of modeling these relationships between individual datum. In this chapter, we use sets to develop the theory of relations which will allow us to formally reason about these relationships.
Definitions and Notation
Intuitively, a relation relates two objects by some property as defined by the relation. To capture this correspondence we combine pairs with sets:
A relation over a universe is a subset of pairs of elements drawn from , i.e., .
Suppose we have a relation . An element of , denotes that and are related by , which we can express in different ways:
| Notation | Form |
|---|---|
| Set notation | |
| Function notation | |
| Infix notation |
For example, let our universe . Then we can define a relation that captures whether one person owes another money. With this relation, the following expressions:
- .
- .
- .
All posit that Miguel owes Li money.
Note that because the order of a pair matters, these expressions do not automatically assert that Li owes Miguel money. We would need to include this fact separately: .
Operations Over Relations
Because we define relations in terms of sets, we can define our fundamental operations over relations using set-theoretic notation.
Domain and Range
First, we can project out the left-hand and right-hand elements of a relation, typically called the domain and range, respectively.
Alternatively, the range may also be called the codomain of the relation.
Frequently, we may want to have the domain and range of a relation come from disparate sets and . This is no problem for our definition of relation; we can simply define the universe to be the union of and . Then our pairs are drawn from this union where the domain is always an element of and the range is always an element of .
In this case, we can define a relation between sets and natural numbers where is the number of elements in . We commonly call this the cardinality of a set. This is normally written , but to align with our relation notation, we can write to denote this fact. For example:
- .
- .
- .
Lifted Operations
Because relations are sets, we can lift any binary operation over sets to relations. As examples, let and be two relations. Then define the following lifted operations over sets to relations as:
As a practical example of applying set-theoretic operations to relations, consider using relations to map items in a store to their stock, i.e., a relation whose domain is (abstract) objects and the codomain is natural numbers. We might have two different stores, with their own separate stocks of disparate items:
- .
- .
Then might represent joining together the stocks into a single store:
- .
Transformations
Beyond lifted operations, we can also define several fundamental transformations over relations.
Let and be relations.
Define the composition of and , written (LaTeX: \circ) as:
Note that with composition that we "run the relation" from right-to-left, first through and then through .
This final transformation is particularly useful when talking about functions which (we will discover shortly) are a special case of relations. In particular, note that if we have a function , then both the definition and notation of image coincides with "run the function", .
Function-like Relations
Functions form the heart of computation within mathematics. Consider the following partially specified relation:
From inspection, you would rightfully conclude that relates a natural number to the number one greater than it, i.e., is the increment function. We can see that the left-hand element of a pair represents an input to the function and the right-hand element of a pair is its corresponding output.
Based on this example, it may feel like functions and relations are the same. However, not all relations are functions. For example, consider the following relation:
If we think of as a function, what is the result of ? It appears there are three choices—, , and ! This does not align with our intuition about how a function works where a single input to a function should generate a single output.
In actuality, functions can be thought of as a special case of relations. Next, we'll develop the definitions necessary to classify certain relations as functions. These definitions will help us understand better the nature of functions as well as leverage the functions-as-relations view in our own mathematical models.
This next section is light on exposition by intention! You should employ the strategies we've discussed in the course to understand and internalize these definitions. Create small example sets that exhibit each of these definitions and try to understand the essence of the definitions by generalizing the structure of the examples.
Totality and Uniqueness
The two main properties that separate functions from other relations are totality and uniqueness. Because functions distinguish between inputs and outputs in a non-symmetric fashion, totality and uniqueness can apply either to the inputs of the function (the "left") or the outputs of the function (the "right").
Totality
Totality concerns whether all the elements in the universe of some relation appear in the relation.
Uniqueness
Uniqueness concerns whether an element is related to a single other element. The way that we express this property formally is that if an element is mapped to two elements, those two elements are in fact the same.
A relation is left-unique if every element in the relation on the left-hand side is mapped to a unique element right.
A relation is right-unique if every element in the relation on the right-hand side is mapped to a unique element on the left.
Refinements of Relations
With totality and uniqueness defined, we can define particular refinements relations in terms of these properties.
To better distinguish from partial functions, we also call right-unique and left-total relations total functions. Note that a total function is one that is well-defined, i.e., "has an answer" every possible input. In contrast, a partial function may be undefined on some inputs; this corresponds to the non-existence of a pair mentioning the undefined element on the left-hand side.
Definition (injectivity): a relation is an injective function if it is a function (right-unique and left-total) as well as left-unique.
Definition (surjectivity): A relation is a surjective function if it is a function (right-unique and left-total) as well as right-total.
Definition (bijection): A relation is a bijection if it is a function (right-unique and left-total) as well as injective and surjective (left-unique and right-total).
Consider the following relation over :
Does the relation fulfill each of the given properties? If so, you can simply say "yes". If not, give a single sentence explaining why not.
- Left-total
- Right-total
- Left-unique
- Right-unique
- Partial function
- Function
- Injective function
- Surjective function
- Bijection