Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definitions & Notation (1) –A binary relation from A to B is a subset of A x B –A binary relation on A is a subset of A x A –A binary relation is defined by Enumerating elements Relations definition: x r y  x + y is odd –Binary relations can be one-to-one one-to-many many-to-one many-to-many –An n-ary relation on S 1,S 2,…,S n is a subset of S 1 x S 2 x S 3 x … x S n S i are called the domains n is called the degree

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Properties of Relations –Let r be a binary relation on set S PropertyDefinition reflexive  x(x  S  (x,x)  r) symmetric  x  y(x  S  y  S  (x,y)  r  (y,x)  r ) transitive  x  y  z(x  S  y  S  z  S  (x,y)  r  (y,z)  r  (x,z)  r) antisymmetric  x  y(x  S  y  S  (x,y)  r  (y,x)  r  x = y) Don’t confuse antisymmetric with “not symmetric”! Likewise irreflexive and “not reflexive”

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations built from Relations (1) –Closure Definition: A binary relation r* on a set S is the closure of a relation r on S with respect to property p if –r* has property p –r  r* –r* is a subset of every other relation on S that includes r and has property p. –Composite Definition: Let r be a relation from A to B and s be a relation from B to C. The composite (r  s) is the relation consisting of ordered pairs (a,c) where a  A, c  C, and for which there exists an element b  B such that (a,b)  r and (b,c)  s. Definition: Let r be a relation on set A. The powers r n, n = 1,2, … are define recursively by –r 1 = r –r i+1 = r i  r

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations built from Relations (2) –Example: Let r be the relation on the set of all people in the world that contains (a,b) if a has met b. What is r n ? –Those pairs (a,b) such that there are people x 1,x 2,…,x n-1 such that a has met x 1, x 1 has met x 2, …, and x n-1 has met b. What is r*? –Those pairs (a,b) such that there is a sequence of people, starting with a and ending with b, in which each person in the sequence has met the next person in the sequence. Who cares? Wait for graphs! What is the difference?

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Types of Relations (1) –Partial Ordering Definition: A relation r on a set S is a partial ordering if it is reflexive, antisymmetric, and transitive. –(S, r) is called a partially ordered set or poset –The elements a and b of a poset (S, r) are called comparable if either (a,b)  r or (b,a)  r –Strict Partial Ordering Definition: A relation r on a set S is a strict partial ordering if it is irreflexive, antisymmetric, and transitive. –Total Ordering Definition: A relation r on a set S is a total ordering if it is (S,r) is a poset and every two elements of S are comparable. –Strict Total Ordering Definition: A relation r on a set S is a strict total ordering if it is (S,r) is a strict poset and every two elements of S are comparable.

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Types of Relations (2) –Equivalence Relations Definition: A relation r on a set S is an equivalence relation if it is reflexive, symmetric, and transitive. –Two elements related by an equivalence relation are said to be equivalent –The set of all elements that are related to an element a of S is called the equivalence class of a. –A partition of a set is a collection of disjoint nonempty subsets of S such that they have S as their union. The equivalence classes of r form a partition of S.

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Relation Representation –Enumeration list the ordered pairs –Zero-One Matrix Suppose r is a relation from A {a 1,a 2,…,a m } to B {b 1,b 2,…,b n } r can be represented by matrix M r = [m ij ] where –Digraph A relation r on a set S is represented by a directed graph (digraph) that has the elements of S as it vertices and the ordered pairs (a,b) where (a,b)  r, as edges. –So how do we represent digraphs in a computer? Later…

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Warshall’s Algorithm (1) –Stephen Warshall circa 1960 –Algorithm to find the transitive closure of a set S transitive closures are particularly interesting in that they provide “connection” information –Suppose r is a relation on S with n elements –Let a 1, a 2, …, a n be an arbitrary listing of those elements –If a,x 1,x 2,…,x m-1,b is a sequence in the transitive closure, then the x i s are called the interior elements of the sequence. –Warshall’s algorithm is based on the construction of a series of zero- one matrices (W 0,W 1, …, W n ) where where there is a sequence from x i to x j using only interior elements {x 1,…,x k } Note: W n = M r*

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Warshall’s Algorithm (2) –Example W 0 is the matrix of the relation. W 1 has a 1 as its (i,j) th entry if there is a sequence from v i to v j moving through only v 1. Since no edges go into v 2, W 2 is the same as W 1. W 3 has a 1 as its (i,j) th entry if there is a sequence from v i to v j moving through only v 1, v 2, or v 3. W 4 has a 1 as its (i,j) th entry if there is a sequence from v i to v j moving through only v 1, v 2, v 3, or v 4.

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Warshall’s Algorithm (3) –How do we calculate the W i s? We can compute W k directly from W k-1 Adding v k to W k-1 can do one of two things: –Leave a sequence untouched (can’t use v k ) W k at (i,j) is 1 only if W k-1 at (i,j) is a 1 –Add a sequence from v i to v k to v j W k at (i,j) is 1 only if W k-1 at (i,k) is 1 and W k-1 at (k,j) is 1 –Algorithm W = M r for k = 1 to n for i = 1 to n for j = 1 to n w ij = w ij  (w ik  w kj )

12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Relational Databases (1) –Recall from CS 185 E-R Modeling Attributes One-to-One, One-to-many, Many-to-one, and Many-to-Many –Both “Entity Sets” and “Relations” in Databases are relations in the mathematical sense Table is a set of n-tuples (rows) –No duplicates and No order –a table is a subset of D 1 x D 2 x … x D n where D i is the domain from which attribute A i takes its value –therefore a table is an n-ary relation on D i s E-R Relations have D i in one table the same as D i for the primary key of another –Joins the attributes into a new cross-product –therefore a relation is an m-ary relation on D i s

13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: Relational Databases (2) –Operations on Relations restrict –Let r be an n-ary relation and c a condition that elements of r must satisfy. Then the restrict operator r c maps the n- ary relation r to the n-ary relation of all n-tuples from r that satisfy the condition c. –leads to the SQL “where” clause project –The projection P i1,i2,…,im maps the n-tuple (a 1,a 2, …,a n ) to the m-tuple (a i1,a i2,…,a im ) where m  n. –leads to the SQL “select” clause join –Let r be a relation of degree m and s a relation of degree n. The join j p (r,s), where p  m and p  n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples (a 1,a 2,…,a m-p,c 1,c 2,…,c p,b 1,b 2,…,b n-p ) where the m-tuple (a 1,a 2,..,a m-p,c 1,c 2,…,c p )  r and the n-tuple (c 1,c 2,…,c p,b 1,b 2,…,b n-p )  s. –leads to the SQL “from a,b,…,c” clause

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: compareTo in JCF –Java Collections Framework provides a collection of container classes Example: HashMap, HashSet, … –Some collections are ordered Example: TreeSet, … –How does Java order the items in the collection? By use of the compareTo(Object obj) method By definition, compareTo(Object obj) must define a strict total ordering of all elements in the container –compareTo(Object obj) must meet x.compareTo(y) == -1 * y.compareTo(x) x.compareTo(y) == y.compareTo(z) == x.compareTo(z) x.equals(y)  x.compareTo(y) == 0 failure to meet these requirements will result in unexpected behavior –for example, Sets with duplicate objects! irreflexive antisymmetric transitive

15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: equals in Java (1) –According to Java API “The equals method implements an equivalence relation on non-null object references” Therefore a.equals(b) must behave the same as b.equals(a) –Most implementations fail on this property (1) class A { private int x; public boolean equals(Object that) { boolean isEqual = false; if ((that != null) && (that instanceof A)) { A castedThat = (A) that; // perform comparisons on private data isEqual = (this.x == castedThat.x); } return isEqual; } } Reflexive Symmetric Transitive

16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: equals in Java (2) –Most implementations fail on this property (2) class B extends A { private int y; public boolean equals(Object that) { boolean isEqual = false; if ((that != null) && (that instanceof B)) { B castedThat = (B) that; // perform comparisons on private data isEqual = (this.y == castedThat.y); } return (isEqual && super.equals(that)); } } instanceA.equals(instanceB) would return true, but instanceB.equals(instanceA) would fail the instanceof test and return false !

17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: equals in Java (3) –Correct Definition (1) abstract class T { public final boolean equals(Object that) { boolean isEqual = false; if ((that != null) && (that instanceof T)) { T castedThat = (T) that; if (this.getTypeEquiv().equals( castedThat.getTypeEquiv())) { isEqual = localEquals(that); } } return isEqual; } protected boolean localEquals(Object that) { return true; // to stop the chaining } abstract protected Class getTypeEquiv(); } Top of hierarchy!

18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Application: equals in Java (4) –Correct Definition (2) class A extends T { private int x; protected boolean localEquals(Object that) { A castedThat = (A) that; // perform comparisons on private data boolean isEqual = (this.x == castedThat.x); return (isEqual && super.localEquals(that)); } protected Class getTypeEquiv() { Class result = null; try { // will never fail, but must try/catch result = Class.forName(“A”); } catch (ClassNotFoundExeception e) { } return result; } }


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations."

Similar presentations


Ads by Google