Functions And Relations
On this page
A function from a set to a set assigns to each element of exactly one element of . You already know functions from programming: they take an input, compute, and return an output. The big difference is that a mathematical function is pure: no side effects, no hidden state, the same input always gives the same output. It is closer to a lookup table than to a method that writes to a database.
Domain, Codomain And Image
Every function has three sets attached to it, and mixing them up causes most of the confusion around injective and surjective functions later on.
| Term | Meaning | Programming analogy |
|---|---|---|
| Domain | The set of allowed inputs | The parameter type |
| Codomain | The set where outputs are allowed to land | The declared return type |
| Image (range) | The outputs that are actually produced | The values the function really returns |
The image is always a subset of the codomain. A function declared to return int that only ever returns positive numbers has codomain "all ints" but image "positive ints".
A First Example
Consider
The notation reads " goes from to ": is the domain and is the codomain. The rule says how to compute the output. To evaluate the function, you substitute a value from the domain:
Here the domain is , the codomain is and the image is , because a square root is never negative. The input is not allowed: is not a real number. It does exist in , so if you changed the codomain to you could extend the domain to all of .
Choosing The Domain
The same rule can be paired with different domains and codomains, as long as it makes sense. Restricting the domain of to is perfectly valid, it just produces fewer outputs. When no domain is stated, the convention is to take the biggest natural domain: every input for which the rule produces a valid output. For that is , for it is .
Functions As Machines And Arrows
Two mental pictures are useful. The first is a machine: input goes in, output comes out, nothing else happens.
The second is a set of arrows from inputs to outputs, which is the picture used for the rest of this note. For on perfect squares: , , , . The defining rule is that one input may never have two arrows leaving it.
Relations
Relations describe connections between elements of sets, and they are more general than functions. They are fundamental in computer science: a database table is literally a relation, and the relational model behind SQL is built on this definition.
Definition
Let and be sets. A relation from to is any set of ordered pairs with and . You write to say that is related to .
With and , both of these are valid relations:
links every number to its double. relates to both and , and leaves unrelated. A relation allows all of that.
Functions Are Special Relations
A function is a relation with two extra conditions:
- Every element of the domain has an output (no input left out).
- Every input has exactly one output (no input with two arrows).
So is a function, while is not: has two outputs and has none.
In SQL terms, a function is like a table where the input column is a primary key: each key appears exactly once, so a lookup always returns one row. A general relation is like a many-to-many join table.
Injective, Surjective And Bijective
Once you know something is a function, you can ask two independent questions about it. Do different inputs always give different outputs? Is every element of the codomain reached? The answers give the three named types.
Injective (One To One)
A function is injective when different inputs always produce different outputs. Formally, is injective if
Read it as: "if two outputs are equal, the inputs were already equal". In the arrow picture, no two arrows land on the same element. Some codomain elements may be left unused.
- is injective: , , , never a collision.
- on is not injective: .
A hash function is the standard CS example of a non-injective function: infinitely many inputs map to a finite set of hashes, so collisions are unavoidable.
Surjective (Onto)
A function is surjective when it reaches every element of the codomain, so the image is equal to the codomain. Formally, for every there exists some with . In the arrow picture, every codomain element receives at least one arrow.
- from to is surjective: every real has the cube root as a preimage.
- from to is not surjective: is never produced.
Surjectivity depends on the codomain you declare. becomes surjective if you define it as , without touching the rule.
Bijective (Perfect Matching)
A function is bijective when it is both injective and surjective. Every element of the codomain is hit by exactly one arrow, so domain and codomain are paired up perfectly.
on is bijective. It is injective because different give different , and it is surjective because any is reached from .
Inverse Functions
Only bijective functions have an inverse. If is bijective, there is a function that reverses it:
For the inverse is , since .
Why both conditions? Flip every arrow. If the function was not injective, some output now has two arrows leaving it, so the flipped version is not a function. If it was not surjective, some element of now has no arrow at all, so the flipped version is not defined on all of . Only a perfect matching survives the flip. This is why encryption must be bijective (you need to decrypt), while hashing deliberately is not.
Summary Table
| Concept | Definition |
|---|---|
| Relation | Any collection of ordered pairs |
| Function | A relation where every input has exactly one output |
| Injective | Different inputs give different outputs |
| Surjective | Every element of the codomain is reached |
| Bijective | Both injective and surjective |
| Inverse function | Exists only for bijective functions |
| Type | Distinct outputs for distinct inputs? | All outputs reached? |
|---|---|---|
| Injective | Yes | Not necessarily |
| Surjective | Not necessarily | Yes |
| Bijective | Yes | Yes |
Testing Properties In Code
For finite functions stored as a dictionary, the definitions translate directly into set operations. This is a good way to check your understanding: each property is one line.
def is_injective(f: dict) -> bool:
return len(set(f.values())) == len(f) # no two keys share a value
def is_surjective(f: dict, codomain: set) -> bool:
return set(f.values()) == codomain # image equals codomain
def is_bijective(f: dict, codomain: set) -> bool:
return is_injective(f) and is_surjective(f, codomain)
f = {1: "a", 2: "b", 3: "b", 4: "c"}
print(is_injective(f), is_surjective(f, {"a", "b", "c"})) # False TrueWorked Exercises
These use the arrow diagrams from the book. The question to ask for each diagram is always the same: count arrows leaving inputs (function?), then arrows arriving at outputs (injective? surjective?).
Is It A Function
, , with domain and codomain : yes, each input has exactly one output.
, , : no, input has two outputs.
Classify The Mapping
, , with codomain : not injective (2 and 3 both map to ).
, , , with codomain : surjective but not injective. Every output is reached, but is reached twice.
, with codomain : not surjective, because is never reached.
Build Your Own
An injective but not surjective function: with , . A surjective but not injective one: with , , . A bijective one: , , . A relation that is not a function: .
Quick Recap
- A function assigns exactly one output to each input, with no side effects.
- Domain = allowed inputs, codomain = where outputs may land, image = outputs actually hit. Image codomain.
- Without a stated domain, take the biggest natural domain.
- A relation is any set of ordered pairs. A function is a relation where every input appears exactly once.
- Injective: no two arrows land on the same output. Test: .
- Surjective: every codomain element gets an arrow, so image = codomain. Depends on the declared codomain.
- Bijective: both, a perfect matching. Only bijections have an inverse with .