Logic, sets, relations, combinatorics, recurrence relations and graph theory essentials.
| Connective | Symbol | Read As | Meaning |
|---|---|---|---|
| Negation | ¬ / ~ | NOT | Reverses truth value |
| Conjunction | ∧ / AND | AND | True only if both are true |
| Disjunction | ∨ / OR | OR | True if at least one is true |
| Exclusive OR | ⊕ / XOR | XOR | True if exactly one is true |
| Implication | → | IF...THEN | False only when T→F |
| Biconditional | ↔ | IF AND ONLY IF | True when both have same truth value |
| Concept | Definition | Example |
|---|---|---|
| Tautology | Always true regardless of truth values | p ∨ ¬p (law of excluded middle) |
| Contradiction | Always false regardless of truth values | p ∧ ¬p |
| Contingency | Neither a tautology nor a contradiction | p → q |
| Satisfiable | At least one assignment makes it true | p ∧ q (satisfiable but not a tautology) |
| p | q | p → q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
| Name | Form | Equivalent to |
|---|---|---|
| Converse | q → p | NOT equivalent to p → q |
| Inverse | ¬p → ¬q | NOT equivalent to p → q |
| Contrapositive | ¬q → ¬p | Logically equivalent to p → q |
| Name | Equivalence |
|---|---|
| Identity | p ∧ T ≡ p, p ∨ F ≡ p |
| Domination | p ∨ T ≡ T, p ∧ F ≡ F |
| Idempotent | p ∧ p ≡ p, p ∨ p ≡ p |
| Double Negation | ¬(¬p) ≡ p |
| Commutative | p ∧ q ≡ q ∧ p, p ∨ q ≡ q ∨ p |
| Associative | (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) |
| Distributive | p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) |
| De Morgan's | ¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬P ∧ ¬Q |
| Absorption | p ∧ (p ∨ q) ≡ p, p ∨ (p ∧ q) ≡ p |
| Implication | p → q ≡ ¬P ∨ q |
| Biconditional | p ↔ q ≡ (p → q) ∧ (q → p) |
| Symbol | Name | Read As | Example |
|---|---|---|---|
| ∀ | Universal Quantifier | For all / For every | ∀x (x + 0 = x) — true for all integers |
| ∃ | Existential Quantifier | There exists | ∃x (x² = 4) — true (x=2 or x=-2) |
| ∃! | Uniqueness Quantifier | There exists exactly one | ∃!x (x + 1 = 0) w.r.t. natural numbers — false |
| ¬∀ | Negation of ∀ | Not for all = some do not | ¬∀x P(x) ≡ ∃x ¬P(x) |
| ¬∃ | Negation of ∃ | There does not exist = for no | ¬∃x P(x) ≡ ∀x ¬P(x) |
| Rule Name | Form | Description |
|---|---|---|
| Modus Ponens | p, p → q ∴ q | If p is true and p implies q, then q is true |
| Modus Tollens | ¬q, p → q ∴ ¬P | If q is false and p implies q, then p is false |
| Hypothetical Syllogism | p → q, q → r ∴ p → r | Chain of implications |
| Disjunctive Syllogism | p ∨ q, ¬P ∴ q | Elimination of one disjunct |
| Addition | p ∴ p ∨ q | Adding a disjunct |
| Simplification | p ∧ q ∴ p | Extracting one conjunct |
| Conjunction | p, q ∴ p ∧ q | Combining propositions |
| Resolution | p ∨ q, ¬P ∨ r ∴ q ∨ r | Used in automated theorem proving |
p \u2192 q is ALWAYS equivalent to ¬q \u2192 ¬p. Proofs are often easier using the contrapositive. Also, p \u2192 q is NOT equivalent to its converse q \u2192 p or its inverse ¬p \u2192 ¬q.| Operation | Notation | Definition | Example (A={1,2,3}, B={3,4,5}) |
|---|---|---|---|
| Union | A ∪ B | Elements in A OR B (or both) | {1,2,3,4,5} |
| Intersection | A ∩ B | Elements in A AND B | {3} |
| Difference | A \ B | Elements in A but NOT in B | {1,2} |
| Complement | A̅ | Elements NOT in A (w.r.t. universal set U) | U \ A |
| Symmetric Diff. | A ▔ B | Elements in A or B but NOT both | {1,2,4,5} |
| Cartesian Product | A × B | All ordered pairs (a,b) | {(1,3),(1,4),(1,5),(2,3),...} |
| Power Set | P(A) / ×(A) | Set of all subsets of A | 8 subsets for |A|=3 |
| Name | Law |
|---|---|
| Identity | A ∪ ∅ = A, A ∩ U = A |
| Domination | A ∪ U = U, A ∩ ∅ = ∅ |
| Idempotent | A ∪ A = A, A ∩ A = A |
| Complement | A ∪ A̅ = U, A ∩ A̅ = ∅ |
| Commutative | A ∪ B = B ∪ A, A ∩ B = B ∩ A |
| Associative | (A ∪ B) ∪ C = A ∪ (B ∪ C) |
| Distributive | A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) |
| De Morgan's | A ∪ B̅ = A̅ ∩ B̅, A ∩ B̅ = A̅ ∪ B̅ |
| Absorption | A ∪ (A ∩ B) = A, A ∩ (A ∪ B) = A |
Two sets: |A \u222A B| = |A| + |B| - |A \u2229 B|
Three sets: |A \u222A B \u222A C| = |A| + |B| + |C| - |A \u2229 B| - |A \u2229 C| - |B \u2229 C| + |A \u2229 B \u2229 C|
General (n sets): Alternate adding and subtracting intersections of all sizes.
# ── Inclusion-Exclusion: Worked Examples ──
# Example 1: How many integers from 1 to 100 are divisible by 3 or 5?
# Let A = divisible by 3, B = divisible by 5
|A| = 100 // 3 = 33 # {3, 6, 9, ..., 99}
|B| = 100 // 5 = 20 # {5, 10, 15, ..., 100}
|A ∩ B| = 100 // 15 = 6 # {15, 30, 45, 60, 75, 90}
|A ∪ B| = 33 + 20 - 6 = 47
# Example 2: Three sets
# In a class: 40 study Math, 50 study Physics, 45 study Chemistry
# 20 study Math & Physics, 15 study Math & Chemistry, 18 study Physics & Chemistry
# 8 study all three. How many study at least one?
# |M ∪ P ∪ C| = 40 + 50 + 45 - 20 - 15 - 18 + 8 = 90
# Example 3: Derangements (nothing in original position)
# Number of derangements of n items: D(n) = n! * Σ((-1)^k / k!) for k=0 to n
# D(4) = 24 * (1 - 1 + 1/2 - 1/6 + 1/24) = 24 * 9/24 = 9D(n) = n! × Σ((-1)^k / k!) or the recurrence D(n) = (n-1)[D(n-1) + D(n-2)].| Property | Definition | Test |
|---|---|---|
| Reflexive | Every element is related to itself: (a,a) ∈ R for all a | Check: diagonal must be fully present |
| Irreflexive | No element is related to itself: (a,a) ∉ R for all a | Check: diagonal must be fully absent |
| Symmetric | If (a,b) ∈ R then (b,a) ∈ R | Matrix must be symmetric about diagonal |
| Antisymmetric | If (a,b) ∈ R and (b,a) ∈ R then a = b | Only diagonal can have symmetric pairs |
| Transitive | If (a,b) ∈ R and (b,c) ∈ R then (a,c) ∈ R | Check all length-2 paths have length-1 shortcut |
| Equivalence | Reflexive + Symmetric + Transitive | Partitions the set into equivalence classes |
| Partial Order | Reflexive + Antisymmetric + Transitive | Can draw Hasse diagram |
An equivalence relation on set A partitions A into equivalence classes [a] = {b \u2208 A : (a,b) \u2208 R}.
| Relation | Ref | Sym | Tra | Equiv? |
|---|---|---|---|---|
| Congruence mod n | ✓ | ✓ | ✓ | Yes |
| Equality (=) | ✓ | ✓ | ✓ | Yes |
| <= on integers | ✓ | ✗ | ✓ | No (not symmetric) |
| "Same parity" | ✓ | ✓ | ✓ | Yes |
| "Divides" on N | ✓ | ✗ | ✓ | No (not symmetric) |
A partial order (poset) is reflexive, antisymmetric, and transitive. Hasse diagrams visualize posets by omitting self-loops, transitive edges, and arrowheads.
| Term | Definition |
|---|---|
| Minimal element | No element is strictly smaller |
| Maximal element | No element is strictly larger |
| Least element (minimum) | Smaller than or equal to all elements |
| Greatest element (maximum) | Larger than or equal to all elements |
| Least upper bound (lub) | Smallest element that is ≥ all elements in subset |
| Greatest lower bound (glb) | Largest element that is ≤ all elements in subset |
| Total order | Partial order where every pair is comparable |
| Lattice | Poset where every pair has both lub and glb |
| Closure Type | Definition | How to Compute |
|---|---|---|
| Reflexive closure | Smallest reflexive relation containing R | Add all (a,a) pairs to R |
| Symmetric closure | Smallest symmetric relation containing R | Add (b,a) for every (a,b) in R |
| Transitive closure | Smallest transitive relation containing R | R* = R ∪ R² ∪ R³ ∪ ... (use Warshall's algorithm) |
# ── Warshall's Algorithm for Transitive Closure ──
# Time: O(n^3), Space: O(n^2)
def warshall(n, edges):
"""Compute transitive closure of a relation on {0, 1, ..., n-1}."""
# Initialize adjacency matrix
R = [[False] * n for _ in range(n)]
for u, v in edges:
R[u][v] = True
# Warshall's algorithm
for k in range(n):
for i in range(n):
if R[i][k]: # Optimization: skip if no path i->k
for j in range(n):
if R[k][j]:
R[i][j] = True
return R
# Example: R = {(0,1), (1,2), (2,0)} on set {0,1,2}
edges = [(0,1), (1,2), (2,0)]
closure = warshall(3, edges)
# R* = {(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)}Given relations R on A\u00D7B and S on B\u00D7C, the composition S \u2218 R = {(a,c) : \u2203b \u2208 B, (a,b) \u2208 R \u2227 (b,c) \u2208 S}.
# ── Composition of Relations ──
def compose(R, S):
"""Compute S ∘ R (S after R).
R: set of (a, b) pairs, S: set of (b, c) pairs.
Result: set of (a, c) pairs."""
result = set()
for a, b in R:
for b2, c in S:
if b == b2:
result.add((a, c))
return result
# Example:
R = {(1, 2), (2, 3), (3, 4)} # relation on natural numbers
S = {(2, 5), (3, 6), (4, 7)}
print(compose(R, S)) # {(1, 5), (2, 6), (3, 7)}
# Matrix method: composition = boolean matrix product
# (S ∘ R)[i][j] = OR over k of (R[i][k] AND S[k][j])| Type | Definition | Property | Horizontal Line Test |
|---|---|---|---|
| Injective (One-to-one) | f(a) = f(b) → a = b | Each output has at most one input | No horizontal line hits more than once |
| Surjective (Onto) | For every b ∈ B, ∃a ∈ A: f(a) = b | Range = Codomain | Every element in codomain is hit |
| Bijective | Both injective AND surjective | Perfect 1-to-1 correspondence | Passes horizontal line test + covers codomain |
Counting functions from set A (size m) to set B (size n):
| Type | Count (m → n) |
|---|---|
| All functions | nᵐ |
| Injective functions | P(n,m) = n! / (n-m)! |
| Surjective functions | n! × S(m,n) [Stirling numbers of 2nd kind] |
| Bijective functions | n! (only if m = n) |
Weak Form:If n items are placed into k containers and n > k, then at least one container contains at least ceil(n/k) items.
Strong Form (Generalized): If n items are placed into k containers, then at least one container contains at least floor((n-1)/k) + 1 items.
# ── Pigeonhole Principle: Worked Examples ──
# Example 1 (Weak Form):
# In a group of 13 people, prove at least 2 share a birth month.
# Pigeons = 13 people, Holes = 12 months
# ceil(13/12) = 2, so at least 2 people share a month. ✓
# Example 2 (Strong Form):
# Among 100 people, how many have the same birthday (day of year)?
# Pigeons = 100, Holes = 365
# floor((100-1)/365) + 1 = 0 + 1 = 1 (trivially true)
# But floor(99/365) + 1 = 1. So at least 2 share? No.
# Actually: ceil(100/365) = 1. Need ceil.
# 100 > 365 is false, so weak form doesn't help.
# Correct: floor(99/365) + 1 = 1. So guaranteed 1 per day minimum.
# Actually: At least 1 person. Trivially true. Not useful.
# Real answer: we CAN'T guarantee 2 sharing among 100 people with 365 days.
# Example 3 (Strong Form - non-trivial):
# Among 400 people, prove at least 2 share the same birthday.
# Pigeons = 400, Holes = 365
# floor((400-1)/365) + 1 = 1 + 1 = 2. ✓ At least 2 people share.
# Example 4 (Interview Classic):
# Show that in any group of 6 people, there are either 3 mutual
# friends or 3 mutual strangers (Ramsey's theorem R(3,3) = 6).
# Consider person A and their 5 relationships with others.
# By PHP (5 people, 2 categories: friend/stranger),
# A has ≥ 3 friends OR ≥ 3 strangers (say 3 friends: B, C, D).
# If any 2 of {B,C,D} are friends, + A = 3 mutual friends.
# If no 2 of {B,C,D} are friends, then {B,C,D} are 3 mutual strangers.
# Example 5 (Counting):
# How many socks must you pull from a drawer with 4 colors
# to guarantee a matching pair?
# PHP: 5 socks (4 colors + 1). The 5th sock MUST match one of the 4.⌈N/k⌉ objects.| Concept | Formula | Description |
|---|---|---|
| Permutation (ordered) | P(n,r) = n! / (n-r)! | Arrangements of r items from n |
| Combination (unordered) | C(n,r) = n! / (r!(n-r)!) | Selections of r items from n |
| Multinomial | (n!)/(k1! × k2! × ... × km!) | Permutations with repeated elements |
| Circular Permutation | (n-1)! | Arrangements in a circle (rotations = same) |
| Permutation with repetition | n^r | Choosing r from n with replacement, order matters |
| Combination with repetition | C(n+r-1, r) | Choosing r from n with replacement, order irrelevant |
Problem: Number of ways to distribute n identical items into k distinct bins = C(n+k-1, k-1) = C(n+k-1, n)
# ── Stars and Bars: Worked Examples ──
# Example 1: How many solutions to x1 + x2 + x3 = 10 where xi >= 0?
# Stars (10 items) and Bars (2 dividers for 3 variables)
# C(10 + 3 - 1, 3 - 1) = C(12, 2) = 66
# Example 2: How many solutions to x1 + x2 + x3 = 10 where xi >= 1?
# Substitute yi = xi - 1: y1 + y2 + y3 = 7 where yi >= 0
# C(7 + 3 - 1, 3 - 1) = C(9, 2) = 36
# Example 3: Distribute 20 identical candies to 5 children, each gets >= 3.
# yi = xi - 3: y1 + ... + y5 = 20 - 15 = 5, yi >= 0
# C(5 + 5 - 1, 5 - 1) = C(9, 4) = 126
# Example 4: Number of non-decreasing sequences of length 4 from {1,2,...,6}
# Equivalent to combinations with repetition: C(6+4-1, 4) = C(9,4) = 126(x + y)^n = Σ C(n,k) × x^{n-k} × y^k for k = 0 to n
| Property | Value |
|---|---|
| Sum of binomial coefficients | C(n,0) + C(n,1) + ... + C(n,n) = 2^n |
| Alternating sum | C(n,0) - C(n,1) + C(n,2) - ... = 0 |
| Sum of even terms | C(n,0) + C(n,2) + C(n,4) + ... = 2^{n-1} |
| Sum of odd terms | C(n,1) + C(n,3) + C(n,5) + ... = 2^{n-1} |
| Vandermonde identity | C(m+n, r) = Σ C(m,k) × C(n,r-k) |
| Pascal identity | C(n,k) = C(n-1,k-1) + C(n-1,k) |
| Symmetry | C(n,k) = C(n, n-k) |
| Hockey-stick identity | C(k,k) + C(k+1,k) + ... + C(n,k) = C(n+1, k+1) |
Linear Homogeneous with Constant Coefficients: a_n = c_1 × a_{n-1} + c_2 × a_{n-2} + ... + c_k × a_{n-k}
| Sequence | Recurrence | Characteristic Equation | Closed Form |
|---|---|---|---|
| Fibonacci | F_n = F_{n-1} + F_{n-2} | r² - r - 1 = 0 | (φ^n - ψ^n) / √5 |
| Factorial-like | a_n = 2a_{n-1} | r - 2 = 0 | C × 2^n |
| Tribonacci | T_n = T_{n-1} + T_{n-2} + T_{n-3} | r³ - r² - r - 1 = 0 | Solve cubic roots |
# ── Solving Linear Recurrence Relations ──
# Step 1: Write characteristic equation
# Step 2: Find roots r1, r2, ...
# Step 3: General solution depends on roots
# Case 1: Distinct roots r1, r2, ..., rk
# a_n = A1*r1^n + A2*r2^n + ... + Ak*rk^n
# Example: a_n = 5a_{n-1} - 6a_{n-2}, a_0=1, a_1=2
# r^2 - 5r + 6 = 0 => r = 2, 3
# a_n = A(2^n) + B(3^n)
# a_0 = A + B = 1, a_1 = 2A + 3B = 2
# A = 1, B = 0 => a_n = 2^n
# Case 2: Repeated root r (multiplicity m)
# (A1 + A2*n + A3*n^2 + ... + Am*n^{m-1}) * r^n
# Example: a_n = 6a_{n-1} - 9a_{n-2}, a_0=1, a_1=6
# r^2 - 6r + 9 = 0 => (r-3)^2 = 0 => r = 3 (double root)
# a_n = (A + Bn)(3^n)
# a_0 = A = 1, a_1 = (1+B)(3) = 6 => B = 1
# a_n = (1 + n)(3^n)
# Case 3: Complex roots a ± bi
# Use polar form: r^n = p^n(A*cos(nθ) + B*sin(nθ))
# where p = sqrt(a^2 + b^2), θ = atan2(b, a)The ordinary generating function for a sequence a_0, a_1, a_2, ... is G(x) = Σ a_n \u00D7 x^n.
| Sequence | Generating Function |
|---|---|
| 1, 1, 1, 1, ... | 1 / (1 - x) |
| 1, 2, 3, 4, ... (n) | 1 / (1-x)² |
| 1, 2, 4, 8, ... (2^n) | 1 / (1 - 2x) |
| C(n,0), C(n,1), ... (fixed n) | (1+x)^n |
| C(n+k,k) = C(n+k,n) | 1 / (1-x)^{n+1} |
| Fibonacci | x / (1 - x - x²) |
| Term | Definition |
|---|---|
| Vertex (Node) | Fundamental unit; a point in the graph |
| Edge | Connection between two vertices |
| Degree (deg(v)) | Number of edges incident to vertex v |
| In-degree / Out-degree | For directed graphs: edges coming in / going out |
| Path | Sequence of vertices connected by edges (no vertex repeated) |
| Walk | Sequence of vertices connected by edges (repetition allowed) |
| Cycle | Path that starts and ends at the same vertex |
| Circuit | Closed walk (start = end, repetition allowed) |
| Connected | There is a path between every pair of vertices |
| Component | Maximal connected subgraph |
| Complete graph K_n | Graph with n vertices, edge between every pair |
| Bipartite | Vertices split into two sets; edges only between sets |
| Type | Requirement |
|---|---|
| Euler Path | Exactly 0 or 2 vertices of odd degree |
| Euler Circuit | All vertices have even degree + connected |
| Euler Circuit (alt) | Every edge visited exactly once, starts/ends at same vertex |
| Hamilton Path | Path visiting every vertex exactly once |
| Hamilton Cycle | Cycle visiting every vertex exactly once |
| K_n has Euler Circuit | When n is odd |
| K_n has Hamilton Cycle | When n ≥ 3 |
| Concept | Definition / Theorem |
|---|---|
| Planar Graph | Can be drawn on a plane with no edge crossings |
| Euler's Formula | V - E + F = 2 (connected planar graph) |
| Max edges (V≥3) | E ≤ 3V - 6 (simple planar graph) |
| Max edges (no triangles) | E ≤ 2V - 4 (bipartite planar) |
| K_5, K_{3,3} | NOT planar (Kuratowski's theorem) |
| Chromatic Number χ(G) | Minimum colors needed so adjacent vertices differ |
| 4-Color Theorem | Any planar graph is 4-colorable |
| χ(K_n) | n (complete graph needs n colors) |
| χ(Bipartite) | 2 (any bipartite graph is 2-colorable) |
| χ(Tree) | 2 (trees are bipartite) |
| χ(Cycle C_n) | 2 if n even, 3 if n odd |
| Greedy Coloring | Upper bound: χ(G) ≤ Δ(G) + 1 (Brooks: ≤ Δ(G) for non-complete/odd-cycle) |
A tree is a connected acyclic graph with |V| - 1 edges. A spanning tree of G is a subgraph that is a tree and includes all vertices. The minimum spanning tree (MST) has the minimum total edge weight.
# ── Kruskal's Algorithm (Greedy, edges sorted by weight) ──
def kruskal(n, edges):
"""edges: list of (weight, u, v). n: number of vertices."""
parent = list(range(n))
rank = [0] * n
edges.sort() # sort by weight
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]] # path compression
x = parent[x]
return x
def union(x, y):
px, py = find(x), find(y)
if px == py: return False
if rank[px] < rank[py]: px, py = py, px
parent[py] = px
if rank[px] == rank[py]: rank[px] += 1
return True
mst = []
for w, u, v in edges:
if union(u, v):
mst.append((w, u, v))
if len(mst) == n - 1: break
return mst # total weight = sum of w
# ── Prim's Algorithm (Greedy, grow from a source) ──
import heapq
def prim(n, adj):
"""adj: adjacency list {u: [(v, weight), ...]}"""
visited = [False] * n
min_heap = [(0, 0, -1)] # (weight, node, parent)
mst = []
while min_heap:
w, u, parent = heapq.heappop(min_heap)
if visited[u]: continue
visited[u] = True
if parent != -1:
mst.append((w, parent, u))
for v, weight in adj[u]:
if not visited[v]:
heapq.heappush(min_heap, (weight, v, u))
return mst
# Kruskal: O(E log E) — good for sparse graphs
# Prim: O(E log V) with heap — good for dense graphsE ≤ 3V - 6 and the non-planarity of K_5 and K_{3,3}.| Structure | Requirements | Example |
|---|---|---|
| Set | Just a collection of elements | {0, 1, 2} |
| Semigroup | Set + associative binary operation | (N, +), ({a,b}, concat) |
| Monoid | Semigroup + identity element | (N, +, 0), (strings, concat, "") |
| Group | Monoid + every element has inverse | (Z, +), (Q*, ×) |
| Abelian Group | Group + commutative operation | (Z, +), (R, +) |
H is a subgroup of G if H \u2260 \u2205 and H is closed under the group operation and inverses.
A group G is cyclic if G = <g> for some g \u2208 G (i.e., every element is a power of g).
| Property | Statement |
|---|---|
| Generator | g generates G if every element = g^k for some k |
| Order of g | |{"<"}g{">"}| = smallest n with g^n = e |
| Classification | Every cyclic group is isomorphic to Z or Z_n |
| Subgroups | Every subgroup of a cyclic group is cyclic |
| Generator count | Z_n has φ(n) generators (Euler totient) |
| Z_n subgroups | For each divisor d of n, exactly one subgroup of order d |
| Concept | Definition |
|---|---|
| Homomorphism f: G→H | f(a * b) = f(a) * f(b) for all a, b |
| Kernel | ker(f) = {g ∈ G : f(g) = e_H} |
| Image | im(f) = {f(g) : g ∈ G} |
| Kernel is subgroup | ker(f) is a normal subgroup of G |
| First Isomorphism Thm | G / ker(f) ≅ im(f) |
| Isomorphism | Bijective homomorphism: G ≅ H |
| Normal subgroup N | gNg^{-1} = N for all g ∈ G |
| Quotient group G/N | Set of cosets with operation (aN)(bN) = abN |
A permutation is a rearrangement of elements. The symmetric group S_n is the group of all permutations of {1, 2, ..., n} under composition, with |S_n| = n!.
# ── Permutation Notation ──
# Two-line notation: σ = (1 2 3 4 5)
# (3 1 5 2 4)
# means: 1→3, 2→1, 3→5, 4→2, 5→4
# Cycle notation: σ = (1 3 5 4 2) — one 5-cycle
# σ = (1 3)(2 4) — two disjoint 2-cycles (transpositions)
# ── Permutation Properties ──
# Order of a permutation = LCM of cycle lengths
# Example: σ = (1 2 3)(4 5) — order = LCM(3,2) = 6
# Even permutation: expressible as even number of transpositions
# Odd permutation: expressible as odd number of transpositions
# Sign of σ: (-1)^(number of transpositions)
# A_n (alternating group) = set of even permutations, |A_n| = n!/2
# ── Composition of permutations ──
# Apply right permutation first, then left
# σ = (1 2 3), τ = (1 3)
# σ ∘ τ: apply τ then σ
# τ: 1→3, 2→2, 3→1
# σ: 1→2, 2→3, 3→1
# (σ ∘ τ)(1) = σ(τ(1)) = σ(3) = 1
# (σ ∘ τ)(2) = σ(τ(2)) = σ(2) = 3
# (σ ∘ τ)(3) = σ(τ(3)) = σ(1) = 2
# σ ∘ τ = (2 3)| Axiom / Rule | Statement |
|---|---|
| Axiom 1 (Non-negativity) | P(A) ≥ 0 for any event A |
| Axiom 2 (Normalization) | P(S) = 1 where S is the sample space |
| Axiom 3 (Additivity) | P(A ∪ B) = P(A) + P(B) if A ∩ B = ∅ |
| Complement Rule | P(A̅) = 1 - P(A) |
| Addition Rule | P(A ∪ B) = P(A) + P(B) - P(A ∩ B) |
| Multiplication Rule | P(A ∩ B) = P(A) × P(B|A) |
| Conditional Prob. | P(A|B) = P(A ∩ B) / P(B) |
| Independence | A ⊩ B iff P(A ∩ B) = P(A) × P(B) |
Formula: P(A|B) = P(B|A) \u00D7 P(A) / P(B) | where P(B) = Σ P(B|A_i) \u00D7 P(A_i) for all mutually exclusive A_i
# ── Bayes' Theorem: Worked Examples ──
# Example 1: Medical Testing
# Disease affects 1% of population. Test is 99% accurate.
# P(D) = 0.01, P(~D) = 0.99
# P(+|D) = 0.99 (sensitivity / true positive rate)
# P(-|~D) = 0.99 => P(+|~D) = 0.01 (false positive rate)
#
# Question: Given a positive test, what's the probability
# you actually have the disease?
#
# P(D|+) = P(+|D) * P(D) / P(+)
# P(+) = P(+|D)*P(D) + P(+|~D)*P(~D)
# = 0.99 * 0.01 + 0.01 * 0.99 = 0.0198
#
# P(D|+) = 0.0099 / 0.0198 = 0.5 = 50% !!
#
# Counterintuitive: Even with 99% accurate test,
# a positive result gives only 50% chance of disease!
# Example 2: Spam Filter
# P(Spam) = 0.3, P(Ham) = 0.7
# P("free" | Spam) = 0.8
# P("free" | Ham) = 0.1
#
# P(Spam | "free") = P("free"|Spam) * P(Spam) / P("free")
# P("free") = 0.8*0.3 + 0.1*0.7 = 0.24 + 0.07 = 0.31
# P(Spam | "free") = 0.24 / 0.31 ≈ 0.774 = 77.4%
# Example 3: Faulty Machine Parts
# Machine A produces 60% of parts, 2% defective
# Machine B produces 40% of parts, 1% defective
# P(A) = 0.6, P(B) = 0.4
# P(D|A) = 0.02, P(D|B) = 0.01
#
# P(A|D) = P(D|A)*P(A) / P(D)
# P(D) = 0.02*0.6 + 0.01*0.4 = 0.012 + 0.004 = 0.016
# P(A|D) = 0.012 / 0.016 = 0.75 = 75%| Measure | Discrete | Continuous |
|---|---|---|
| Expected Value E[X] | Σ x × P(X=x) | ∫ x × f(x) dx |
| Variance Var(X) | E[X²] - (E[X])² | E[X²] - (E[X])² |
| Std Deviation | √Var(X) | √Var(X) |
| E[aX + b] | aE[X] + b | aE[X] + b |
| Var(aX + b) | a² Var(X) | a² Var(X) |
| E[X + Y] | E[X] + E[Y] | E[X] + E[Y] (always) |
| Var(X + Y) | Var(X) + Var(Y) + 2Cov(X,Y) | Var(X) + Var(Y) + 2Cov(X,Y) |
| Type | Description | Examples |
|---|---|---|
| Discrete RV | Countable set of values | Number of heads, dice roll, # of emails |
| Continuous RV | Uncountable (real-valued) | Height, time, temperature |
| Bernoulli | 0 or 1, success/failure | Coin flip, pass/fail test |
| Indicator | Bernoulli that event occurs | I_A = 1 if A happens, 0 otherwise |
| Geometric | Trials until first success | First heads, first 6 on a die |
| Distribution | PMF/PDF | E[X] | Var(X) | Use Case |
|---|---|---|---|---|
| Binomial B(n,p) | C(n,k) p^k (1-p)^{n-k} | np | np(1-p) | n independent yes/no trials |
| Poisson Poisson(λ) | λ^k e^{-λ} / k! | λ | λ | Events in fixed interval |
| Geometric Geo(p) | (1-p)^{k-1} p | 1/p | (1-p)/p² | Trials until first success |
| Uniform U(a,b) | 1/(b-a) | (a+b)/2 | (b-a)²/12 | Equally likely outcomes |
| Normal N(μ,σ²) | (1/σ√2π) e^{-(x-μ)²/2σ²} | μ | σ² | Natural phenomena (CLT) |
| Exponential Exp(λ) | λ e^{-λx} | 1/λ | 1/λ² | Time between Poisson events |
| Hypergeometric | C(K,k)C(N-K,n-k)/C(N,n) | nK/N | ... | Sampling without replacement |
Answer: There are only n possible remainders when dividing by n: {0, 1, 2, ..., n-1}. These are our "holes." We have n+1 integers (pigeons). By the PHP, at least two integers must fall into the same remainder category. Therefore, two integers a and b exist where a \u2261 b (mod n), meaning n divides (a - b).
Extension:In any set of 101 integers, at least two must differ by a multiple of 100. In any set of 367 people, at least two share a birthday (since 366 > 365 possible birthdays).
Answer: The chromatic number χ(C_n) = 2 when n is even, and χ(C_n) = 3 when n is odd.
Proof: For even n, alternate two colors around the cycle (e.g., R,B,R,B,...). The last vertex (n) is adjacent to vertex 1 which is R, so vertex n gets B. Since n is even, this works perfectly. For odd n (say n=5: R,B,R,B,?), the 5th vertex is adjacent to both B (vertex 4) and R (vertex 1), so a third color is needed. Therefore χ(C_n) = 2 if n even, 3 if n odd.
Interview bonus: Any cycle is bipartite iff it has even length. Bipartite graphs have chromatic number 2. A graph containing an odd cycle is NOT bipartite and needs at least 3 colors.
Answer: An Euler path/circuit visits every edge exactly once. A Hamilton path/cycle visits every vertex exactly once.
Euler (efficient check): Count vertices with odd degree. 0 odd-degree vertices → Euler circuit exists. 2 odd-degree vertices → Euler path exists (between those two). More than 2 →No Euler path or circuit. Algorithm: Hierholzer's (O(V+E)).
Hamilton (NP-complete!):No efficient algorithm exists for general graphs. Sufficient conditions: Dirac's theorem (if every vertex has degree ≥ n/2 in a graph with n ≥ 3 vertices, then Hamilton cycle exists). Ore's theorem (if deg(u) + deg(v) ≥ n for all non-adjacent pairs, then Hamilton cycle exists). Common approach: backtracking with pruning.
Step 1: Students studying at least one subject (Inclusion-Exclusion):
|M \u222A P \u222A C| = |M| + |P| + |C| - |M\u2229P| - |M\u2229C| - |P\u2229C| + |M\u2229P\u2229C|
= 70 + 60 + 50 - 40 - 30 - 25 + 15 = 100
So all 100 students study at least one subject.
Step 2: Exactly one subject:
Only Math: |M| - |M\u2229P| - |M\u2229C| + |M\u2229P\u2229C| = 70 - 40 - 30 + 15 = 15
Only Physics: |P| - |M\u2229P| - |P\u2229C| + |M\u2229P\u2229C| = 60 - 40 - 25 + 15 = 10
Only Chemistry: |C| - |M\u2229C| - |P\u2229C| + |M\u2229P\u2229C| = 50 - 30 - 25 + 15 = 10
Exactly one = 15 + 10 + 10 = 35
Verification: Exactly one (35) + Exactly two (25+15+10=50) + All three (15) = 100 ✓
Answer: To go from (0,0) to (m-1, n-1), you must make exactly (m-1) right moves and (n-1) down moves, for a total of (m+n-2) moves. The number of distinct paths is:
Paths = C(m+n-2, m-1) = C(m+n-2, n-1) = (m+n-2)! / ((m-1)!(n-1)!)
Example (3\u00D74 grid = 3 rows, 4 columns):
Need 2 down moves and 3 right moves. Total = 5 moves.
Paths = C(5,2) = C(5,3) = 10
# ── Counting Grid Paths with Obstacles ──
def count_paths(m, n, obstacles=None):
"""Count paths in m×n grid with obstacles.
obstacles: set of (row, col) cells that are blocked."""
if obstacles is None:
obstacles = set()
dp = [[0] * n for _ in range(m)]
dp[0][0] = 1 if (0, 0) not in obstacles else 0
for i in range(m):
for j in range(n):
if (i, j) in obstacles:
dp[i][j] = 0
continue
if i > 0:
dp[i][j] += dp[i-1][j]
if j > 0:
dp[i][j] += dp[i][j-1]
return dp[m-1][n-1]
# Example: 3×4 grid, obstacle at (1,1)
print(count_paths(3, 4)) # 10 (no obstacles)
print(count_paths(3, 4, {(1, 1)})) # fewer paths
# ── Using Combinatorics (no obstacles) ──
from math import comb
def grid_paths(m, n):
"""C(m+n-2, m-1) paths from top-left to bottom-right."""
return comb(m + n - 2, m - 1)
print(grid_paths(3, 4)) # 10
print(grid_paths(20, 20)) # 137846528820