9 BINARY RELATIONS - tmarris.com

19 **BINARY** **RELATIONS**We find pairs of objects all around us: wives and their husbands, friends and their telephonenumbers, students and the subjects they study, customers and their bank accounts, guests and theirhotel reservations. We see that a binary relation is just a set of ordered pairs. We see how the firstand second functions split an ordered pair into its coordinates. We look at the domain and range,and the source and target, of a binary relation. We see how domain and range restriction work likedatabase queries. We see how relational image works like a table lookup. We see how the overrideoperator works like a database update. We look at the inverse function. We see how two binaryrelations may be **com**posed to form a third binary relation.9.1 **BINARY** **RELATIONS**A binary relation is just a set of pairs.a pair{ (madge, homer), (wilma, fred) }{ (kylie, 1), (kylie, 2), (robbie, 3) }{ (sue, formalMethods), (sam, formalMethods), (tim, maths) }We can use the usual set operations - union, intersection, difference and cardinality - on sets ofbinary pairs.{ (1, a), (2, b) } U { (2, b), (3, c) } = { (1, a), (2, b), (3, c) }{ (1, a), (2, b) } I { (2, b), (3, c) } = { (2, b) }{ (1, a), (2, b) } \ { (2, b) (3, c) } = { (1, a) }#{ (1, a), (3, b), (5, c) } = 3Each element in a binary relation is a pair of objects.

2EXERCISE 9.11 Describe three further examples of binary relations.2 Evaluatea { (a, x), (b, y) } U { (a, z), (b, w) }b { (a, x), (b, y) } U { (b, y), (a, x), (c, z) }c { (a, x), (b, y) } I { (a, z), (b, y) }d { (a, x), (b, y) } \ { (a, x), (b, w) }e #{ (a, x), (b, y), (c, z), (d, w) }9.2 THE PROJECTION FUNCTIONSEach element in a binary relation is a pair of objects, e.g. (madge, homer).(madge, homer) is not the same pair as (homer, madge); order matters.The ordered pair (madge, homer) may be written as madge å homer. This is known asmaplet notation, and madge å homer is known as a maplet.(madge, homer) = madge å homerIn general, we use maplet notation to represent an element in a binary relation; we use coordinatenotation to represent a pair by itself.partners = { madge å homer, wilma å fred }aPair = (madge, homer)The Z functions first and second split an ordered pair into its first and second coordinates.first (madge, homer) = madgesecond (madge, homer) = homerfirst and second are known as the projection functions for ordered pairs.EXERCISE 9.21 Evaluate ( first (a, 1), second (a, 1) )

39.3 DOMAIN AND RANGELook at this binary relation:{ 1 å a, 2 å b, 3 å c }The set formed by all the first coordinates{ 1, 2, 3 }is known as its domain. We writedom { 1 å a, 2 å b, 3 å c } = { 1, 2, 3 }where dom is Z for domain.The set formed by all the second coordinates{ a, b, c }is known as its range. We writeran { 1 å a, 2 å b, 3 å c } = { a, b, c }where ran is Z for range.EXERCISE 9.31 Evaluatea dom { A301 å office, A302 å classRoom, A303 å lab, A304 å classRoom }b ran { A301 å office, A302 å classRoom, A303 å lab, A304 å classRoom }c dom { terry å Access, garry å ECDL, kanti å ALevel, kanti å HND }d ran { terry å Access, garry å ECDL, kanti å ALevel, kanti å HND }e dom { richard å 1921, richardII å 1952, richardIII å 1976 }f ran { richard å 1921, richardII å 1952, richardIII å 1976 }

49.4 SOURCE AND TARGETLook at the binary relation { 1 å a, 2 å b, 3 å c }. We can see thatdom { 1 å a, 2 å b, 3 å c } = { 1, 2, 3 }and{ 1, 2, 3 } z ZZ is known as the source. The domain of a binary relation is a subset of its source.Now we define CHARACTER - the set of all characters of the alphabet:[ CHARACTER ]We can see thatran { 1 å a, 2 å b, 3 å c } = { a, b, c }and{ a, b, c } z CHARACTERCHARACTER is known as the target. The range of a binary relation is a subset of its target.

59.5 DECLARING A **BINARY** RELATIONWe introduce as given types the set of all dates and the set of all people.[ DATE, PERSON ]We define appointments as a binary relation from DATE to PERSON like this:appointments : DATE j PERSONAn example set of appointments is:appointments = { 7Nov å tom, 7Nov å ann, 8Nov å jerry }j is the binary relation operator. Even though it looks like a double-headed arrow, itassociates from left to right.Naming a binary relation is easy if you can describe the relationship between the first and secondelement of each pair. For example:• lecturers teach subjects teaches : LECTURER j SUBJECT e.g. marris teaches Z• owners have motor vehicles owns : OWNER j VEHICLE e.g. marris owns BMW320• parents give birth to children: hasChild : PERSON j PERSON e.g. albert hasChild haroldEXERCISE 9.41 Introduce appropriate types and declare the binary relations described below, and give anexample of an element in each binary relation.a debtors and the money they oweb driving instructors and their pupilsc owners and their horsesd students and the subjects they studye authors and the titles of the books they have had published

69.6 DOMAIN RESTRICTIONDomain restriction works like a database query, a bit like using an internet search engine to list allentries that contain the phrase "Z Notation". You might get many entries listed, or none at all.Look at this binary relation:{ 1 å a, 2 å b, 3 å c }Its domain is { 1, 2, 3 }If we restrict the binary relation so that its domain is { 2 } we get{ 2 å b }We write{ 2 } r { 1 å a, 2 å b, 3 å c } = { 2 å b }where r means domain restriction. r is known as the domain restriction operator.The domain restriction operator defines a subset of a given binary relation. For example:{ 4 } r { 1 å a, 2 å b, 3 å c } = { }{ 1, 3 } r { 1 å a, 2 å b, 3 å c } = { 1 å a, 3 å c }

79.7 RANGE RESTRICTIONLook again at this binary relation:{ 1 å a, 2 å b, 3 å c }Its range is { a, b, c }.If we restrict the binary relation so that its range is { b } we get{ 2 å b }We write{ 1 å a, 2 å b, 3 å c } t { b } = { 2 å b }where t means range restriction. t is known as the range restriction operator.The range restriction operator defines a subset of a given binary relation. For example:{ 1 å a, 2 å b, 3 å c } t { d } = { }{ 1 å a, 2 å b, 3 å c } t { a, c } = { 1 å a, 3 å c }EXERCISE 9.51 Evaluatea { A, C }r { A å carrots, B1 å pasta, C å potatoes, C å tomatoes, D å milk }b { red å 1, brown å 2, green å 3, yellow å 4, blue å 5, pink å 6, black å 7 } t 3..5c { king } r { pawn å 1, rook å 5, knight å 3, bishop å 3, queen å 9 }d { pawn å 1, rook å 5, knight å 3, bishop å 3, queen å 9 }t { n : Z | n > 3 }

89.8 RELATIONAL IMAGEThe relational image operator works like a table look-up. For example, look at this table:colour valuered 1brown 4green 3yellow 2blue 5pink 6black 7You ask: what is blue's numerical value? You look down the colour column for blue, and thenacross see that blue's numerical value is 5.Look at this binary relation:{ 1 å a, 2 å b, 3 å c }What is the second coordinate of the pair whose first coordinate is 2? The answer is b.We write:{ 1 å a, 2 å b, 3 å c } · { 2 } ‚ = { b }where the fat brackets · ... ‚ is the relational image operator. You provide a subset of a binaryrelation's source, it gives you the corresponding elements in the binary relation's range. Forexample:{ 1 å a, 2 å b, 3 å c } · { 4 } ‚ = { }Input from domain{ 1 å a, 2 å b, 3 å c } · { 1, 3 } ‚ = { a, c }Output from range

9EXERCISE 9.61 Evaluatea { eatsMeat å tom, vegetarian å ann, eatsMeat å jerry } · { eatsMeat } ‚b { 65 å A, 66 å B, 67 å C, 68 å D, 69 å E } · { 65, 67, 69 } ‚c { tea å 50, coffee å 75, hotChocolate å 75 } · { soup } ‚d { tom å 12:00, ann å 12:20, jerry å 12:40 } · { ann } ‚e { 1 å 2, 2 å 3, 3 å 5, 4 å 7, 5 å 11 } · 1..5 ‚2 If R = { 1 å a, 2 å b, 3 å c, 4 å d, 5 å e } and S = 2..4 evaluate:a R · S ‚b ran(S : R)What do you notice about your answers to 2a and 2b above?9.9 OVERRIDEThe override operator, ±, works like a database update. For example:{ 1 å a, 2 å b, 3 å c } ± { 2 å B } = { 1 å a, 2 å B, 3 å c } 2 å b is replacedwith 2 å B{ 1 å a, 2 å b, 3 å c } ± { 4 å d } = { 1 å a, 2 å b, 3 å c, 4 å d } 4 å d has beenaddedEXERCISE 9.71 Evaluatea { tea å 50, coffee å 75, hotChocolate å 75 } ± { tea å 55 }b { smith å beer, kline å cider, french å wine } ± { jack å whisky }c { andy å mon, bettie å tue, carol å wed } ± { carol å thu, fred å wed }

109.10 INVERSEWe introduce as a given type DRINK, the set of all drinks.[ DRINK ]We define costs as a binary relation from drink to price.costs : DRINK j Zcosts = { tea å 50, coffee å 75, hotChocolate å 75, soup å 75 }So, for example, tea costs 50 pence.If we reversed the binary relation, if we made it from price to drink and called it buys, so, forexample, 50 pence buys tea:buys : Z j DRINKthenbuys = { 50 å tea, 75 å coffee, 75 å hotChocolate, 75 å soup }buys is the inverse of costs. We write:costs~ = buys(say costs inverse equals buys). ~ (tilde) is the inverse operator. The inverse of a binary relation isanother binary relation with the coordinates of their ordered pairs reversed.

11EXERCISE 9.81 We introduce the type NAME, the set of all people's names, and definealias : NAME j NAMEalias = { tom å dopey, sam å dozy, stu å dreamy, tom å sleepy, sam å titch }Evaluate:a alias~b alias t { dozy, dreamy }c alias · { tom, sam } ‚d alias · { sue } ‚e alias~ · { dreamy, dozy } ‚

129.11 COMPOSITIONGiven the types[ PERSON, ROOM ]the set of all persons and the set of all rooms in a department respectively, look at the two binaryrelations, hasPhone and phoneInRoom, shown below.hasPhone : PERSON j ZhasPhone = { roy å 317, tom å 208, tom å 209, jim å 326, lee å 225 }phoneInRoom : Z j ROOMphoneInRoom = { 317 å A306, 208 å A309, 209 å A309, 326 å A306, 225 å A309 }roy has phone 317; phone 317 is in room A306. Therefore, we can conclude that roy is in roomA306. Starting from PERSON we can reach ROOM via Z (their phone number).The **com**position of two binary relations is another binary relation where the range of one is asubset of the domain of the other.ran hasPhone z dom phoneInRoomWe write:hasPhone ; phoneInRoom = { roy å A306, tom å A309, jim å A306, lee å A309 }where ; is called the sequential **com**position operator.

13EXERCISE 9.91 In a horse trials **com**petition, an owner may enter more than one horse and a rider may ride morethan one horse. Given the types [HORSE, OWNER, RIDER ] and the binary relationsentered : OWNER j HORSEentered = { sam å merryTom, sam å jumpingJack, pam å hissingSid,jan å ticTac, tel å isAGas }riddenBy : HORSE j RIDERriddenBy = { merryTom å jones, ticTac å jan, jumpingJack å french,isAGas å jan, hissingSid å fraser }write down the contents of ridesFor when ridesFor = riddenBy~ ; entered~.REVIEWWe find pairs of objects all around us: wives and their husbands, friends and their telephonenumbers, students and the subjects they study, customers and their bank accounts, guests and theirhotel reservations. We noted that a binary relation is just a set of ordered pairs. We saw how thefirst and second functions extracted the first and second coordinates of an ordered pair. We sawhow domain and range restriction work like database queries. We saw how relational image workslike a table lookup. We saw how the override operator works like database update. We noted thatinverse reverses the order of pairs in a binary relation, and that **com**position **com**bines two binaryrelations.Next we look at functions.BIBLIOGRAPHYBARDEN R., STEPNEY S. & COOPER D. 1994 Z in Practice Prentice Hall HemelHempstead UK pp 57, 179JACKY J. 1997 The Way of Z Cambridge University Press Cambridge UK pp 82SPIVEY J.M. 1992 The Z Notation Prentice Hall Hemel Hempstead UK pp 93, 95WOODCOCK J. & DAVIES J 1996 Using Z Prentice Hall Hemel Hempstead UK pp 83