← Math|6 of 100
Mathematics for Data Science & AI

Discrete Mathematics — The Foundation of Computing

Master discrete mathematics for computer science: logic, sets, combinatorics, graph theory, and number theory.

📂 Discrete Mathematics📖 Lesson 6 of 100🎓 Free Course

Advertisement

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:

SymbolNameRead asTruth Table
¬PNOT"not P"T→F, F→T
P ∧ QAND"P and Q"Both T → T, else F
P ∨ QOR"P or Q"Both F → F, else T
P → QIMPLIES"if P then Q"P=T, Q=F → F, else T
P ↔ QIFF"P if and only if Q"Same → T, Different → F

Important equivalences:

ℹ️ Logical Equivalences

De Morgan's Laws:

  • ¬(PQ)=¬P¬Q\neg(P \wedge Q) = \neg P \vee \neg Q
  • ¬(PQ)=¬P¬Q\neg(P \vee Q) = \neg P \wedge \neg Q

Contrapositive:

(PQ)(¬Q¬P)(P \rightarrow Q) \equiv (\neg Q \rightarrow \neg P)

Double negation:

¬¬PP\neg\neg P \equiv P

Predicate Logic (First-Order Logic)

ℹ️ Predicate Logic Components

  • Variables: x,y,zx, y, z
  • Constants: 0,1,"Alice"0, 1, \text{"Alice"}
  • Predicates: P(x),Q(x,y)P(x), Q(x,y)
  • Quantifiers:
    • xP(x)\forall x \, P(x) — "for all x, P(x) is true"
    • xP(x)\exists x \, P(x) — "there exists x such that P(x) is true"

📝Examples of Quantifiers

  • x(x+0=x)\forall x \, (x + 0 = x) — "Adding zero to any number gives itself"
  • x(x2=4)\exists x \, (x^2 = 4) — "There exists a number whose square is 4"
  • xy(x+y=0)\forall x \exists y \, (x + y = 0) — "For every x, there exists y such that x + y = 0"

Set Theory

Basic Operations

📝Set Operations

Let A={1,2,3,4}A = \{1, 2, 3, 4\} and B={3,4,5,6}B = \{3, 4, 5, 6\}

  • Union: AB={1,2,3,4,5,6}A \cup B = \{1, 2, 3, 4, 5, 6\}
  • Intersection: AB={3,4}A \cap B = \{3, 4\}
  • Difference: AB={1,2}A - B = \{1, 2\}
  • Complement: Ac={all elements not in A}A^c = \{\text{all elements not in A}\}
  • Symmetric: AB={1,2,5,6}A \triangle B = \{1, 2, 5, 6\}

Set Relationships

ℹ️ Set Relationships

  • Subset: ABA \subseteq B (every element of A is in B)
  • Proper subset: ABA \subset B (ABA \subseteq B and ABA \neq B)
  • Empty set: ={}\emptyset = \{\}
  • Power set: P(A)\mathcal{P}(A) = set of all subsets of A

Set Laws

ℹ️ Set Laws

  • Commutative: AB=BAA \cup B = B \cup A
  • Associative: (AB)C=A(BC)(A \cup B) \cup C = A \cup (B \cup C)
  • Distributive: A(BC)=(AB)(AC)A \cap (B \cup C) = (A \cap B) \cup (A \cap C)
  • Identity: A=AA \cup \emptyset = A, AU=AA \cap U = A
  • De Morgan: (AB)c=AcBc(A \cup B)^c = A^c \cap B^c

Counting and Combinatorics

Permutations (Order matters)

Permutations

P(n,r)=n!(nr)!P(n,r) = \frac{n!}{(n-r)!}

Here,

  • nn=Total items
  • rr=Items to arrange

📝Example: Permutations

How many ways to arrange 3 books from 5?

P(5,3)=5!2!=60P(5,3) = \frac{5!}{2!} = 60

Combinations (Order doesn't matter)

Combinations

C(n,r)=(nr)=n!r!(nr)!C(n,r) = \binom{n}{r} = \frac{n!}{r!(n-r)!}

Here,

  • nn=Total items
  • rr=Items to choose

📝Example: Combinations

How many ways to choose 3 books from 5?

C(5,3)=5!3!×2!=10C(5,3) = \frac{5!}{3! \times 2!} = 10

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

AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|

Here,

  • A|A|=Cardinality of set A

For three sets:

ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

Binomial Theorem

Binomial Theorem

(a+b)n=k=0n(nk)akbnk(a + b)^n = \sum_{k=0}^{n} \binom{n}{k} a^k b^{n-k}

Here,

  • nn=Exponent

📝Example: Binomial Theorem

(x+1)3=x3+3x2+3x+1(x + 1)^3 = x^3 + 3x^2 + 3x + 1

Graph Theory

Basic Concepts

DfGraph

A graph G=(V,E)G = (V, E) consists of:

  • VV = set of vertices (nodes)
  • EE = set of edges (connections)

Types of Graphs

TypeDescription
UndirectedEdges have no direction
Directed (Digraph)Edges have direction (arrows)
WeightedEdges have values
BipartiteVertices can be divided into two groups
CompleteEvery pair of vertices is connected
TreeConnected graph with no cycles

Degree

Degree

deg(v)=number of edges connected to vertex v\text{deg}(v) = \text{number of edges connected to vertex } v

Here,

  • vv=A vertex

Handshaking Lemma: deg(v)=2E\sum \text{deg}(v) = 2|E| (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: O(V+E)O(V + E)

DFS (Depth-First Search):

ℹ️ DFS

  • Explores as far as possible before backtracking
  • Uses a stack (or recursion)
  • Time: O(V+E)O(V + E)

Dijkstra's Algorithm:

ℹ️ Dijkstra's Algorithm

  • Finds shortest path in weighted graphs
  • Uses a priority queue
  • Time: O((V+E)logV)O((V + E) \log V)

Trees

Properties:

  • Connected and acyclic
  • nn vertices → n1n-1 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: O(logn)O(\log n) height (AVL, Red-Black)
  • Heap: Parent ≥ (or ≤) children

Number Theory

Modular Arithmetic

DfModular Arithmetic

ab(modn)a \equiv b \pmod{n} means nn divides (ab)(a - b)

📝Example: Modular Arithmetic

172(mod5) because 172=15=3×517 \equiv 2 \pmod{5} \text{ because } 17 - 2 = 15 = 3 \times 5

Properties:

  • (a+b)modn=((amodn)+(bmodn))modn(a + b) \bmod n = ((a \bmod n) + (b \bmod n)) \bmod n
  • (a×b)modn=((amodn)×(bmodn))modn(a \times b) \bmod n = ((a \bmod n) \times (b \bmod n)) \bmod n

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

  1. List numbers 2 to n
  2. Mark 2 as prime, cross out all multiples of 2
  3. Find next unmarked number, mark as prime, cross out its multiples
  4. Repeat until done

GCD and LCM

GCD and LCM

GCD(a,b)×LCM(a,b)=a×b\text{GCD}(a,b) \times \text{LCM}(a,b) = a \times b

Here,

  • GCD(a,b)\text{GCD}(a,b)=Greatest Common Divisor
  • LCM(a,b)\text{LCM}(a,b)=Least Common Multiple

Euclidean Algorithm for GCD:

GCD(a,b)=GCD(b,amodb)\text{GCD}(a,b) = \text{GCD}(b, a \bmod b)

Boolean Algebra

Boolean Operations

ℹ️ Boolean Operations

  • AND: 00=00 \wedge 0 = 0, 01=00 \wedge 1 = 0, 10=01 \wedge 0 = 0, 11=11 \wedge 1 = 1
  • OR: 00=00 \vee 0 = 0, 01=10 \vee 1 = 1, 10=11 \vee 0 = 1, 11=11 \vee 1 = 1
  • NOT: ¬0=1\neg 0 = 1, ¬1=0\neg 1 = 0
  • XOR: 00=00 \oplus 0 = 0, 01=10 \oplus 1 = 1, 10=11 \oplus 0 = 1, 11=01 \oplus 1 = 0

Boolean Laws

ℹ️ Boolean Laws

  • Commutative: AB=BAA \wedge B = B \wedge A
  • Associative: (AB)C=A(BC)(A \wedge B) \wedge C = A \wedge (B \wedge C)
  • Distributive: A(BC)=(AB)(AC)A \wedge (B \vee C) = (A \wedge B) \vee (A \wedge C)
  • De Morgan: ¬(AB)=¬A¬B\neg(A \wedge B) = \neg A \vee \neg B
  • Absorption: A(AB)=AA \vee (A \wedge B) = A
  • Idempotent: AA=AA \wedge A = A

Logic Gates

ℹ️ Logic Gates

  • AND gate: Output = ABA \wedge B
  • OR gate: Output = ABA \vee B
  • NOT gate: Output = ¬A\neg A
  • XOR gate: Output = ABA \oplus B
  • NAND gate: Output = ¬(AB)\neg(A \wedge B) — universal gate!
  • NOR gate: Output = ¬(AB)\neg(A \vee B) — universal gate!

Applications: Digital circuits, database queries, search engines


📋Key Takeaways

  • Logic is the language of computation. Propositional connectives (\wedge, \vee, ¬\neg, \rightarrow) and De Morgan's Laws ¬(PQ)=¬P¬Q\neg(P \wedge Q) = \neg P \vee \neg Q form the basis of Boolean circuits and query optimization.

  • Combinatorics counts possibilities efficiently. Permutations P(n,r)=n!(nr)!P(n,r) = \frac{n!}{(n-r)!} count ordered arrangements; combinations C(n,r)=n!r!(nr)!C(n,r) = \frac{n!}{r!(n-r)!} count unordered selections — essential for algorithm analysis and complexity theory.

  • Graphs model relationships. A graph G=(V,E)G = (V, E) captures nodes and edges; BFS finds shortest paths in O(V+E)O(V+E), Dijkstra's handles weighted graphs in O((V+E)logV)O((V+E)\log V), and trees guarantee unique paths between any two vertices.

  • Modular arithmetic underpins cryptography. ab(modn)a \equiv b \pmod{n} means n(ab)n \mid (a-b) — hash functions, RSA encryption, and rolling hashes all rely on modular operations.

  • The Fundamental Theorem of Arithmetic guarantees unique prime factorization. Every integer >1> 1 factors uniquely into primes — critical for number theory applications and understanding algorithmic complexity.

  • Set theory provides the foundation for data structures. Union ABA \cup B, intersection ABA \cap B, and the Inclusion-Exclusion Principle AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B| are used everywhere from database queries to probability calculations.

Lesson Progress6 / 100