Relations
Download
1 / 96

Relations - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

Relations. Chapter 9 Formal Specification using Z. Formal definition of Relation. The Z examples so far have been limited to one basic type. We need of relating one set to another. A relation is a set which is based in the idea of the Cartesian product. Formal definition of Relation.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Relations' - fulton-fernandez


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Relations

Relations

Chapter 9

Formal Specification using Z


Formal definition of relation
Formal definition of Relation

  • The Z examples so far have been limited to one basic type. We need of relating one set to another.

  • A relation is a set which is based in the idea of the Cartesian product.


Formal definition of relation1
Formal definition of Relation

  • A Cartesian product is the pairing of values of two or more sets. The Cartesian product is written:

  • X x Y x Z

  • For example

  • {red, green} x{hardtop,softtop} =

  • {(red, hardtop), (red, softtop), (green, hardtop), (green,softtop)}

  • This set consists of ordered paired called a tuple


Formal definition of relation2
Formal definition of Relation

  • A binary relation R from the set X to the set Y is a subset of the Cartisian Product X Y. If (x,y) R, we write xRy and say x is related to y. In the case X=Y, we call R a binary relation on X.

  • The domain of R is the set

  • { x X | (x,y) R from some y Y}

  • The range of R is the set

  • {y Y | (x,y) R for some x X}


Relation as a table
Relation as a table

  • A relation can be thought of as a table that lists the relationship of elements to other elements

    Student Course

    Bill Computer Science

    Mary Maths

    Bill Art

    Beth History

    Beth Computer Science

    Dave Math

    The table is a set of ordered pairs


Relation as a set of ordered pairs
Relation as a set of ordered Pairs

  • If we let

  • X = { Bill, Mary, Beth, Dave}

  • Y = {ComputerScience, Math, Art, History}

  • then the relation from the prevuous table is:

  • R = {(Bill,ComputerScience), (Mary, Math), (Bill,Art), (Beth,ComputerScience), (Beth,History), (Dave, Math) }

  • We say Mary R Math. The domain is the domain (first column) of R is the set X the range (second column) of R is the set Y. So far the relation has been defined by specifying the ordered pairs involved.


Relation defined by rule
Relation defined by rule

  • It is also possible to define a relation by giving a rule for membership of the relation.

  • Let X = { 2,3,4} and Y = {3,4,5,6,7}

  • If we define R from X to Y by:

  • (x,y) R if x divides y (with zero remainder)

  • We get:

  • R = { (2,4), (2,6), (3,3), (3,6), (4,4) }


Relation written as table
Relation written as table

  • This can be written as a table .

    X Y

    2 4

    2 6

    3 3

    3 6

    4 4

    The domain of R is the set {2,3,4} and the range is {3,4,5}


Let R be the relation on X = {1,2,3,4}

defined by :

(x,y) R if x y,

x,y X.

is shown

The digraph for R

1

2

3

4

R = {(1,1), (1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)}

The domain and the range are both equal to x. R is reflexive, transitive and antisymmetric.


Let R be the relation on X = {1,2,3,4}

defined by :

(x,y) R if x < y,

x,y X.

is shown

The digraph for R

1

2

3

4

R = { (1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}

The domain and the range are both equal to x. R is reflexive, transitive and non-symmetric.


Types of relation
Types of relation

  • A relation R on a set X is reflexive if (x,x) R for every x X.

  • A relation R on a set X is symmetric if for all x,y X, if (x,y) R, then (y,x) R.

  • A relation R on a set X is antisymmetric if for all x,y X, if (x,y) R, (y,x) and x y , then (y,x) R.


Types of relation1
Types of relation

  • A relation R on a set X is transitive if for all x,y,z X, if (x,y) R, and (y,z) R then (x,z) R.


Types of relation2
Types of relation

  • Let be a relation on the set A, then

  • is reflexive if x x for all x A

  • is symmetric if x y implies y  x

  • is transitive if x y and y z imply x z

  • is antisymmetric if x y and y x imply that x=y


Relations1
Relations

  • A special case of Cartesian product is a pair. A binary relation is set of pairs, of related values.

  • [COUNTRY] the set of all countries

  • [LANGUAGE] set of all languages


A relation on COUNTRY and LANGUAGE called SPEAKS could have values: {(France,French),(Canada,French),(Canada,English),(GB,English),(USA,English)}

SPEAKS relation

France

Canada

GB

USA

French

English

LANGUAGE

COUNTRY


Relations2
RELATIONS values:

  • As can be seen from the SPEAKS relation, there are no restrictions requiring values of the two sets to be paired only one-to-one.

  • We are free to choose the direction or order of the relation:

  • SPOKEN:  (LANGUAGE COUNTRY)


Declaring relations in z
Declaring Relations in Z values:

  • R: XY ( equivalent to (X Y) )

  • SPEAKS: COUNTRY LANGUAGE

  • ( equivalent to (COUNTRY  LANGUAGE) )

  • In English we say:

  • R relates X to Y.

  • SPEAKS relates COUNTRY toLANGUAGE


Maplets
MAPLETS values:

  • A maplet relates x to y and is written:

  • x y == (x,y)

  • ‘x is related to y’ or ‘x maps to y’

  • GB  English  SPEAKS

  • is equivalent to

  • (GB, English)  SPEAKS


Membership
Membership values:

  • To discover if a certain pair of values are related it is sufficient to see if the pair or maplet is an element of the relation:

  • GB  English  SPEAKS

  • OR

  • (GB, English)  SPEAKS


Infix notation
Infix Notation values:

  • If we declare a relation using low-line characters as place holders to show the fact that is infix

    _SPEAKS_ : COUNTRY LANGUAGE

  • GB speaks English

  • In general

    xRy == x  y  R==(x,y)  R


The DOMAIN and RANGE of the relation SPEAKS values: dom SPEAKS is set of countries where at least one language is spoken.ran SPEAKS is set of languages which are spoke in at least one country.

SPEAKS relation

French

English

France

Canada

GB

USA

LANGUAGE

(TARGET)

COUNTRY

(SOURCE)

dom SPEAKS ran SPEAKS


Domain and range
Domain and Range values:

A relation relates values of a set called source or from-set to a set called target or to-set.

Below the source is X and the target Y.

R: X Y

______________________

R = {(x1,y2), (x2,y2), (x4,y3), (x6, y3) ), (x4,y2)}

________________________


Domain
Domain values:

In most cases only a subset of of the source is involved in the relation. This subset is called the domain

dom R = {x1,x2,x4,x6}


Range
Range values:

In most cases only a subset of of the target is involved in the relation. This subset is called the range

ran R = {y2,y3}


The domain and range of the relation r dom r x 1 x 2 x 4 x 6 ran r y 2 y 3
The DOMAIN and RANGE of the relation R values: dom R = {x1,x2,x4,x6}ran R = {y2,y3}

R relation

y1

x5

x1

x2

x4

x6

Y2

y3

y4

Y

(TARGET)

X

(SOURCE)

x3

dom R ran R


Relational image
Relational Image values:

  • To discover the set of values from the range of a relation related to a set of values from the domain of the relation one can use the relational image:

  • RS

  • pronounced the ‘relational image of S in R’. For the languages spoken in France and Switzerland would be:

  • SPEAKS {France, Switzerland}

    == {French,German, Italian, Romansch}


Constant value for a relation
Constant value for a Relation values:

  • Some relations have constant values. If the value of a relation is known, it can be given by an axiomatic definition. For example the infix relation greater-than-or-equal:

    _ _ 

    ______________

     i,j: i  jn:  i=j+ n

    ________________


Domain restriction
Domain Restriction values:

  • The domain restriction operator restricts a relation to that part where the domain is contained in a particular set. The relation R domain restricted to S, is written as:

  • S  R

  • The point of the operator can be thought of as pointing left to the domain


Range restriction
Range Restriction values:

  • The range restriction operator restricts a relation to that part where the range is contained in a particular set. The relation R range restricted to S, is written as:

  • R  S

  • The point of the operator can be thought of as pointing right to the range


Domain subtraction
Domain Subtraction values:

  • The Domain Subtraction operator restricts a relation to that part where the domain is not contained in a particular set. The relation R domain subtracted to S, is written as:

  • S  R

  • The point of the operator can be thought of as pointing left to the domain


Range subtraction
Range Subtraction values:

  • The Range Subtraction operator restricts a relation to that part where the Range is not contained in a particular set. The relation R range subtracted to S, is written as:

  • R  S


Example of domain restriction
Example of Domain Restriction values:

Hols_____

holidays: COUNTRY DATE

__________

  • EU: COUNTRY

  • The relation mapping only EU countries to their public holidays is:

  • EU  holidays

  • The relation mapping only non-EU countries to their public holidays is:

  • EU  holidays


Composition
Composition values:

  • Relations can be joined together in an operation called composition. Given:

  • R: XY and

  • Q: YZ

  • We can have forward composition or backward composition


Forward composition
Forward Composition values:

  • Given:

  • R: XY and

  • Q: YZ

  • forward composition is defined as:

  • R;Q: XZ


Forward composition1
Forward Composition values:

R;Q

R

Q

X

Z

Y


For any pair (x,y) related by R forward composed with Q values:

x R;Q z

there is a y where R relates x to y and Q relates y to z;

y:Y  xRy  yQz

R;Q

R

Q

X

Z

Y


Backward composition
Backward Composition values:

  • Given:

  • R: XY and

  • Q: YZ

  • backward composition is defined as:

  • QR: R;Q


Repeated composition
Repeated Composition values:

  • A homogeneous relation is one which relates values from a type to values of the same type. Such a relation can be composed with iteslf

  • R: XX

  • R ; R: XX


Composition example
Composition Example values:

  • Countries are related by the borders relation if the share a border:

  • borders: COUNTRY COUNTRY

  • e.g. France borders Switzerland

  • Switzerland borders Austria

  • Countries are related by borders composed with borders if they each share a border with a third country.


Composition example1
Composition Example values:

  • The two facts:

    • 1. France borders Germany

    • 2. Germany borders Austria

  • can be represented by:

  • France borders;borders Austria

  • We can write:

  • Spain borders3 Denmark

  • for

  • Spain borders; borders; borders Denmark


Transitive closure
Transitive Closure values:

  • In general the transitive closure R+ as used in

  • x R+ y

  • means that there is a repeated composition of R which relates x to y. For example

  • France borders+ India.

  • Means France is on the same land-mass as India


Identity relation
Identity Relation values:

  • The identity relation

  • id X

  • is the relation which maps all x’s on to themselves:

  • id X == {x:X x  x }


Reflexive transitive closure
Reflexive Transitive Closure values:

  • The repeated composition

    R* = x R+ id X

    includes the identity relation. The Reflexive Transitive Closure is similar to the Transitive Closure except that it includes the identity relation. For example

  • x R* xis true, even if x Rx is false, (id x)

  • France borders* France is true


Relation example
Relation Example values:

  • Public holidays around the world can be described as follows:

  • [COUNTRY] the set of all countries

  • [DATE] the dates of a given year

    Hols_____

    holidays: COUNTRY DATE

    __________


Relation example1
Relation Example values:

  • An operation to discover whether a date d? is a public holiday in country c? is:

  • REPLY ::= yes | no

    Enquire_____

    Hols

    c?: COUNTRY

    d?: DATE

    rep! : REPLY

    ________ 

    (c? d? holidays rep! = yes) 

    (c? d? holidays rep! = no)

    __________


Relation example2
Relation Example values:

  • An operation to decree whether a public holiday in country c? on date d? is:

    Decree_____

    Hols

    c?: COUNTRY

    d?: DATE

    ________ 

    holidays’ = holidays  {c? d?}

    __________


Relation example3
Relation Example values:

  • An operation to abolish a public holiday in country c? on date d? is:

    Abolish_____

    Hols

    c?: COUNTRY

    d?: DATE

    ________ 

    holidays’ = holidays \ {c? d?}

    __________


Relation example4
Relation Example values:

  • An operation to find the dates ds! of all public holidays in country c? is:

    Dates_____

    Hols

    c?: COUNTRY

    ds?: DATE

    ________ 

    ds! = holidays c?

    __________


Inverse of a relation
Inverse of a relation values:

  • The inverse of a relation R from X to Y

  • R: XY is written

  • R~

  • and it relate Y to X (mirror image). So if

  • x R y

  • then

  • y R~x


Family example
Family Example values:

  • [PERSON] the set of all persons

  • father,mother: PERSON PERSON

  • ‘x father y’ meaning x has father y

  • ‘v mother w’ meaning v has mother w


Family example parent relation
Family Example: parent relation values:

  • parent: PERSON PERSON

  • can be defined as

  • parent = father  mother


Family example sibling
Family Example: sibling values:

  • sibling: PERSON  PERSON

  • can be defined as

  • sibling = (parent ; parent ~) \ id PERSON

  • This is defined as the relation parent composed with its own inverse, in other words the set of persons with the same parent. Note the inverse of parent that relates parent to child. A person is not usually counted as their own sibling, hence the use of the identity relation.


sibling: PERSON values:  PERSON

can be defined as

sibling = (parent ; parent ~) \ id PERSON

children parents : PERSON

children = {Pat, Bernard,Marion}

parents = {Liam, Mary}

parent;parent~

R

Q

Pat

Bernard

Marion

Liam

Mary

Pat

Bernard

Marion

children

Z

Y


(parent ; parent values: ~) = {(Pat, Pat), (Pat,Bernard), (Pat,Marion), (Bernard, Bernard),

(Bernard,Pat), (Bernard,Marion), (Marion,Marion),

(Marion,Pat), (Marion,Bernard)}

(parent ; parent ~) \ id PERSON= { (Pat,Bernard), (Pat,Marion),(Bernard,Pat),

(Bernard,Marion), (Marion,Pat),

(Marion,Bernard)}

parent;parent~

parent

Pat

Bernard

Marion

parent~

Liam

Mary

Pat

Bernard

Marion

children

Z

Y


Family example ancestor
Family Example: ANCESTOR values:

  • ancestor: PERSON PERSON

  • can be defined as

  • ancestor = parent +

  • The relation ancestor can be defined as the repeated composition of parent.


Chapter 9 question 1
Chapter 9 Question 1 values:

  • Express the fact that the language Latin is not the official language of any country:


Chapter 9 question 11
Chapter 9 Question 1 values:

  • Express the fact that the language Latin is not the official language of any country:

  • Latin:LANGUAGE

  • Latin  ran speaks


Chapter 9 question 2
Chapter 9 Question 2 values:

  • Express the fact that Switzerland has four languages


Chapter 9 question 21
Chapter 9 Question 2 values:

  • Express the fact that Switzerland has four languages

  • #speaks  {Switzerland} = 4


Chapter 9 question 3
Chapter 9 Question 3 values:

  • Give a value to the relation speaksInEU which relates countries which are in the set EU to their languages.


Chapter 9 question 31
Chapter 9 Question 3 values:

  • Give a value to the relation speaksInEU which relates countries which are in the set EU to their languages.

  • EU:  COUNTRY

  • speaksInEU : COUNTRY  LANGUAGE

  • speaksInEU = EU  speaks


Chapter 9 question 4
Chapter 9 Question 4 values:

  • Give a value to the relation grandParent


Chapter 9 question 41
Chapter 9 Question 4 values:

  • Give a value to the relation grandParent

    grandParent : PERSON  PERSON

    grandParent = parent ; parent


Chapter 9 question 5
Chapter 9 Question 5 values:

  • A person’s first cousin is defined as a child of the person’s aunt or uncle. Give a value for the relation firstCousin.


Chapter 9 question 51
Chapter 9 Question 5 values:

  • A person’s first cousin is defined as a child of the person’s aunt or uncle. Give a value for the relation firstCousin.

    firstCousin : PERSON  PERSON

    firstCousin = (grandParent ; grandParent ~) \ sibling


Chapter 9 question 6
Chapter 9 Question 6 values:

Given

[PERSON] set of uniquely identifiable persons

[MODULE] set module numbers at a university

students,teachers,EU,inter:  PERSON

offered:  MODULE

studies: PERSON MODULE

teaches: PERSON MODULE


Chapter 9 question 61
Chapter 9 Question 6 values:

[PERSON] set of uniquely identifiable persons

[MODULE] set module numbers at a university

students,teachers,EU,inter:  PERSON

offered:  MODULE

studies: PERSON MODULE

teaches: PERSON MODULE

Explain the predicates:

EU  inter = 

EU  inter = students

dom studies  students

dom teaches  teachers

ran studies  offered

ran teaches = ran studies


Chapter 9 question 62
Chapter 9 Question 6 values:

Explain the effect of each predicates:

EU  inter = 

EU  inter = students

dom studies  students

dom teaches  teachers

ran studies  offered

ran teaches = ran studies

Students are either from the EU or overseas, but not from both.

Students study and teachers teach.

Only offered modules can be studied.

Those modules that are taught are studied.


Chapter 9 question 7
Chapter 9 Question 7 values:

Give an expression for the set of modules studied by a person p.


Chapter 9 question 71
Chapter 9 Question 7 values:

Give an expression for the set of modules studied by a person p.

studies  {p} 


Chapter 9 question 8
Chapter 9 Question 8 values:

Give an expression for the number of modules taught by a person q.


Chapter 9 question 81
Chapter 9 Question 8 values:

Give an expression for the number of modules taught by a person q.

#(teaches  {q} )


Chapter 9 question 9
Chapter 9 Question 9 values:

  • Explain what is meant by the inverse of the relation studies

    studies~


Chapter 9 question 91
Chapter 9 Question 9 values:

  • Explain what is meant by the inverse of the relation studies

    studies~

  • The inverse of the relation studies relates modules to persons studying them.


Chapter 9 question 10
Chapter 9 Question 10 values:

  • Explain what is meant by the composition of the relations

    studies ; teaches~


Chapter 9 question 101
Chapter 9 Question 10 values:

  • Explain what is meant by the composition of the relations

    studies ; teaches~

  • The composition of relates students to the teachers who teach modules the students study.


Chapter 9 question 111
Chapter 9 Question 11 values:

  • Give an expression for the set of persons who teach person p.


Chapter 9 question 112
Chapter 9 Question 11 values:

  • Give an expression for the set of persons who teach person p.

    (studies ; teaches~) {p} 


Chapter 9 question 12
Chapter 9 Question 12 values:

  • Give an expression for the number of persons who teach both person p andperson q.


Chapter 9 question 121
Chapter 9 Question 12 values:

  • Give an expression for the number of persons who teach both person p andperson q.

    #((studies ; teaches~) {p}  (studies ; teaches~) {q} )


Chapter 9 question 13
Chapter 9 Question 13 values:

  • Give an expression for that part of the relation studies that pertains to international students(inter)


Chapter 9 question 131
Chapter 9 Question 13 values:

  • Give an expression for that part of the relation studies that pertains to international students(inter)

  • inter  studies


Chapter 9 question 14
Chapter 9 Question 14 values:

  • Give an expression for that states that p and q teach some of the same international students.


Chapter 9 question 141
Chapter 9 Question 14 values:

  • Give an expression for that states that p and q teach some of the same international students.

    ((teaches; studies ~) {p}   (teaches; studies ~)  {q} ) inter 


Chapter 9 question 15
Chapter 9 Question 15 values:

  • The following declarations are part of the description of an international conference.

    [PERSON] set of uniquely identifiable persons

    [LANGUAGE] the set of world languages

    official:  LANGUAGE


Chapter 9 question 151
Chapter 9 Question 15 values:

  • The following declarations are part of the description of an international conference.

    [PERSON] set of uniquely identifiable persons

    [LANGUAGE] the set of world languages

    official:  LANGUAGE


Chapter 9 question 152
Chapter 9 Question 15 values:

[PERSON] set of uniquely identifiable persons

[LANGUAGE] the set of world languages

official:  LANGUAGE

CONFERENCE_____

  PERSON

 LANGUAGE

 _speaks_ : PERSON LANGUAGE

__________


Chapter 9 question 15a
Chapter 9 Question 15A values:

Write expression that states every delegate speaks at least one language

delagates  dom speaks


Chapter 9 question 15b
Chapter 9 Question 15B values:

Write expression that states every delegate speaks at least one official language of the conference

delagates  ran speaks 


Chapter 9 question 15c
Chapter 9 Question 15C values:

Write expression that states every delegate speaks at least one official language of the conference

lang:LANGUAGE 

(del:PERSON | del delagates  del speaks lang)


Chapter 9 question 15e
Chapter 9 Question 15E values:

  • Write an operation schema to register a new delegate and the set of languages he or she speaks.

    Register_____________

     del?:PERSON

     lang?:LANGUAGE

    CONFERENCE

    ________________

    del?  delegates

    delegates = delegates  {del?}

    speaks =

    speaks  {lan:LANGUAGE | lan lang?  del? lan}

    official = official’

    _________________


Labs values:

  • Using pen and paper do Chapter 2 Exercises 5.

  • Using pen and paper do exam example Edward, Fleur, and Gareth.

  • Type both of the above into Word using symbols or Z fonts.


Labs values:

  • Calculate the following unions.

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S1,S2,W).

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S1,S3,W).

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S3,S2,W).


Labs values:

  • Start Prolog

  • Copy sets.pro from: X:\distrib\wmt2\maths\prolog

  • Consult sets.pro

  • Use the sets programs to solve the previous union of S1,S2 and S3.


Labs values:

  • Using prolog do exam example Edward, Fleur, and Gareth.


Labs values:

  • 1)Using pen and paper do Chapter 2 Exercises 5.

  • 2)Using pen and paper do exam example Edward, Fleur, and Gareth.

  • 3)Calculate the following unions.

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S1,S2,W).

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S1,S3,W).

  • S1 = [1,2], S2 = [2, 3, 4, 5], S3 = [1, 2, [2, 3, 4, 5]], union(S3,S2,W).

  • 4)Type both 1 and 2 above into Word using symbols or Z fonts.

  • 5)Start Prolog

  • 6)Copy sets.pro from: X:\distrib\wmt2\maths\prolog

  • 7)Consult sets.pro

  • 8)Use the sets programs to solve the previous union of S1,S2,S3

  • 9)Using prolog do exam example Edward, Fleur, and Gareth.


ad