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
- : Element belongs to set .
- : Element does not belong to set .
- or : The empty set (contains no elements).
- : is a subset of (every element of is also in ).
2. Set Builder Notation
Used to define a set based on a property : Example: represents the set of all positive integers.
3. Fundamental Set Operations
These operations have direct mappings to SQL and data processing:
- Union (): . Corresponds to
UNIONin SQL. - Intersection (): . Corresponds to
INTERSECTin SQL. - Difference (): . Corresponds to
EXCEPTorMINUSin SQL. - Complement (): where is the universal set.
4. Advanced Set Concepts
- Cartesian Product (): The set of all possible ordered pairs where and . This defines a Cross Join in databases.
- Power Set (): The set of all subsets of . If , then .
- 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 (): AND operation.
- Disjunction (): OR operation.
- Negation (): NOT operation.
- Exclusive Or (): XOR operation (true if exactly one is true).
2. Truth Tables
| T | T | T | T | F | T |
| T | F | F | T | T | F |
| F | T | F | T | T | T |
| F | F | F | F | F | T |
3. Implications and Equivalences
- Implication (): βIf , then .β Only false if is true and is false.
- Contrapositive (): Logically equivalent to .
- Converse (): NOT necessarily equivalent to the original implication.
4. De Morganβs Laws
Crucial for simplifying complex boolean logic in code and SQL queries:
π΄ Part 3: Predicate Logic and Quantifiers
Predicate logic extends propositional logic by allowing variables and quantifiers.
1. Predicates
A predicate is a property that a variable can have. It becomes a proposition once is assigned a value. Example: . is True, is False.
2. Quantifiers
- Universal Quantifier (): βFor all β. The statement must be true for every element in the domain.
- Existential Quantifier (): βThere exists an β. 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., ).
- Contradiction: A statement that is always false (e.g., ).
π» 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.