Learn/cs fundamentals/Discrete Mathematics
Beginner~15 min read

Discrete Mathematics

Logic, set theory, relations, graph theory, combinatorics, and proof techniques for CS.

LogicSet TheoryGraph TheoryCombinatorics

Why Discrete Math for CS?

Discrete mathematics studies structures that are fundamentally countable — integers, graphs, logic — as opposed to continuous mathematics (calculus, real analysis). It's the mathematical foundation of computer science: logic underlies programming languages, graph theory underlies networks and algorithms, combinatorics appears in complexity analysis and probability, and proof techniques are used in algorithm correctness and cryptography.

Logic and Propositions

A proposition is a statement that is either true (T) or false (F). Logical operators combine propositions into compound statements.

OperatorSymbolTrue when
AND (conjunction)p ∧ qBoth p and q are true
OR (disjunction)p ∨ qAt least one of p, q is true
NOT (negation)¬pp is false
IMPLIES (implication)p → qp is false, OR q is true (false only when p=T and q=F)
IFF (biconditional)p ↔ qp and q have the same truth value
XOR (exclusive or)p ⊕ qExactly one of p, q is true

A tautology is a formula that is always true (e.g., p ∨ ¬p). A contradiction is always false (e.g., p ∧ ¬p). Key laws: De Morgan's: ¬(p ∧ q) = ¬p ∨ ¬q and ¬(p ∨ q) = ¬p ∧ ¬q.

Set Theory

A set is an unordered collection of distinct elements. Sets are the foundation of modern mathematics and appear everywhere in CS — type systems, database theory, language semantics.

OperationSymbolMeaning
UnionA ∪ BAll elements in A or B (or both)
IntersectionA ∩ BElements in both A and B
DifferenceA − BElements in A but not in B
ComplementA'Elements not in A (relative to universal set)
SubsetA ⊆ BEvery element of A is in B
Power setP(A)Set of all subsets of A; |P(A)| = 2^|A|
Cartesian productA × BAll ordered pairs (a, b) where a ∈ A, b ∈ B

Inclusion-Exclusion Principle

|A ∪ B| = |A| + |B| − |A ∩ B|. Extends to three sets: |A ∪ B ∪ C| = |A| + |B| + |C| − |A∩B| − |A∩C| − |B∩C| + |A∩B∩C|. Used to count "at least one" conditions by avoiding double-counting overlaps.

Relations and Functions

A relation R from set A to set B is a subset of A × B. Key properties of relations on a set A: reflexive (a R a for all a), symmetric (a R b implies b R a), transitive (a R b and b R c implies a R c). A relation that is all three is an equivalence relation (partitions A into equivalence classes).

A function f: A → B maps each element of A to exactly one element of B. Injective (one-to-one): different inputs give different outputs. Surjective (onto): every output has at least one input. Bijective: both — a perfect pairing. Bijections enable counting and cryptography.

Combinatorics

Combinatorics counts arrangements and selections. It's essential for algorithm analysis (how many operations?), probability, and cryptography (how many keys are possible?).

ConceptFormulaWhen to use
PermutationP(n,r) = n!/(n−r)!Ordered arrangements of r items from n (order matters)
CombinationC(n,r) = n! / (r!(n−r)!)Unordered selections of r items from n (order doesn't matter)
Multiplication Rule|A| × |B|Independent sequential choices (first do A, then B)
Binomial Theorem(x+y)ⁿ = Σ C(n,k) xᵏ yⁿ⁻ᵏExpanding binomials; Pascal's triangle gives C(n,k)
Pigeonhole Principlen items in k boxes → ≥1 box has ≥⌈n/k⌉Proving collisions exist (hash functions, birthday paradox)
// Examples:
// How many 3-letter passwords from 26 letters (no repeat)?  P(26,3) = 26×25×24 = 15,600
// How many ways to choose a 5-card hand from 52?  C(52,5) = 2,598,960
// Birthday paradox: how many people needed for >50% chance two share a birthday?
//   ~23 people — probability ≈ 1 − (365/365)(364/365)...(343/365) ≈ 0.507

Graph Theory

A graph G = (V, E) is a set of vertices V and edges E. Graphs model networks, dependencies, social connections — anything with relationships between entities.

TermDefinition
DegreeNumber of edges incident to a vertex; handshaking lemma: Σ degrees = 2|E|
PathSequence of vertices where each consecutive pair is connected by an edge
CycleA path that starts and ends at the same vertex
ConnectedEvery vertex is reachable from every other vertex
TreeConnected graph with no cycles; always has exactly |V| − 1 edges
BipartiteVertices can be divided into two disjoint sets with no edges within a set
PlanarCan be drawn without crossing edges; Euler's formula: V − E + F = 2

Proof Techniques

Direct Proof

Assume hypothesis, derive conclusion step by step. Example: prove "if n is even, n² is even."

Let n be even → n = 2k for some integer k.
n² = (2k)² = 4k² = 2(2k²).
Since 2k² is an integer, n² = 2·(integer) → n² is even. □

Proof by Contradiction

Assume the opposite of what you want to prove, derive a contradiction.

// √2 is irrational:
Assume √2 = p/q (fully reduced, p,q integers, gcd(p,q) = 1).
Then 2 = p²/q² → p² = 2q² → p² is even → p is even → p = 2k.
Then (2k)² = 2q² → 4k² = 2q² → q² = 2k² → q is even.
But p and q both even contradicts gcd(p,q) = 1. ⊥ → √2 is irrational. □

Mathematical Induction

Prove for base case, then prove if true for n it's true for n+1.

// Prove 1 + 2 + ... + n = n(n+1)/2
Base case (n=1): 1 = 1·2/2 = 1. ✓
Inductive step: Assume true for n (i.e., sum = n(n+1)/2).
Prove for n+1:
1+2+...+n+(n+1) = n(n+1)/2 + (n+1)
                = (n+1)[n/2 + 1]
                = (n+1)(n+2)/2   ← this is (n+1)((n+1)+1)/2. ✓ □

Key Takeaways

  • Logic operators (AND, OR, NOT, IMPLIES) map directly to programming: &&, ||, !. De Morgan's law is used constantly in code simplification.
  • Sets appear everywhere — database relational algebra is set algebra.
  • Counting techniques determine algorithm complexity: C(n,2) = O(n²) explains why nested loops are quadratic.
  • Pigeonhole principle: with n+1 inputs into n buckets (hash slots, categories), at least one collision is guaranteed.
  • Graph theory is the backbone of algorithm problems — almost every hard problem is a graph problem in disguise.
  • Induction is how you prove algorithms correct on all inputs — prove base case, prove the step.