Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relations Rosen 5 th ed., ch. 7. Binary Relations Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is.

Similar presentations


Presentation on theme: "Relations Rosen 5 th ed., ch. 7. Binary Relations Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is."— Presentation transcript:

1 Relations Rosen 5 th ed., ch. 7

2 Binary Relations Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is (can be identified with) a subset of the set A×B.Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is (can be identified with) a subset of the set A×B. –E.g., let < : N↔N :≡ {(n,m) | n < m} The notation a R b or aRb means that (a,b)  R.The notation a R b or aRb means that (a,b)  R. –E.g., a < b means (a,b)  < If aRb we may say “a is related to b (by relation R)”,If aRb we may say “a is related to b (by relation R)”, –or just “a relates to b (under relation R)”. A binary relation R corresponds to a predicate function P R :A×B→{T,F} defined over the 2 sets A,B;A binary relation R corresponds to a predicate function P R :A×B→{T,F} defined over the 2 sets A,B; –e.g., predicate “eats” :≡ {(a,b)| organism a eats food b}

3 Complementary Relations Let R:A,B be any binary relation.Let R:A,B be any binary relation. Then, R:A×B, the complement of R, is the binary relation defined by R :≡ {(a,b) | (a,b)  R} = (A×B) − RThen, R:A×B, the complement of R, is the binary relation defined by R :≡ {(a,b) | (a,b)  R} = (A×B) − R Note this is just R if the universe of discourse is U = A×B; thus the name complement.Note this is just R if the universe of discourse is U = A×B; thus the name complement. Note the complement of R is R.Note the complement of R is R. < = {(a,b) | (a,b)  <} = {(a,b) | ¬a<b} = ≥ Example: < = {(a,b) | (a,b)  <} = {(a,b) | ¬a<b} = ≥

4 Inverse Relations Any binary relation R:A×B has an inverse relation R −1 :B×A, defined by R −1 :≡ {(b,a) | (a,b)  R}.Any binary relation R:A×B has an inverse relation R −1 :B×A, defined by R −1 :≡ {(b,a) | (a,b)  R}. E.g., a} = >. E.g., if R:People→Foods is defined by a R b  a eats b, then: b R −1 a  b is eaten by a. (Passive voice.)E.g., if R:People→Foods is defined by a R b  a eats b, then: b R −1 a  b is eaten by a. (Passive voice.)

5 Relations on a Set A (binary) relation from a set A to itself is called a relation on the set A.A (binary) relation from a set A to itself is called a relation on the set A. E.g., the “ < ” relation from earlier was defined as a relation on the set N of natural numbers.E.g., the “ < ” relation from earlier was defined as a relation on the set N of natural numbers. The (binary) identity relation I A on a set A is the set {(a,a)|a  A}.The (binary) identity relation I A on a set A is the set {(a,a)|a  A}.

6 Reflexivity A relation R on A is reflexive if  a  A, aRa.A relation R on A is reflexive if  a  A, aRa. –E.g., the relation ≥ :≡ {(a,b) | a≥b} is reflexive. A relation R is irreflexive iff its complementary relation R is reflexive.A relation R is irreflexive iff its complementary relation R is reflexive. –Example: < is irreflexive, because ≥ is reflexive. –Note “irreflexive” does NOT mean “not reflexive”! For example: the relation “likes” between people is not reflexive, but it is not irreflexive either.For example: the relation “likes” between people is not reflexive, but it is not irreflexive either. –Since not everyone likes themselves, but not everyone dislikes themselves either!

7 Symmetry & Antisymmetry A binary relation R on A is symmetric iff R = R −1, that is, if (a,b)  R ↔ (b,a)  R.A binary relation R on A is symmetric iff R = R −1, that is, if (a,b)  R ↔ (b,a)  R. –E.g., = (equality) is symmetric. < is not. –“is married to” is symmetric, “likes” is not. A binary relation R is antisymmetric if  a≠b, (a,b)  R → (b,a)  R.A binary relation R is antisymmetric if  a≠b, (a,b)  R → (b,a)  R. –Examples: < is antisymmetric, “likes” is not. –Exercise: prove this definition of antisymmetric is equivalent to the statement that R  R −1  I A.

8 Transitivity A relation R is transitive iff (for all a,b,c) (a,b)  R  (b,c)  R → (a,c)  R.A relation R is transitive iff (for all a,b,c) (a,b)  R  (b,c)  R → (a,c)  R. A relation is intransitive iff it is not transitive.A relation is intransitive iff it is not transitive. Some examples:Some examples: –“is an ancestor of” is transitive. –“likes” between people is intransitive. –“is located within 1 mile of” is… ?

9 Totality A relation R:A×B is total if for every a  A, there is at least one b  B such that (a,b)  R.A relation R:A×B is total if for every a  A, there is at least one b  B such that (a,b)  R. If R is not total, then it is called strictly partial.If R is not total, then it is called strictly partial. A partial relation is a relation that might be strictly partial. (Or, it might be total.)A partial relation is a relation that might be strictly partial. (Or, it might be total.) –In other words, all relations are considered “partial.”

10 Functionality A relation R:A×B is functional if, for any a  A, there is at most 1 b  B such that (a,b)  R.A relation R:A×B is functional if, for any a  A, there is at most 1 b  B such that (a,b)  R. –“R is functional”   a  A: ¬  b 1 ≠b 2  B: aRb 1  aRb 2. –Iff R is functional, then it corresponds to a partial function R:A→B where R(a)=b  aRb; e.g.where R(a)=b  aRb; e.g. –E.g., The relation aRb :≡ “a + b = 0” yields the function −(a) = b. R is antifunctional if its inverse relation R −1 is functional.R is antifunctional if its inverse relation R −1 is functional. –Note: A functional relation (partial function) that is also antifunctional is an invertible partial function. R is a total function R:A→B if it is both functional and total, that is, for any a  A, there is exactly 1 b such that (a,b)  R. I.e.,  a  A: ¬  !b: aRb.R is a total function R:A→B if it is both functional and total, that is, for any a  A, there is exactly 1 b such that (a,b)  R. I.e.,  a  A: ¬  !b: aRb. –If R is functional but not total, then it is a strictly partial function. –Exercise: Show that iff R is functional and antifunctional, and both it and its inverse are total, then it is a bijective function.

11 Lambda Notation The lambda calculus is a formal mathematical language for defining and operating on functions.The lambda calculus is a formal mathematical language for defining and operating on functions. –It is the mathematical foundation of a number of functional (recursive function-based) programming languages, such as LISP and ML. It is based on lambda notation, in which “λa: f(a)” is a way to denote the function f without ever assigning it a specific symbol.It is based on lambda notation, in which “λa: f(a)” is a way to denote the function f without ever assigning it a specific symbol. –E.g., (λx. 3x 2 +2x) is a name for the function f:R→R where f(x)=3x 2 +2x. Lambda notation and the “such that” operator “  ” can also be used to compose an expression for the function that corresponds to any given functional relation.Lambda notation and the “such that” operator “  ” can also be used to compose an expression for the function that corresponds to any given functional relation. –Let R:A×B be any functional relation on A,B. –Then the expression (λa: b  aRb) denotes the function f:A→B where f(a) = b iff aRb. E.g., If I write: f :≡ (λa: b  a+b = 0), this is one way of defining the function f(a)=−a.E.g., If I write: f :≡ (λa: b  a+b = 0), this is one way of defining the function f(a)=−a.

12 Composite Relations Let R:A×B, and S:B×C. Then the composite S  R of R and S is defined as:Let R:A×B, and S:B×C. Then the composite S  R of R and S is defined as: S  R = {(a,c) |  b: aRb  bSc} Note that function composition f  g is an example.Note that function composition f  g is an example. Exer.: Prove that R:A×A is transitive iff R  R = R.Exer.: Prove that R:A×A is transitive iff R  R = R. The n th power R n of a relation R on a set A can be defined recursively by: R 0 :≡ I A ;R n+1 :≡ R n  R for all n≥0.The n th power R n of a relation R on a set A can be defined recursively by: R 0 :≡ I A ;R n+1 :≡ R n  R for all n≥0. –Negative powers of R can also be defined if desired, by R −n :≡ (R −1 ) n.

13 §7.2: n-ary Relations An n-ary relation R on sets A 1,…,A n, written (with signature) R:A 1 ×…×A n or R:A 1,…,A n, is simply a subset R  A 1 × … × A n.An n-ary relation R on sets A 1,…,A n, written (with signature) R:A 1 ×…×A n or R:A 1,…,A n, is simply a subset R  A 1 × … × A n. The sets A i are called the domains of R.The sets A i are called the domains of R. The degree of R is n.The degree of R is n. R is functional in the domain A i if it contains at most one n-tuple (…, a i,…) for any value a i within domain A i.R is functional in the domain A i if it contains at most one n-tuple (…, a i,…) for any value a i within domain A i.

14 Relational Databases A relational database is essentially just an n-ary relation R.A relational database is essentially just an n-ary relation R. A domain A i is a primary key for the database if the relation R is functional in A i.A domain A i is a primary key for the database if the relation R is functional in A i. A composite key for the database is a set of domains {A i, A j, …} such that R contains at most 1 n-tuple (…,a i,…,a j,…) for each composite value (a i, a j,…)  A i ×A j ×…A composite key for the database is a set of domains {A i, A j, …} such that R contains at most 1 n-tuple (…,a i,…,a j,…) for each composite value (a i, a j,…)  A i ×A j ×…

15 Selection Operators Let A be any n-ary domain A=A 1 ×…×A n, and let C:A→{T,F} be any condition (predicate) on elements (n-tuples) of A.Let A be any n-ary domain A=A 1 ×…×A n, and let C:A→{T,F} be any condition (predicate) on elements (n-tuples) of A. Then, the selection operator s C is the operator that maps any (n-ary) relation R on A to the n-ary relation of all n-tuples from R that satisfy C.Then, the selection operator s C is the operator that maps any (n-ary) relation R on A to the n-ary relation of all n-tuples from R that satisfy C. –I.e.,  R  A, s C (R) = {a  R | s C (a) = T}

16 Selection Operator Example Suppose we have a domain A = StudentName × Standing × SocSecNosSuppose we have a domain A = StudentName × Standing × SocSecNos Suppose we define a certain condition on A, UpperLevel(name,standing,ssn) :≡ [(standing = junior)  (standing = senior)]Suppose we define a certain condition on A, UpperLevel(name,standing,ssn) :≡ [(standing = junior)  (standing = senior)] Then, s UpperLevel is the selection operator that takes any relation R on A (database of students) and produces a relation consisting of just the upper- level classes (juniors and seniors).Then, s UpperLevel is the selection operator that takes any relation R on A (database of students) and produces a relation consisting of just the upper- level classes (juniors and seniors).

17 Projection Operators Let A = A 1 ×…×A n be any n-ary domain, and let {i k }=(i 1,…,i m ) be a sequence of indices all falling in the range 1 to n,Let A = A 1 ×…×A n be any n-ary domain, and let {i k }=(i 1,…,i m ) be a sequence of indices all falling in the range 1 to n, –That is, where 1 ≤ i k ≤ n for all 1 ≤ k ≤ m. Then the projection operator on n-tuples is defined by:Then the projection operator on n-tuples is defined by:

18 Projection Example Suppose we have a ternary (3-ary) domain Cars=Model×Year×Color. (note n=3).Suppose we have a ternary (3-ary) domain Cars=Model×Year×Color. (note n=3). Consider the index sequence {i k }= 1,3. (m=2)Consider the index sequence {i k }= 1,3. (m=2) Then the projection P simply maps each tuple (a 1,a 2,a 3 ) = (model,year,color) to its image:Then the projection P simply maps each tuple (a 1,a 2,a 3 ) = (model,year,color) to its image: This operator can be usefully applied to a whole relation R  Cars (a database of cars) to obtain a list of the model/color combinations available.This operator can be usefully applied to a whole relation R  Cars (a database of cars) to obtain a list of the model/color combinations available. {ik}{ik}

19 Join Operator Puts two relations together to form a sort of combined relation.Puts two relations together to form a sort of combined relation. If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ).If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ). –A, B, and C here can also be sequences of elements (across multiple fields), not just single elements.

20 Join Example Suppose R 1 is a teaching assignment table, relating Professors to Courses.Suppose R 1 is a teaching assignment table, relating Professors to Courses. Suppose R 2 is a room assignment table relating Courses to Rooms,Times.Suppose R 2 is a room assignment table relating Courses to Rooms,Times. Then J(R 1,R 2 ) is like your class schedule, listing (professor,course,room,time).Then J(R 1,R 2 ) is like your class schedule, listing (professor,course,room,time).

21 §7.3: Representing Relations Some ways to represent n-ary relations:Some ways to represent n-ary relations: –With an explicit list or table of its tuples. –With a function from the domain to {T,F}. Or with an algorithm for computing this function.Or with an algorithm for computing this function. Some special ways to represent binary relations:Some special ways to represent binary relations: –With a zero-one matrix. –With a directed graph.

22 Using Zero-One Matrices To represent a binary relation R:A×B by an |A|×|B| 0-1 matrix M R = [m ij ], let m ij = 1 iff (a i,b j )  R.To represent a binary relation R:A×B by an |A|×|B| 0-1 matrix M R = [m ij ], let m ij = 1 iff (a i,b j )  R. E.g., Suppose Joe likes Susan and Mary, Fred likes Mary, and Mark likes Sally.E.g., Suppose Joe likes Susan and Mary, Fred likes Mary, and Mark likes Sally. Then the 0-1 matrix representation of the relation Likes:Boys×Girls relation is:Then the 0-1 matrix representation of the relation Likes:Boys×Girls relation is:

23 Zero-One Reflexive, Symmetric Terms: Reflexive, non-reflexive, irreflexive, symmetric, asymmetric, and antisymmetric.Terms: Reflexive, non-reflexive, irreflexive, symmetric, asymmetric, and antisymmetric. –These relation characteristics are very easy to recognize by inspection of the zero-one matrix. Reflexive: all 1’s on diagonal Irreflexive: all 0’s on diagonal Symmetric: all identical across diagonal Antisymmetric: all 1’s are across from 0’s any- thing anything

24 Using Directed Graphs A directed graph or digraph G=(V G,E G ) is a set V G of vertices (nodes) with a set E G  V G ×V G of edges (arcs,links). Visually represented using dots for nodes, and arrows for edges. Notice that a relation R:A×B can be represented as a graph G R =(V G =A  B, E G =R).A directed graph or digraph G=(V G,E G ) is a set V G of vertices (nodes) with a set E G  V G ×V G of edges (arcs,links). Visually represented using dots for nodes, and arrows for edges. Notice that a relation R:A×B can be represented as a graph G R =(V G =A  B, E G =R). Matrix representation M R : Graph rep. G R : Joe Fred Mark Susan Mary Sally Node set V G (black dots) Edge set E G (blue arrows)

25 Digraph Reflexive, Symmetric It is extremely easy to recognize the reflexive/irreflexive/ symmetric/antisymmetric properties by graph inspection.       Reflexive: Every node has a self-loop Irreflexive: No node links to itself Symmetric: Every link is bidirectional   Antisymmetric: No link is bidirectional    These are asymmetric & non-antisymmetricThese are non-reflexive & non-irreflexive

26 §7.4: Closures of Relations For any property X, the “X closure” of a set A is defined as the “smallest” superset of A that has the given property.For any property X, the “X closure” of a set A is defined as the “smallest” superset of A that has the given property. The reflexive closure of a relation R on A is obtained by adding (a,a) to R for each a  A. I.e., it is R  I AThe reflexive closure of a relation R on A is obtained by adding (a,a) to R for each a  A. I.e., it is R  I A The symmetric closure of R is obtained by adding (b,a) to R for each (a,b) in R. I.e., it is R  R −1The symmetric closure of R is obtained by adding (b,a) to R for each (a,b) in R. I.e., it is R  R −1 The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in R.The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in R. –I.e., it is

27 Paths in Digraphs/Binary Relations A path of length n from node a to b in the directed graph G (or the binary relation R) is a sequence (a,x 1 ), (x 1,x 2 ), …, (x n−1,b) of n ordered pairs in E G (or R).A path of length n from node a to b in the directed graph G (or the binary relation R) is a sequence (a,x 1 ), (x 1,x 2 ), …, (x n−1,b) of n ordered pairs in E G (or R). –An empty sequence of edges is considered a path of length 0 from a to a. –If any path from a to b exists, then we say that a is connected to b. (“You can get there from here.”) A path of length n≥1 from a to itself is called a circuit or a cycle.A path of length n≥1 from a to itself is called a circuit or a cycle. Note that there exists a path of length n from a to b in R if and only if (a,b)  R n.Note that there exists a path of length n from a to b in R if and only if (a,b)  R n.

28 Simple Transitive Closure Alg. A procedure to compute R * with 0-1 matrices. procedure transClosure(M R :rank-n 0-1 mat.) A := B := M R ; for i := 2 to n begin A := A for i := 2 to n begin A := A ⊙ M R ; B := B  A {join} end return B {Alg. takes Θ(n 4 ) time} {note A represents R i, B represents }

29 A Faster Transitive Closure Alg. procedure transClosure(M R :rank-n 0-1 mat.) A := M R ; for i := 1 to log 2 n  begin A := A for i := 1 to  log 2 n  begin A := A ⊙ (A+I n ); {A represents } end return A {Alg. takes only Θ(n 3 log n) time!}

30 Roy-Warshall Algorithm Uses only Θ(n 3 ) operations!Uses only Θ(n 3 ) operations! Procedure Warshall(M R : rank-n 0-1 matrix) 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 ) return W {this represents R * } w ij = 1 means there is a path from i to j going only through nodes ≤k

31 §7.5: Equivalence Relations An equivalence relation (e.r.) on a set A is simply any binary relation on A that is reflexive, symmetric, and transitive.An equivalence relation (e.r.) on a set A is simply any binary relation on A that is reflexive, symmetric, and transitive. –E.g., = itself is an equivalence relation. –For any function f:A→B, the relation “have the same f value”, or = f :≡ {(a 1,a 2 ) | f(a 1 )=f(a 2 )} is an equivalence relation, e.g., let m=“mother of” then = m = “have the same mother” is an e.r.e.g., let m=“mother of” then = m = “have the same mother” is an e.r.

32 Equivalence Relation Examples “Strings a and b are the same length.”“Strings a and b are the same length.” “Integers a and b have the same absolute value.”“Integers a and b have the same absolute value.” “Real numbers a and b have the same fractional part.” (i.e., a − b  Z)“Real numbers a and b have the same fractional part.” (i.e., a − b  Z) “Integers a and b have the same residue modulo m.” (for a given m>1)“Integers a and b have the same residue modulo m.” (for a given m>1)

33 Equivalence Classes Let R be any equiv. rel. on a set A.Let R be any equiv. rel. on a set A. The equivalence class of a, [a] R :≡ { b | aRb } (optional subscript R)The equivalence class of a, [a] R :≡ { b | aRb } (optional subscript R) –It is the set of all elements of A that are “equivalent” to a according to the eq.rel. R. –Each such b (including a itself) is called a representative of [a] R. Since f(a)=[a] R is a function of a, any equivalence relation R can be defined using aRb :≡ “a and b have the same f value”, given f.Since f(a)=[a] R is a function of a, any equivalence relation R can be defined using aRb :≡ “a and b have the same f value”, given f.

34 Equivalence Class Examples “Strings a and b are the same length.”“Strings a and b are the same length.” –[a] = the set of all strings of the same length as a. “Integers a and b have the same absolute value.”“Integers a and b have the same absolute value.” –[a] = the set {a, −a} “Real numbers a and b have the same fractional part (i.e., a − b  Z).”“Real numbers a and b have the same fractional part (i.e., a − b  Z).” –[a] = the set {…, a−2, a−1, a, a+1, a+2, …} “Integers a and b have the same residue modulo m.” (for a given m>1)“Integers a and b have the same residue modulo m.” (for a given m>1) –[a] = the set {…, a−2m, a−m, a, a+m, a+2m, …}

35 Partitions A partition of a set A is the set of all the equivalence classes {A 1, A 2, … } for some equivalence relation on A.A partition of a set A is the set of all the equivalence classes {A 1, A 2, … } for some equivalence relation on A. –The A i ’s are all disjoint and their union = A. They “partition” the set into pieces. Within each piece, all members of that set are equivalent to each other.They “partition” the set into pieces. Within each piece, all members of that set are equivalent to each other.

36 §7.6: Partial Orderings A relation R on A is called a partial ordering or partial order iff it is reflexive, antisymmetric, and transitive.A relation R on A is called a partial ordering or partial order iff it is reflexive, antisymmetric, and transitive. –We often use a symbol looking something like ≼ (or analogous shapes) for such relations. –Examples: ≤, ≥ on real numbers, ,  on sets. –Another example: the divides relation | on Z +. Note it is not necessarily the case that either a ≼ b or b ≼ a.Note it is not necessarily the case that either a ≼ b or b ≼ a. A set A together with a partial order ≼ on A is called a partially ordered set or poset and is denoted (A, ≼ ).A set A together with a partial order ≼ on A is called a partially ordered set or poset and is denoted (A, ≼ ).

37 Posets as Noncyclical Digraphs There is a one-to-one correspondence between posets and the reflexive+transitive closures of noncyclical digraphs.There is a one-to-one correspondence between posets and the reflexive+transitive closures of noncyclical digraphs. –Noncyclical: Containing no directed cycles. Example: consider the poset ({0,…,10},|)Example: consider the poset ({0,…,10},|) –Its minimal digraph: 1 248 3 5 7 6 9 10 0

38 More to come… More slides on partial orderings to be added in the future…More slides on partial orderings to be added in the future…


Download ppt "Relations Rosen 5 th ed., ch. 7. Binary Relations Let A, B be any sets. A binary relation R from A to B, written (with signature) R:A×B, or R:A,B, is."

Similar presentations


Ads by Google