Discrete Mathematics — The Foundation of Computing
ℹ️ Why It Matters
Computer science is built on discrete math. Algorithms, data structures, complexity theory, cryptography, and logic all rely on discrete mathematics.
Logic
Propositional Logic
Basic connectives:
| Symbol | Name | Read as | Truth Table |
|---|---|---|---|
| ¬P | NOT | "not P" | T→F, F→T |
| P ∧ Q | AND | "P and Q" | Both T → T, else F |
| P ∨ Q | OR | "P or Q" | Both F → F, else T |
| P → Q | IMPLIES | "if P then Q" | P=T, Q=F → F, else T |
| P ↔ Q | IFF | "P if and only if Q" | Same → T, Different → F |
Important equivalences:
ℹ️ Logical Equivalences
De Morgan's Laws:
Contrapositive:
Double negation:
Predicate Logic (First-Order Logic)
ℹ️ Predicate Logic Components
- Variables:
- Constants:
- Predicates:
- Quantifiers:
- — "for all x, P(x) is true"
- — "there exists x such that P(x) is true"
📝Examples of Quantifiers
- — "Adding zero to any number gives itself"
- — "There exists a number whose square is 4"
- — "For every x, there exists y such that x + y = 0"
Set Theory
Basic Operations
📝Set Operations
Let and
- Union:
- Intersection:
- Difference:
- Complement:
- Symmetric:
Set Relationships
ℹ️ Set Relationships
- Subset: (every element of A is in B)
- Proper subset: ( and )
- Empty set:
- Power set: = set of all subsets of A
Set Laws
ℹ️ Set Laws
- Commutative:
- Associative:
- Distributive:
- Identity: ,
- De Morgan:
Counting and Combinatorics
Permutations (Order matters)
Permutations
Here,
- =Total items
- =Items to arrange
📝Example: Permutations
How many ways to arrange 3 books from 5?
Combinations (Order doesn't matter)
Combinations
Here,
- =Total items
- =Items to choose
📝Example: Combinations
How many ways to choose 3 books from 5?
Important Counting Principles
Addition Principle: If task A can be done in m ways and task B in n ways, and they can't both be done, total = m + n
Multiplication Principle: If task A can be done in m ways and task B in n ways, total = m × n
Inclusion-Exclusion:
Inclusion-Exclusion
Here,
- =Cardinality of set A
For three sets:
Binomial Theorem
Binomial Theorem
Here,
- =Exponent
📝Example: Binomial Theorem
Graph Theory
Basic Concepts
DfGraph
A graph consists of:
- = set of vertices (nodes)
- = set of edges (connections)
Types of Graphs
| Type | Description |
|---|---|
| Undirected | Edges have no direction |
| Directed (Digraph) | Edges have direction (arrows) |
| Weighted | Edges have values |
| Bipartite | Vertices can be divided into two groups |
| Complete | Every pair of vertices is connected |
| Tree | Connected graph with no cycles |
Degree
Degree
Here,
- =A vertex
Handshaking Lemma: (Sum of all degrees = twice the number of edges)
Paths and Cycles
ℹ️ Path and Cycle Definitions
- Path: A sequence of edges connecting two vertices
- Cycle: A path that starts and ends at the same vertex
- Eulerian circuit: A cycle that visits every edge exactly once
- Hamiltonian path: A path that visits every vertex exactly once
Graph Algorithms
BFS (Breadth-First Search):
ℹ️ BFS
- Explores level by level
- Uses a queue
- Finds shortest path in unweighted graphs
- Time:
DFS (Depth-First Search):
ℹ️ DFS
- Explores as far as possible before backtracking
- Uses a stack (or recursion)
- Time:
Dijkstra's Algorithm:
ℹ️ Dijkstra's Algorithm
- Finds shortest path in weighted graphs
- Uses a priority queue
- Time:
Trees
Properties:
- Connected and acyclic
- vertices → edges
- Unique path between any two vertices
Types:
- Binary tree: Each node has at most 2 children
- Binary search tree: Left < Root < Right
- Balanced tree: height (AVL, Red-Black)
- Heap: Parent ≥ (or ≤) children
Number Theory
Modular Arithmetic
DfModular Arithmetic
means divides
📝Example: Modular Arithmetic
Properties:
Applications:
- Hash functions
- Cryptography (RSA)
- Rolling hash (Rabin-Karp algorithm)
Prime Numbers
DfPrime Number
A number > 1 that has no divisors other than 1 and itself.
ThFundamental Theorem of Arithmetic
Every integer > 1 can be uniquely factored into primes.
Sieve of Eratosthenes: Find all primes up to n
ℹ️ Sieve Algorithm
- List numbers 2 to n
- Mark 2 as prime, cross out all multiples of 2
- Find next unmarked number, mark as prime, cross out its multiples
- Repeat until done
GCD and LCM
GCD and LCM
Here,
- =Greatest Common Divisor
- =Least Common Multiple
Euclidean Algorithm for GCD:
Boolean Algebra
Boolean Operations
ℹ️ Boolean Operations
- AND: , , ,
- OR: , , ,
- NOT: ,
- XOR: , , ,
Boolean Laws
ℹ️ Boolean Laws
- Commutative:
- Associative:
- Distributive:
- De Morgan:
- Absorption:
- Idempotent:
Logic Gates
ℹ️ Logic Gates
- AND gate: Output =
- OR gate: Output =
- NOT gate: Output =
- XOR gate: Output =
- NAND gate: Output = — universal gate!
- NOR gate: Output = — universal gate!
Applications: Digital circuits, database queries, search engines
📋Key Takeaways
-
Logic is the language of computation. Propositional connectives (, , , ) and De Morgan's Laws form the basis of Boolean circuits and query optimization.
-
Combinatorics counts possibilities efficiently. Permutations count ordered arrangements; combinations count unordered selections — essential for algorithm analysis and complexity theory.
-
Graphs model relationships. A graph captures nodes and edges; BFS finds shortest paths in , Dijkstra's handles weighted graphs in , and trees guarantee unique paths between any two vertices.
-
Modular arithmetic underpins cryptography. means — hash functions, RSA encryption, and rolling hashes all rely on modular operations.
-
The Fundamental Theorem of Arithmetic guarantees unique prime factorization. Every integer factors uniquely into primes — critical for number theory applications and understanding algorithmic complexity.
-
Set theory provides the foundation for data structures. Union , intersection , and the Inclusion-Exclusion Principle are used everywhere from database queries to probability calculations.