Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne Room K308 Based on Chapter...

Preview:

Citation preview

Computing Fundamentals 1 Lecture 7Relations

Lecturer: Patrick Brownehttp://www.comp.dit.ie/pbrowne/

Room K308Based on Chapter 14.

A Logical approach to Discrete Math By David Gries and Fred B. Schneider

• The term tuple is used to describe a row in a database. Tuples are record like structure e.g. <name,age>.

• Cross products represent the types (or sorts in CafeOBJ) in a tuple e.g. String Nat

• Relations describe relationships between objects.• Functions (covered in next lecture) are relations with

some special properties.

Relations and Functions

• Given expressions b and c then b,c is called an ordered pair. Well known ordered pairs are x,y coordinates in the plane, or name,address in a database.

• Axiom, Pair equality:b,c = b’,c’ b = b’ c = c’

• A 2-tuple is called an ordered pair.

Tuples1

Cross Products

• The Cross product (or Cartesian product) ST 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):

ST = {b,c | bS cT : b,c}• Cross products can be extended to n sets

producing n-tuples.

Cross Products

• A 2-tuple: • S = {2,5}• T = {1,2,3}ST = {2,1,2,2,2,3,5,1,5,2,5,3}

Cross Products

• Example of a 2-tuple: S = {Mary,John}

T = {John,Bill,Brent}

ST = {Mary, John, Mary,Bill , Mary,Brent , John, John , John,Bill , John,Brent}

Cross Products Example

•ST =/= TS : S = {Mary,John}T = {John,Bill,Brent}TS = {<John,Mary>, <John,John>, <Bill,Mary>,<Bill,John>, <Brent,Mary>,<Brent,John>}

ST = {Mary, John, Mary,Bill , Mary,Brent , John, John , John,Bill , John,Brent}

Some Theorems for Cross products

Membership:

x,y ST xS yTDistributivity of over S (TU) (S T) (S U) (S T) U (S U) (T U)Monotonicity

T U (S T) (S U)

Relations

• A relation is a subset of a cross product B1 B2 … Bn

• A binary relation over BC is a subset of BC.

• It is possible to have a relation on BB.

• Well know relation lessThan(i,j)is difficult to enumerate (or list), so use:

{i,j : | j-i is positive : i,j}

Relations

• 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)

Relation as Directed Graph

Set = {1,2,3,4,5,6}

Relation = {<1,1>,<1,3>,<2,5>,<2,1>, <5,3>}

1

54

3

2

6

One vertex for each element of the set. There is a directed edge from each vertex b to vertex c iff <b,c> is in the binary relation.

Diagraph of Relations

• Given the relation R ={<1,2>,<2,1>,<3,3>,<1,1>,<2,2>}

• On the set {1,2,3}

• Below is the digraph of R.

1

2

3

DigraphRelations is a set of ordered pairs:(a) P = {<1, 4> ,<2,1>, <3, 2>}(b) Q = {<1, 4> ,<2, 4>}

1

3

4

2

1

4

2

(a)(b)

DigraphsRelations as sets of ordered pairs and digraphs:(c) R = {<1, 4>, <2, 3>, <3, 3>,<2, 1>}(d) S = {<3, 1> ,<1, 2> ,<2, 3>}

1

3

4

2

1

2

3

(c) (d)

A set of ordered pairsA Set of Relations (Ordered Pairs)

With sets order is not significant{<3, 1> ,<1, 2> ,<2, 3>} = {<1, 2> ,<2, 3>, <3, 1>}

With relations order is significant <3, 1> =/= <1, 3>

{<3, 1> ,<1, 2> ,<2, 3>}=/={<1, 3> ,<1, 2> ,<2, 3>}

12

3

12

3

Relations on Sets

• The relation R on {1,2,3,4} is defined by <x,y> R if x2y .

• The relation R can be written as a set of ordered pairs:

R = {<1,1>,<2,1>,<3,1>,<4,1>,<2,2>,<3,2>,<4,2>,<2,3>,<3,3>,<4,3>,<2,4>,<3,4>,<4,4>}

Some Relations

• The empty relation on ST 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.

Relations

• Lower case Greek letters represent relations. • The Greek letter is pronounced rho.• The Greek letter is pronounced sigma.• The domain, Dom., and range Ran. of a

relation on BC are defined as: – Dom. = {Õb:B | (c |: b c)}– Ran. = {Õc:C | (b |: b c)}

Relations

Dom

Ran

B

C

Dom. = {b:B | (c|: b c)} Ran. ={c:C | (b|: b c)}

Operations on Relations

• Let be a relation on BC and be a relation CD. The product (or composition) of and denoted is defined by:

b,d (c | cC : b,c c,d)

B

C

D

bc

d

Alternative notation b()d holds iff bcd holds for some c.

Composing Relations

Powers of a relation

• For example parentparent can be written parent2.

0 = iB (the identity relation on B)

n+1 = n (for n0)

n+m = n m

nm = (n )m

Classes of relations

• Name Property• Reflexive (b| b b)• Irreflexive (b| (b b))• Symmetric (b,c | (b c) (c b))

Classes of relations

• Antisymmetric (b,c | (b c) (c b) b=c)

• Asymmetric (non-symmetric, see notes section)(b,c | (b c) (c b))

• Transitive(b,c,d | (b c) (c d) b d)

Relations Discussion

• Common relations

Relations 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

{<1,1>,<2,2>,<3,3>}• The relation < (less than) is irreflexive.• The relation = (equal to) is reflexive,

transitive, symmetric, and antisymmetric

Relations Discussion

• Consider the relation square • b square c iff b = c * c • square is not reflexive because it does

not contain {<2,2>}• square is not irreflexive because it does

contains pair {<1,1>}• Thus square is neither reflexive or

irreflexive. A relation that is not reflexive need not be irreflexive.

Relations 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} = {<1,2>,<2,1>,<1,3>}• is neither symmetric nor antisymmetric.• It is not symmetric because there is no <3,1> to

match the (1,3).• It is not antisymmetric because it has both <2,1> and <1,2>, but 12. Antisymmetric allows <1,1>.

Relations 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 A=B.

(A B) (B C) (A = B)• In general, a relation is antisymmetric if

there is no pair of distinct elements of X each of which is related by R to each other.

Relation Properties

• S = { 1 , 2 , 3} .• R2 = { <1,1> , <2 ,2 > , <3, 3> , <3,2>} .

• reflexive, yes <a,a>• symmetric, no, has <3,2> and not <2,3>• Antisymmetric, yes, because it is not

symmetric and has symmetry on <a,a>.• Asymmetric (which precludes <a,a>), no,

because it is antisymmetric (which allows <a,a>).

Relation Properties

• S = { 1 , 2 , 3} .• R1 = { <2,1> , <1,2> , <2,3> , <3,2>} .

• reflexive, no because <a,a> doesn’t hold

• symmetric, yes for all <a,b> then <b,a>• asymmetric, no, because it is symmetric.

• antisymmetric, no because no <a,b>,<b,a> with a=b.

Relation closure Example

• Let R = {<a,b>,<c,a>,<c,c> } be a relation on the set A={a,b,c}.

• The reflexive closure is: • r(R) = {<a,b>,<c,a>,<a,a>,<b,b>,<c,c>}

• The symmetric closure is: • s(R) = {<a,b>,<c,a>,<c,c>,<b,a>,<a,c>}

Relation Properties

• Let X be the set of all four bit strings. Define a relation R on X as <s1,s2>R if a substring of s1 of length 2 is equal to a substring of s2 length 2.

• Examples<0111,1010> R <1110,0001> R

• Is this relation reflexive, symmetric, antisymmetric, and/or a partial order?

Relation Properties

• Is <s1,s2>R reflexive?

• Yes. Example: <0111,0111> R• Is <s1,s2>R symmetric?

• Yes. Example: • <0100,0111> R <0111,0100> R

• Is <s1,s2>R antisymmetric?

• No, because it is symmetric.

Relation Properties

• Is <s1,s2>R transitive? No, here is a counter example:

• 1000 R 1011 R 0011

• Is <s1,s2>R partial order?

• No, because it is not reflexive, not antisymmetric, and not transitive. Partial order is a property of certain relations, we will look this at later in the lecture.

Reflexive 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 matrix to check whether the relation is reflexive and symmetric.

• Using matrix operations we can identify the presence of many relations (not covered here)

Closure

• 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 < 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 <.

Closures

• Reflexive closure r(<) of < on integers is .• parent is the relation “b is a parent or a child

of c” . Symmetric closure s(parent) of parent is (parent child), since if <b,c> is in the symmetric closure so is <c,b>.

• Transitive closure parent+ of parent is ancestor, since if <b,c> and <c,d> is in the transitive closure so is <b,d>.

• The reflexive transitive closure parent* of parent-or-self.

Various Closures(OUT)

• Reflexive Closure r()– ( 0)

• Symmetric s() – ( -1)

• Transitive closure +

– (1 2 3 4 .. n )

• Reflexive Transitive closure *

– (0 1 2 3 4 .. n )

Sensible Closures(OUT)

• 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 ’ ’’ . (i.e. smallest subset)

Closures

• Let R = {<1,2>,<3,1>,<3,3> } be a relation on the set A={1,2,3}.

• Calculate the following closures:

• the reflexive closure,

• the symmetric closure,

Closures

• Reflexive Closure:

r(R) = {<1,2>,<3,1>,<1,1>,<2,2>,<3,3>}

• Symmetric Closure:

s(R) = {<1,2>,<3,1>,<3,3>,<2,1>,<1,3>}

R = {<1,2>,<3,1>,<3,3> }

A={1,2,3}.

Equivalence Relations

• A relation 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

Equivalence 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.

Equivalence Relations

..

.

..

..

.

.

[b]green

b [c]green

[d]blue

c

d

Objects b and c are in the same partition and [b]=[c]

Equivalence Relations

[b]green

[c]green

[d]blue

Objects b and c are in the same partition and [b]green=[c]green

Equivalence 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

Equivalence 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}

Equivalence Relations

• Consider the relation defined on the set of people by:– (b c) iff b and c are female or b and c

are the same person or b is c’s sister.

• Relation is reflexive, symmetric, and transitive, so it is an equivalence relation. For female b, [b] consists of b and b’s sisters, while the equivalence for a male c consists of only that male c.

Equivalence Relations

• There is a correspondence between equivalence in relations and partitions in sets.

b,c sameEye b and c have same eye colour

Equivalence 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.

Equivalence 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.

Equivalence Relation(*)

• Draw S = {{1,3,5},{4},{2,6}}

..

..

.

.[2]2

2 [6]2

[4]3

6

4

1

3

5

[1]1

[3]1

[5]1

Venn Diagram

The equivalence classes are subscripted according to their respective partitions

Equivalence 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)}

Equivalence Relation(*)

Graph 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

3

5

6

2

4

Order Relations

• The order relation compares some members of a set. A typical order relation is < 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.

Equivalence Relation(*)

• Draw P = {{a,c,e},{d},{b,f}}

..

..

.

.[b]2

b [f]2

[d]3

f

d

a

c

e

[a]1

[c]1

[e]1

Venn Diagram

The equivalence classes are subscripted according to their respective partitions

Equivalence Relation(*)

• Draw the diagraph of R.

a c

e

f

b

d

Equivalence 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)}

Def. Partial Order Relation

• A binary relation on a set b is called a partial order on B if it is Reflexive, Antisymmetric, and Transitive. <B, > is called a partially ordered set or poset.

• We will write partial orders using ab and bc. This notation differs from the course text book.

Partial Order Relation1

Is this relation reflexive, antisymmetric, and transitive?

Hasse 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.

Order Relation on Power set.

• <,> is a poset and is a partial order on .• Let B be a set. Then <B,> 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 c1c2 if c1=c2 or c1 is a prerequisite of c2. Then <C,> is a poset and is a partial order on C.

Order 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

Order Relation.

• The POSET diagram on the right is based on the power set (all possible subsets) of the three element set {1, 2, 3}.

Partial 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.

Order 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. <J, > is a poset.

Total 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.

Total Order Definition

• A partial order over B is called a total order or linear order if

• Def 1• (b,c | : bc cb)• Def 2• iff -1 = B B• <B,> is called a linearly ordered set or

a chain.

Total Orders

• <,> and <,> 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 a≤b or b≤a or both).

Partial 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.

Total Orders

• The letters of the alphabet ordered by the standard dictionary order (e.g., A < B < C) is a partial order.

UML Association as a Relation

• A representation of a UML association as 1)a diagram and 2)a relation

UML Aggregation Relation

• Informal ‘whole-part’ relationships can be modelled using aggregation– a specialized form of an association– can have standard annotations on ends

UML: Cyclic Object Structures

• Aggregation is useful for ruling out invalid cyclic object structures– eg where an assembly contains itself, directly

or indirectly

UML: Properties of Aggregation

• Aggregation rules this out because it is– antisymmetric: an object can’t link to itself– transitive: if a links to b and b to c, a links to c

part end

whole end

Meaning of Aggregation

• Sometimes there is a conflict• E.g. people cannot be their own ancestors

– this can be specified using aggregation

– but a person’s ancestors are not a part of them!

– The aggregation does not make sense in this case

Meaning (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.

Recommended