Skip to main content

2.1 Binary Relations, Representations & Relational Matrix Algebra

πŸ“šModule 02: Binary Relations & ClosuresTopic 2.1: Binary Relations, Representations & Matrix Algebra⏱️5 min read
🎯High-Yield For:Semester Exams β€’ GATE CSE β€’ Technical Interviews

πŸ’‘ Core Intuition: The 3-Second Mental Model​

🍳 The Everyday Analogy: The Airline Flight Route Network​

Imagine an international airline operating flights between airports:

  1. The Cartesian Product (AΓ—BA \times B): If airport set AA contains departure cities {London, Tokyo} and set BB contains arrivals {New York, Paris, Dubai}, the Cartesian product AΓ—BA \times B represents every theoretically imaginable flight path from any city in AA to any city in BB (all 2Γ—3=62 \times 3 = 6 possible routes).
  2. The Binary Relation (RR): The actual operating schedule of the airline is a subset of this theoretical maximum space. The airline might only fly (London, New York) and (Tokyo, Paris). That scheduled subset is a binary relation!
  3. The Inverse Relation (Rβˆ’1R^{-1}): The return flights! If an outbound flight flies from Tokyo to Paris, its inverse flies from Paris to Tokyo.
  4. Relational Composition (R∘SR \circ S): Connecting flights with a layover! If flight RR takes you from London to New York, and domestic flight SS takes you from New York to San Francisco, the composite relation R∘SR \circ S represents the end-to-end journey from London to San Francisco via the intermediate hub.

πŸ’» Bridging to Computer Science: Foreign Keys, Graph Databases & Adjacency Matrices​

In computer science, relations are everywhere. In relational databases (RDBMS), tables model relations, and an INNER JOIN over primary-foreign key pairs is mathematically identical to relational composition. In graph databases and network routing protocols, relations are encoded as 0-1 Boolean adjacency matrices where matrix multiplication computes multi-hop reachability in O(V3)O(V^3) time.


🌐 The Cartesian Product: Foundational Space of Ordered Pairs​

Before defining a relation, we must define the combinatorial space from which relations are drawn: the Cartesian Product.

πŸ“–Definition 2.1: The Cartesian Productdefinition

Let AA and BB be two arbitrary sets. The Cartesian product AΓ—BA \times B is the set of all ordered pairs (a,b)(a, b) such that the first coordinate belongs to AA and the second coordinate belongs to BB:

AΓ—B={(a,b)∣a∈A∧b∈B}A \times B = \{(a, b) \mid a \in A \land b \in B\}∣AΓ—B∣=∣Aβˆ£β‹…βˆ£B∣=mβ‹…n|A \times B| = |A| \cdot |B| = m \cdot n

Foundational Properties of Cartesian Products​

  1. Non-Commutative Nature: In general, the Cartesian product is not commutative: AΓ—Bβ‰ BΓ—A(unlessΒ A=BΒ orΒ A=βˆ…Β orΒ B=βˆ…)A \times B \neq B \times A \quad (\text{unless } A = B \text{ or } A = \emptyset \text{ or } B = \emptyset) Concrete Witness: Let A={1}A = \{1\} and B={2}B = \{2\}.
    • AΓ—B={(1,2)}A \times B = \{(1, 2)\}
    • BΓ—A={(2,1)}B \times A = \{(2, 1)\}
    • Since (1,2)β‰ (2,1)(1, 2) \neq (2, 1), AΓ—Bβ‰ BΓ—AA \times B \neq B \times A.
  2. Distributivity over Union and Intersection: AΓ—(BβˆͺC)=(AΓ—B)βˆͺ(AΓ—C)A \times (B \cup C) = (A \times B) \cup (A \times C) AΓ—(B∩C)=(AΓ—B)∩(AΓ—C)A \times (B \cap C) = (A \times B) \cap (A \times C)
  3. Monotonicity under Subsets: AβŠ†B∧CβŠ†Dβ€…β€ŠβŸΉβ€…β€Š(AΓ—C)βŠ†(BΓ—D)A \subseteq B \land C \subseteq D \implies (A \times C) \subseteq (B \times D)

πŸ”— Binary Relations: Subsets of the Cartesian Universe​

A binary relation from set AA to set BB is formally defined as any subset of their Cartesian product.

πŸ“–Definition 2.2: Binary Relation & Total Combinationsdefinition

A relation RR from AA to BB is a subset of AΓ—BA \times B:

RβŠ†AΓ—Bβ€…β€ŠβŸΊβ€…β€ŠR∈P(AΓ—B)R \subseteq A \times B \iff R \in \mathcal{P}(A \times B)

If (a,b)∈R(a, b) \in R, we write a R ba \, R \, b (read: "aa is related to bb by RR"). If (a,b)βˆ‰R(a, b) \notin R, we write aR̸ ba \not{R} \, b.

If ∣A∣=m|A| = m and ∣B∣=n|B| = n, the total number of distinct relations from AA to BB is:

Nrelations=2∣AΓ—B∣=2mnN_{\text{relations}} = 2^{|A \times B|} = 2^{mn}

Binary Relations on a Single Set​

When A=BA = B, a subset RβŠ†AΓ—AR \subseteq A \times A is called a binary relation on set AA.

πŸ“Formula 2.1: Relations on a Set of Size nformula

For a set AA containing ∣A∣=n|A| = n elements:

  1. The Cartesian product has cardinality ∣AΓ—A∣=n2|A \times A| = n^2.
  2. The total number of possible binary relations on AA is: NbinaryΒ relations=2n2N_{\text{binary relations}} = 2^{n^2}
  3. Smallest Relation: The empty relation βˆ…\emptyset (βˆ£βˆ…βˆ£=0|\emptyset| = 0).
  4. Largest Relation: The universal relation AΓ—AA \times A (∣AΓ—A∣=n2|A \times A| = n^2).
  5. Identity Relation: The diagonal relation Ξ”A={(x,x)∣x∈A}\Delta_A = \{(x, x) \mid x \in A\} (βˆ£Ξ”A∣=n|\Delta_A| = n).

πŸ“Š Representations of Relations: Roster, Matrix & Digraph​

To analyze and compute over relations efficiently, computer systems utilize three representations.

Matrix Representation (M_R) vs Directed Graph (Digraph)

Comparing computational bit-level matrix storage against visual structural graphs

Matrix Form

0-1 Boolean Matrix (M_R)

πŸ”’
Dominant Architecture / DomainComputer Memory & Vectorized Bitmasks
  • β€’A binary matrix of size m Γ— n where rows represent A and columns represent B.
  • β€’M_R[i, j] = 1 if (a_i, b_j) ∈ R, and 0 if (a_i, b_j) βˆ‰ R.
  • β€’Enables O(1) membership lookup: check index M_R[i, j].
  • β€’Computes relational composition via hardware-accelerated Boolean matrix multiplication.
"Ideal for algorithmic graph engines, bitsets, and transitive closure computation."
Digraph Form

Directed Graph G = (V, E)

πŸ•ΈοΈ
Dominant Architecture / DomainVisual Topology & Network Traversals
  • β€’Vertices V represent the elements of set A.
  • β€’A directed edge (arrow) extends from vertex a to vertex b if and only if (a, b) ∈ R.
  • β€’Self-loops (a β†’ a) immediately reveal diagonal reflexive pairs.
  • β€’Bidirectional arrows (a ⇄ b) visually expose symmetric pairings.
"Ideal for human cognitive intuition, path checking, and cycle detection."

1. Roster Form​

Explicit enumeration of all participating ordered pairs inside curly braces:

R={(1,2),β€…β€Š(1,3),β€…β€Š(2,3),β€…β€Š(3,1)}R = \{(1, 2), \; (1, 3), \; (2, 3), \; (3, 1)\}

2. Zero-One Boolean Matrix​

For A={1,2,3}A = \{1, 2, 3\} and R={(1,1),(1,3),(2,2),(3,1),(3,2)}R = \{(1, 1), (1, 3), (2, 2), (3, 1), (3, 2)\}:

MR=(101010110)M_R = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 1 & 0 \end{pmatrix}
  • The entry at Row 1, Column 3 is 11 because (1,3)∈R(1, 3) \in R.
  • The entry at Row 2, Column 1 is 00 because (2,1)βˆ‰R(2, 1) \notin R.

πŸ”„ Relation Complement, Inverse & Dual Invariants​

Like ordinary sets, relations support complementation and inversion operations.

πŸ“–Definition 2.3: Complement of a Relationdefinition

Let RβŠ†AΓ—BR \subseteq A \times B. The complement of relation RR, denoted by RcR^c or Rβ€²R' or Rβ€Ύ\overline{R}, consists of all ordered pairs in AΓ—BA \times B that do not belong to RR:

Rc=(AΓ—B)βˆ’R={(a,b)∈AΓ—B∣(a,b)βˆ‰R}R^c = (A \times B) - R = \{(a, b) \in A \times B \mid (a, b) \notin R\}∣R∣+∣Rc∣=∣AΓ—B∣=mn|R| + |R^c| = |A \times B| = mnRβˆͺRc=AΓ—BandR∩Rc=βˆ…R \cup R^c = A \times B \qquad \text{and} \qquad R \cap R^c = \emptyset

In Boolean matrix form, the complement inverts every bit: MRc[i,j]=1βˆ’MR[i,j]M_{R^c}[i, j] = 1 - M_R[i, j].

πŸ“–Definition 2.4: Inverse of a Relationdefinition

Let RβŠ†AΓ—BR \subseteq A \times B. The inverse of relation RR, denoted by Rβˆ’1R^{-1}, is the relation from BB to AA formed by reversing the coordinates of every ordered pair in RR:

Rβˆ’1={(b,a)∈BΓ—A∣(a,b)∈R}R^{-1} = \{(b, a) \in B \times A \mid (a, b) \in R\}∣Rβˆ’1∣=∣R∣and(Rβˆ’1)βˆ’1=R|R^{-1}| = |R| \qquad \text{and} \qquad (R^{-1})^{-1} = R

In Boolean matrix form, inversion corresponds exactly to the matrix transpose:

MRβˆ’1=(MR)TM_{R^{-1}} = (M_R)^T

⚑ Composition of Relations & Boolean Matrix Multiplication​

The most powerful algebraic operation over relations is composition, which chains relationships across an intermediate bridge set.

Multi-Hop Relational Composition Pipeline (R ∘ S)

Tracing path existence across intermediate set B to establish direct composite linkage

Domain Set A
Bridge Set B
Codomain Set C
Composite Link (A β†’ C)
1
Domain Set A→Bridge Set B

Relation R Linkage: (a, b) ∈ R

2
Bridge Set B→Codomain Set C

Relation S Linkage: (b, c) ∈ S

3
Domain Set A→Composite Link (A → C)

Composite Formation: (a, c) ∈ (R ∘ S)

πŸ“–Definition 2.5: Relational Composition (R ∘ S)definition

Let RβŠ†AΓ—BR \subseteq A \times B and SβŠ†BΓ—CS \subseteq B \times C. The composite relation R∘SR \circ S (also written S∘RS \circ R in some functional notations; here we evaluate left-to-right: RR first, then SS) is a relation from AA to CC defined by:

R∘S={(a,c)∈AΓ—Cβˆ£βˆƒb∈BΒ suchΒ thatΒ (a,b)∈R∧(b,c)∈S}R \circ S = \{(a, c) \in A \times C \mid \exists b \in B \text{ such that } (a, b) \in R \land (b, c) \in S\}

Computing Composition via Boolean Matrix Product​

The matrix of the composite relation MR∘SM_{R \circ S} is the Boolean matrix product of MRM_R and MSM_S (denoted MRβŠ™MSM_R \odot M_S). In Boolean matrix arithmetic, addition is logical OR (∨\lor) and multiplication is logical AND (∧\land):

πŸ“Formula 2.2: Boolean Matrix Multiplication for Compositionformula
MR∘S[i,j]=⋁k=1∣B∣(MR[i,k]∧MS[k,j])M_{R \circ S}[i, j] = \bigvee_{k=1}^{|B|} \Big( M_R[i, k] \land M_S[k, j] \Big)

An entry MR∘S[i,j]=1M_{R \circ S}[i, j] = 1 if and only if there exists at least one index kk such that row ii of MRM_R and column jj of MSM_S both have a 11.

Foundational Invariants of Relational Composition​

  1. Associativity: (R∘S)∘T=R∘(S∘T)(R \circ S) \circ T = R \circ (S \circ T)
  2. Non-Commutative: In general, R∘Sβ‰ S∘RR \circ S \neq S \circ R.
  3. Reversal Law for Inverses: (R∘S)βˆ’1=Sβˆ’1∘Rβˆ’1(R \circ S)^{-1} = S^{-1} \circ R^{-1}

🌍 Real-World Engineering Context​

  1. Database Relational Calculus & SQL JOINs: In SQL, an INNER JOIN matches rows where TableA.user_id = TableB.user_id. This query operation is literally computing the relational composition of two relations R1βŠ†OrdersΓ—UsersR_1 \subseteq \text{Orders} \times \text{Users} and R2βŠ†UsersΓ—AddressesR_2 \subseteq \text{Users} \times \text{Addresses}.
  2. Access Control Matrices (RBAC): In cloud platforms, authorization uses composition:
    • Relation R1R_1: Maps Users to Roles (User β†’ Role).
    • Relation R2R_2: Maps Roles to Permissions (Role β†’ Permission).
    • The composite relation R1∘R2R_1 \circ R_2 computes effective user permissions (User β†’ Permission).
  3. Network Routing & Reachability: In IP networks, routing tables maintain binary adjacency relations. Squaring the adjacency matrix (MRβŠ™MRM_R \odot M_R) yields all 2-hop reachable routers. Higher powers MRkM_R^k compute all kk-hop reachable nodes.

πŸ“ Step-by-Step Worked Examples​

Example 1: Cartesian Product and Subset Counting​

Problem Statement:
Let A={1,2,3}A = \{1, 2, 3\} and B={x,y}B = \{x, y\}.

  1. List all elements of AΓ—BA \times B in roster form.
  2. Determine the total number of binary relations from AA to BB.
  3. Determine how many of these relations contain exactly 3 ordered pairs.

Step-by-Step Solution:

  1. Constructing AΓ—BA \times B: AΓ—B={(1,x),β€…β€Š(1,y),β€…β€Š(2,x),β€…β€Š(2,y),β€…β€Š(3,x),β€…β€Š(3,y)}A \times B = \{(1, x), \; (1, y), \; (2, x), \; (2, y), \; (3, x), \; (3, y)\} Cardinality: ∣AΓ—B∣=∣Aβˆ£β‹…βˆ£B∣=3Γ—2=6|A \times B| = |A| \cdot |B| = 3 \times 2 = 6.
  2. Total Number of Relations: Each relation is a subset of AΓ—BA \times B. By the power set formula: Nrelations=2∣AΓ—B∣=26=64N_{\text{relations}} = 2^{|A \times B|} = 2^6 = 64
  3. Relations Containing Exactly 3 Pairs: Selecting any 3 pairs out of the 6 available: (63)=6Γ—5Γ—43Γ—2Γ—1=20Β relations\binom{6}{3} = \frac{6 \times 5 \times 4}{3 \times 2 \times 1} = 20 \text{ relations}

Key Takeaway:
Counting relations with specific cardinality bounds corresponds to choosing subsets of the Cartesian product using binomial coefficients (∣AΓ—B∣k)\binom{|A \times B|}{k}.


Example 2: Matrix Representation and Inverse Derivation​

Problem Statement:
Let A={1,2,3}A = \{1, 2, 3\} and R={(1,2),(1,3),(2,1),(3,2)}R = \{(1, 2), (1, 3), (2, 1), (3, 2)\}.

  1. Construct the 0-1 Boolean matrix MRM_R.
  2. Derive the matrix MRβˆ’1M_{R^{-1}} and express Rβˆ’1R^{-1} in roster form.
  3. Compute the complement relation RcR^c and state its cardinality.

Step-by-Step Solution:

  1. Constructing MRM_R: Rows and columns correspond to index order [1,2,3][1, 2, 3]:
    • Row 1: (1,1)βˆ‰Rβ†’0(1, 1) \notin R \to 0, (1,2)∈Rβ†’1(1, 2) \in R \to 1, (1,3)∈Rβ†’1β€…β€ŠβŸΉβ€…β€Š[0,1,1](1, 3) \in R \to 1 \implies [0, 1, 1]
    • Row 2: (2,1)∈Rβ†’1(2, 1) \in R \to 1, (2,2)βˆ‰Rβ†’0(2, 2) \notin R \to 0, (2,3)βˆ‰Rβ†’0β€…β€ŠβŸΉβ€…β€Š[1,0,0](2, 3) \notin R \to 0 \implies [1, 0, 0]
    • Row 3: (3,1)βˆ‰Rβ†’0(3, 1) \notin R \to 0, (3,2)∈Rβ†’1(3, 2) \in R \to 1, (3,3)βˆ‰Rβ†’0β€…β€ŠβŸΉβ€…β€Š[0,1,0](3, 3) \notin R \to 0 \implies [0, 1, 0]
    MR=(011100010)M_R = \begin{pmatrix} 0 & 1 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{pmatrix}
  2. Deriving MRβˆ’1M_{R^{-1}} via Transpose: Transposing rows into columns: MRβˆ’1=(MR)T=(010101100)M_{R^{-1}} = (M_R)^T = \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix} Converting back to roster form: Rβˆ’1={(2,1),β€…β€Š(3,1),β€…β€Š(1,2),β€…β€Š(2,3)}R^{-1} = \{(2, 1), \; (3, 1), \; (1, 2), \; (2, 3)\}
  3. Computing the Complement RcR^c: ∣AΓ—A∣=32=9|A \times A| = 3^2 = 9. Since ∣R∣=4|R| = 4, ∣Rc∣=9βˆ’4=5|R^c| = 9 - 4 = 5. Inverting the zeros in MRM_R: Rc={(1,1),β€…β€Š(2,2),β€…β€Š(2,3),β€…β€Š(3,1),β€…β€Š(3,3)}R^c = \{(1, 1), \; (2, 2), \; (2, 3), \; (3, 1), \; (3, 3)\}

Key Takeaway:
The inverse relation corresponds directly to matrix transposition ((MR)T(M_R)^T), preserving cardinality while flipping directional flow.


Example 3: Relational Composition via Boolean Matrix Multiplication​

Problem Statement:
Let A={1,2,3}A = \{1, 2, 3\}, and let relations RR and SS on AA be defined by:

  • R={(1,2),(2,3),(3,1)}R = \{(1, 2), (2, 3), (3, 1)\}
  • S={(1,1),(2,3),(3,2)}S = \{(1, 1), (2, 3), (3, 2)\}

Compute the composite relation R∘SR \circ S using Boolean matrix multiplication.

Step-by-Step Solution:

  1. Construct Matrices MRM_R and MSM_S: MR=(010001100),MS=(100001010)M_R = \begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix}, \qquad M_S = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix}
  2. Compute Boolean Matrix Product MR∘S=MRβŠ™MSM_{R \circ S} = M_R \odot M_S:
    • Row 1:
      • Col 1: (0∧1)∨(1∧0)∨(0∧0)=0(0 \land 1) \lor (1 \land 0) \lor (0 \land 0) = 0
      • Col 2: (0∧0)∨(1∧0)∨(0∧1)=0(0 \land 0) \lor (1 \land 0) \lor (0 \land 1) = 0
      • Col 3: (0∧0)∨(1∧1)∨(0∧0)=1(0 \land 0) \lor (1 \land 1) \lor (0 \land 0) = 1
      • β€…β€ŠβŸΉβ€…β€Š[0,0,1]\implies [0, 0, 1]
    • Row 2:
      • Col 1: (0∧1)∨(0∧0)∨(1∧0)=0(0 \land 1) \lor (0 \land 0) \lor (1 \land 0) = 0
      • Col 2: (0∧0)∨(0∧0)∨(1∧1)=1(0 \land 0) \lor (0 \land 0) \lor (1 \land 1) = 1
      • Col 3: (0∧0)∨(0∧1)∨(1∧0)=0(0 \land 0) \lor (0 \land 1) \lor (1 \land 0) = 0
      • β€…β€ŠβŸΉβ€…β€Š[0,1,0]\implies [0, 1, 0]
    • Row 3:
      • Col 1: (1∧1)∨(0∧0)∨(0∧0)=1(1 \land 1) \lor (0 \land 0) \lor (0 \land 0) = 1
      • Col 2: (1∧0)∨(0∧0)∨(0∧1)=0(1 \land 0) \lor (0 \land 0) \lor (0 \land 1) = 0
      • Col 3: (1∧0)∨(0∧1)∨(0∧0)=0(1 \land 0) \lor (0 \land 1) \lor (0 \land 0) = 0
      • β€…β€ŠβŸΉβ€…β€Š[1,0,0]\implies [1, 0, 0]
    MR∘S=(001010100)M_{R \circ S} = \begin{pmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{pmatrix}
  3. Convert Matrix to Roster Pairs: R∘S={(1,3),β€…β€Š(2,2),β€…β€Š(3,1)}R \circ S = \{(1, 3), \; (2, 2), \; (3, 1)\} Verification via direct chaining:
    • (1,2)∈R(1, 2) \in R and (2,3)∈Sβ€…β€ŠβŸΉβ€…β€Š(1,3)∈R∘S(2, 3) \in S \implies (1, 3) \in R \circ S
    • (2,3)∈R(2, 3) \in R and (3,2)∈Sβ€…β€ŠβŸΉβ€…β€Š(2,2)∈R∘S(3, 2) \in S \implies (2, 2) \in R \circ S
    • (3,1)∈R(3, 1) \in R and (1,1)∈Sβ€…β€ŠβŸΉβ€…β€Š(3,1)∈R∘S(1, 1) \in S \implies (3, 1) \in R \circ S Both methods match.

Key Takeaway:
Boolean matrix multiplication implements relational composition without manual combinatorial backtracking across intermediate elements.


🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions

Question 1: "Why is the Cartesian product A Γ— B not commutative, while |A Γ— B| = |B Γ— A| is commutative?"
Key Focus Points:

  • Ordered Pair Asymmetry: The elements of AΓ—BA \times B are ordered pairs (a,b)(a, b). In general, (a,b)β‰ (b,a)(a, b) \neq (b, a) unless a=ba = b. Thus the set of ordered pairs is structurally distinct.
  • Scalar Cardinality: The cardinality is the arithmetic product of two non-negative integers ∣Aβˆ£β‹…βˆ£B∣|A| \cdot |B|. Scalar multiplication over W\mathbb{W} is commutative (mβ‹…n=nβ‹…mm \cdot n = n \cdot m).
  • Implication: AΓ—BA \times B and BΓ—AB \times A are equivalent sets (∣AΓ—B∣=∣BΓ—A∣|A \times B| = |B \times A|), but not equal sets.

Question 2: "What is the reversal law for relational composition and why does it hold?"
Key Focus Points:

  • Formula: (R∘S)βˆ’1=Sβˆ’1∘Rβˆ’1(R \circ S)^{-1} = S^{-1} \circ R^{-1}. Notice the order of relations is reversed!
  • Path Reversal Intuition: If a path goes from aβ†’Rbβ†’Sca \xrightarrow{R} b \xrightarrow{S} c, then traveling backward from cc to aa requires first stepping backward through SS (cβ†’Sβˆ’1bc \xrightarrow{S^{-1}} b), then stepping backward through RR (bβ†’Rβˆ’1ab \xrightarrow{R^{-1}} a).
Common Interview Traps: Architectural Trade-Offs

Trap 1: Is a relation from A to B the same as a relation from B to A?
Answer: No. A relation from AA to BB is a subset of AΓ—BA \times B, while a relation from BB to AA is a subset of BΓ—AB \times A. They represent completely different directional spaces unless A=BA = B.

Trap 2: Does an empty set have any relations?
Answer: Yes, exactly one! If A=βˆ…A = \emptyset, then AΓ—A=βˆ…Γ—βˆ…=βˆ…A \times A = \emptyset \times \emptyset = \emptyset. The number of relations on βˆ…\emptyset is 202=20=12^{0^2} = 2^0 = 1. The only relation is the empty relation βˆ…\emptyset itself.


πŸ’¬

Discussion & Doubts