Mathematics for Computer Science 201-N11 • CEGEP, Montreal
Corrected exercises: injective, surjective and bijective functions (201-N11)
This is the corrected exercise set on functions between sets, injections, surjections and bijections, for Mathematics for Computer Science, 201-N11, the discrete mathematics course taken at CEGEP by students in computer science programs. Part A lays the foundations: what distinguishes a function from an arbitrary relation, the image, the inverse image of a subset and the fibres, the three natures read on an arrow diagram, the three standard write-ups for proving injectivity, proving surjectivity and disproving, then composition and what it really inherits. Part B moves up to exam level: the substitution cipher and its inverse, counting functions, injections, surjections and bijections between finite sets, a hash function examined as a function, and the enumeration of the integers by the natural numbers.
The thread of the set: injective, surjective and bijective are not properties of a formula, they are properties of the triple made of the rule, the domain and the codomain. The same rule x↦x2 takes all four possible natures depending on the two sets it is given, so the move repeated from one exercise to the next is always the same: look at the domain and the codomain before answering.
The second thread is the one that makes this chapter a computer science chapter: cardinality. An injection from A to B says ∣A∣≤∣B∣, a surjection says the opposite inequality, a bijection says equality, and that is how two sets are compared without counting either one. It is also what makes any injective hash function impossible, and what puts the natural numbers and the integers on an equal footing.
Self-checking setType your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.
•A function f:A→B is a relation that gives each element of A ONE image and ONLY ONE. On an arrow diagram: exactly one arrow leaves each element on the left.
•INJECTIVE: each element of the codomain receives AT MOST one arrow. SURJECTIVE: AT LEAST one. BIJECTIVE: EXACTLY one.
•These three words never describe a formula alone, but the triple rule, domain, codomain. With R+=[0,∞), the nonnegative reals, x↦x2 is bijective from R+ onto R+ and none of the three from R to R.
•Proving injectivity: start from f(a)=f(b) and arrive at a=b. Proving surjectivity: take any y and BUILD a preimage. Disproving: a single counterexample with numbers.
•f−1(Q), applied to a SUBSET of the codomain, always exists and returns the set of preimages. The inverse FUNCTION f−1 exists only if f is bijective.
•The nonempty fibres f−1({y}) form a partition of the domain: they are the classes of the relation having the same image.
•Composition: if g∘f is injective, then f is; if g∘f is surjective, then g is. Injectivity travels back to the input, surjectivity travels forward to the output.
•Inverse of a composition: (g∘f)−1=f−1∘g−1, the order is reversed.
•Between finite sets: ∣B∣∣A∣ functions, (∣B∣−∣A∣)!∣B∣! injections, ∣A∣! bijections when the cardinalities are equal.
•An injection gives ∣A∣≤∣B∣, a surjection gives ∣A∣≥∣B∣, a bijection gives equality. For finite sets of the same cardinality, injective is equivalent to surjective; for infinite sets, it is not.
Part A: the basics (/50)
Exercise 1: Function or relation: what the word function requires
A relation from A to B is any subset of A×B. A FUNCTION f:A→B is a relation that meets two conditions, and only two: every element of A has an image (EXISTENCE), and it has only one (UNIQUENESS). The whole chapter rests on this starting point, because a relation that fails either condition does not even deserve the question of whether it is injective.
The figure shows four relations from A={1,2,3} to B={a,b,c}, drawn as arrow diagrams: one arrow from the domain to the codomain for each pair in the relation.
a) Which of the four relations in the figure are functions from A to B? For each relation you reject, name the missing condition.
b) For relation (1), give f(1), f(2) and f(3), then the image f(A) and its cardinality.
c) Is relation (4) a function? Give its image and its cardinality.
d) Can a function from A to B have two elements of A with the same image? Can it have two elements of B with the same preimage?
e) Seen as a subset of A×B, exactly how many pairs does a function from A to B contain? Compare with the number of possible relations from A to B.
Show the solution
Answers
a)The functions are (1) and (4); (2) fails existence, (3) fails uniqueness
b)f(1)=a, f(2)=b, f(3)=b, so f(A)={a,b}, of cardinality 2
c)Yes, the constant function with value c; f(A)={c}, of cardinality 1
d)Yes for the same image, no for the same preimage: a function collapses, it never splits
e)Exactly 3 pairs, against 9 in A×B and 512 possible relations
a)
The functions are (1) and (4). Relation (2) fails EXISTENCE: no arrow leaves the element 2, so f(2) refers to nothing and the notation itself loses its meaning. Relation (3) fails UNIQUENESS: two arrows leave 1, towards a and towards b, so f(1) would refer to two objects at once. The test is read at a glance on an arrow diagram: there must be EXACTLY ONE arrow leaving EACH element on the left. Not zero, not two. Nothing at all is required on the codomain side, however: an element of B may receive zero, one or ten arrows without leaving the world of functions. It is precisely this asymmetry between domain and codomain that will produce injectivity and surjectivity in the next exercises.
b)
f(1)=a, f(2)=b, f(3)=b. The image is f(A)={a,b}, of cardinality 2. It is not written {a,b,b}: a set does not count repetitions, and that is exactly why the cardinality of the image can be strictly smaller than that of the domain. Here ∣f(A)∣=2<3=∣A∣. The lost element is the visible sign that two arrows land in the same place.
c)
Yes, relation (4) is a function: each of the three elements of A has exactly one arrow, and nothing forbids them from all ending at the same point. It is the constant function with value c. Its image is f(A)={c}, of cardinality 1. The classic error is to reject it on the grounds that c receives three arrows: that condition does not exist. It costs the whole question, when rereading the definition was enough.
d)
Two elements of A can perfectly well have the same image, and relation (4) gives the extreme example. It is in fact exactly the defect that will be called non-injectivity. On the other hand, two elements of B can never have the same preimage: that would mean an element of A has two images, which uniqueness forbids. Remember the asymmetric wording: a function is allowed to COLLAPSE, it is not allowed to SPLIT.
e)
A function from A to B contains exactly ∣A∣=3 pairs, one per element of the domain, never one more or one less. The Cartesian product A×B, on the other hand, has 3×3=9 pairs, and it has 29=512 subsets, so there are 512 relations from A to B. Functions are therefore a narrow minority among relations, 33=27 out of 512, or about 5.3 percent. This count comes back in exercise 7, where it is used to count injections and bijections.
Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.
Exercise 2: Image and inverse image of a subset
Two notations look alike and say completely different things. f(P), for P a subset of the domain, is the set of images of the elements of P. f−1(Q), for Q a subset of the codomain, is the set of PREIMAGES of the elements of Q. The second one can be read and computed for any function, even the most collapsing one.
Take E={1,2,3,4,5,6}, F={a,b,c,d} and the function f:E→F given by f(1)=a, f(2)=c, f(3)=a, f(4)=b, f(5)=c and f(6)=a.
The figure below shows the situation.
a) Give the image f(E) and its cardinality. Which element of F is nobody's image?
b) Compute f(P) for P={1,3,4}, then f(P′) for P′={2,5}. Each time, compare the cardinality of the subset with that of its image.
c) Compute f−1({a}), f−1({c}), f−1({d}) and f−1({a,b}).
d) Check that the four sets f−1({a}), f−1({b}), f−1({c}) and f−1({d}) have union E and are pairwise disjoint. What do you recognize from the chapter on relations?
e) Does the notation f−1({a}) assume that f has an inverse function?
Show the solution
Answers
a)f(E)={a,b,c}, of cardinality 3; d is hit by nothing
b)f(P)={a,b} of cardinality 2 for ∣P∣=3; f(P′)={c} of cardinality 1 for ∣P′∣=2
c){1,3,6}, {2,5}, ∅ and {1,3,4,6}
d)Union E, pairwise disjoint subsets: the fibres form a partition, the classes of having the same image
e)No: f−1 applied to a SUBSET is always defined, the inverse function only if f is bijective
a)
f(E)={a,b,c}, of cardinality 3. The element d has no preimage: no arrow reaches it, and we say it is not HIT. The image is therefore a proper subset of F, and that is exactly what makes f fail to be surjective, in the sense defined in exercise 3. The reflex to build: the image is not the codomain, it is the part of the codomain actually used, and the two coincide only in the surjective case.
b)
f(P)={f(1),f(3),f(4)}={a,a,b}={a,b}, of cardinality 2 while ∣P∣=3. f(P′)={f(2),f(5)}={c}, of cardinality 1 while ∣P′∣=2. The general rule is ∣f(P)∣≤∣P∣, with equality exactly when f merges no two elements of P. An image never grows: this is the first cardinality result of the chapter, and it is the source of the inequality ∣A∣≤∣B∣ attached to an injection.
c)
f−1({a})={1,3,6}, the three elements whose image is a. f−1({c})={2,5}. f−1({d})=∅: no preimage, and the empty set is a perfectly legitimate answer, not a failed computation. Finally f−1({a,b})={1,3,6}∪{4}={1,3,4,6}. The classic trap is to answer with an ELEMENT when f−1 returns a SET: f−1({b}) is {4} and not 4, and the distinction is the one the chapter on sets already made between 3 and {3}.
d)
The four sets are {1,3,6}, {4}, {2,5} and ∅. Their union is indeed {1,2,3,4,5,6}=E, since every element has an image and therefore belongs to exactly one of these subsets, and the cardinalities add up to 3+1+2+0=6. They are pairwise disjoint, because an element cannot have two images. We therefore recover a PARTITION of E, up to the empty parts: these are the classes of the equivalence relation having the same image, studied in the chapter on relations. Each set f−1({y}) is called a FIBRE of f over y, and the word is used as is in exercise 9 on hashing.
e)
No, and this is the most common misreading of the chapter. The notation f−1(Q), with a SET inside the parentheses, is defined for every function without exception: it denotes a subset of the domain, obtained by following the arrows backwards. The inverse FUNCTION f−1:F→E, on the other hand, exists only if f is bijective, and our f is nowhere near that, since d has no preimage and a has three. Two identical notations, two different objects: one takes a set and returns a set, the other would take an element and return an element. Confusing them leads to writing f−1(a)=1, which means nothing here and costs all the marks for the question.
Exercise 3: The same formula, four domains and four codomains
Injective, surjective and bijective are not properties of a formula. They are properties of the TRIPLE made of the rule, the domain and the codomain. Changing one of the two sets without touching the rule changes the answer, and the student who says that squaring is not injective without having looked at the domain has already lost the marks.
The definitions, to keep in front of you: f is INJECTIVE when two distinct elements of the domain have distinct images, in other words when each element of the codomain receives AT MOST one arrow; it is SURJECTIVE when every element of the codomain has at least one preimage, in other words receives AT LEAST one; it is BIJECTIVE when it is both, so when each one receives EXACTLY one.
The figure shows four functions between finite sets, one per panel.
a) For each of the four functions in the figure, say whether it is injective only, surjective only, bijective, or none of the three. Justify by the number of arrows received.
b) Consider the rule x↦x2, where R+=[0,∞) denotes the nonnegative reals, zero included. Study the injectivity and the surjectivity of f:R→R, then of f:R+→R.
c) Same questions for f:R→R+, then for f:R+→R+.
d) Same questions for f:Z→N, n↦n2.
e) Between questions b), c) and d), what was changed and what was left untouched? State the reading rule this forces on you.
Show the solution
Answers
a)(1) injective only, (2) surjective only, (3) bijective, (4) none of the three
b)R→R: none of the three; R+→R: injective only
c)R→R+: surjective only; R+→R+: bijective, with the square root as inverse
d)Z→N: none of the three; the image is the set of perfect squares, only 6 values from 0 to 25
e)Only the domain and the codomain changed: look at the domain and the codomain before answering
a)
Panel (1): each element of B receives at most one arrow, but d receives none, so the function is INJECTIVE ONLY. Panel (2): all three elements of B are hit, but a receives two arrows, those from 1 and from 2, so SURJECTIVE ONLY. Panel (3): each element of B receives exactly one arrow, so BIJECTIVE. Panel (4): a receives two and b none, so NONE OF THE THREE. Counting the arrows received is the only move to remember for a finite set: at most one gives injectivity, at least one surjectivity, exactly one bijectivity. The arrows LEAVING, on the other hand, are not counted: they have already been used to decide that the object is a function.
b)
From R to R: not injective, because f(−2)=f(2)=4 with −2=2, and a single counterexample is enough; not surjective either, because −1 has no preimage, the square of a real number never being negative. From R+ to R: it becomes INJECTIVE, because if a and b are nonnegative and a2=b2 then a=b, the candidate −b having been ruled out by the domain; it remains non-surjective, since the codomain has not moved and −1 is still not hit.
c)
From R to R+: it becomes SURJECTIVE, because every y≥0 has the preimage y, and even two as soon as y>0; it remains non-injective, since the domain has not moved and −2 and 2 still have the same image. From R+ to R+: the two corrections add up, the function is BIJECTIVE, and its inverse is the square root. We have thus obtained all four possible natures with ONE SINGLE computation rule, without ever changing a sign.
d)
From Z to N: neither one nor the other. Not injective, because (−3)2=32=9. Not surjective either, and that is the trap of the question: the codomain N looks tailor-made, since squares of integers are indeed natural numbers, but 2, 3, 5, 6 and 7 are the squares of no integer. Of the 26 integers from 0 to 25, only 6 are perfect squares and 20 are not. The image is the set of perfect squares, a very thin part of N. Remember that shrinking the codomain is not enough: it must be shrunk ALL THE WAY DOWN to the exact image.
e)
Only the domain and the codomain were changed. The rule x↦x2 did not move. The reading rule that follows is the thread of the whole set: LOOK AT THE DOMAIN AND THE CODOMAIN BEFORE ANSWERING. A question that asks whether squaring is injective without specifying the two sets is not a hard question, it is an ill-posed one. The precise mechanism is worth memorizing: shrinking the DOMAIN can only help injectivity and never makes a function surjective; shrinking the CODOMAIN can only help surjectivity and never changes injectivity.
Exercise 4: The three standard write-ups: prove, prove, disprove
Three questions, three write-ups that look nothing alike. Proving injectivity is a UNIVERSAL statement: start from f(a)=f(b) and you must arrive at a=b, for all pairs at once. Proving surjectivity is an EXISTENCE statement: take any y in the codomain and BUILD a preimage. Disproving either one is a COUNTEREXAMPLE: a single one is enough, and it must be shown with its values.
The choice of write-up is not a matter of style. Writing an existence statement as if it were universal, or believing that you disprove something by saying it does not always work, are the two ways of losing all the marks on a theory question.
a) Prove that f:R→R, f(x)=3x−7, is injective, starting from f(a)=f(b).
b) Prove that the same f is surjective, by solving f(x)=y for an arbitrary y. Check your preimage on y=5.
c) Disprove the injectivity of g:R→R, g(x)=x2−4x+1. Is a single counterexample enough, and why?
d) Let h:Z→Z, h(n)=2n. Is it injective? surjective? Answer the question again for h:Z→2Z, where 2Z denotes the set of even integers.
e) Sort the three write-ups: which one proves a universal statement, which one an existence statement, and which one settles for a single example?
Show the solution
Answers
a)3a−7=3b−7 implies a=b: f is injective
b)x=3y+7 works for every y; for y=5, x=4 and f(4)=5
c)g(0)=g(4)=1 with 0=4; a single counterexample is enough, since it negates a universal statement
d)Z→Z: injective only; Z→2Z: bijective
e)Injectivity universal, surjectivity existential, disproof by a single example
a)
Let a and b be two real numbers such that f(a)=f(b). Then 3a−7=3b−7, so 3a=3b by adding 7 to both sides, so a=b by dividing by 3, which is legitimate since 3=0. Two elements with the same image are therefore equal: f is injective. The write-up begins with LET and ends with the requested conclusion, in a chain of implications, without a single numerical example. Checking three values proves nothing, it only tests, and that is worth zero on the question.
b)
Let y be an arbitrary real number. We look for x such that 3x−7=y, that is 3x=y+7, that is x=3y+7. This number is indeed real, whatever y is, since division by 3 is always possible. Mandatory check: f(3y+7)=3⋅3y+7−7=y+7−7=y. So every real number has a preimage and f is surjective. For y=5: x=312=4, and f(4)=12−7=5. The function is therefore bijective, which was predictable: a linear function with nonzero slope, from R to R, always is.
c)
g(0)=1 and g(4)=16−16+1=1. So g(0)=g(4) with 0=4: g is not injective. A single counterexample is enough, because injectivity asserts something for ALL pairs, and the negation of a universal statement is the existence of ONE failing case. This pair is found without trial and error, by noticing that the quadratic has its vertex at x=2 and that two points symmetric about this axis have the same image; 0 and 4 are at distance 2 on either side. Writing only that the quadratic is not monotonic disproves nothing: you must produce the two values and their common image.
d)
From Z to Z: injective, because 2a=2b gives a=b; not surjective, because 3 is odd and no integer n satisfies 2n=3. From Z to 2Z: the rule and the domain are unchanged, so injectivity still holds without proving anything again, and surjectivity becomes true since every even integer can be written 2k with k an integer, this k being the preimage we want. The function is therefore BIJECTIVE from Z onto 2Z. It is the thread of exercise 3 applied once more, and this bijection comes back in exercise 10, where it says something surprising about infinite cardinalities.
e)
Injectivity is a UNIVERSAL statement: for all a and b, if f(a)=f(b) then a=b. Surjectivity is an EXISTENCE statement: for every y, there EXISTS x such that f(x)=y, and the write-up must produce this x, not merely assert that it exists. Disproving is the only one of the three moves that settles for an example, and that is because it negates a universal statement. The table to remember fits in three lines: to ESTABLISH a universal statement you need a general proof; to DESTROY it one example is enough; to ESTABLISH an existence statement, on the contrary, one example is enough, here the preimage built in b).
Exercise 5: Composition and what it inherits
The composition g∘f is read from right to left: apply f, then g. It goes from the domain of f to the codomain of g, and it is defined only if the codomain of f is indeed the domain of g.
The figure shows f:A→B and g:B→C with A={1,2,3}, B={a,b,c,d} and C={x,y,z}. It is built to defuse the most stubborn idea of the chapter, namely that a composition inherits the defects of its two pieces.
a) Give (g∘f)(1), (g∘f)(2) and (g∘f)(3), then say whether g∘f is injective, surjective, bijective.
b) Is f surjective? Is g injective? What becomes of the idea that a composition inherits the defects of both functions?
c) Prove that if f and g are injective then g∘f is, then that if f and g are surjective then g∘f is.
d) Prove that if g∘f is injective then f is injective. Does the figure show that g, for its part, may fail to be?
e) Prove that if g∘f is surjective then g is surjective. Does the figure show that f, for its part, may fail to be?
Show the solution
Answers
a)x, y and z: g∘f is bijective
b)f not surjective, d is missed; g not injective, b and d go to y; and yet the composition is bijective
c)Both implications are true: injectivity and surjectivity are passed on to the composition
d)g∘f injective implies only f injective; the figure shows g not injective
e)g∘f surjective implies only g surjective; the figure shows f not surjective
a)
f(1)=a then g(a)=x, so (g∘f)(1)=x. Likewise (g∘f)(2)=g(b)=y and (g∘f)(3)=g(c)=z. The three images are distinct, so g∘f is injective, and they exhaust C={x,y,z}, so it is surjective: g∘f is BIJECTIVE. The computation is always done in this order, following the arrows; writing (f∘g)(1) would make no sense here, since g does not start from A.
b)
f is NOT surjective: d receives no arrow from A. g is NOT injective: b and d have the same image y. And yet the composition is bijective. The idea that a composition inherits defects is therefore false, and this figure is a complete counterexample: a function that does not hit everything, followed by a function that merges two elements, can give a bijection, because the merging done by g involves precisely the element d that f never reached. What is passed on are the QUALITIES, and not even in both directions, as questions d) and e) show.
c)
INJECTIVITY. Let a1 and a2 be in A with (g∘f)(a1)=(g∘f)(a2), that is g(f(a1))=g(f(a2)). Since g is injective, f(a1)=f(a2). Since f is injective, a1=a2. So g∘f is injective. SURJECTIVITY. Let c be in C. Since g is surjective, there exists b in B with g(b)=c. Since f is surjective, there exists a in A with f(a)=b. Then (g∘f)(a)=g(b)=c. So g∘f is surjective. Notice the difference in movement: the injectivity proof UNWINDS, it starts from an equality and removes the functions one by one; the surjectivity proof BUILDS, it starts from the codomain and works its way back to the domain. Mixing the two movements produces a write-up that proves nothing.
d)
Suppose g∘f is injective and let a1,a2 satisfy f(a1)=f(a2). Applying g to both sides, g(f(a1))=g(f(a2)), so (g∘f)(a1)=(g∘f)(a2), so a1=a2 by injectivity of the composition. So f is injective. The figure does show that g can fail: g∘f is injective there and yet g(b)=g(d)=y. The reason fits in one sentence: the composition only sees g through its restriction to f(A)={a,b,c}, and d is invisible to it. Remember the asymmetry: from an injective composition, only the FIRST function inherits.
e)
Suppose g∘f is surjective and let c be in C. There exists a in A with g(f(a))=c. Then the element b=f(a) of B satisfies g(b)=c, so c has a preimage under g: g is surjective. The figure confirms it and shows that f, for its part, can fail: g∘f is surjective there while f misses d. It is the mirror image of the previous asymmetry: from a surjective composition, only the SECOND function inherits. Both results sum up in one sentence, and that is the form to memorize: injectivity travels back to the input, surjectivity travels forward to the output.
Part B: problems and reasoning (/50)
Exercise 6: Problem: the bijection as a guarantee of decryption
A substitution cipher replaces each letter by another one, according to a fixed table. The question that settles everything is not how strong the scheme is, it is whether it can be decrypted: an encrypted message can be recovered only if the table is a BIJECTION from the alphabet onto itself.
We work on the reduced alphabet Σ={A,B,C,D,E} and on the table c given by the figure.
a) Check that c is a bijection from Σ onto Σ. How many functions from Σ to Σ are there, and how many are bijections? What proportion is that?
b) Build c−1, letter by letter, then decrypt the message EDBCA.
c) Check c−1(c(B))=B and c(c−1(B))=B. Why are BOTH equalities required before speaking of an inverse function?
d) We compose with a second cipher d, the shift by one position: d(A)=B, d(B)=C, d(C)=D, d(D)=E, d(E)=A. Give the table of d∘c, say whether it is a bijection, then express (d∘c)−1 in terms of c−1 and d−1.
e) An intern proposes e(A)=A, e(B)=A, e(C)=C, e(D)=C, e(E)=E, explaining that this compresses the message. How many five-letter messages have AAAAA as their ciphertext? Is decryption possible?
Show the solution
Answers
a)c is a bijection; 55=3,125 functions, 5!=120 bijections, that is 3.84 percent
b)c−1:A→C, B→D, C→A, D→E, E→B; EDBCA decrypts to BEDAC
c)Both equalities equal B; one expresses injectivity, the other surjectivity
d)d∘c:A→D, B→A, C→B, D→C, E→E, bijective, and (d∘c)−1=c−1∘d−1
e)25=32 messages give AAAAA; decryption impossible, e is not injective
a)
The table gives c(A)=C, c(B)=E, c(C)=A, c(D)=B, c(E)=D. The five images are C, E, A, B and D: they are pairwise distinct, so c is injective, and they exhaust Σ, so c is surjective. It is a bijection. Since the domain and the codomain have the same finite cardinality here, one of the two checks would have been enough, a result proved in exercise 7. There are 55=3,125 functions from Σ to Σ, of which 5!=120 are bijections, that is 3.84 percent. In other words, more than 96 functions out of 100 are useless as a cipher, so drawing a table at random is not good enough.
b)
Reverse each arrow. c(C)=A gives c−1(A)=C; c(D)=B gives c−1(B)=D; c(A)=C gives c−1(C)=A; c(E)=D gives c−1(D)=E; c(B)=E gives c−1(E)=B. EDBCA is decrypted letter by letter: E→B, D→E, B→D, C→A, A→C, which gives BEDAC. Check in the other direction: c applied to BEDAC does give back E, D, B, C, A. The trap is to read the table in the wrong direction, which produces meaningless text without any visible computation error: checking by re-encrypting is therefore mandatory.
c)
c(B)=E and c−1(E)=B: the first equality holds. c−1(B)=D and c(D)=B: so does the second. Both are required because they do not say the same thing. The equality c−1∘c=idΣ says that you always get back the ORIGINAL message, which corresponds to the injectivity of c; the equality c∘c−1=idΣ says that every possible encrypted letter does come from a plaintext letter, which corresponds to surjectivity. A function satisfying only one of the two has only a left inverse or a right inverse, never an inverse, and on an infinite set both situations really occur: the doubling map n↦2n, taken from N to N, has the left inverse m↦⌊m/2⌋ but no right inverse, since 1 has no preimage.
d)
Apply c then d: A→C→D, B→E→A, C→A→B, D→B→C, E→D→E. The table of d∘c is therefore A→D, B→A, C→B, D→C, E→E, and its five images are distinct: it is a bijection, as guaranteed by the result of exercise 5 on the composition of two bijections. Its inverse is (d∘c)−1=c−1∘d−1, and the order is reversed: to undo two chained operations, you first undo the LAST one performed. Writing d−1∘c−1 is the classic error, and it shows up immediately: in the general case where the three sets differ, that composition does not even start from the right set.
e)
The function e is not injective: A and B have the same image A, C and D have the same image C. To obtain AAAAA, each of the five positions must contain a letter whose image is A, so A or B: that makes 25=32 different plaintext messages that give the same ciphertext. Decryption is therefore impossible, not for lack of cleverness but on principle: e, not being injective, has no inverse, and a recipient who must choose among 32 messages has no way to decide. It is exactly the argument the chapter on proof techniques uses against the universal lossless compressor, and here it is the same property, read on the function rather than on a count.
Exercise 7: Counting functions, injections and bijections
Three tools from the course are used here and not proved again: the multiplication principle and the k-permutations (n−k)!n!, covered in the counting set of the same course, and the inclusion-exclusion principle, covered in the set on sets. They are used as they are to count functions between finite sets.
The goal is the second thread of the set: an injection from A to B forces ∣A∣≤∣B∣, a surjection forces ∣A∣≥∣B∣, a bijection forces equality. That is how two sets are compared without counting either one.
a) ∣A∣=3 and ∣B∣=5. How many functions from A to B? How many injections? How many bijections?
b) ∣A∣=4 and ∣B∣=3. How many functions? How many injections, and why? How many surjections, by inclusion-exclusion?
c) ∣A∣=∣B∣=4. How many functions, how many bijections, and what proportion?
d) State the three cardinality inequalities attached to the existence of an injection, a surjection and a bijection between finite sets.
e) A and B are finite with the SAME cardinality. Prove that an injective function f:A→B is automatically surjective. Does this result still hold if A and B are infinite?
e)Yes for finite sets, since f(A) is a subset of B with the same cardinality; false for infinite sets, as 2Z⊂Z shows
a)
FUNCTIONS: each element of A chooses its image freely among the 5 elements of B, independently of the others, so 53=125 by the multiplication principle. INJECTIONS: the images must be pairwise distinct, so the first element has 5 choices, the second 4, the third 3, that is 5×4×3=60. You recognize the k-permutation count 2!5!=60 from the counting set: counting the injections from A to B is exactly counting the words of 3 distinct letters over an alphabet of 5. BIJECTIONS: none, that is 0, since a bijection would require 3=5. The general formula to remember: ∣B∣∣A∣ functions and (∣B∣−∣A∣)!∣B∣! injections.
b)
FUNCTIONS: 34=81. INJECTIONS: none, that is 0. The reason takes one line with the pigeonhole principle: 4 objects are placed in 3 boxes, so at least two share a box, so at least two elements have the same image. SURJECTIONS: remove from the 81 functions those that miss at least one element of B. Those that miss a given element number 24=16, and there are 3 ways to choose the missed element; those that miss two given elements number 14=1, with 3 choices of pair; none misses all three, since a function must have images. Inclusion-exclusion gives 81−3×16+3×1=81−48+3=36 surjections. The classic error is to forget the correction term +3 and answer 33: the functions that miss two elements would then have been subtracted twice.
c)
FUNCTIONS: 44=256. BIJECTIONS: these are the permutations of 4 objects, that is 4!=24. The proportion is 25624=0.09375, that is 9.375 percent. The figure is worth remembering for its lesson: even between two sets of the same size, a function picked at random does not have one chance in ten of being a bijection, and the proportion collapses as the cardinality grows, nnn! tending to zero very quickly.
d)
If an injection from A to B exists, then ∣A∣≤∣B∣: the images are ∣A∣ distinct elements of B. If a surjection from A onto B exists, then ∣A∣≥∣B∣: the fibres are ∣B∣ nonempty, pairwise disjoint subsets of A. If a bijection exists, then ∣A∣=∣B∣, by the previous two. These three statements are the comparison tool of the chapter: ∣A∣=∣B∣ is proved by EXHIBITING a bijection, which requires counting neither A nor B.
e)
Let f:A→B be injective, with ∣A∣=∣B∣=n finite. Injectivity gives ∣f(A)∣=∣A∣=n, since no image is repeated. Now f(A) is a subset of B, which has n elements: a subset of a FINITE set that has as many elements as the set itself is the whole set. So f(A)=B and f is surjective. For infinite sets the result fails, and it is the second step that breaks: 2Z⊂Z, the two are in bijection as exercise 4 showed, and yet 2Z=Z. A proper subset of an infinite set can have the same cardinality as the set, which is even the definition of an infinite set. Exercise 10 exploits this point.
Exercise 8: Five statements to correct
Each one is false, and each one is a sentence actually heard in class. Say why it is false, give the exact correction and, when possible, a counterexample with numbers.
1) “Every function has an inverse.”
2) “Injective means strictly increasing.”
3) “You can change the codomain without changing the nature of the function.”
4) “A bijection between two infinite sets is impossible.”
5) “Surjective means that every element of the domain has an image.”
Show the solution
Answers
1)FALSE: only a bijection has an inverse; f(x)=x2 on R has none
2)FALSE: strictly monotonic implies injective, never the converse; x↦1/x shows it
3)FALSE: injectivity does not move, surjectivity does; x↦x2 becomes surjective from R to R+
4)FALSE: n↦2n from Z onto 2Z is one; Cantor only says there is none from N onto R
5)FALSE: that is the definition of a function; surjective is about the CODOMAIN and preimages
1)
FALSE. Only BIJECTIONS have an inverse function. The confusion comes from the notation: f−1(Q), applied to a subset of the codomain, exists for every function and denotes the set of preimages, whereas f−1(y), applied to an element, makes sense only if f is bijective. Counterexample: f:R→R, f(x)=x2 has no inverse, since f−1({4})={−2,2} contains two elements and f−1({−1})=∅. The complete correction: a function has an inverse if and only if it is bijective, and exercise 6 showed that this is checked through BOTH compositions.
2)
FALSE, twice over. First, being increasing requires an ORDER on the domain and on the codomain: between two arbitrary finite sets, such as {1,2,3} and {a,b,c}, the sentence simply makes no sense, whereas injectivity does. Next, even on R, the statement is false: x↦x1 on R∖{0} is injective and not increasing, since −1<1 gives −11=−1<1=11, which looks increasing, whereas 1<2 gives 11=1>0.5=21. The correct implication goes in one direction only: strictly monotonic on an interval implies injective, never the converse.
3)
FALSE, and it is the thread of the whole set. Changing the codomain does not affect injectivity, but it changes surjectivity: x↦x2 from R to R is not surjective, while the same rule from R to R+ is. The precise correction is worth memorizing: shrinking the CODOMAIN can only help surjectivity and never changes injectivity; shrinking the DOMAIN can only help injectivity and never makes a function surjective. A function is given only together with its two sets; without them, the question of its nature is not even asked.
4)
FALSE. Exercise 4 already gives one: n↦2n is a bijection from Z onto 2Z, two infinite sets. Exercise 10 gives another, between N and Z. It is in fact through a bijection that equality of two infinite cardinalities is DEFINED, since counting is impossible. What is true, and probably the origin of the confusion, is a much subtler statement due to Cantor: there is no bijection from N onto R, so not all infinities have the same size. Two infinite sets can be in bijection, and sometimes they are not.
5)
FALSE: the sentence describes the definition of a FUNCTION, not surjectivity. Every element of the domain has an image: that is the existence condition of exercise 1, and every function satisfies it without exception. Surjectivity is about the CODOMAIN: every element of the codomain has at least one PREIMAGE. The counterexample is immediate: f:{1,2,3}→{a,b,c,d} with f(1)=a, f(2)=b, f(3)=c is a function, so every element of the domain does have an image, and it is not surjective since d has no preimage. Confusing the two sentences leads to answering that every function is surjective, which empties the word of its meaning.
Exercise 9: Problem: the hash function that collapses
A hash table stores keys in slots by means of a function h that goes from the universe of keys to {0,1,…,m−1}. Everything this chapter says about functions can be read on this single object, and the reading explains a programming decision that many people apply without knowing why.
The figure shows six keys k1 to k6 and three slots numbered 0, 1 and 2.
a) Read the figure and give h−1({0}), h−1({1}) and h−1({2}). Check that these three fibres form a partition of the six keys.
b) Is h surjective? injective? Which of the two properties do we ask of a good hash function, and which one is out of reach?
c) We now hash all words of 8 characters taken from an alphabet of 62 symbols, into 1,024 slots. Compute the cardinality of the universe of keys, compare it with 1,024 and conclude about injectivity.
d) Can a key be recovered from its slot number alone? Phrase the answer with the word inverse, and say what consequence this has on what a hash table must store.
e) We want instead a directory that assigns each employee a file number, and that makes it possible to find the employee from the number. What property is required of the function, and what constraint does that put on the two cardinalities?
Show the solution
Answers
a){k1,k3,k6}, {k4} and {k2,k5}; partition checked, 3+1+2=6, and 2 collisions in slot 0
b)Surjective only; we want surjectivity and balanced fibres, injectivity is out of reach
c)628=218,340,105,584,896 against 1,024: no injection possible, about 2.13×1011 keys per slot
d)No: without injectivity, no inverse; only the fibre is recovered, hence the key must be stored in the slot
e)A bijection, so the two cardinalities are equal; a counter, not a hash
a)
The figure gives h(k1)=0, h(k2)=2, h(k3)=0, h(k4)=1, h(k5)=2 and h(k6)=0. So h−1({0})={k1,k3,k6}, h−1({1})={k4} and h−1({2})={k2,k5}. The three subsets are nonempty, pairwise disjoint, and their union is the set of six keys: 3+1+2=6. It is indeed a partition, the partition into fibres introduced in exercise 2. Slot 0 holds three keys, so two collisions, since a collision is one more key in a slot that is already occupied.
b)
h is SURJECTIVE: all three slots receive at least one key, none is empty. It is NOT injective, since k1 and k3 share slot 0. That is exactly what we ask of a good hash function, and even a little more: we want surjectivity, so as not to waste slots, and above all fibres of comparable sizes, so that the cost of a search stays bounded. Injectivity, on the other hand, is out of reach as soon as the universe of keys is larger than the number of slots, which is always the case in practice: question c).
c)
The universe of keys has 628=218,340,105,584,896 words, about 2.18×1014. There are 1,024 slots. An injection from the universe into the slots would require 628≤1,024, which is false by eleven orders of magnitude: the quotient is about 2.13×1011 keys per slot on average. No hash function into 1,024 slots is therefore injective, and the argument uses nothing about the chosen function, only the two cardinalities. It is the inequality of exercise 7 read backwards: no injection is possible when the domain is larger than the codomain.
d)
No. h, not being injective, has no inverse function: knowing h(x) does not determine x, it only determines the FIBRE that x belongs to, that is, the set of all keys that land in the same slot. The practical consequence is the one every implementation applies: a hash table STORES the key next to the value, in the slot. Without it, a search that lands on slot 0 would not know whether it has found k1, k3 or k6. The key field is not a redundancy added for comfort, it is made mandatory by the non-injectivity of h.
e)
We require a BIJECTION between the set of employees and the set of file numbers in use. Injectivity guarantees that two employees never have the same number, so that a number does identify one person; surjectivity guarantees that no assigned number is left orphaned. By exercise 7, this forces the two cardinalities to be equal, and that is precisely why a directory is NOT built with a hash function: hashing is designed to store quickly, not to identify. If you insist on a short number, it must be assigned by a counter, which is injective by construction, and not by a computation on the name.
Exercise 10: Problem: counting the infinite, from N to Z
Two sets have the same cardinality when there is a bijection from one onto the other. This definition requires no counting, and it is the only one that survives in the infinite: you do not count N, you set it against something else.
The figure shows one way to enumerate Z, that is, to make it stand in line: rank 0 for the integer 0, then alternately a negative and a positive.
a) Read the figure and give φ(0) to φ(6), then write φ:N→Z as a two-case formula depending on the parity of n.
b) Give φ−1(5) and φ−1(−5), then the formula for φ−1 depending on the sign of k.
c) Why does the fact that φ−1 is a well-defined function from Z to N, satisfying both compositions, suffice to prove that φ is bijective?
d) Let ψ:N→2N, ψ(n)=2n, where 2N is the set of even natural numbers. Show that ψ is bijective. Now 2N is a PROPER subset of N: what do you conclude?
e) Exercise 7 proved that between FINITE sets of the same cardinality, injective implies surjective. At exactly which point does that proof break down for ψ?
Show the solution
Answers
a)0,−1,1,−2,2,−3,3; φ(n)=2n if n is even, −2n+1 if n is odd
b)φ−1(5)=10, φ−1(−5)=9; φ−1(k)=2k if k≥0, −2k−1 if k<0
c)Both compositions equal the identity, which gives the injectivity and then the surjectivity of φ
d)ψ is bijective: an infinite set can be in bijection with one of its proper subsets
e)The second step: a subset of an INFINITE set can have the same cardinality without being the whole set
a)
The figure gives φ(0)=0, φ(1)=−1, φ(2)=1, φ(3)=−2, φ(4)=2, φ(5)=−3 and φ(6)=3. Even ranks receive the positives, odd ranks the negatives. The formula is therefore φ(n)=2n if n is even, and φ(n)=−2n+1 if n is odd. A check on two values, mandatory whenever you write a two-case formula: φ(4)=2 and φ(5)=−26=−3, which matches the figure. The trap is to write −2n for the odd ranks, which does not even give an integer.
b)
To reach 5, which is positive, you need an even rank n with 2n=5, so n=10: φ−1(5)=10. To reach −5, which is negative, you need an odd rank with −2n+1=−5, so n+1=10 and n=9: φ−1(−5)=9. The general formula is φ−1(k)=2k if k≥0, and φ−1(k)=−2k−1 if k<0. Check on k=−5: −2×(−5)−1=10−1=9, as expected. The case k=0 goes with the positives and gives φ−1(0)=0: a two-case formula requires you to say explicitly on which side the boundary falls, otherwise the function is not defined everywhere, or is defined twice.
c)
Because exhibiting a function g satisfying g∘φ=idN AND φ∘g=idZ is enough to establish bijectivity, without going back to the definitions. The first equality makes φ injective, by the result of exercise 5: if g∘φ is injective, and the identity is, then φ is. The second makes φ surjective by the symmetric result: if φ∘g is surjective, then φ is. It is the most economical method of the chapter, and it is the one to use whenever a candidate inverse is easy to compute. Both checks are immediate here: n even gives φ−1(n/2)=n, n odd gives φ−1(−(n+1)/2)=(n+1)−1=n.
d)
ψ is injective: 2a=2b gives a=b. It is surjective onto 2N: every even natural number can be written 2k with k a natural number, and this k is the preimage. So ψ is bijective, and N and 2N have the same cardinality. Now 2N is a proper subset of N, since 1, 3, 5 are not in it. Conclusion: an infinite set can be in bijection with one of its proper subsets, and it therefore has as many elements as half of itself. This is not a paradox, it is the very definition of an infinite set, Dedekind's, and it separates the finite from the infinite far more sharply than the vague idea of going on forever.
e)
The proof in exercise 7 had two steps. The first, ∣ψ(N)∣=∣N∣, still holds: injectivity loses no element. It is the SECOND that fails, the one saying that a subset of a finite set with as many elements as the set is the whole set. Here 2N has the same cardinality as N and yet is not N. The word finite was therefore not a drafting precaution, it was the heart of the argument, and that is the lesson in method to take away: when a result carries a hypothesis, you must be able to say on which line it is used. Note finally that N, Z, 2N and even Q all have the same cardinality, but that R is in bijection with none of them: not all infinities are equal.
Something missing from this set?A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.
Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space.
Rather not create an account? Write to me directly.
Looking for a discrete mathematics tutor in Montreal?
Get in touch for a first session. With a B.Sc. in computer science from McGill and an M.Sc. in applied computer science from Concordia, I connect injections and bijections to what they become later on: hash tables, primary keys in a database and encryption functions.