Functions and Relations
π§© Functions and Relations
In discrete mathematics, Relations and Functions describe how elements from different sets are connected. In computer science, these concepts underpin everything from database schemas to functional programming.
π’ Part 1: Binary Relations
A binary relation from set to set is a subset of the Cartesian product .
1. Representation
- Ordered Pairs:
- Directed Graph: Nodes represent elements, and arrows represent the relationship.
- Matrix: A boolean matrix where if .
2. Properties of Relations
- Reflexive: .
- Symmetric: .
- Transitive: .
- Equivalence Relation: A relation that is reflexive, symmetric, and transitive. This is used for Data Clustering and defining equivalent states.
π‘ Part 2: Functions
A function is a special type of relation where every element in is related to exactly one element in .
1. Key Terminology
- Domain (): The set of all possible inputs.
- Codomain (): The set of all potential outputs.
- Range (): The actual set of outputs produced by the function.
2. Types of Functions (The Big Three)
Understanding these is critical for maintaining Data Integrity in databases.
- Injective (One-to-One): Each element in is mapped to by at most one element in .
- CS Context: This is the definition of a Unique Constraint or Primary Key.
- Surjective (Onto): Every element in is mapped to by at least one element in .
- CS Context: Ensuring that a lookup table covers all possible return categories.
- Bijective (One-to-One Correspondence): Both injective and surjective.
- CS Context: Necessary for Encryption/Decryption and Data Encoding.
π΄ Part 3: Composition and Inverse
1. Function Composition
. In software engineering, this is the essence of building modular systems. A complex data pipeline is simply a chain of function compositions.
2. Inverse Functions ()
Only bijective functions have an inverse. If , then . Application: Lossless data compression algorithms must be invertible.
π» Programming Example: Mapping and Injections
# A simple function (deterministic mapping)
def square(n):
return n * n
# Injective check: Are all outputs unique for these inputs?
inputs = [1, 2, 3, 4]
outputs = [square(i) for i in inputs]
is_injective = len(set(inputs)) == len(set(outputs))
print(f"Is the mapping injective for these inputs? {is_injective}")Database Analogy
- Relation: A SQL table where one user can have multiple orders.
- Function: A SQL table where each user has exactly one profile.
- Injective Function: A SQL table where each user has a unique, non-shared email address.