Download presentation
Presentation is loading. Please wait.
1
Computing Fundamentals 1 Lecture 7 Relations
Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach to Discrete Math By David Gries and Fred B. Schneider
2
Relations and Functions
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.
3
Tuples1 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. 2.
4
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.
5
Cross Products A 2-tuple: S = {2,5} T = {1,2,3}
6
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}
7
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}
8
Some Theorems for Cross products
Membership: x,y ST xS yT Distributivity of over S (TU) (S T) (S U) (S T) U (S U) (T U) Monotonicity T U (S T) (S U)
9
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}
10
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)
11
Relation as Directed Graph
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. 2 1 3 6 4 5 Set = {1,2,3,4,5,6} Relation = {<1,1>,<1,3>,<2,5>,<2,1>, <5,3>}
12
Diagraph of Relations Given the relation
On the set {1,2,3} Below is the digraph of R. 1 2 3
13
Digraph Relations is a set of ordered pairs:
Q = {<1, 4> ,<2, 4>} 2 2 1 1 3 4 4 (a) (b)
14
Digraphs Relations 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>} 2 2 1 1 3 3 4 (c) (d)
15
A set of ordered pairs A 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>} 2 1 2 1 3 3
16
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>}
17
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. 1. Every positive number x has two square roots.
18
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)} The Greek letter is pronounced rho (r in English) The Greek letter is pronounced sigma (s in English)
19
Relations Dom. = {b:B | (c|: b c)} Ran. ={c:C | (b|: b c)}
20
Classes of relations Name Property Reflexive (b| b b)
Irreflexive (b| (b b)) Symmetric (b,c | (b c) (c b))
21
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) We will take the terms ‘non-symmetric’ and ‘not symmetric’ to mean asymmetric as defined above. Strictly speaking the ‘asymmetry’ described in the course text is stronger than ‘not symmetric’. There are definitions of ‘non-symmetric’ that may permit some pairs to have symmetry. Hence, we are omitting the mixed case where the relation holds for some elements of the relation, but not for others. For example, A = {1,2,3} = {<1,2>, <2,1>,<2,3>} Then the relation is neither symmetric nor antisymmetric, nor does it fit the definition of asymmetric above, which is kind of the opposite to symmetry. The relation < on integers is asymmetric. The relation <= on integers is antisymmetric. The relation ‘a fatherOf b’ is asymmetric. The stronger course definition of asymmetry implies ‘non-symmetric’, but the reverse implication does not hold.
22
Relations Discussion Common relations
23
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>. Recall that we will take the terms ‘non-symmetric’ and ‘not symmetric’ to mean asymmetric as defined above. The love(X,Y) relation is neither symmetric nor antisymmetric,
24
Relation Properties 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>).
25
Relation Properties 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.
26
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>}
27
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 <.
28
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, Introductory Logic and Sets for Computer Scientists Addison Wesley Longman, 1999.
29
Closures Reflexive Closure:
A={1,2,3}. 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>}
30
Functions Functions can be considered as a restricted form of relation. This is useful because the terminology and theory of relations carries over to function. In programming languages like Python or CafeOBJ a function can have a signature, which includes its name, type of argument and the type of the expected return value.
31
Fibonacci Function in Python and CafeOBJ
Signature of a function consists of a name, argument(s) type, and return type Function Name Argument type p is a predecessor function mod* FIBO-NAT { pr(NAT) op fib : Nat -> Nat var N : Nat eq fib(0) = 0 . eq fib(1) = 1 . ceq fib(N) = fib(p(N)) + fib(p(p(N))) if N > 1 .} def fib(n): a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() Argument variable
32
Functions as relations
While we can consider functions as a rule we can also think of functions as a binary relation B C, that contains all pairs <b,c> such that f.b=c. A relation can have distinct values c and c’ that satisfy bfc and bfc’, but a function cannot. c Informally we can say that there is ‘no fan out’ from domain to range. Allowed for relations but not allowed for functions b c’
33
Types of Function Surjective : A function f:A → B is an surjective, or onto, function if the range of f equals the codomain of f. Injective : A function f : A → B is injective, or one-to-one, if no member of B is the image under f of two distinct elements of A. Bijective : A function f : A → B is bijective (a bijection) if it is both surjective and injective. A bijective function has an inverse.
34
A Function Given the data type Side. Side = left right the function
driveON Country Side Representing the side of the road vehicles drive on is a surjection, because the range includes all the values of the type Side. Several countries may allow driving on the same side, It would usually be considered total because driveOn should be defined in all countries.
35
A function f from X to Y is a relation from X to Y having the
Some examples, range on the left domain on the right A function is called a bijection , if it is onto and one-to-one. A bijective function has an inverse Function Application A function f from X to Y is a relation from X to Y having the properties: 1. The domain of f is in X. 2. If (x,y),(x,y’)f, then y=y’ (no fan out from domain). A total function from X to Y is denoted f: X Y.
36
Injective Surjective For each map state whether or not it represents a function. If it is a function state whether or not it represents a surjective, injective or bijective function. In each case explain your reasoning.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.