Search

Search papers and lessons

Injective Function

Level 1 · 9 min read

An injective function, also called one-to-one, never sends two different inputs to the same output, so each output can be traced back to exactly one input.

On this page

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

TL;DR

An injective function is a Function in which different inputs always give different outputs. Once you know the output, you know which input produced it, because no other input could have produced the same value. This property decides whether a rule can be reversed, whether a labelling is unambiguous, and whether two objects can be told apart by their images.

Builds on: Function · Leads to: How Powerful are Graph Neural Networks?

Where it's used

Identification numbers. A university gives each student a student number. The rule "student to number" is injective: two students never share a number, so a number on a form always points to one person. If two students were given the same number, the records would be ambiguous, and the school would have to fix the rule.

Lossless compression. A lossless compressor, such as the one behind a ZIP file, turns a file into a shorter one, and it must be possible to get the original back exactly. That is only possible if two different original files never become the same compressed file. The compressor's rule is therefore injective on the set of files it accepts. A rule that squeezes many inputs into few outputs has to lose information, which is why it cannot be lossless.

Graph neural networks. A graph neural network learns about a node by combining the features of its neighbours. The paper "How Powerful are Graph Neural Networks?" (see the GIN paper) studies this combining step and argues that it must be injective, so that two different neighbourhoods produce two different feature vectors. If the step is not injective, the network cannot tell some different neighbourhoods apart, and it loses information that a good test for graph similarity, such as the Weisfeiler-Lehman test, would keep.

How it works

Take a tiny rule with three inputs. Call the inputs 11, 22 and 33, and let the rule ff give these outputs:

  1. Write down the rule as a list. Here f(1)=10f(1) = 10, f(2)=20f(2) = 20 and f(3)=40f(3) = 40. Every input has exactly one output, so ff is a Function.
  2. Compare the outputs in pairs. The outputs are 1010, 2020 and 4040. The pair (1,2)(1, 2) gives 1010 and 2020, which differ. The pair (1,3)(1, 3) gives 1010 and 4040, which differ. The pair (2,3)(2, 3) gives 2020 and 4040, which differ.
  3. Every pair of different inputs has different outputs. So ff is injective. Check: if you see the output 2020, the only input that could have made it is 22.
  4. Now change one value and let f(3)=20f(3) = 20 instead. The rule is still a function, since 33 still has one output.
  5. Compare again. The pair (2,3)(2, 3) now gives 2020 and 2020. Two different inputs share an output, so ff is no longer injective. If you see the output 2020, you cannot tell whether it came from 22 or from 33.

So the test is always the same: look for two different inputs with the same output. Finding one is enough to show that the function is not injective.

The same test works on a picture of a graph. Consider the rule f(x)=x2f(x) = x^2 on the numbers from −3-3 to 33. The points (2,4)(2, 4) and (−2,4)(-2, 4) both lie on the graph, so two different inputs give the output 44. Draw a horizontal line at height 44: it hits the curve twice. A function is injective exactly when every horizontal line hits its graph at most once. This is the horizontal line test, and it is the picture version of step 5.

−3−2−1012302468xf(x)
  • f(x) = x²
The horizontal line at height 4 meets the curve at x = 2 and x = -2. Two inputs, one output: this function is not injective on this domain.

Here is the same pattern as a diagram, with arrows from each input to its output for the first version of ff:

flowchart LR
  i1[input 1] --> o10[output 10]
  i2[input 2] --> o20[output 20]
  i3[input 3] --> o40[output 40]

Each arrow leaves a different input and lands on a different output, so no two arrows meet. In the second version, the arrows from 22 and 33 would meet at 2020.

The math

The arrow →\to reads "maps to", and A→BA \to B means a function from the set AA to the set BB. A function is injective when the following statement is true.

Definition: Injective function

A function f:A→Bf : A \to B is injective, or one-to-one, if for all a,a′∈Aa, a' \in A,

f(a)=f(a′)  ⟹  a=a′.f(a) = f(a') \implies a = a'.

Here aa and a′a' are two inputs (read "a prime" for the second), f(a)f(a) and f(a′)f(a') are their outputs, and   ⟹  \implies means "then". In words: if two inputs give the same output, they must be the same input. Equivalently, if a≠a′a \neq a', then f(a)≠f(a′)f(a) \neq f(a'). This second form is the Contrapositive of the first, and it is the one that reads most naturally when you are checking that outputs differ.

Step 2 in symbols

For the first rule, f(1)=10f(1) = 10, f(2)=20f(2) = 20, f(3)=40f(3) = 40. The condition has to hold for every pair. There are three pairs of different inputs, and each gives different outputs:

f(1)≠f(2),f(1)≠f(3),f(2)≠f(3).f(1) \neq f(2), \qquad f(1) \neq f(3), \qquad f(2) \neq f(3).

Since no pair breaks the condition, ff is injective.

Step 5 in symbols

For the second rule, f(2)=f(3)=20f(2) = f(3) = 20 while 2≠32 \neq 3. This one pair breaks the implication: the outputs are equal but the inputs are not. A single such pair is enough to show that the function is not injective, however many other pairs are fine.

Proving injectivity for a formula

Checking every pair is impossible when the domain is infinite, so we use algebra. Let f(x)=2x+3f(x) = 2x + 3 on the real numbers, a straight-line rule. Suppose two inputs aa and bb give the same output. Then:

2a+3=2b+3.2a + 3 = 2b + 3.

Subtract 33 from both sides to get 2a=2b2a = 2b. Divide both sides by 22 to get a=ba = b. The assumption f(a)=f(b)f(a) = f(b) forced a=ba = b, so ff is injective. This is the pattern to follow in general: start from f(a)=f(b)f(a) = f(b) and try to reach a=ba = b.

Injectivity and the inverse

Let f:A→Bf : A \to B be injective. For each output bb in the range of ff, there is exactly one input aa with f(a)=bf(a) = b. So the rule that sends each output back to its input is well defined on the range. This reverse rule is the inverse of ff on its range. An injective function is therefore exactly one that can be undone, provided we only undo outputs that the function actually produces.

Worked examples

Example
1. A tiny rule on four numbers

Let A={1,2,3,4}A = \{1, 2, 3, 4\} and f(n)=3nf(n) = 3n.

  1. Compute the outputs: f(1)=3f(1) = 3, f(2)=6f(2) = 6, f(3)=9f(3) = 9, f(4)=12f(4) = 12.
  2. List the outputs: 33, 66, 99, 1212. All four are different.
  3. Since every pair of different inputs gives different outputs, ff is injective on AA.
  4. The inverse on the range is easy to read off: 1212 came from 44, 99 from 33, and so on.

Interpretation: multiplying by a non-zero number is injective, because different inputs get scaled to different outputs.

Example
2. Squaring on a small set, and then on a bigger set

Let f(n)=n2f(n) = n^2. First take A={1,2,3}A = \{1, 2, 3\}.

  1. The outputs are f(1)=1f(1) = 1, f(2)=4f(2) = 4 and f(3)=9f(3) = 9.
  2. All three outputs are different, so ff is injective on {1,2,3}\{1, 2, 3\}.

Now take the bigger set A={−2,−1,0,1,2}A = \{-2, -1, 0, 1, 2\}.

  1. The outputs are f(−2)=4f(-2) = 4, f(−1)=1f(-1) = 1, f(0)=0f(0) = 0, f(1)=1f(1) = 1 and f(2)=4f(2) = 4.
  2. The output 11 comes from both −1-1 and 11, and the output 44 comes from both −2-2 and 22.
  3. So ff is not injective on this bigger set.

Interpretation: the same formula can be injective on one domain and not on another. Squaring is injective on the non-negative numbers, but it is not injective on all numbers, because xx and −x-x always give the same square.

Example
3. Product codes in a shop

A shop gives each product a short code. It wants the rule "product to code" to be injective, so that a code scanned at the till always identifies one product. The current assignment is:

ProductCode
MugM1
PenP2
CupM1
NotebookN4
  1. Look for two different products with the same code. Mug and Cup both have code M1.
  2. So the rule is not injective. Scanning M1 would not say which product was sold.
  3. The fix is to change one code. Give the cup a new code, M2, and check that the codes are now M1, P2, M2 and N4.
  4. Check the pairs once more, and make sure the new code is not used by any other product.

Try it: the shop now wants to add a fifth product, a bag, and gives it the code M2. Is the rule still injective? Answer: no. The cup already has the code M2, so two products would share a code again. The bag needs a code that no other product uses, such as B5.

Common mistakes and tradeoffs

"Injective" means every output is used. It is tempting to think that an injective function covers its whole codomain. In fact that is a different property, called surjective, which asks that every element of the codomain is some output. A function can be injective and still miss most of its codomain: f(n)=3nf(n) = 3n from the whole numbers to the whole numbers is injective, but it never produces 11. Injectivity is about outputs never repeating, not about all outputs being reached.

A few passing checks prove nothing. It is tempting to test two or three inputs, see different outputs, and call the function injective. In fact the definition requires the condition for every pair. A few checks can only disprove injectivity, by finding a repeated output. To prove it, you need an argument like the algebra in "Proving injectivity for a formula", or a check of every case in a finite domain.

Injectivity depends on the domain. It is tempting to ask whether x2x^2 is injective, as if the formula were the whole story. In fact the answer depends on the inputs allowed: x2x^2 is injective on the non-negative numbers and not on all numbers. Always state the domain.

When to use it. Use an injective rule when information must not be lost or confused: identifiers, reversible encodings, and labels that must point to one thing. Many-to-one rules are the right tool when you want to summarise, such as taking an average of a list of scores, or when you want to squeeze a large input into a small fixed-size label on purpose, as a hash function does. A hash function accepts that different inputs will sometimes share a label, and it is designed to make such clashes rare. In that case you accept that many inputs share an output, because you only need the summary.

Alternatives. If one input can legitimately have several outputs, a function is the wrong model, and a relation describes the situation better. If you need to undo a many-to-one rule, you have to store extra information, such as a timestamp or a full copy of the input, since the output alone is ambiguous.

History

Early work on injections was done in the 1870s and 1880s by Georg Cantor, when he asked whether two infinite sets have the same size. His answer was to look for a one-to-one correspondence between them: a rule that pairs every element of one set with exactly one element of the other, with nothing left over on either side. Injectivity is one half of that idea, and it lets you show that one set is no bigger than another.

The words "injective" and "surjective" came later, in the mid-20th century, when the Bourbaki group of French mathematicians rewrote much of mathematics in terms of sets and functions. Their terms made it easy to talk about the two halves of a one-to-one correspondence separately. The idea caught on because it applies to any sets at all, not only to numbers.

Recap

  • A Function gives each input exactly one output.
  • A function is injective when different inputs never share an output, so each output points back to one input.
  • To disprove injectivity, find two different inputs with the same output. To prove it for a formula, assume f(a)=f(b)f(a) = f(b) and show a=ba = b.
  • The horizontal line test is the picture version: a horizontal line meets an injective graph at most once.
  • Injectivity depends on the domain, and it is different from surjectivity, which asks that every element of the codomain be hit.

Next, read the GIN paper to see injective aggregation applied to graph neural networks. For a family of injective functions with a straight-line graph, see Linear Function. For the idea of chaining rules together, see Function Composition.

Check yourself

QuestionLet f:{a,b,c}→{1,2,3}f : \{a, b, c\} \to \{1, 2, 3\} with f(a)=1f(a) = 1, f(b)=3f(b) = 3 and f(c)=1f(c) = 1. Is ff injective?Show answer

No. Inputs aa and cc are different, but f(a)=f(c)=1f(a) = f(c) = 1. One pair of equal outputs from different inputs is enough to disprove injectivity.

QuestionFor f(x)=5x−2f(x) = 5x - 2 on the real numbers, solve f(x)=13f(x) = 13 and use the answer to check that the equation has only one solution.Show answer

Solve 5x−2=135x - 2 = 13. Add 22 to get 5x=155x = 15, then divide by 55 to get x=3x = 3. Check: 5⋅3−2=135 \cdot 3 - 2 = 13. The steps only ever gave one value of xx, which matches injectivity: the output 1313 has exactly one input.

QuestionWhy can a lossless compressor not map every file to a shorter file of the same kind, and what property must it have?Show answer

A lossless compressor must be injective on the files it accepts. If two different files shared a compressed form, the decompressor would not know which original to return. Since there are more long files than short ones, some files must become longer, so no lossless rule can shrink every input.

QuestionIs f(x)=x2f(x) = x^2 injective on the set {0,1,2,3}\{0, 1, 2, 3\}? Is it injective on the set {−1,0,1}\{-1, 0, 1\}?Show answer

On {0,1,2,3}\{0, 1, 2, 3\} the outputs are 0,1,4,90, 1, 4, 9, all different, so yes. On {−1,0,1}\{-1, 0, 1\} the outputs are 1,0,11, 0, 1, and −1-1 and 11 share the output 11, so no. The formula is the same; the domain decides the answer.

Sources