Search

Search papers and lessons

Surjective Function

Level 1 · 8 min read

A surjective function, also called onto, hits every element of its codomain, so no target is left unreached by some input.

On this page

  • Colour = topic mix
  • Discrete math
  • ReadReady to readNot yet

TL;DR

A surjective function is a Function whose outputs cover its whole target set. Every element of the codomain is the output of at least one input, so nothing in the target is missed. Surjectivity tells you that a rule can reach every possible answer, and it is the half of a one-to-one correspondence that is about coverage rather than repetition.

Builds on: Function · Leads to: bijective functions

Where it's used

Card suits. Sort a standard deck by suit: the rule "card to suit" maps 52 cards onto four suits. It is surjective, because every suit has at least one card. A rule that sent every card to hearts would not be surjective, since clubs, diamonds and spades would never be reached.

Classifiers. A machine-learning classifier, a learned function such as a linear classifier, chooses one label from a fixed list, such as cat, dog, bird or fish. The rule "image to label" is meant to reach every label. If, over a large test set, the model never predicts fish, the rule is not surjective onto its label list. That is a useful warning sign: either the training data lacks fish, or the code has a bug.

Mapping a formula's outputs. A rule that turns a temperature in degrees Celsius into a reading on a scale can be checked for surjectivity against the scale it is supposed to cover. If it never outputs values below a certain level, some readings are impossible, and the designer needs to know that before building an instrument on it.

How it works

Take a small set of inputs A={1,2,3,4}A = \{1, 2, 3, 4\} and a target set B={a,b,c}B = \{a, b, c\}. Let ff send the inputs as follows: f(1)=af(1) = a, f(2)=af(2) = a, f(3)=bf(3) = b and f(4)=cf(4) = c.

  1. Read off the outputs. They are aa, aa, bb and cc. Every input has exactly one output, so ff is a function from AA to BB.
  2. List the target set. It has three elements, aa, bb and cc.
  3. Check each target in turn. Is aa an output? Yes, from input 11 (and also input 22). Is bb an output? Yes, from input 33. Is cc an output? Yes, from input 44.
  4. Every target is reached, so ff is surjective. Repeats do not matter: input 11 and input 22 both land on aa, and that is allowed.
  5. Now change one value and let f(4)=af(4) = a instead. The outputs are aa, aa, bb and aa.
  6. Check the targets again. aa and bb are reached, but cc is not. No input leads to cc, so ff is no longer surjective.

The rule for checking is always the same: go through the target set and look for an input that reaches each element. One unreached target is enough to show that the function is not surjective.

The same idea appears on a graph. Consider f(x)=x2f(x) = x^2 on the real numbers, with the reals as the target. A horizontal line at height −1-1 never touches the curve, because squares are never negative. So the value −1-1 is not an output, and ff is not surjective onto the reals. A surjective function is one for which every horizontal line, at every height in the target, meets the graph at least once.

−3−2−10123−202468xf(x)
  • f(x) = x²
  • y = -1
The dashed line at height -1 never meets the curve, so -1 is not an output. Squaring is not surjective onto the real numbers.

Here is the first rule as a diagram. Each target must have at least one arrow arriving at it:

flowchart LR
  i1[input 1] --> ta[a]
  i2[input 2] --> ta
  i3[input 3] --> tb[b]
  i4[input 4] --> tc[c]

Targets aa, bb and cc each have an arrow arriving, so every target is covered.

The math

The symbol ∀\forall means "for every", and ∃\exists means "there exists". The arrow →\to reads "maps to", as before. A function f:A→Bf : A \to B is surjective when this statement is true.

Definition: Surjective function

A function f:A→Bf : A \to B is surjective, or onto, if for every b∈Bb \in B there exists a∈Aa \in A with f(a)=bf(a) = b:

∀ b∈B    ∃ a∈A:  f(a)=b.\forall\, b \in B \;\; \exists\, a \in A : \; f(a) = b.

Equivalently, the range of ff, which is the set { f(a):a∈A }\{\, f(a) : a \in A \,\} of all outputs, equals the whole codomain BB.

Step 3 in symbols

For the first rule, B={a,b,c}B = \{a, b, c\}. We need an input for each target:

f(1)=a,f(3)=b,f(4)=c.f(1) = a, \qquad f(3) = b, \qquad f(4) = c.

Each of aa, bb and cc appears as an output, so the condition holds. Input 22 is not needed for coverage, and it is allowed to repeat an output.

Steps 5 and 6 in symbols

After the change, the outputs are f(1)=af(1) = a, f(2)=af(2) = a, f(3)=bf(3) = b and f(4)=af(4) = a. Take the target cc. No aa in AA satisfies f(a)=cf(a) = c, since every output is aa or bb. The statement "there exists aa with f(a)=cf(a) = c" is false, so ff is not surjective. One false instance of the ∀\forall statement settles the matter.

Proving surjectivity for a formula

Let f(x)=3x+1f(x) = 3x + 1, a linear function, from the real numbers to the real numbers. To show surjectivity, take any target yy and find an input xx that reaches it. Set 3x+1=y3x + 1 = y. Subtract 11 to get 3x=y−13x = y - 1, then divide by 33 to get

x=y−13.x = \frac{y - 1}{3}.

This xx is a real number for every real yy. Check it: f ⁣(y−13)=3⋅y−13+1=(y−1)+1=yf\!\left(\frac{y-1}{3}\right) = 3 \cdot \frac{y-1}{3} + 1 = (y - 1) + 1 = y. Every target is reached, so ff is surjective. The pattern to follow in general is to start from f(a)=bf(a) = b and solve for aa. If the solution always exists, the function is surjective.

Surjectivity depends on the codomain

The range of a function is fixed by its rule, but the codomain is a choice. Take g(x)=x2g(x) = x^2 from the reals to the non-negative reals [0,∞)[0, \infty). Every target y≥0y \ge 0 is reached, since x=yx = \sqrt{y} gives g(x)=yg(x) = y. So gg is surjective onto [0,∞)[0, \infty), even though the same rule is not surjective onto the reals. Replacing the codomain by the range always makes a function surjective.

Worked examples

Example
1. A tiny rule onto two targets

Let A={1,2,3}A = \{1, 2, 3\} and B={x,y}B = \{x, y\}. Define f(1)=xf(1) = x, f(2)=yf(2) = y and f(3)=xf(3) = x.

  1. Compute the outputs: xx, yy and xx.
  2. Check the target xx. It is reached from input 11.
  3. Check the target yy. It is reached from input 22.
  4. Both targets are covered, so ff is surjective.

Interpretation: a surjective rule can send several inputs to the same target, as long as no target is left out. Input 33 adds nothing new, but it is harmless.

Example
2. Remainders on division by three

Let f(n)f(n) be the remainder when nn is divided by 33, for nn in {0,1,2,3,4,5,6,7}\{0, 1, 2, 3, 4, 5, 6, 7\}.

  1. Compute the outputs: f(0)=0f(0) = 0, f(1)=1f(1) = 1, f(2)=2f(2) = 2, f(3)=0f(3) = 0, f(4)=1f(4) = 1, f(5)=2f(5) = 2, f(6)=0f(6) = 0 and f(7)=1f(7) = 1.
  2. The outputs are only 00, 11 and 22.
  3. With codomain {0,1,2}\{0, 1, 2\}, each target is reached, so ff is surjective onto that set.
  4. With codomain {0,1,2,3}\{0, 1, 2, 3\}, the target 33 is never an output, because a remainder on division by 33 is always less than 33. So ff is not surjective onto this larger set.

Interpretation: the same rule is surjective or not depending only on the set we declare as the target. The remainder rule is designed to land in {0,1,2}\{0, 1, 2\}, so that set is its natural codomain.

Example
3. Labels from a classifier

A model labels photos as cat, dog, bird or fish. On six test photos it predicts: cat, dog, cat, dog, cat, dog. We ask whether the prediction rule is surjective onto the four labels.

  1. The predictions reach only cat and dog.
  2. Bird is a label, and no photo is predicted as bird, so bird is not reached.
  3. Fish is also a label, and it is not reached.
  4. So the rule is not surjective onto the four labels. It reaches two of them.

Try it: a seventh photo is labelled bird. Is the rule now surjective onto the four labels? Answer: no. Bird is now reached, but fish is still missing, so one target remains uncovered.

Common mistakes and tradeoffs

"Surjective means one-to-one." It is tempting to read "onto" as "no repeats". In fact the two are different properties. Surjectivity asks that every target be hit, and it allows many inputs to share an output. The property that forbids repeats is injectivity. A function can have one property without the other, as the examples above show.

"Surjective depends only on the formula." It is tempting to test x2x^2 and decide that it is or is not surjective. In fact the answer depends on the codomain. The same rule can be surjective onto one set and not onto another, as the squaring example showed.

"The outputs must be spread evenly." It is tempting to think that a surjective rule must hit each target the same number of times. In fact it only has to hit each target at least once. The card rule above sends 13 cards to each suit, but a rule that sent 49 cards to hearts and one card each to the other three suits would still be surjective.

When to care. Surjectivity matters when a target set is a list of options that should all be possible: the labels of a classifier, the positions on a scale, the suits of a deck. Checking it catches dead categories early.

Alternatives. If a rule is not surjective, you can often fix it by shrinking the target to its range, as the squaring example did. Surjectivity also survives composition: if ff and gg are both surjective, so is g∘fg \circ f. You can also keep the target and accept that some outputs are never produced, which is common in practice; the question then becomes whether the missing outputs matter. A function that is both surjective and injective is called bijective, and it is the case with an inverse.

History

Long before formal set theory, mathematicians described a rule that covers its whole target in plain words such as "onto". Georg Cantor's comparisons of infinite sets in the 1870s and 1880s depended on matching the elements of one set with another.

In 1891 Cantor proved that no function from a set can be surjective onto its power set, the set of all its subsets. The formal terms "surjective", "injective" and "bijective" were introduced by the Bourbaki group of French mathematicians in their treatment of set theory, from the late 1930s, giving the two halves of a one-to-one correspondence separate names.

Recap

  • A Function assigns each input one output.
  • A function is surjective when every element of its codomain is the output of at least one input.
  • To disprove surjectivity, find one target that no input reaches. To prove it for a formula, take any target and solve for an input.
  • Surjectivity depends on the declared codomain, and shrinking the codomain to the range always makes a function surjective.
  • Next, read Injective Function for the property that forbids repeated outputs. Together they define bijective functions, which are the functions with inverses.

Check yourself

QuestionLet f:{1,2,3,4}→{p,q}f : \{1, 2, 3, 4\} \to \{p, q\} with f(1)=pf(1) = p, f(2)=pf(2) = p, f(3)=pf(3) = p and f(4)=pf(4) = p. Is ff surjective?Show answer

No. Every input goes to pp, so the target qq is never reached. Repeats are allowed, but a missing target is not.

QuestionFor f(x)=2x−5f(x) = 2x - 5 from the real numbers to the real numbers, find the input that gives the output 99, and state whether ff is surjective.Show answer

Solve 2x−5=92x - 5 = 9. Then 2x=142x = 14 and x=7x = 7. Check: 2⋅7−5=92 \cdot 7 - 5 = 9. The same algebra works for any real target yy, since x=(y+5)/2x = (y + 5)/2 is always a real number. So ff is surjective.

QuestionWhy is x↦x2x \mapsto x^2 surjective onto the non-negative reals but not onto all the reals?Show answer

Every non-negative yy equals x2x^2 for x=yx = \sqrt{y}, so the non-negative targets are all reached. A negative target, such as −1-1, is never a square of a real number, so it is not reached when the codomain is all the reals.

QuestionA quality-control program assigns each manufactured part to one of five defect categories, and it wants the rule to be surjective, so that every category is actually observed. After a week, four categories have appeared but one never has. What does this tell you, and what are two possible reasons?Show answer

The rule is not surjective onto the five categories, since one category is never produced. Possible reasons are that the category is genuinely rare, so a week of data is too short to see it, or that the assignment rule has a gap and never selects that category. The program should check the rule and collect more data before drawing a conclusion.

Sources