Title: Computing Fundamentals 1 Lecture 7 Relations
1Computing Fundamentals 1 Lecture 7Relations
- Lecturer Patrick Browne
- http//www.comp.dit.ie/pbrowne/
- Room K308
- Based on Chapter 14.
- A Logical approach to Discrete Math
- By David Gries and Fred B. Schneider
2Relations and Functions
- Tuples are record like structure e.g. ltname,agegt
- Cross products represent the types (or sorts in
CafeOBJ) in a tuple e.g. String ? Nat - Relations are relationships between objects.
- Functions are relations with some special
properties.
3Tuples1
- Given expressions b and c then ?b,c? is called an
ordered pair. Well known ordered pairs are ?x,y?
coordinates in the plane, ?name,address?. - Axiom, Pair equality
- ?b,c? ?b,c? ? b b ? c c
- A 2-tuple is called an ordered pair.
4Cross Products
- The Cross product (or Cartesian product) S?T of
two sets S and T is the set of all ordered pairs
?b,c? such that b is in S and c is in T. - Axiom Cross product (used in later proof)
- S?T b,c b?S ? c?T ?b,c?
- Cross products can be extended to n sets
producing n-tuples.
5Cross Products
- A 2-tuple
- S 2,5
- T 1,2,3
- S?T ?2,1?,?2,2?,?2,3?,?5,1?,?5,2?,?5,3?
6Cross Products()
- Example of a 2-tuple
- S Mary,John
- T John,Bill,Brent
- S?T ?Mary, John?,? Mary,Bill ?,
- ?Mary,Brent ?,? John, John ?,
- ?John,Bill ?,? John,Brent?
7Cross Products Example()
- SxT / TXS
- S Mary,John
- T John,Bill,Brent
- T?S ltJohn,Marygt,ltJohn,Johngt,ltBill,Marygt,ltBill,J
ohngt, ltBrent,Marygt,ltBrent,Johngt
8Some Theorems for Cross products
- Membership
- ?x,y? ? S?T ? x?S ? y?T
- Distributivity of ? over ?
- S ? (T?U) ? (S ? T) ? (S ? U)
- (S ? T) ? U ? (S ? U) ? (T ? U)
- Monotonicity
- T ? U ? (S ? T) ? (S ? U)
9Relations
- A relation is a subset of a cross product B1? B2
? ? Bn - A binary relation over B?C is a subset of B?C.
- It is possible to have a relation on B?B.
- Well know relation lessThan(i,j).
- Difficult to enumerate (or list), so use
- i,j ? j-i is positive ?i,j?
10Relations
- A relation can be represented by
- A Set of ordered pairs
- A table e.g. a tuple in a database
- A directed graph
- A set comprehension a rule based description
similar to the quantifications of Chapter 8 - Two notations for membership of relation ?
- ?b,c? ? ? (set of pairs)
- b ? c (infix, conjunctional)
11Relation as Directed Graph
One vertex for each element of the set. There is
a directed edge from each vertex b to vertex c
iff ltb,cgt is in the binary relation.
2
1
3
6
4
5
- Set 1,2,3,4,5,6
- Relation lt1,1gt,lt1,3gt,lt2,5gt,lt2,1gt, lt5,3gt
12Diagraph of Relations
- Given the relation
- R lt1,2gt,lt2,1gt,lt3,3gt,lt1,1gt,lt2,2gt
- On the set
- 1,2,3
- Below is the digraph of R.
1
2
3
13Digraph
- Here are digraphs of some relations
- P lt1, 4gt ,lt2,1gt, lt3, 2gt
- Q lt1, 4gt ,lt2, 4gt
2
2
1
1
3
4
4
14Digraphs
- Here are digraphs of some relations
- (c) R lt1, 4gt, lt2, 3gt, lt3, 3gt,lt2, 1gt
- (d) S lt3, 1gt ,lt1, 2gt ,lt2, 3gt
2
2
1
1
3
3
4
15Relations on Sets
- The relation R on 1,2,3,4 is defined by ltx,ygt ?
R if x2?y . - The relation R can be written as a set of ordered
pairs - R lt1,1gt,lt2,1gt,lt3,1gt,lt4,1gt,lt2,2gt,lt3,2gt,lt4,2gt,lt2
,3gt,lt3,3gt,lt4,3gt,lt2,4gt,lt3,4gt,lt4,4gt
16Some Relations
- The empty relation on S?T is the empty set ?.
- The identity relation iB on B is
- x x ? B ?x,x?
- The relation parent, b is a parent of c.
- The relation predecessor (pred(x)).
- The relation square root (see book1).
- The algorithm relation between input and output
state.
17Relations
- Lower case Greek letters represent relations.
- The domain, Dom.?, and range Ran.? of a relation
? on B?C are defined as - Dom.? bB (?c b ? c)
- Ran.? cC (?b b ? c)
18Relations
?
Dom
Ran
B
C
Dom.? bB (?c b ? c) Ran.? cC
(?b b ? c)
19Operations on Relations
- Let ? be a relation on B?C and ? be a relation
C?D. The product (or composition) of ? and ?
denoted ? ? ? is defined by - ?b,d? ? ??? ? (?c c?C ?b,c??? ? ?c,d???)
???
d
b
c
?
?
D
B
C
Alternative notation b(???)d holds iff b?c?d
holds for some c.
20Composing Relations
21Powers of a relation
- For example parent?parent can be written parent2.
- ?0 iB (the identity relation on B)
- ?n1 ?n ? ? (for n?0)
- ?nm ?n ? ?m
- ?n?m (?n )m
22Classes of relations
- Name Property
- Reflexive (?b b ? b)
- Irreflexive (?b ?(b ? b))
- Symmetric (?b,c (b ? c) ? (c ? b))
23Classes of relations
- Antisymmetric
- (?b,c (b ? c) ? (c ? b) ? bc)
- Asymmetric (non-symmetric, see notes section)
- (?b,c (b ? c) ? ?(c ? b))
- Transitive
- (?b,c,d (b ? c) ? (c ? d) ? b ? d)
24Relations Discussion
- ? is a relation is on a set, A 1,2,3
- For ? to be either reflexive and irreflexive it
must hold for all of A. - For ? to be reflexive then ? must contain the
following ordered pairs - lt1,1gt,lt2,2gt,lt3,3gt
- The relation lt (less than) is irrflexive.
25Relations Discussion
- Consider the relation square
- b square c iff b c c
- square is not reflexive because it does not
contain lt2,2gt - square is not irreflexive because it does
contains pair lt1,1gt - Thus square neither reflexive or irrflexive. A
relation that is not reflexive need not be
irreflexive.
26Relations Discussion
- For symmetric relations ? does not have to hold
for all of A, (b ? c) need only hold where (c ?
b) holds and visa versa. - The relation ? on 1,2,3
- ? lt1,2gt,lt2,1gt,lt1,3gt
- is neither symmetric nor antisymmetric.
- It is not symmetric because there is no lt3,1gt to
match the (1,3). - It is not antisymmetric because it has both lt2,1gt
and lt1,2gt, but 1?2.
27Relations Discussion
- The subset (? or ?) relation is antisymmetric.
If A and B are two sets, and if A is a subset of
B and B is a subset of A then AB. - (A ? B) ? (B ? C) ? A B
28Relation Properties()
- S 1 , 2 , 3 .
- R2 lt1,1gt , lt2 ,2 gt , lt3, 3gt , lt3,2gt .
- reflexive, yes lta,agt
- symmetric, no, has lt3,2gt and not lt2,3gt
- Antisymmetric, yes, because it is not symmetric
and has symmetry on lta,agt. - Asymmetric (which precludes lta,agt), no, because
it is antisymmetric (which allows lta,agt).
29Relation Properties()
- S 1 , 2 , 3 .
- R1 lt2,1gt , lt1,2gt , lt2,3gt , lt3,2gt .
- reflexive, no lta,agt
- symmetric, yes for all lta,bgt then ltb,agt
- asymmetric, no, because it is symmetric.
- antisymmetric, no because no lta,bgt,ltb,agt with ab.
30Relation closure Example
- Let R lta,bgt,ltc,agt,ltc,cgt be a relation on the
set Aa,b,c. - The reflexive closure is
- r(R) lta,bgt,ltc,agt,lta,agt,ltb,bgt,ltc,cgt
- The symmetric closure is
- s(R) lta,bgt,ltc,agt,ltc,cgt,ltb,agt,lta,cgt
31Relation Properties
- Let X be the set of all four bit strings. Define
a relation R on X as lts1,s2gt?R if a substring of
s1 of length 2 is equal to a substring of s2
length 2. - Examples
- lt0111,1010gt ? R
- lt1110,0001gt ? R
- Is this relation reflexive, symmetric,
antisymmetric, and/or a partial order?
32Relation Properties
- Is lts1,s2gt?R reflexive?
- Yes. Example lt0111,0111gt ? R
- Is lts1,s2gt?R symmetric?
- Yes. Example
- lt0100,0111gt ? R ? lt0111,0100gt ? R
- Is lts1,s2gt?R antisymmetric?
- No, because it is symmetric.
33Relation Properties
- Is lts1,s2gt?R transitive? No, here is a counter
example - 1000 R 1011 R 0011
- Is lts1,s2gt?R partial order?
- No, because it is not reflexive, not
antisymmetric, and not transitive. Partial order
is a property of a relation which is a partial
order, we will look at later in the lecture.
34Reflexive or Symmetric?
- On the set 1, 2, 3, 4 we have relation
(1,1),(1,3),(2,2),(2,4),(3,1),(3,3),(4,2),(4,4) - We can use a table to check whether the relation
is reflexive and symmetric.
35Closure
- The closure of a relation ? with respect to some
property (e.g. reflexivity) is the smallest
relation that both has the property and contains
?. To construct a closure, add pairs to ?, but
not too many, until it has the property. - The less-than relation is not reflexive, but the
less-than-or-equal-to relation is. - For example, the reflexive closure of lt over
integers is the relation constructed by adding to
the relation all pairs (b,b) for b an integer.
Therefore ? is the reflexive closure of lt.
36Sensible Closures
- Reflexive Closure r(?)
- (? ? ?0)
- Symmetric s(?)
- (? ? ?-1)
- Transitive closure ?
- (?1 ? ?2 ? ?3 ? ?4 .. ?n )
- Reflexive Transitive closure ?
- (?0 ? ?1 ? ?2 ? ?3 ? ?4 .. ?n )
37Sensible Closures
- Let ? be a relation on a set. The reflexive
(symmetric, transitive) closure of ? is the
relation ? that satisfies - ? is reflexive (symmetric, transitive)
- ? ? ?
- If ? is reflexive (symmetric, transitive) and
? ? ? then ? ? ? . (e.g. smallest subset)
38Sensible Closures
- The less-than relation is not reflexive, but
the less-than-or-equal-to relation is. Yet we
can form the reflexive closure of less-than - The reflexive closure of lt over integers is the
relation constructed by adding to the relation
all pairs (b,b) for b an integer. Therefore ? is
the reflexive closure of lt.
39Closures
- Reflexive closure r(lt) of lt on integers is ?.
- Symmetric closure s(parent) of parent is (parent
? child), since if ltb,cgt is in the symmetric
closure so is ltc,bgt. - Transitive closure parent of parent is ancestor,
since if ltb,cgt and ltc,dgt is in the transitive
closure so is ltb,dgt. - The reflexive transitive closure parent of
parent-or-self.
40Closures
- Let R lt1,2gt,lt3,1gt,lt3,3gt be a relation on the
set A1,2,3. - Calculate the following closures
- the reflexive closure,
- the symmetric closure,
- the reflexive transitive closure.
41Closures
- Reflexive Closure
- r(R) lt1,2gt,lt3,1gt,lt1,1gt,lt2,2gt,lt3,3gt
- Symmetric Closure
- s(R) lt1,2gt,lt3,1gt,lt3,3gt,lt2,1gt,lt1,3gt
- Irreflexive Transitive Closure
- R lt1,2gt,lt3,1gt,lt3,3gt,lt3,2gt
- Reflexive Transitive Closure
- R lt1,2gt,lt3,1gt,lt3,3gt,lt2,2gt,lt1,1gt,lt3,2gt
42Equivalence Relations
- A relations is an equivalence relation iff it is
reflexive, symmetric and transitive (e.g. ). - An equivalence relation ? on a set B partitions
the set into non-empty disjoint subsets. Elements
that are equivalent under ? are placed in the
same partition. Elements that are not equivalent
under ? are placed in different partitions. For
example - ?b,c? ? sameEye ? b and c have same eye colour
43Equivalence Relations
- Let ? be an equivalence relation on B. Then b?,
the equivalence class of b, is the subset of
elements of B that are equivalent (under ?) to b. - x ? b? ? x ? b
- The sets b? form an equivalence ? relation on B
partition of B. Thus an equivalence relation on B
induces a partition of B, where each partition
element consists of equivalent elements.
44Equivalence Relations
Objects b and c are in the same partition and
bc
bgreen
.
b
.
cgreen
.
.
c
.
.
.
.
.
d
dblue
45Equivalence Relations
Objects b and c are in the same partition and
bgreencgreen
bgreen
cgreen
dblue
46Equivalence Classes1
Theorem The equivalence classes of an
equivalence relation R partition the set A into
disjoint nonempty subsets whose union is the
entire set. This partition is denoted A/R and
called, the quotient set, or the partition of A
induced by R, or A modulo R. Definition Let S1,
S2, . . ., Sn be a collection of subsets of A.
Then the collection forms a partition of A if the
subsets are nonempty, disjoint and exhaust A
Si ? ? Si ?Sj ? if i ? j ? Si A
47Equivalence Relations
- The relation b 4 c on integers 0..9(must be )
- b 4 c ? (b c) is a multiple of 4
- Their difference is a multiple of 4
- We have 4 partitions
- 0 4 8 0,4,8
- 1 5 9 1,5,9
- 2 6 2,6
- 3 7 3,7
48Equivalence Relations
- Consider the relation ? defined on the set of
people by - (b ? c) iff b and c are female and either b and
c are the same person or b is cs sister. - Relation ? is reflexive, symmetric, and
transitive, so it is an equivalence relation. For
female b, b consists of b and bs sisters,
while the equivalence for a male consists of only
that male.
49Equivalence Relations
- There is a correspondence between equivalence in
relations and partitions in sets. - ?b,c? ? sameEye ? b and c have same eye colour
50Equivalence Relations
- Theorem 1 Let S be a partition of a set X.
Define (x R y) to mean that for some S in S,
both x and y belong to S. Then R is reflexive,
symmetric, and transitive. - Stating this another way, a relation that is
reflexive, symmetric, and transitive is an
equivalence relation.
51Equivalence Relation()
- Let S be a partition of X which gives rise to an
equivalence relation R on X. - X 1,2,3,4,5,6
- S 1,3,5,4,2,6 partition X
- The relation R on X is an equivalence relation
therefore R is reflexive, symmetric, and
transitive. Draw S. List the ordered pairs of the
relation R on X. Draw the diagraph of R.
52Equivalence Relation()
11
22
.
.
2
62
1
.
.
6
3
31
.
5
51
.
4
43
Venn Diagram The equivalence classes are
subscripted according to their respective
partitions
53Equivalence Relation()
- List the ordered pairs of the relation R on X.
- R (1,1), (1,3),(1,5),(2,2),(2,6),
- (3,1),(3,3),(3,5),(4,4),(5,1),
- (5,3),(5,5),(6,2),(6,6)
54Graph of Equivalence Relation
- R (1,1), (1,3),(1,5),(2,2),(2,6),
- (3,1),(3,3),(3,5),(4,4),(5,1),
- (5,3),(5,5),(6,2),(6,6)
1
2
3
5
4
6
55Order Relations
- The order relation compares some members of a
set. A typical order relation is lt on integers. - An order relation need not be a comparison of
every member of a set, e.g. while A and B may be
parents parent(A,B) may not hold.
56Equivalence Relation()
a1
b2
.
.
b
f2
a
.
.
f
c
c1
.
e
e1
.
d
d3
Venn Diagram The equivalence classes are
subscripted according to their respective
partitions
57Equivalence Relation()
b
a
c
e
d
f
58Equivalence Relation()
- List the ordered pairs of the relation R on X.
- R (a,a), (a,c),(a,e),(b,b),(b,e),
- (c,a),(c,c),(c,e),(d,d),(e,a),
- (e,c),(e,e),(f,b),(f,f)
59Def. Partial Order Relation
- A binary relation ? on a set b is called a
partial order on B if it is Reflexive,
Antisymmetric, and Transitive. ltB, ? gt
is called a partially ordered set or poset. - We will write partial orders using a?b and b?c.
This notation differs from the course text book.
60Partial Order Relation1
Is this relation reflexive, antisymmetric, and
transitive?
61Hasse Diagrams
- Hasse diagrams are a form of simplified digraph
that allow us to represent partial orders. They
are simplified as follows. - Loops may be suppressed
- Arrows not necessary, read from bottom to top.
- Transitive arrows may be suppressed.
62Order Relation on Power set.
- lt?,?gt is a poset and ? is a partial order on ?.
- Let B be a set. Then lt?B,?gt is a poset and ? is
a partial order on ?B (the power set of B), since
is symmetric, antisymmetric, and transitive. - Let C be the set of courses offered at DIT.
Define the relation ? by c1?c2 if c1c2 or c1 is
a prerequisite of c2. Then ltC,?gt is a poset and ?
is a partial order on C.
63Order Relation on Power set1.
- A partially order set can be represented with
using a POSET diagram. The POSET diagram on the
right is based on the power set (all possible
subsets) of the three element set a, b, c.
These subsets form a special kind of partial
order that is referred to as a lattice
64Order Relation.
- The POSET diagram on the right is based on the
power set (all possible subsets) of the three
element set 1, 2, 3.
65Partial Order Relation on Divisibility1.
- The set
- A 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60
- of all divisors of 60, partially ordered by
divisibility.
66Order Relations
- In the construction of a house, certain jobs have
to be done before other jobs. Let J be the set of
jobs and let defined on J be defined by j1 ? j2
if j1has to be completed before j2 can be
started. ltJ, ?gt is a poset.
67Total Order Relations
- With partial orders because not all elements need
to be comparable. But some total relations give
rise to total order. For example less than or
equal to" is a total relation over the set of
real numbers, because for two numbers either the
first is less than or equal to the second, or the
second is less than or equal to the first. We can
define a total order in two ways 1)in terms of
membership and 2)in terms of operations on sets.
68Total Order Definition
- A partial order ? over B is called a total order
or linear order if - Def 1
- (?b,c b?c ? c?b)
- Def 2
- iff ???-1 B ? B
- ltB,?gt is called a linearly ordered set or a
chain.
69Total Orders
- lt?,?gt and lt?,?gt are chains
- Let set S contain more than one element. Then ?
over ?S is not a total order. Because if b and c
are distinct elements of S, then neither b?c
nor c?b holds. - Any subset of a totally ordered set, with the
restriction of the order on the whole set. - Any partially ordered set X where every two
elements are comparable (i.e. if a,b are members
of X either ab or ba or both).
70Partial but not Total Order
- Let C be the set of courses at DIT. Let b ?
c mean that b c or b is a prerequisite for c.
The relation is a partial order but not a total
order.
71Total Orders
- The letters of the alphabet ordered by the
standard dictionary order (e.g., A lt B lt C) is a
partial order.
72UML Association as a Relation
- A representation of a UML association as 1)a
diagram and 2)a relation
73UML Aggregation Relation
- Informal whole-part relationships can be
modelled using aggregation - a specialized form of an association
- can have standard annotations on ends
74UML Cyclic Object Structures
- Aggregation is useful for ruling out invalid
cyclic object structures - eg where an assembly contains itself, directly or
indirectly
75UML Properties of Aggregation
- Aggregation rules this out because it is
- antisymmetric an object cant link to itself
- transitive if a links to b and b to c, a links
to c
part end
whole end
76Meaning of Aggregation
- Sometimes there is a conflict
- E.g. people cannot be their own ancestors
- this can be specified using aggregation
- but a persons ancestors are not a part of them!
- The aggregation does not make sense in this case
77Meaning (semantics) of Aggregation Relation
- A hand may be considered as part of a person and
a person may be considered as part of an
orchestra, but we would not consider hand as part
of an orchestra. - Heuristic If the part moves, can one deduce that
the whole moves with it in normal circumstances.