1 / 18

Relations

Relations. 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

tolla
Download Presentation

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. Relations

  2. Definitions & Notation (1) • A binary relationfrom 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 S1,S2,…,Sn is a subset of S1 x S2 x S3 x … x Sn • Si are called the domains • n is called the degree

  3. Properties of Relations • Let r be a binary relation on set S Don’t confuse antisymmetric with “not symmetric”! Likewise irreflexive and “not reflexive”

  4. Relations built from Relations (1) • Closure • Definition: A binary relation r* on a set S is theclosure 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 rn, n = 1,2, … are define recursively by • r1 = r • ri+1 = ri  r

  5. What is the difference? • 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 ahas metb. • What is rn? • Those pairs (a,b) such that there are people x1,x2,…,xn-1 such that a has met x1, x1 has met x2, …, and xn-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!

  6. 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. 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. Application: Relation Representation • Enumeration • list the ordered pairs • Zero-One Matrix • Suppose r is a relation from A {a1,a2,…,am} to B {b1,b2,…,bn} • r can be represented by matrix Mr = [mij] 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. 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 a1, a2, …, an be an arbitrary listing of those elements • If a,x1,x2,…,xm-1,b is a sequence in the transitive closure, then the xis are called the interior elements of the sequence. • Warshall’s algorithm is based on the construction of a series of zero-one matrices (W0,W1, …, Wn) where • where • there is a sequence from xi to xj using only interior elements {x1,…,xk} Note: Wn = Mr*

  10. Application: Warshall’s Algorithm (2) • Example W0 is the matrix of the relation. W1 has a 1 as its (i,j)th entry if there is a sequence from vi to vj moving through only v1. Since no edges go into v2, W2 is the same as W1. W3 has a 1 as its (i,j)th entry if there is a sequence from vi to vj moving through only v1, v2, or v3. W4 has a 1 as its (i,j)th entry if there is a sequence from vi to vj moving through only v1, v2, v3, or v4.

  11. Application: Warshall’s Algorithm (3) • How do we calculate the Wis? • We can compute Wk directly from Wk-1 • Adding vk to Wk-1 can do one of two things: • Leave a sequence untouched (can’t use vk) • Wk at (i,j) is 1 only if Wk-1 at (i,j) is a 1 • Add a sequence from vi to vk to vj • Wk at (i,j) is 1 only if Wk-1 at (i,k) is 1 and Wk-1 at (k,j) is 1 • Algorithm • W = Mr • for k = 1 to n • for i = 1 to n • for j = 1 to n • wij = wij  (wik  wkj)

  12. 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 D1 x D2 x … x Dn where Di is the domain from which attribute Ai takes its value • therefore a table is an n-ary relation on Dis • E-R Relations have Di in one table the same as Di for the primary key of another • Joins the attributes into a new cross-product • therefore a relation is an m-ary relation on Dis

  13. 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 rc 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 Pi1,i2,…,im maps the n-tuple (a1,a2, …,an) to the m-tuple (ai1,ai2,…,aim) 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 jp(r,s), where p m and p  n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples (a1,a2,…,am-p,c1,c2,…,cp,b1,b2,…,bn-p) where the m-tuple (a1,a2,..,am-p,c1,c2,…,cp)  r and the n-tuple (c1,c2,…,cp,b1,b2,…,bn-p)  s. • leads to the SQL “from a,b,…,c” clause

  14. 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! antisymmetric transitive irreflexive

  15. 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. 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. 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. 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; } • }

More Related