1 / 15

Simple Operations with Relations

Simple Operations with Relations. All set operations may be performed on relations. Let R1 and R2 represent two relations. R1 ∩ R2 defines a relation a(R1 ∩ R2)b = (aR1b) / ( aR2b) R1 U R2 defines a relation a(R1 U R2)b = (aR1b) / (aR2b)

ravi
Download Presentation

Simple Operations with Relations

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Operations with Relations All set operations may be performed on relations. Let R1 and R2 represent two relations. R1 ∩ R2 defines a relation a(R1 ∩ R2)b = (aR1b) /\ ( aR2b) R1 U R2 defines a relation a(R1 U R2)b = (aR1b) \/ (aR2b) R1 - R2 defines a relation a(R1–R2)b = (aR1b) /\ ~(aR2b) ~R1 defines a relation where a (~R1) b = ~ (a R1 b) but in the rest of the cross product , A x B, over which R1 is defined. Note: a R1 b is the same as saying (a,b) is an element of R1

  2. Examples of Simple Operations • Let A = {a1, a2, a3, a4}, B = {b1, b2, b3},X= {a1,a2,a3}, and Y = {b1, b2}; so X is subset of A & Y is subset of B. • Let R1: A x B , R1 = { (a1,b1), (a4, b3) } - “married” • Let R2: X x Y, R2 = {(a1,b1), (a2,b1), (a3,b2)} – “live together” • R1 U R2 = { (a1,b1), (a4,b3), (a2,b1), (a3,b2) } • married or live together ; • R1 ∩ R2 = { (a1, b1) } • Married and live together ; • R1 – R2 = { (a4,b3) } • Married but don’t live together ; • ~ R2 = { (a1,b2), (a2,b2), (a3,b1) } • Those in X x Y that don’t live together

  3. Creation of “new” Relation • Let FP = { Jane, Tom, Kim, Steve, Jill, Bob, John} • Let BBrbe blood related brother-of : FP x FP, and BBr = { (Tom, Jane), (Tom,Jill), (Bob, Steve), (Steve, Bob)} • Let BSisbe blood related sister-of : FP x FP, and BSis = {(Jane,Tom), (Jill,Tom), (Jane, Jill), (Jill, Jane) } • Then what is BBr U BSis ? • BBr U BSis = { (Tom, Jane), (Tom, Jill), (Bob, Steve), (Steve, Bob), (Jane, Tom), (Jill, Tom), (Jane, Jill), (Jill, Jane) } • BBr U BSis = blood related “siblings” “same,” but order matters

  4. Composite Relation • Let R be a relation and S be a relation, then the composition, R;S , is defined as follows: • x ( R;S) y   (z ) ( xRz /\ zSy) • Note: that the 1st element R;S is from the 1st element of R and the 2nd element of R;Sis from the 2nd element of S. • Example : • Let modules = {a, b, c, d} • 1st_level_calls = { (a,b), (c,d), (d,b), (b,c) } • 2nd_level_depth = 1st_level_calls ; 1st_level_calls • = { (a,c), (c,b), (d,c), (b,d) } Also note: The range of R must be of the same type as domain of S Look at the definition of R1;R2 on page 121 of your book; if you were critical, how would you modify the definition? ----- do we need to add “ (t1, t3)”

  5. More on Composite of Relations • Consider another example : • Let Functions or “Methods” = {f1, f2, f3, f4} • Let Objects = { o1, o2, o3} • Let Packages = { p1, p2 } • Let R1(used by) = {(f1,o1), (f2,o1), (f3,o2), (f2,o3), (f4,o3) } • Let R2 (packaged in) = {(o1,p1), (o2,p1), (o3,p2) } • Let R3(functionally packaged) = R1;R2 • R3 = { (f1,p1), (f2,p1), (f3,p1), (f2,p2), (f4,p2) } • this says that the function, f2, needs to be functionally packaged in both P1, and P2 since it is used by both o1 and o3.

  6. Composite Relation and Tables We can represent a relation with a table, as shown earlier. The following are the tabular representations of the R1 (used by), R2 (packaged in), and R3 (functionally packaged) relations. R1 o1 o2 o3 R2 R3 p1 p2 p1 p2 o1 1 0 f1 1 0 0 f1 1 0 f2 1 0 1 o2 1 f2 1 1 0 0 0 f3 0 1 f3 1 0 1 o3 f4 0 0 1 f4 0 1 Note: we use 1 to represent that the ordered pair (f1,o1) R1, and 0 to represent that the ordered pair (f1,o2) R1.

  7. Composite Relation and Matrices • Can we turn the table into a matrix and define the composite operation in some “matrix” operation? • The answer is yes ----- define the ; operator as a matrix multiplication where xij “times” yji is the “logical and” and the “sum“ is the “logical or” R1 = X R2 = Y R3 = R1; R2 = Z 0 0 1 0 1 1 0 1 1 1 1 ; = 0 1 0 0 0 0 1 1 1 0 0 0 1 1 0 zij = \/ ( xik /\ ykj ) , where the logical \/ is performed over k=1, - - #(“range set”)

  8. Homogeneous Relation • A relation may be composed with itself. • In order for a relation, R, to be composed with itself, the domain type and the range type of R must be the same. Such an R is called a Homogeneous relation. • e.g. R : Integer x Integer • e.g. Consider modules = {m1,m2.m3.m4}; R: modules x modules and R “calls relation” = { (m1,m3), (m3,m2), (m3,m4), (m4,m2) } • then R ; R = { (m1,m2), (m1,m4), (m3,m2) } • and R ; R ; R = { (m1,m2) } • and R ; R ; R ; R = { } Is this termination good or bad, from programming systems perspective?

  9. Homogeneous Relation (cont.) • The previous composites of Homogenous Relation may also be represented with matrix “operations” 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 ; = 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 R;R R R We may write a 2-fold composition R;R with a superscript R2, and an n-fold composition as Rn

  10. Identity Relation • An Identity relation is a Homogeneous relation, id: A x A, such that • id A = { a: A (a,a) } • Example : A = { 3, 4, 53 } , then id A = { (3,3), (4,4), (53,53) } • Example : X = {mod1, mod2} id X = { (mod1,mod1), (mod2, mod2) } • In the above examples, the relation, “id” may beinterpreted as “equal”. The relations will hold true with that interpretation. • Note that : id -1 = id and id;id; - - - ;id = idn = id

  11. Reflexive Relation • A homogenous relation R : Y x Y is reflexive if for every y in Y, (y,y) R. • R is reflexive  \/(y) (yRy) • Example 1: let R: Int x Int and R is “=“. Then \/ (i) (i = i) • Example 2 : let R : Int x Int and R is “>=“. Then \/ (i) (i >= i) • A relation R: Y x Y is irreflexive if for every y , (y,y) R. • Example : let R: Int x Int and R is “>”. Then \/ (y) ~(y>y) Note: Reflexive and Irreflexive states that its condition is true for all its domain, or all the y’s for the above examples.

  12. Reflexive relation • Some relations are neither reflexive nor irreflexive. • Consider R = { (2,2), (1,3), (3,2) } defined over the set {1,2,3} • R is not reflexive because it does not contain (1,1) and (3,3). • R is also not irreflexivebecause it contains (2,2) • For the above R to be reflexive it must contain, at least, {(1,1), (2,2), (3,3) }, and in matrix form it will look as follows: 1 0 0 Can it contain more then the identity relation? R = 0 1 0 0 0 1

  13. Symmetric relation • A homogeneous relation R : Y x Y is symmetric, if for every y1, y2 in Y, y1 R y2 -> y2 R y1 • Example : R : M x M where M= {m1,m2,m3,m4}, and R = {(m1,m2}, (m2,m1), (m2,m3), (m3,m2)}. R may be interpreted as “directly connected.” • The above R’s matrix representation would look as follows 0 1 0 0 Note that if xij in this matrix has a value of ‘1’ then so does xji. R = 1 0 1 0 0 1 0 0 0 0 0 0

  14. Transitive relation • A homogeneous relationR: Y x Y is transitive, if for every x, y, z in Y, whenever xRy and yRz then xRz. • R is transitive  \/x \/y \/z (xRy /\ yRz -> xRz) • Example : Let N1 = {2,8,33,9,11} where R: N1 x N1 and R is the > relation. Then: • 11> 9 and 9>8 -> 11>8 • 33> 8 and 8>2 -> 33>2

  15. Interesting “Example” (from p. 124 of text book) • Let Mod = { m1,m2,m3} and Var = {v1,v2,v3,v4} and a relation Update1 : Mod x Var. More specifically, Update1 = {(m1,v1), (m2,v2), (m2,v3), (m3,v4), (m2,v1) } • Define a new relation, Update2: Mod x Mod, to mean two modules are related (“coupled”) if they update the same variable. • Update2 = { a, b Mod I (v) [ (a,v) Update1 /\ (b,v)  Update1] (a,b) } • Update2 = Update1;Update1-1 \ id (Update) P125 of text, is this correct ? Update1 Update1-1 To get Update2, we need to get rid of the diagonal 1’s, which represent “identity” of Mod ? 1 0 0 1 1 0 1 1 0 0 ; = 1 1 1 0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 1 0 0 1 So, Update 2 = Update1 ; Update1-1 \ id (Mod) ? ; what do you think?

More Related