Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1: Functions, Composition, Inverse & the Pigeonhole Principle

Lesson 4 of 17 in the free Mathematical Foundation of CS notes on Siksha Sarovar, written by Rohit Jangra.

3.1 Functions as Special Relations

A function f: A → B is a relation from A to B in which every element of A is related to exactly one element of B. A is the domain, B the codomain, and the range f(A) = {f(a) | a ∈ A} ⊆ B is the set of values actually hit.

Not every relation is a function: R = {(1, x), (1, y)} fails (1 has two images); R = {(1, x)} on domain {1, 2} fails (2 has no image).

3.2 Types of Functions

TypeFormal ConditionIntuitionExample (R → R)
Injective (one-to-one)f(a) = f(b) → a = bNo two inputs collidef(x) = 3x + 2
Surjective (onto)∀b ∈ B ∃a ∈ A: f(a) = bRange = codomainf(x) = x^3
BijectiveInjective and surjectivePerfect pairingf(x) = 3x + 2

Counts, for |A| = m, |B| = n: total functions = n^m; injections require m ≤ n and number P(n, m); bijections require m = n and number n!.

Note: f(x) = x^2 on R → R is neither injective (f(2) = f(−2) = 4) nor surjective (−1 is never hit). Restricting to f: [0, ∞) → [0, ∞) makes it bijective — injectivity and surjectivity depend on the declared domain and codomain, not just the formula.

3.3 Composition of Functions

Given f: A → B and g: B → C, the composition (g ∘ f)(x) = g(f(x)) maps A → C. Composition is associative but not commutative.

Numeric check: f(x) = x^2 and g(x) = x + 1.

  • (f ∘ g)(x) = f(x + 1) = (x + 1)^2, so (f ∘ g)(2) = 9.
  • (g ∘ f)(x) = x^2 + 1, so (g ∘ f)(2) = 5. Hence f ∘ g ≠ g ∘ f.

Useful theorems: if f and g are both injective, g ∘ f is injective; if both surjective, g ∘ f is surjective.

3.4 Worked Example 1 — Prove Bijective and Find the Inverse

Problem: Show f: R → R, f(x) = 3x + 2 is a bijection and find f⁻¹.

  1. Injective: suppose f(x1) = f(x2). Then 3x1 + 2 = 3x2 + 2 → 3x1 = 3x2 → x1 = x2. ✔
  2. Surjective: take any y ∈ R. Solve y = 3x + 2 → x = (y − 2)/3, which is a real number, and f((y − 2)/3) = y. ✔
  3. Therefore f is bijective and the inverse exists: f⁻¹(y) = (y − 2)/3.
  4. Verify: (f⁻¹ ∘ f)(x) = f⁻¹(3x + 2) = (3x + 2 − 2)/3 = x. ✔ (A function has an inverse iff it is bijective.)

3.5 Floor and Ceiling

⌊x⌋ = greatest integer ≤ x; ⌈x⌉ = least integer ≥ x. So ⌊3.7⌋ = 3, ⌈3.2⌉ = 4, and — the classic trap — ⌊−1.5⌋ = −2, not −1. These appear throughout CS: a binary search on n elements makes at most ⌈log2(n + 1)⌉ comparisons; n items packed k-per-page need ⌈n/k⌉ pages.

3.6 The Pigeonhole Principle (PHP)

Simple form: if n + 1 or more objects are placed into n boxes, some box holds at least 2 objects. Generalized form: if N objects go into k boxes, some box holds at least ⌈N/k⌉ objects.

3.7 Worked Example 2 — Pigeonhole Arguments

(a) Among any 5 integers, two leave the same remainder on division by 4. The possible remainders are the 4 boxes {0, 1, 2, 3}; with 5 integers (pigeons), two must share a box. Consequence: their difference is divisible by 4.

(b) In a group of 100 people, at least ⌈100/12⌉ = 9 share a birth month. Boxes = 12 months, pigeons = 100 people; some month contains at least 9.

(c) Hashing: a hash table with m = 1000 slots receiving 1001 keys must have a collision — no clever hash function can avoid it. This is also why lossless compression cannot shrink every file: there are 2^n files of n bits but only 2^n − 1 shorter outputs, so two files would collide.

3.8 Common Mistakes

  • Confusing codomain with range: surjectivity compares them; they may differ.
  • Computing g ∘ f in the wrong order — apply the rightmost function first.
  • Claiming f(x) = x^2 has inverse √x on all of R: it has no inverse there because it is not injective.

🎯 Exam Focus

  1. Define injective, surjective, and bijective functions. Give an example of a function that is surjective but not injective on Z.
  2. Let f(x) = 2x + 3 and g(x) = x^2 − 1 on R. Compute f ∘ g, g ∘ f, and (f ∘ g)(2). Are they equal?
  3. Prove that f: R − {2} → R − {1}, f(x) = x/(x − 2) is a bijection and find f⁻¹.
  4. State and prove the generalized pigeonhole principle.
  5. Show that among any 8 distinct integers, two differ by a multiple of 7.
  6. How many functions and how many injections exist from a 3-element set to a 5-element set? (Answers: 5^3 = 125 and P(5,3) = 60.)