Relations
Relations
UNIVERSITY
SCHOOL OF ENGINEERING
DISCRETE MATHEMATICS
BY
HIMA SAXENA
Problem Statement
How do we represent relationship
between two related elements ?
Relations
Definition
Example: F: A B is a subset of A x B
Relations on a Set
A relation on a set A is represented as A to A
Example: A = {1,2,3}
R1 ={(1,1),(1,2),(2,1)}
R2 ={(1,1), (1,2), (1,3), (2,1), (2,2), (3,3)}
Example: A = {1,2,3}
R1 ={(1,1),(1,2),(2,1),(2,2)}
R1 is transitive.
Combining Relations
R1 ={(1,1),(2,2),(3,3)}
R2 ={(1,1), (1,2), (1,3), (1,4)}
S:BC
S = {(1,0), (2,0), (3,1), (3,2), (4,1)}
S o R = {(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)}
Composite Relations
Definition:
Let A1, A2, …, An be sets. An n-ary relation on these sets
is a subset of A1 x A2 x .. X An. The sets A1, A2, .., An are
domains. n is its degree.
Definition:
Projection Pi1, i2,..,im maps the n-tuple (a1,a2,..,an) to the
m-tuple (ai1,..,aim) where m<= n.
Example: P(1,3) on Students Database: <Student_Name,
Major>.
Operations on N-ary Relations
Student_Name ID_Number Major GPA
Definition:
Selection Sc maps the n-ary relation R to the n-ary relation
of all n-tuples from R that satisfy the condition C.
Example: Condition C can be Major = Computer Science.
Gives a set of n-tuples with students majoring CS.
Operations on N-ary Relations
Student_Name Address Phone Cell
Definition:
R: relation of degree m. S: relation of degree n. Join Jp(R,S)
[p <= m and p <= n], is a relation of degree m + n – p that
consists of all (m + n – p) tuples (a1, a2,..,am-p, c1, c2,..,
cp, b1, b2,…, bn-p). (a1, a2,..,am-p, c1, c2,.., cp) in R.
(c1, c2,.., cp, b1, b2,…, bn-p) in S.
Example:Join J1 on the 2 databases: Produces a database
with tuples <Student_Name, ID_Number, Major, GPA,
Address, Phone, Cell>
Representing Relation
1 if ( ai ,b j )R
mij
0 if ( ai ,b j )R
Representing Relation using Matrices
Example 1:
A = {1,2,3} B = {1,2} 0 0
Given R = {(2,1),(3,1),(3,2)} MR 1 0
Find MR? 1 1
0 1 0 0 0
Example 2:
A = {1,2,3} B = {1,2,3,4,5} Given MR 1 0 1 1 0
Find R? 1 0 1 0 1
1 0 1 1
R is symmetric if and only 1 1 0 1
if mij = mji, for all pairs of
integers i and j with M R 0 1 1 0
i = 1,2,…,n and 1 0 1 1
j = 1,2,…,n 1 1 0 1
Relation Properties using Matrices
1 0 0 1
R is antisymmetric if and
0 1 0 0
only if mij = 1 with i ≠ j,
then mji = 0 M R 1 0 1 0
1 1 0 1
0 1 1 0
Example: The relation R on a
set is given by 1 1 0
Is R reflexive, symmetric, and/or M R 111
antisymmetric ?
011
R is reflexive, symmetric and not
antisymmetric.
Relations & Matrices
1 0 1 1 0 1
M R1 1 0 0 M R 2 0 1 1
0 1 0 1 0 0
1 0 1 1 01
MR1UR2 = 1 1 1 MR1 o MR2 = 1 01
11 0 0 11
Representing Relation using Digraphs
A directed graph, or digraph, consists of a set V of
vertices together with a set E of ordered pairs of
elements of V called edges. The vertex a is call the initial
vertex of the edge (a,b), and the vertex b is called the
terminal vertex of this edge.
E
Example:
D
R = {(A,B), (A,C), (A,D), (B,D),
(C,D), (C,E), (D,E), (E,A)} C
B
A
Relation Properties using Digraphs
Relation Properties using Digraphs
A relation R is transitive if and only if whenever there is an
edge from vertex x to a vertex y and an edge from a
vertex y to a vertex z, there is an edge from x to z
Closures of Relations
Let R be a relation on a set A. R may or may not have
some property P, such as reflexivity, symmetry, or
transitivity.
If there is a relation S with property P containing R such
that S is a subset of every relation with property P
containing R, then S is called the closure of R with
respect to P.
3 types of Closures exists:
1. Reflexive Closure
2. Symmetric Closure
3. Transitive Closure
Reflexive Closure
Given a relation R on a set A is not reflexive.
Example: A = {1,2,3}
Let R = {(1,1), (1,2), (2,1), (3,2)}
Example: A = {1,2,3}
Let R = {(1,1), (1,2), (2,1), (3,2)}
0 0 1 1
1 0 0 0
MR
0 1 0 0
0 0 0 0
Transitive Closure using Matrices
0 0 1 1 0 1 0 0 1 0 0 0
1 0 0 0 0 0 1 1 0 1 0 0
M R M R[ 2 ] M R[ 3]
0 1 0 0 1 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
1 0 0 0 1 1 1 1
M R[ 4 ] M R* M R M R[ 2 ] M R[3] M R[ 4 ]
0 1 0 0 1 1 1 1
0 0 0 0 0 0 0 0
Warshall’s Algorithm
0 0 0 1 0 0 0 1 0 0 0 1
1 0 1 0 1 0 1 1 1 0 1 1
W0 M R W1 W2
1 0 0 1 1 0 0 1 1 0 0 1
0 0 1 0 0 0 1 0 0 0 1 0
0 0 01 1 011
1 0 11 1 011
W3 W4
1 0 01 1 011
1 0 1 1 1 0 11
W4 is the matrix of the transitive closure.
Equivalence Relations
A relation R on a set A is called an equivalence relation
if and only if
1. R is reflexive
2. R is symmetric, and
3. R is transitive
Example:
The congruent modulo m relation on the set of integers i.e.
{<a, b>| a Ξ b (mod m)}, where m is a positive integer
greater than 1, is an equivalence relation.
Equivalence Classes
For an equivalence relation R on a set A, the set of the
elements of A that are related to an element, say a, of A
is called the equivalence class of element a.
2. [a ] [b]
1. A A
i 1
i
2. Ai A j if Ai ≠Aj , 1≤ i, j ≤ n
Example:
A = {1, 2, 3, 4, 5}
Conversely, given a partition {Ai | i Є I} of the set S, there is an
equivalence relation R that has the sets Ai, i Є I, as its equivalence
classes.
Example: S = { 1,2,3,4,5,6}
{A1, A2, A3} are the equivalence classes of R. The pair (a, b) Є R
if and only if a and b are in the same equivalence class.
Meaning if (a, b) R or (b, a) R
It can’t be both because is antisymmetric
Unless a = b, of course
If neither a b nor b a, then a and b are incomparable
Meaning they are not related to each other
Example: (Z,≤)
is a total ordered poset (every element is comparable to
every other element)
It has no least element
Thus, it is not a well-ordered set
Consider two posets: (S,1) and (T,2)
Let s1 S and s2 S
Let t1 T and t2 T
(s1,t1) (s2,t2) if either:
s 1 1 s2
s1 = s2 and t1 2 t2
(a1, a2, a3, …, am) (b1, b2, b3, …, bn) using the
comparisons discussed
Example: “run” “set”
If m ≠ n, then let t be the minimum of m and n then a1a2a3…am,
is less than b1b2b3…bn if and only if either of the following are true:
(a1, a2, a3, …, at) (b1, b2, b3, …, bt)
Example: “run” “sets” (t = 3)
(a1, a2, a3, …, at) = (b1, b2, b3, …, bt) and m < n
Example: “run” “running”
Hasse Diagrams
Consider the directed graph for a finite poset
({1,2,3,4},≤).
Many edges in the directed graph for a finite poset do
not have to be shown since they must be present.
4 4 4 4
3 3 3 3
Hasse
Diagram
2 2 2 2
1 1 1 1
Hasse Diagrams
Example: For a poset ({1,2,3,4,5,6},|)
Maximal and Minimal Elements
Let (A, R) be a poset.
12 20
Minimal Elements : 2, 5
2 5
Least and Greatest Element
Let (A,R} be a poset.
A element a in A is the least element in A if every
element b in A, a R b.
A element a in A is the greatest element in A if every
element b in A, b R a.
c d d
b c c
h j Subsets:
a
Least Upper and Greatest Lower Bound
h j
Upper Bound : g ,h
upper bound.
a Lower Bound : a, b
Lattices
A partially ordered set in which every pair of elements has
both a least upper bound and greatest lower bound is
called a lattice.
Example:
Lattices
Example:
Topological Sorting
while ( A ≠ Φ ) {
pick a minimal element bi from A;
A := A - {bi};
i := i + 1;
output b
}
Topological Sorting
Example: Consider a poset ({1,2,4,5,12,20},|)
12 20
The algorithm selects the minimal
4 1 5 2 4 20 12.