Skip to content

Mathematical Foundations

Before we can rigorously analyze algorithms, we must establish the mathematical language and reasoning tools that will serve as our foundation. This chapter introduces the essential concepts from set theory, logic, proof techniques, and combinatorics that underpin all algorithmic analysis.

While these topics may seem abstract, they provide the precise vocabulary needed to discuss computational problems and their solutions. Every algorithm we encounter will be described using sets, analyzed using logical reasoning, proven correct through formal techniques, and evaluated using combinatorial methods.

1.1 Set Theory

A set is a collection of distinct objects, called elements or members. Sets form the foundation of mathematical discourse because they allow us to discuss collections of objects with precision.

1.1.1 Basic Definitions and Notation

We write aSa \in S to denote that aa is an element of set SS, and aSa \notin S to denote that aa is not an element of SS. Sets can be specified in several ways:

Roster notation: S={1,2,3,4,5}S = \{1, 2, 3, 4, 5\} Set-builder notation: S={x:x is a positive integer and x5}S = \{x : x \text{ is a positive integer and } x \leq 5\}

Two sets are equal if they contain exactly the same elements. Order and repetition do not matter: {1,2,3}={3,1,2}={1,1,2,3}\{1, 2, 3\} = \{3, 1, 2\} = \{1, 1, 2, 3\}.

Important sets:

  • N={1,2,3,}\mathbb{N} = \{1, 2, 3, \ldots\} (natural numbers)
  • Z={,2,1,0,1,2,}\mathbb{Z} = \{\ldots, -2, -1, 0, 1, 2, \ldots\} (integers)
  • Q\mathbb{Q} (rational numbers)
  • R\mathbb{R} (real numbers)
  • \emptyset (empty set)

1.1.2 Set Operations

Given sets AA and BB:

Union: AB={x:xA or xB}A \cup B = \{x : x \in A \text{ or } x \in B\} Intersection: AB={x:xA and xB}A \cap B = \{x : x \in A \text{ and } x \in B\} Difference: AB={x:xA and xB}A \setminus B = \{x : x \in A \text{ and } x \notin B\} Complement: A={x:xA}\overline{A} = \{x : x \notin A\} (relative to some universal set)

Exercise 1.1: Let A={1,2,3,4}A = \{1, 2, 3, 4\} and B={3,4,5,6}B = \{3, 4, 5, 6\}. Find ABA \cup B, ABA \cap B, ABA \setminus B, and BAB \setminus A.

1.1.3 Relations Between Sets

Set AA is a subset of set BB, written ABA \subseteq B, if every element of AA is also an element of BB. Formally: AB    x(xAxB)A \subseteq B \iff \forall x (x \in A \rightarrow x \in B)

If ABA \subseteq B and ABA \neq B, then AA is a proper subset of BB, written ABA \subset B.

Theorem 1.1: For any sets AA, BB, and CC:

  1. AAA \subseteq A (reflexivity)
  2. If ABA \subseteq B and BAB \subseteq A, then A=BA = B (antisymmetry)
  3. If ABA \subseteq B and BCB \subseteq C, then ACA \subseteq C (transitivity)

Exercise 1.2: Prove that if ABA \subseteq B, then AB=AA \cap B = A and AB=BA \cup B = B.

1.1.4 Cartesian Products and Relations

The Cartesian product of sets AA and BB is: A×B={(a,b):aA and bB}A \times B = \{(a, b) : a \in A \text{ and } b \in B\}

A relation RR from set AA to set BB is a subset of A×BA \times B. We write aRbaRb or (a,b)R(a, b) \in R to indicate that aa is related to bb.

Exercise 1.3: Let A={1,2}A = \{1, 2\} and B={a,b,c}B = \{a, b, c\}. Find A×BA \times B and B×AB \times A. Are they equal?

1.1.5 Functions

A function f:ABf: A \rightarrow B is a special relation where each element of AA is related to exactly one element of BB. We write f(a)=bf(a) = b to denote that aa maps to bb.

  • AA is the domain of ff
  • BB is the codomain of ff
  • The range of ff is {bB:aA,f(a)=b}\{b \in B : \exists a \in A, f(a) = b\}

A function f:ABf: A \rightarrow B is:

  • Injective (one-to-one) if f(a1)=f(a2)f(a_1) = f(a_2) implies a1=a2a_1 = a_2
  • Surjective (onto) if for every bBb \in B, there exists aAa \in A such that f(a)=bf(a) = b
  • Bijective if it is both injective and surjective

Exercise 1.4: Determine whether the function f:NNf: \mathbb{N} \rightarrow \mathbb{N} defined by f(n)=2nf(n) = 2n is injective, surjective, or bijective.

1.2 Logic and Proof Techniques

Mathematical logic provides the framework for precise reasoning about mathematical statements. Understanding logical structure is essential for constructing and evaluating proofs.

1.2.1 Propositions and Logical Connectives

A proposition is a statement that is either true or false. We use variables like pp, qq, rr to represent propositions.

Logical connectives:

  • Negation: ¬p\neg p (“not pp”)
  • Conjunction: pqp \land q (”pp and qq”)
  • Disjunction: pqp \lor q (”pp or qq”)
  • Implication: pqp \rightarrow q (“if pp then qq”)
  • Biconditional: pqp \leftrightarrow q (”pp if and only if qq”)

Truth tables:

ppqq¬p\neg ppqp \land qpqp \lor qpqp \rightarrow qpqp \leftrightarrow q
TTFTTTT
TFFFTFF
FTTFTTF
FFTFFTT

Exercise 1.5: Construct truth tables for (pq)(qp)(p \rightarrow q) \land (q \rightarrow p) and pqp \leftrightarrow q. What do you observe?

1.2.2 Logical Equivalences

Two propositions are logically equivalent if they have the same truth value under all possible assignments of truth values to their variables.

Important equivalences:

  • De Morgan’s Laws: ¬(pq)¬p¬q\neg(p \land q) \equiv \neg p \lor \neg q, ¬(pq)¬p¬q\neg(p \lor q) \equiv \neg p \land \neg q
  • Contrapositive: pq¬q¬pp \rightarrow q \equiv \neg q \rightarrow \neg p
  • Double negation: ¬(¬p)p\neg(\neg p) \equiv p
  • Distributivity: p(qr)(pq)(pr)p \land (q \lor r) \equiv (p \land q) \lor (p \land r)

Exercise 1.6: Prove that pq¬pqp \rightarrow q \equiv \neg p \lor q using truth tables.

1.2.3 Quantifiers

Universal quantifier: xP(x)\forall x P(x) means “for all xx, P(x)P(x) is true” Existential quantifier: xP(x)\exists x P(x) means “there exists an xx such that P(x)P(x) is true”

Quantifier negation:

  • ¬(xP(x))x¬P(x)\neg(\forall x P(x)) \equiv \exists x \neg P(x)
  • ¬(xP(x))x¬P(x)\neg(\exists x P(x)) \equiv \forall x \neg P(x)

Exercise 1.7: Express the following statements using quantifiers:

  1. Every positive integer has a successor
  2. There exists a real number that is not rational
  3. For every real number, there exists a larger real number

1.2.4 Proof Techniques

Direct Proof

To prove pqp \rightarrow q, assume pp is true and show that qq must be true.

Example: Prove that if nn is an even integer, then n2n^2 is even. Proof: Suppose nn is even. Then n=2kn = 2k for some integer kk. Therefore, n2=(2k)2=4k2=2(2k2)n^2 = (2k)^2 = 4k^2 = 2(2k^2). Since 2k22k^2 is an integer, n2n^2 is even. □

Proof by Contradiction

To prove pp, assume ¬p\neg p and derive a contradiction.

Example: Prove that 2\sqrt{2} is irrational. Proof: Suppose 2\sqrt{2} is rational. Then 2=ab\sqrt{2} = \frac{a}{b} where aa and bb are integers with gcd(a,b)=1\gcd(a,b) = 1. Squaring both sides: 2=a2b22 = \frac{a^2}{b^2}, so 2b2=a22b^2 = a^2. This means a2a^2 is even, so aa is even. Let a=2ca = 2c. Then 2b2=4c22b^2 = 4c^2, so b2=2c2b^2 = 2c^2. This means b2b^2 is even, so bb is even. But if both aa and bb are even, then gcd(a,b)2\gcd(a,b) \geq 2, contradicting our assumption. Therefore, 2\sqrt{2} is irrational. □

Proof by Contrapositive

To prove pqp \rightarrow q, prove ¬q¬p\neg q \rightarrow \neg p.

Example: Prove that if n2n^2 is odd, then nn is odd. Proof: We prove the contrapositive: if nn is even, then n2n^2 is even. If nn is even, then n=2kn = 2k for some integer kk. Therefore, n2=4k2=2(2k2)n^2 = 4k^2 = 2(2k^2), which is even. □

Mathematical Induction

To prove nn0,P(n)\forall n \geq n_0, P(n):

  1. Base case: Prove P(n0)P(n_0)
  2. Inductive step: Prove P(k)P(k+1)P(k) \rightarrow P(k+1) for arbitrary kn0k \geq n_0

Example: Prove that 1+2++n=n(n+1)21 + 2 + \cdots + n = \frac{n(n+1)}{2} for all n1n \geq 1. Proof: Base case: For n=1n = 1, LHS =1= 1 and RHS =122=1= \frac{1 \cdot 2}{2} = 1. ✓ Inductive step: Assume the formula holds for kk. Then: 1+2++k+(k+1)=k(k+1)2+(k+1)=k(k+1)+2(k+1)2=(k+1)(k+2)21 + 2 + \cdots + k + (k+1) = \frac{k(k+1)}{2} + (k+1) = \frac{k(k+1) + 2(k+1)}{2} = \frac{(k+1)(k+2)}{2} This is the formula for n=k+1n = k+1. By induction, the formula holds for all n1n \geq 1. □

Exercise 1.8: Prove by induction that 2n>n2^n > n for all n1n \geq 1.

1.3 Basic Combinatorics

Combinatorics studies methods of counting, which is essential for analyzing algorithm efficiency and correctness.

1.3.1 Fundamental Counting Principles

Addition Principle: If task AA can be done in mm ways and task BB can be done in nn ways, and the tasks cannot be done simultaneously, then choosing to do either AA or BB can be done in m+nm + n ways.

Multiplication Principle: If task AA can be done in mm ways and task BB can be done in nn ways, then doing both AA and BB can be done in m×nm \times n ways.

Exercise 1.9: A restaurant offers 5 appetizers, 8 main courses, and 4 desserts. How many different three-course meals are possible?

1.3.2 Permutations

A permutation is an arrangement of objects in a specific order.

Permutations of nn distinct objects: n!n! (read ”nn factorial”) Permutations of rr objects from nn distinct objects: P(n,r)=n!(nr)!P(n,r) = \frac{n!}{(n-r)!}

Exercise 1.10: In how many ways can 8 people be arranged in a line? In how many ways can 3 people be selected from 8 people and arranged in a line?

1.3.3 Combinations

A combination is a selection of objects where order doesn’t matter.

Combinations of rr objects from nn distinct objects: C(n,r)=(nr)=n!r!(nr)!C(n,r) = \binom{n}{r} = \frac{n!}{r!(n-r)!}

Properties of binomial coefficients:

  • (n0)=(nn)=1\binom{n}{0} = \binom{n}{n} = 1
  • (nr)=(nnr)\binom{n}{r} = \binom{n}{n-r}
  • (nr)=(n1r1)+(n1r)\binom{n}{r} = \binom{n-1}{r-1} + \binom{n-1}{r} (Pascal’s identity)

Exercise 1.11: Prove Pascal’s identity both algebraically and combinatorially.

1.3.4 The Binomial Theorem

Theorem 1.2 (Binomial Theorem): For any real numbers xx and yy and non-negative integer nn: (x+y)n=k=0n(nk)xnkyk(x + y)^n = \sum_{k=0}^{n} \binom{n}{k} x^{n-k} y^k

Exercise 1.12: Use the binomial theorem to expand (x+2)4(x + 2)^4.

1.3.5 The Pigeonhole Principle

Theorem 1.3 (Pigeonhole Principle): If nn pigeons are placed in mm pigeonholes and n>mn > m, then at least one pigeonhole contains more than one pigeon.

Generalized Pigeonhole Principle: If nn pigeons are placed in mm pigeonholes, then at least one pigeonhole contains at least n/m\lceil n/m \rceil pigeons.

Exercise 1.13: Prove that among any 13 people, at least two have birthdays in the same month.

1.3.6 Inclusion-Exclusion Principle

Theorem 1.4 (Inclusion-Exclusion Principle): For finite sets A1,A2,,AnA_1, A_2, \ldots, A_n: A1A2An=iAii<jAiAj+i<j<kAiAjAk|A_1 \cup A_2 \cup \cdots \cup A_n| = \sum_{i} |A_i| - \sum_{i < j} |A_i \cap A_j| + \sum_{i < j < k} |A_i \cap A_j \cap A_k| - \cdots

Exercise 1.14: Use inclusion-exclusion to find the number of integers from 1 to 1000 that are divisible by 2, 3, or 5.

1.4 Connecting Foundations to Algorithms

These mathematical foundations directly support algorithmic analysis:

Set theory provides the vocabulary for describing input domains, output ranges, and data structures. Every algorithm operates on sets of data.

Logic enables precise specification of algorithm correctness. We express what an algorithm should do using logical statements, then prove these statements hold.

Proof techniques allow us to rigorously demonstrate that algorithms work correctly. We’ll use direct proof, contradiction, and induction repeatedly.

Combinatorics gives us tools to count operations, analyze worst-case scenarios, and compute probabilities in randomized algorithms.

Exercise 1.15: Consider the problem of finding the maximum element in a set S={a1,a2,,an}S = \{a_1, a_2, \ldots, a_n\} of distinct integers.

  1. Express the problem statement using set theory and logic
  2. Describe an algorithm to solve this problem
  3. Prove your algorithm is correct
  4. Count the number of comparisons your algorithm makes

Summary

This chapter established the mathematical language we’ll use throughout our study of algorithms. We covered:

  • Set theory: Basic operations, relations, and functions
  • Logic: Propositions, quantifiers, and logical reasoning
  • Proof techniques: Direct proof, contradiction, contrapositive, and induction
  • Combinatorics: Counting principles, permutations, combinations, and important theorems

These tools provide the foundation for rigorous algorithm analysis. In the next chapter, we’ll begin applying these concepts to analyze the correctness and efficiency of basic algorithms.

Key takeaways:

  1. Precision in mathematical language prevents ambiguity in algorithm specification
  2. Logical reasoning enables rigorous correctness proofs
  3. Combinatorial analysis quantifies algorithm performance
  4. Mathematical maturity develops through practice with formal reasoning

The exercises in this chapter are essential for developing the mathematical fluency needed for advanced algorithm analysis. Work through them systematically before proceeding to the next chapter.

Selected Exercise Solutions

Solution 1.1: AB={1,2,3,4,5,6}A \cup B = \{1, 2, 3, 4, 5, 6\}, AB={3,4}A \cap B = \{3, 4\}, AB={1,2}A \setminus B = \{1, 2\}, BA={5,6}B \setminus A = \{5, 6\}.

Solution 1.8: Base case: For n=1n = 1, 21=2>12^1 = 2 > 1. ✓ Inductive step: Assume 2k>k2^k > k. Then 2k+1=22k>2k2^{k+1} = 2 \cdot 2^k > 2k. Since k1k \geq 1, we have 2kk+12k \geq k + 1, so 2k+1>k+12^{k+1} > k + 1. By induction, 2n>n2^n > n for all n1n \geq 1. □