Discrete Mathematics
The reasoning toolkit the rest of computer science quietly assumes: logic, proof, sets and relations, counting, recurrences and the number theory behind cryptography.
If you remember nothing else
- The contrapositive of p → q is ¬q → ¬p, and it is logically equivalent; the converse q → p is not.
- p → q is false in exactly one case: p true and q false. A false premise makes the implication vacuously true.
- An equivalence relation is reflexive, symmetric and transitive, and it partitions its set into disjoint classes.
- Permutations count arrangements (order matters), combinations count selections (order does not): C(n,r) = P(n,r)/r!.
- The pigeonhole principle: n+1 items in n boxes force some box to hold at least two — the basis of most 'prove there exist two…' questions.
- The handshake lemma: the sum of all vertex degrees equals twice the number of edges, so the count of odd-degree vertices is always even.
Propositional logic
A proposition is a declarative statement with a definite truth value. Connectives build compound propositions, and a truth table settles any question about them mechanically.
| Connective | Symbol | Read as | True when |
|---|---|---|---|
| Negation | not p | p is false | |
| Conjunction | p and q | both are true | |
| Disjunction | p or q | at least one is true | |
| Exclusive or | p xor q | exactly one is true | |
| Implication | if p then q | always, except when p is true and q is false | |
| Biconditional | p if and only if q | both have the same truth value |
| Form | Statement | Equivalent to the original? |
|---|---|---|
| Original | — | |
| Converse | No | |
| Inverse | No (it is the converse's contrapositive) | |
| Contrapositive | Yes — always |
"If it rains, the ground is wet" does not mean "if the ground is wet, it rained" (converse). It does mean "if the ground is not wet, it did not rain" (contrapositive).
Equivalences worth memorising
| Law | Form |
|---|---|
| De Morgan | and |
| Distributive | |
| Implication | |
| Contrapositive | |
| Absorption | |
| Double negation |
Predicate logic and quantifiers
Propositional logic cannot express "every student passed" — it has no way to talk about individuals. Predicate logic adds predicates and quantifiers over a domain.
- Universal — P holds for every element. A single counterexample makes it false.
- Existential — P holds for at least one element. One witness makes it true.
- Negation rules: , and . Pushing a negation inward flips the quantifier.
| Rule of inference | Form | Name |
|---|---|---|
| From and , infer | Affirm the premise | Modus ponens |
| From and , infer | Deny the conclusion | Modus tollens |
| From and , infer | Chain | Hypothetical syllogism |
| From and , infer | Eliminate | Disjunctive syllogism |
Proof techniques
| Technique | To prove | Method | Use when |
|---|---|---|---|
| Direct | Assume p, derive q | The implication is straightforward | |
| Contraposition | Assume , derive | The negation is easier to work with | |
| Contradiction | Any statement s | Assume , derive an absurdity | Proving something does not exist |
| Induction | for all | Base case, then | The claim is indexed by a natural number |
| Strong induction | for all n | Assume , prove | The step needs more than the previous case |
| Counterexample | Exhibit one x with | Disproving a universal claim |
Sets, relations and functions
- The power set of a set with n elements has elements — including the empty set and the set itself.
- Cardinality of a union: (inclusion-exclusion).
- The Cartesian product has ordered pairs.
- A relation on A is any subset of ; with n elements there are possible relations.
| Property | Definition | Example |
|---|---|---|
| Reflexive | , | "is equal to", "divides" |
| Symmetric | "is a sibling of" | |
| Antisymmetric | and both in R | "", "is a subset of" |
| Transitive | and in R | "", "is an ancestor of" |
| Function type | Condition | Consequence |
|---|---|---|
| Injective (one-to-one) | Distinct inputs give distinct outputs | |
| Surjective (onto) | Every element of the codomain is hit | |
| Bijective | Both | ; an inverse exists |
Counting and combinatorics
| Rule | Formula | Use |
|---|---|---|
| Product rule | Independent sequential choices | |
| Sum rule | Mutually exclusive alternatives | |
| Permutation | Arrange r of n — order matters | |
| Combination | Choose r of n — order does not matter | |
| With repetition (arrange) | r positions, n choices each | |
| With repetition (choose) | r items from n types, repeats allowed | |
| Circular permutation | Seat n people around a table |
Two principles that carry whole questions
- Pigeonhole principle — placing objects into boxes forces some box to contain at least two. Generalised: objects in boxes force some box to hold at least . Standard use: among any 13 people, two share a birth month.
- Inclusion-exclusion — . Add the singles, subtract the pairs, add the triples, alternating.
Graph theory
- Handshake lemma — . Every edge contributes 2 to the total degree, so the number of odd-degree vertices is always even.
- A complete graph has edges. A tree on n vertices has exactly edges and is connected and acyclic — any two of those three properties imply the third.
- A bipartite graph has no odd-length cycle; equivalently, it is 2-colourable.
- A graph is planar if it can be drawn without crossings. Euler's formula: for a connected planar graph. By Kuratowski's theorem, a graph is planar iff it contains no subdivision of or .
- By the four colour theorem, every planar graph is 4-colourable.
Recurrence relations
A recurrence defines a term from earlier terms. Solving it means finding a closed form — which for algorithm analysis is exactly the running time.
Substitution (iteration)
Expand the recurrence repeatedly until the pattern is visible, then apply the base case. expands to .Characteristic equation
For a linear homogeneous recurrence such as , substitute to get , giving roots 2 and 3, so with A and B fixed by the initial conditions.Master theorem
For divide-and-conquer recurrences , compare against and read off the case. Covered in full in the Data Structures & Algorithms subject.
| Recurrence | Closed form | Arises in |
|---|---|---|
| Linear search | ||
| Bubble sort, insertion sort worst case | ||
| Binary search | ||
| Mergesort | ||
| Towers of Hanoi |
Number theory essentials
- means m divides . Congruences may be added and multiplied, but not divided freely.
- Euclid's algorithm: , terminating when the remainder is 0. It runs in .
- .
- Euler's totient counts integers in coprime to n. For a prime p, ; for distinct primes p and q, .
- Fermat's little theorem: if p is prime and p does not divide a, then .
- A modular inverse of a mod m exists iff ; the extended Euclidean algorithm finds it.
int gcd(int a, int b) { // iterative
while (b) { int t = b; b = a % b; a = t; }
return a;
}
int gcd_rec(int a, int b) { // recursive - the definition itself
return b == 0 ? a : gcd_rec(b, a % b);
}
// gcd(48, 18) -> gcd(18, 12) -> gcd(12, 6) -> gcd(6, 0) = 6Self-check
10 questions on Discrete Mathematics · nothing is stored