Skip to content

Set Theory and Mathematical Logic

🧩 Set Theory and Mathematical Logic

This module covers the fundamental building blocks of discrete mathematics: Set Theory and Propositional Logic. These concepts are the bedrock of relational databases, programming logic, and formal verification.


🟒 Part 1: Set Theory Foundations

A set is a well-defined collection of distinct objects, called elements. In computer science, sets are the formal foundation for data structures like HashSet and relational models (SQL).

1. Basic Notation and Membership

  • a∈Aa \in A: Element aa belongs to set AA.
  • aβˆ‰Aa \notin A: Element aa does not belong to set AA.
  • βˆ…\emptyset or {}\{\}: The empty set (contains no elements).
  • AβŠ†BA \subseteq B: AA is a subset of BB (every element of AA is also in BB).

2. Set Builder Notation

Used to define a set based on a property P(x)P(x): S={x∣P(x)}S = \{ x \mid P(x) \} Example: {x∣x∈Z,x>0}\{x \mid x \in \mathbb{Z}, x > 0\} represents the set of all positive integers.

3. Fundamental Set Operations

These operations have direct mappings to SQL and data processing:

  • Union (AβˆͺBA \cup B): {x∣x∈A∨x∈B}\{x \mid x \in A \lor x \in B\}. Corresponds to UNION in SQL.
  • Intersection (A∩BA \cap B): {x∣x∈A∧x∈B}\{x \mid x \in A \land x \in B\}. Corresponds to INTERSECT in SQL.
  • Difference (Aβˆ–BA \setminus B): {x∣x∈A∧xβˆ‰B}\{x \mid x \in A \land x \notin B\}. Corresponds to EXCEPT or MINUS in SQL.
  • Complement (AcA^c): {x∣xβˆ‰A,x∈U}\{x \mid x \notin A, x \in U\} where UU is the universal set.

4. Advanced Set Concepts

  • Cartesian Product (AΓ—BA \times B): The set of all possible ordered pairs (a,b)(a, b) where a∈Aa \in A and b∈Bb \in B. This defines a Cross Join in databases.
  • Power Set (P(A)\mathcal{P}(A)): The set of all subsets of AA. If ∣A∣=n|A| = n, then ∣P(A)∣=2n|\mathcal{P}(A)| = 2^n.
  • Partitions: A collection of non-empty disjoint subsets that cover the entire set. This is the basis for Database Sharding.

🟑 Part 2: Propositional Logic

Logic is the study of truth values and how they are combined. In programming, this is the foundation of if-else statements and boolean expressions.

1. Propositions and Connectives

A proposition is a statement that is either True (T) or False (F).

  • Conjunction (∧\land): AND operation.
  • Disjunction (∨\lor): OR operation.
  • Negation (Β¬\neg): NOT operation.
  • Exclusive Or (βŠ•\oplus): XOR operation (true if exactly one is true).

2. Truth Tables

ppqqp∧qp \land qp∨qp \lor qpβŠ•qp \oplus qpβ€…β€ŠβŸΉβ€…β€Šqp \implies q
TTTTFT
TFFTTF
FTFTTT
FFFFFT

3. Implications and Equivalences

  • Implication (pβ€…β€ŠβŸΉβ€…β€Šqp \implies q): β€œIf pp, then qq.” Only false if pp is true and qq is false.
  • Contrapositive (Β¬qβ€…β€ŠβŸΉβ€…β€ŠΒ¬p\neg q \implies \neg p): Logically equivalent to pβ€…β€ŠβŸΉβ€…β€Šqp \implies q.
  • Converse (qβ€…β€ŠβŸΉβ€…β€Špq \implies p): NOT necessarily equivalent to the original implication.

4. De Morgan’s Laws

Crucial for simplifying complex boolean logic in code and SQL queries:

  1. Β¬(p∧q)≑¬p∨¬q\neg(p \land q) \equiv \neg p \lor \neg q
  2. Β¬(p∨q)≑¬p∧¬q\neg(p \lor q) \equiv \neg p \land \neg q

πŸ”΄ Part 3: Predicate Logic and Quantifiers

Predicate logic extends propositional logic by allowing variables and quantifiers.

1. Predicates

A predicate P(x)P(x) is a property that a variable xx can have. It becomes a proposition once xx is assigned a value. Example: P(x):x>5P(x): x > 5. P(10)P(10) is True, P(2)P(2) is False.

2. Quantifiers

  • Universal Quantifier (βˆ€x\forall x): β€œFor all xx”. The statement must be true for every element in the domain.
  • Existential Quantifier (βˆƒx\exists x): β€œThere exists an xx”. There must be at least one element in the domain for which the statement is true.

3. Tautologies and Contradictions

  • Tautology: A statement that is always true (e.g., p∨¬pp \lor \neg p).
  • Contradiction: A statement that is always false (e.g., p∧¬pp \land \neg p).

πŸ’» Programming Example: Set Operations in Python

# Defining sets
set_a = {1, 2, 3, 4, 5}
set_b = {4, 5, 6, 7, 8}

# Union
print(f"Union: {set_a | set_b}") # {1, 2, 3, 4, 5, 6, 7, 8}

# Intersection
print(f"Intersection: {set_a & set_b}") # {4, 5}

# Difference
print(f"Difference (A-B): {set_a - set_b}") # {1, 2, 3}

# Symmetric Difference (XOR)
print(f"Symmetric Difference: {set_a ^ set_b}") # {1, 2, 3, 6, 7, 8}

Understanding these logical and set-based foundations allows you to write cleaner code and design more efficient data models.