Download Relations in Software Engineering: Properties and Operations and more Slides Software Engineering in PDF only on Docsity!
LECTURE 15: RELATIONS
Software Engineering
1 Introduction
- We saw in earlier lectures that a function is just a set of maplets, for example: tel == {mikew 7 โ 1531 , eric 7 โ 1489 }. A maplet is just an ordered pair, so another way of writing this is tel == {(mikew, 1531), (eric, 1489)} Formally, if f : T 1 โ T 1 , then f is a subset of the cartesian product of T 1 ร T 1 : f โ T 1 ร T 2. But functions canโt be defined in this way, as they must have the uniqueness property โ the following is not a function: {(mike, 1531), (eric, 1531)}.
- A more general way of capturing a relationship between two sets is to use a relation.
- Relations are similar to functions, but do not have the uniquness property.
2 Operations on Relations
- Everything you can do with a function, you can do with a relation:
dom R domain of R ran R range of R S R R with domain restricted to S R S R with range restricted to S S โ R R with S subtracted from domain of R R โ S R with S subtracted from range of R
R == {(a, 1), (a, 2), (b, 1), (c, 3)} then dom R = {a, b, c} ran R = { 1 , 2 , 3 } {a} R = {(a, 1), (a, 2)} {a, b} R = {(a, 1), (a, 2), (b, 1)} R { 1 } = {(a, 1), (b, 1)} R { 1 , 3 } = {(a, 1), (b, 1), (c, 3)} {a} โ R = {(b, 1), (c, 3)} {a, b} โ R = {(c, 3)} R โ { 1 } = {(a, 2), (b, 3)} R โ { 1 , 2 , 3 } = โ
.
f == {a 7 โ 1 , b 7 โ 1 } Now f is certainly a function (though it is not one-to-one.) But take the inverse of f : f โผ^ = {(1, a), (1, b)}. Although f โผ^ is a relation, it is not a function.
2.2 Relational Image
- Can we do relation application...?
- EXAMPLE. If
R == {(a, 1), (a, 2), (b, 1), (c, 3)} then R(a) =?
- No, we canโt, since it is not defined what the value of R should be here โ should it be 1 or 2 or both?
- However, we can do something a bit more general โ we can get the relation image of a value, using the image brackets โ(| |)โ.
3 Properties of Relations
- An interesting class of relations are those that are defined on just one set.
- EXAMPLE. Consider the set of binary relations on IN: IN โ IN.
- Definition: If R : T โ T, then we say R is a binary relation on T.
- We shall now consider the properties of these types of relation.
3.1 Reflexivity
- Suppose we have a relation R on T such that for any element x โ (dom R โช ran R), we have (x, x) โ R. Then R is said to be reflexive.
- EXAMPLES.
- {(1, 1), (1, 2), (2, 2), (1, 3), (3, 3)} ... is a reflexive relation on IN.
- {(1, 1), (1, 2), (2, 2), (1, 3)} ... is not a reflexive relation on IN (because (3, 3) is not a member).
- The subset relation, โ, is reflexive, because S โ S, for any set S.
- The โless thatโ relation, <, is not reflexive, because it is not true that n < n, for any value n.
- The โis the father ofโ relation (defined on the set of people) is not reflexive, because it is not true that p is the father of p, for any person p.
3.3 Transitivity
- A relation R is said to be transitive iff whenever we have (x, y) โ R and (y, z) โ R, we also have (x, z) โ R.
- EXAMPLES.
- {(2, 1), (1, 2), (2, 3), (3, 2)} ... is not a transitive relation, as it does not contain (1, 3).
- The less than relation, <, is transitive, since if a < b and b < c then a < c.
- The โis an ancestor ofโ relation is transitive.
- Equality is transitive.
3.4 Equivalence Relations
- Definition: If a relation is reflexive, symmetric, and transitive, then it is called an equivalence relation.
- The general idea behind equivalence relations is that they classify objects which are โalikeโ in some respect.
- EXAMPLES.
- Equality is an equivalence relation.
- The relation โis the same species asโ, defined on the set of all animals, is an equivalence relation.
- The relation โowns the same make car asโ, defined on the set of people, is an equivalence relation.
- Neither < nor โ are equivalence relations.
- Equivalence relation are often written โก or โผ.