08 — Discrete Mathematics for Computer Science

A structured introduction to the logic, sets, relations, functions, and proof methods that form the mathematical foundation of computer science.

Foundations and the Computer Science Perspective

Discrete mathematics focuses on finite, countable, and other non-continuous structures. In computer science, it supplies precise ways to specify program behavior, represent data, analyze algorithms, and establish correctness.

The main ideas build on one another:

  • Logic describes truth and conditions.

  • Predicates and quantifiers describe properties of objects and collections of objects.

  • Sets describe domains and collections.

  • Relations describe connections, equivalence, and ordering.

  • Functions describe deterministic input-output behavior.

  • Proof methods justify correctness and impossibility claims.

A useful general workflow is to identify the objects and their domains, translate informal claims into definitions or symbolic statements, make assumptions explicit, and then select a proof strategy that matches the claim.

Takeaway: Discrete mathematics provides a common language for describing computational objects and reasoning rigorously about them.

Logic, Truth, and Inference

A is a declarative statement with exactly one truth value: true or false. For example, “77 is prime” is true, while “1010 is less than 33” is false. A symbolic variable such as PP can stand for either .

A forms compound propositions:

  • Negation: ¬P\neg P, meaning “not PP.”

  • Conjunction: P∧QP \land Q, meaning “PP and QQ.”

  • Disjunction: P∨QP \lor Q, meaning inclusive “PP or QQ.”

  • Implication: P→QP \to Q, meaning “if PP, then QQ.”

  • Biconditional: P↔QP \leftrightarrow Q, meaning “PP if and only if QQ.”

For an implication P→QP \to Q, PP is the antecedent and QQ is the consequent. The implication is false only in the case where PP is true and QQ is false. A truth table checks every possible assignment of truth values.

Important equivalences include:

  • Double negation: ¬(¬P)≡P\neg(\neg P) \equiv P.

  • De Morgan’s laws: ¬(P∧Q)≡¬P∨¬Q\neg(P \land Q) \equiv \neg P \lor \neg Q and ¬(P∨Q)≡¬P∧¬Q\neg(P \lor Q) \equiv \neg P \land \neg Q.

  • Contrapositive: P→Q≡¬Q→¬PP \to Q \equiv \neg Q \to \neg P.

  • Implication rewriting: P→Q≡¬P∨QP \to Q \equiv \neg P \lor Q.

A tautology is true for every assignment, whereas a contradiction is false for every assignment. Two propositions are logically equivalent when they have the same truth value in every possible case.

A rule of inference gives a valid pattern for deriving a conclusion from premises. Modus ponens uses PP and P→QP \to Q to conclude QQ. Modus tollens uses ¬Q\neg Q and P→QP \to Q to conclude ¬P\neg P. These forms matter in program conditions, query languages, digital circuits, and formal verification.

Takeaway: Truth tables and equivalence laws let you analyze conditions independently of their application domain.

Predicates, Domains, and Quantifiers

A predicate is a statement containing variables whose truth depends on assigned values. For example, Prime(x)Prime(x) can express that xx is prime. Before a value is assigned to xx, the predicate is not yet a .

A connects a predicate to its domain:

  • ∀x P(x)\forall x\,P(x) means that P(x)P(x) is true for every xx in the domain.

  • ∃x P(x)\exists x\,P(x) means that at least one element xx makes P(x)P(x) true.

For example,

∀x∈N, x+0=x\forall x \in \mathbb{N},\ x + 0 = x

asserts a property of every natural number, while

∃x∈Z, x2=4\exists x \in \mathbb{Z},\ x^2 = 4

is true because both x=2x = 2 and x=−2x = -2 satisfy the predicate.

Negating quantifiers reverses their type and negates the predicate:

¬(∀x P(x))≡∃x ¬P(x)\neg(\forall x\,P(x)) \equiv \exists x\,\neg P(x)
¬(∃x P(x))≡∀x ¬P(x)\neg(\exists x\,P(x)) \equiv \forall x\,\neg P(x)

Thus, “not every input is valid” means that there exists an invalid input. The order of multiple quantifiers must be examined carefully because changing it can change the meaning of a specification.

Takeaway: Always identify the domain and scope before deciding whether a quantified statement is true.

Sets and Operations

A is a collection of distinct elements. It can be described by listing elements, as in A={1,2,3}A = \{1,2,3\}, or by a condition, as in E={x∈Z∣x is even}E = \{x \in \mathbb{Z} \mid x\text{ is even}\}. The statement x∈Ax \in A expresses membership, and ∅\varnothing denotes the empty .

If every element of AA is also in BB, then AA is a of BB, written A⊆BA \subseteq B. Common operations are:

  • Union: A∪BA \cup B contains elements in AA or BB.

  • Intersection: A∩BA \cap B contains elements in both AA and BB.

  • Difference: A∖BA \setminus B contains elements in AA but not in BB.

  • Complement: AcA^c contains elements in the chosen universal that are not in AA.

The Cartesian product is the of ordered pairs

A×B={(a,b)∣a∈A and b∈B}.A \times B = \{(a,b) \mid a \in A \text{ and } b \in B\}.

For A={0,1}A = \{0,1\} and B={x,y}B = \{x,y\},

A×B={(0,x),(0,y),(1,x),(1,y)}.A \times B = \{(0,x),(0,y),(1,x),(1,y)\}.

The power P(A)\mathcal{P}(A) contains every of AA. If AA has nn elements, then P(A)\mathcal{P}(A) has 2n2^n elements.

To prove equality, prove both inclusions: A⊆BA \subseteq B and B⊆AB \subseteq A. Equivalently, choose an arbitrary element xx and show that x∈Ax \in A exactly when x∈Bx \in B. For example, the distributive law

A∩(B∪C)=(A∩B)∪(A∩C)A \cap (B \cup C) = (A \cap B) \cup (A \cap C)

follows from the corresponding propositional equivalence P∧(Q∨R)↔(P∧Q)∨(P∧R)P \land (Q \lor R) \leftrightarrow (P \land Q) \lor (P \land R).

Takeaway: notation turns informal collections and membership conditions into objects that can be manipulated and proved equal.

Relations, Equivalence, and Order

A from AA to BB is a of A×BA \times B. If (a,b)(a,b) belongs to the relation RR, it may be written as aRbaRb. Relations can represent enrollment, network connectivity, numerical comparison, or having the same remainder modulo a fixed number.

When a relation RR is defined on a AA, its properties include:

  • Reflexive: aRaaRa for every a∈Aa \in A.

  • Symmetric: if aRbaRb, then bRabRa.

  • Antisymmetric: if aRbaRb and bRabRa, then a=ba=b.

  • Transitive: if aRbaRb and bRcbRc, then aRcaRc.

An is reflexive, symmetric, and transitive. It partitions a into equivalence classes. For example, having the same remainder modulo 33 is an on the integers.

A partial order is reflexive, antisymmetric, and transitive. The relation ⊆\subseteq is a partial order on a power . Partial orders model dependencies, version constraints, and task precedence.

Relations can be represented as ordered pairs, tables, Boolean matrices, or directed graphs. The representation can be chosen to suit an application such as a database, graph algorithm, access-control system, or state-transition model.

Takeaway: To classify a relation, test each defining property separately; combinations of properties reveal its mathematical structure.

Functions and Mappings

A is written f ⁣:A→Bf\colon A \to B. It assigns every element of the domain AA exactly one element of the codomain BB. The range, or image, is the of values actually produced:

{f(a)∣a∈A}.\{f(a) \mid a \in A\}.

An injective maps distinct inputs to distinct outputs:

f(a1)=f(a2)⇒a1=a2.f(a_1)=f(a_2) \Rightarrow a_1=a_2.

A surjective reaches every element of its codomain:

∀b∈B, ∃a∈A such that f(a)=b.\forall b \in B,\ \exists a \in A\text{ such that }f(a)=b.

A is both injective and surjective. For example, f ⁣:Z→Zf\colon \mathbb{Z} \to \mathbb{Z} defined by f(n)=n+1f(n)=n+1 is bijective, with inverse f−1(n)=n−1f^{-1}(n)=n-1. In contrast, g(n)=n2g(n)=n^2 from Z\mathbb{Z} to Z\mathbb{Z} is not injective because g(1)=g(−1)g(1)=g(-1), and it is not surjective because no integer maps to −1-1.

Functions can be composed. If f ⁣:A→Bf\colon A \to B and g ⁣:B→Cg\colon B \to C, then

(g∘f)(a)=g(f(a)).(g \circ f)(a)=g(f(a)).

This composition applies ff first and then gg. Functions therefore provide a formal model for algorithms, encodings, transformations, hash computations, and state updates.

Takeaway: When analyzing a , distinguish its domain, codomain, and actual range before testing injectivity or surjectivity.

Proof Strategies and Mathematical Rigor

A proof is a logically valid argument that derives a conclusion from definitions, accepted facts, and established results. The best proof method depends on the structure of the claim.

Direct proof

To prove P→QP \to Q directly, assume PP, apply definitions and valid deductions, and derive QQ. If aa and bb are even, write a=2ma=2m and b=2nb=2n. Then a+b=2(m+n)a+b=2(m+n), so a+ba+b is even.

Proof by contrapositive

Because P→QP \to Q is equivalent to ¬Q→¬P\neg Q \to \neg P, prove the contrapositive when it is easier. To prove that an even square has an even root, show that an odd root has an odd square. If n=2k+1n=2k+1, then

n2=(2k+1)2=2(2k2+2k)+1,n^2=(2k+1)^2=2(2k^2+2k)+1,

which is odd.

Proof by contradiction

Assume the negation of the desired statement and derive an impossibility such as R∧¬RR \land \neg R or 0=10=1. The original statement then follows because the assumption of its negation cannot hold.

Proof by cases

Divide the possibilities into exhaustive cases and prove the conclusion in each one. For example, an integer is either even or odd, so a claim about every integer can be addressed through those two cases.

To prove P(n)P(n) for every natural number in a specified range:

  1. Prove the base case, such as P(0)P(0).

  2. Assume P(k)P(k) for an arbitrary allowed kk; this is the inductive hypothesis.

  3. Use that hypothesis to prove P(k+1)P(k+1).

For example, induction establishes

0+1+2+⋯+n=n(n+1)2.0+1+2+\cdots+n=\frac{n(n+1)}{2}.

Induction is especially useful for loops, recursive algorithms, data structures, and recursively generated objects.

Counterexamples

To disprove a universal claim, one is enough. The statement that every prime number is odd is false because 22 is prime and even. The must satisfy the assumptions while violating the conclusion.

Takeaway: Match the proof technique to the logical form: direct reasoning for constructive implications, contrapositive for reversed conditions, contradiction for impossible negations, cases for exhaustive alternatives, induction for recursive structure, and counterexamples for universal claims.

Connecting the Tools in Computer Science

The concepts become most useful when combined in a formal specification. A sorting algorithm, for example, should produce an output that is a permutation of its input and is ordered. The ordering condition can be written as

∀i<j, A[i]≤A[j].\forall i<j,\ A[i] \leq A[j].

The permutation requirement can be expressed using sets or multisets. Quantifiers state what must hold for every relevant pair of positions, relations express the ordering comparison, and functions can model the algorithm's transformation from input to output. A proof can then use induction on the number of elements or on the algorithm's recursive structure.

A disciplined reasoning process is:

  1. State the objects and their domains precisely.

  2. Translate informal language into definitions or symbolic statements.

  3. Identify the assumptions and the desired conclusion.

  4. Choose a proof strategy suited to the logical form.

  5. Justify every non-obvious step.

  6. Test universal claims with small examples and search for counterexamples.

These tools support algorithm correctness, security guarantees, type-system soundness, formal specifications, databases, networks, and theoretical computer science.

Final takeaway: Logic states what a computation should do, sets and relations describe its objects and structure, functions model its behavior, and proofs establish that the intended properties actually hold.