Skip to content

11.3.4 Relations, functions, and validation

This part of Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections develops the worked mechanisms below.

Concepts from other sections (12)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Check relation properties and prove function/bijection claims with explicit domains.

q34. Let R={(1,1),(2,2),(3,3),(1,2),(2,1)}R = \{(1,1), (2,2), (3,3), (1,2), (2,1)\} on {1,2,3}\{1, 2, 3\}. Is RR (a) reflexive, (b) symmetric, (c) transitive? [bool]

Answer

(a) True, (b) true, (c) true. (a) All of (1,1),(2,2),(3,3)(1,1), (2,2), (3,3) are in RR. (b) The only non-diagonal pairs (1,2)(1,2) and (2,1)(2,1) come together. (c) Every chain xRyRzx R y R z stays inside {1,2}\{1, 2\} or inside {3}\{3\}, and both blocks are fully related. RR is the equivalence relation with classes {1,2}\{1, 2\} and {3}\{3\}.

q35. How many equivalence relations are there on a 4-element set? [number]

Answer

1515. Equivalence relations on a set are its partitions, so the answer is the Bell number B4B_4. Count partitions of {1,2,3,4}\{1, 2, 3, 4\} by block sizes: 44 (1 way), 3+13 + 1 (4), 2+22 + 2 (3), 2+1+12 + 1 + 1 (6), 1+1+1+11 + 1 + 1 + 1 (1), total 15. Common mistakes: 16 =24= 2^4 counts subsets, and 24 =4!= 4! counts orderings.

q36. Prove: for any function f:X→Yf: X \to Y, the relation x∼y  ⟺  f(x)=f(y)x \sim y \iff f(x) = f(y) is an equivalence relation on XX. (Unicode normalization in The tokenizer protocol and the char tokenizer is this relation with f=NFCf = \mathrm{NFC}.) [proof]

Answer

Claim: for any function f:X→Yf: X \to Y, the relation x∼y  ⟺  f(x)=f(y)x \sim y \iff f(x) = f(y) is an equivalence relation on XX.

Method: check the three defining properties directly; each reduces to the same property of equality in YY.

Reflexive: for every x∈Xx \in X, f(x)=f(x)f(x) = f(x), so x∼xx \sim x.

Symmetric: if x∼yx \sim y then f(x)=f(y)f(x) = f(y), so f(y)=f(x)f(y) = f(x), so y∼xy \sim x.

Transitive: if x∼yx \sim y and y∼zy \sim z then f(x)=f(y)f(x) = f(y) and f(y)=f(z)f(y) = f(z), so f(x)=f(z)f(x) = f(z), so x∼zx \sim z.

So ∼\sim is an equivalence relation; its classes are the nonempty preimages f−1({y})f^{-1}(\{y\}).

Model proof also in q36.md.

q37. Prove: divisibility (a∣ba \mid b when b=kab = ka for some integer kk) is a partial order on the positive integers. [proof]

Answer

Claim: on the positive integers, a∣ba \mid b (there is an integer kk with b=kab = ka) is a partial order: reflexive, antisymmetric, and transitive.

Method: check each property from the definition.

Reflexive: a=1⋅aa = 1 \cdot a, so a∣aa \mid a.

Antisymmetric: suppose a∣ba \mid b and b∣ab \mid a, so b=kab = ka and a=lba = lb for integers k,lk, l. Then a=lkaa = lka, and since a>0a > 0, lk=1lk = 1. Both k=b/ak = b/a and l=a/bl = a/b are positive because a,b>0a, b > 0, and the only positive integers with product 1 are k=l=1k = l = 1. So b=ab = a.

Transitive: if b=kab = ka and c=lbc = lb, then c=(lk)ac = (lk)a with lklk an integer, so a∣ca \mid c.

So divisibility is a partial order on the positive integers. Antisymmetry needs positivity: on all integers 2∣−22 \mid -2 and −2∣2-2 \mid 2 but 2≠−22 \ne -2.

Model proof also in q37.md.

q38. Let f:Z→Zf: \mathbb{Z} \to \mathbb{Z}, f(n)=2n+1f(n) = 2n + 1. Is ff (a) injective, (b) surjective? [bool]

Answer

(a) True, (b) false. (a) 2n+1=2n′+12n + 1 = 2n' + 1 gives n=n′n = n'. (b) 2n+12n + 1 is always odd, so no even integer (for example 0) is an image. Injective and surjective are the two halves of the bijection test in Injective, surjective, bijective: the GPT-2 byte map, and they are independent.

q39. (a) How many bijections are there from a 5-element set to itself? (b) How many injective functions are there from a 3-element set to a 5-element set? [number]

Answer

(a) 120120, (b) 6060. (a) A bijection of a 5-set is an ordering: 5!=1205! = 120. (b) The first element has 5 images, the next 4, the last 3: 5⋅4⋅3=605 \cdot 4 \cdot 3 = 60. Common mistakes: 55=31255^5 = 3125 and 53=1255^3 = 125 count every function, and (53)=10\binom{5}{3} = 10 forgets that which element goes where matters.

q40. Prove: if f:X→Yf: X \to Y and g:Y→Zg: Y \to Z are bijections, then g∘fg \circ f is a bijection. [proof]

Answer

Claim: if f:X→Yf: X \to Y and g:Y→Zg: Y \to Z are bijections, then g∘f:X→Zg \circ f: X \to Z, x↦g(f(x))x \mapsto g(f(x)), is a bijection.

Method: prove injective and surjective separately.

Injective: suppose g(f(x))=g(f(x′))g(f(x)) = g(f(x')). Since gg is injective, f(x)=f(x′)f(x) = f(x'); since ff is injective, x=x′x = x'.

Surjective: let z∈Zz \in Z. Since gg is surjective there is y∈Yy \in Y with g(y)=zg(y) = z; since ff is surjective there is x∈Xx \in X with f(x)=yf(x) = y. Then (g∘f)(x)=z(g \circ f)(x) = z.

Being injective and surjective, g∘fg \circ f is a bijection (its inverse is f−1∘g−1f^{-1} \circ g^{-1}).

Model proof also in q40.md.

q41. GPT-2’s byte-to-character map (Injective, surjective, bijective: the GPT-2 byte map) keeps the 188 printable bytes P={33,…,126}∪{161,…,172}∪{174,…,255}P = \{33, \dots, 126\} \cup \{161, \dots, 172\} \cup \{174, \dots, 255\} as themselves (f(b)=bf(b) = b) and sends the other 68 bytes, in increasing order, to 256,257,…,323256, 257, \dots, 323. Prove that f:{0,…,255}→Nf: \{0, \dots, 255\} \to \mathbb{N} is injective, so it is a bijection onto its image. [proof]

Answer

Claim: the GPT-2 byte map ff is injective on {0,…,255}\{0, \dots, 255\}.

Setup: P={33..126}∪{161..172}∪{174..255}P = \{33..126\} \cup \{161..172\} \cup \{174..255\} has 94+12+82=18894 + 12 + 82 = 188 elements, so its complement Q={0..255}∖PQ = \{0..255\} \setminus P has 256−188=68256 - 188 = 68 elements. List QQ in increasing order as q0<q1<⋯<q67q_0 < q_1 < \dots < q_{67}; then f(b)=bf(b) = b for b∈Pb \in P and f(qt)=256+tf(q_t) = 256 + t.

Method: take bytes a≠ba \ne b and show f(a)≠f(b)f(a) \ne f(b), in three cases that cover every pair.

Both in PP: f(a)=a≠b=f(b)f(a) = a \ne b = f(b).

Both in QQ: a=qsa = q_s, b=qtb = q_t with s≠ts \ne t (the listing has no repeats), so f(a)=256+s≠256+t=f(b)f(a) = 256 + s \ne 256 + t = f(b).

One in each, say a∈Pa \in P, b∈Qb \in Q: f(a)=a≤255<256≤f(b)f(a) = a \le 255 < 256 \le f(b).

So ff is injective. A function is always surjective onto its image, so ff is a bijection from the 256 bytes onto its 256 image characters, and unicode_to_bytes is its inverse.

Model proof also in q41.md.

The common wrong answers discussed under the problems come from a small number of recurring mistakes. Each has a symptom you would see in real code, and a habit that prevents it:

PitfallSymptomHow to avoid it
Combining masks with OR, or forgetting the padding keya pad position gets attention weightintersect the two masks (AND) and clear the pad column (q4)
Negating only the inner predicate of a nested quantifierone tests “some entry is 0” instead of “some row is all 0”swap every quantifier, outermost first, then negate the predicate (q5)
Counting multiply-adds instead of FLOPsFLOP and roofline numbers off by 2count a multiplication and an addition separately: 2k2k per output entry (q12)
Using 2ND2ND (forward only) for training computea training budget 3 times too smalluse 6ND6ND: 2 for the forward pass, 4 for the backward (q13)
Caching keys only, or sizing the cache by query headsKV memory off by 2 or by the GQA group sizewrite the factor 2 for keys and values and use HkvH_{kv}, not HH (q14)
Adding overlapping cases twiceinclusion-exclusion overcountsubtract the intersection once (q16)
Checking many cases instead of provinga “for every nn” claim believed from n=1n = 1 to 4040look for one counterexample, n=41n = 41, instead of checking cases (q23)
Using the conclusion inside the inductive stepa circular proofstate the hypothesis for kk and use it only to derive the case k+1k + 1 (q26 to q30)
Forgetting the running sum must be rescaled when the max changesonline softmax overflows or is wrong after a new maxmultiply ss by em−m′e^{m - m'} on every step, so every term is shifted by the current max (q30, q33)
Confusing all functions with injective onesnkn^k where n!(n−k)!\frac{n!}{(n-k)!} was askedcount images one element at a time without reuse: n(n−1)⋯n(n-1)\cdots (q39)

The tools of this set are used directly in code throughout the book and in AI Engineering:

  • Hammack, Book of Proof (free), chapters 1 to 10 and 12.
  • Velleman, How to Prove It, chapters 3 and 6.
  • Set: an unordered collection of distinct elements.
  • Contrapositive: ¬Q⇒¬P\lnot Q \Rightarrow \lnot P, logically equivalent to P⇒QP \Rightarrow Q.
  • Binomial coefficient: (nk)\binom{n}{k}, the number of kk-element subsets of an nn-element set.
  • Induction hypothesis: the assumption P(k)P(k) used to prove P(k+1)P(k + 1).
  • Loop invariant: a statement that holds before a loop, survives each iteration, and with the exit condition implies the result.
  • Equivalence relation: a reflexive, symmetric, transitive relation; its classes partition the set.
  • Bijection: a function that is both injective and surjective, and so has an inverse.
  • An attention mask is a set of allowed (query, key) pairs, so combining masks is set intersection and a fully masked row is the negation of “every row has a key” (q4, q5).
  • Every size in the system is a count: parameters are sums of matrix shapes, FLOPs are 2mkn2mkn per matmul, and KV-cache bytes are 2⋅L⋅Hkv⋅dh⋅b2 \cdot L \cdot H_{kv} \cdot d_h \cdot b per token (q9 to q15).
  • A loop is correct when an invariant holds before it, survives one iteration, and implies the result at exit; the online softmax kernel is proved this way (q29, q30).
  • An equivalence relation is the same thing as “has the same image under some function”, which is how Unicode normalization groups strings (q36).
  • A bijection is injective plus surjective; GPT-2’s byte map is checked by exactly those two halves (q38 to q41).
  1. Why are equal finite cardinalities alone not enough to prove a proposed function is a bijection?

    Answer

    A specific function still needs the relevant injectivity or surjectivity proof; cardinality alone does not establish its mapping behavior.

Contents