Section 9.1 1 Relations and their Properties. Binary Relation from A to B 2 Let A and B be sets. A...

download Section 9.1 1 Relations and their Properties. Binary Relation from A to B 2 Let A and B be sets. A binary relation from A to B is a subset of A x B.

If you can't read please download the document

Transcript of Section 9.1 1 Relations and their Properties. Binary Relation from A to B 2 Let A and B be sets. A...

  • Slide 1

Section 9.1 1 Relations and their Properties Slide 2 Binary Relation from A to B 2 Let A and B be sets. A binary relation from A to B is a subset of A x B. Slide 3 Example 3 Let A = { a, b, c } and B = { x, y } then A x B = {(a,x), (a,y), (b,x), (b,y), (c,x), (c,y)} Any subset of A x B is a relation from A to B. Therefore let, for example: R 1 = {(a,x), (c,y)} R 2 = {(a,y), (b,x)} How many relations from A to B are there? Slide 4 Binary Relation from A to B. 4 A binary relation from A to B is a set R of ordered pairs where the first element of each ordered pair comes from A and the second element comes from B. aRb denotes that (a,b) is an element of R and aRb indicates that (a,b) is not an element of R. When (a,b) belongs to R then a is related to b by R. Slide 5 Example 5 Let: R 1 = { (a,x), (c,y) } R 2 = { (a,y), (b,x) } Notation a R 1 x b R 1 x b R 2 x Slide 6 Depicting Relations Directed Graphs Tables Example: Suppose A = {0, 1, 2} and B = {a, b}. Let R be the relation on A x B defined as: {(0, a), (0, b), (1, a), (2, b)} Diagrams on next slide 6 Slide 7 Slide 8 Functions as Relations 8 A function is a relation from A to B such that every element in A is the first element of exactly one ordered pair of R. Slide 9 Relations on a Set 9 A relation on the set A is a relation from A to A. A relation on the set A is a subset of A x A Slide 10 How Many Relations? 10 If |A| = n then |A x A| = n 2. Since a relation is a subset of A x A then the number of possible relations is Slide 11 Reflexive Relations 11 Consider the following relations on the set {1,2,3,4} {(2,2),(2,3),(2,4),(3,2),(3,3),(3,4)} {(1,1),(1,2),(2,1),(2,2),(3,3),(4,4)} {(2,4),(4,2)} {(1,2),(2,3),(3,4)} {(1,1),(2,2),(3,3),(4,4)} {(1,3),(1,4),(2,3),(2,4),(3,1),(3,4)} Slide 12 Symmetric Relations 12 Consider the following relations on the set {1,2,3,4} {(2,2),(2,3),(2,4),(3,2),(3,3),(3,4)} {(1,1),(1,2),(2,1),(2,2),(3,3),(4,4)} {(2,4),(4,2)} {(1,2),(2,3),(3,4)} {(1,1),(2,2),(3,3),(4,4)} {(1,3),(1,4),(2,3),(2,4),(3,1),(3,4)} Slide 13 Antisymmetric Relations 13 Consider the following relations on the set {1,2,3,4} {(2,2),(2,3),(2,4),(3,2),(3,3),(3,4)} {(1,1),(1,2),(2,1),(2,2),(3,3),(4,4)} {(2,4),(4,2)} {(1,2),(2,3),(3,4)} {(1,1),(2,2),(3,3),(4,4)} {(1,3),(1,4),(2,3),(2,4),(3,1),(3,4)} Slide 14 Examples The relation on the real numbers is antisymmetric The relation on sets is antisymmetric The equality relation, =, on the real numbers is both symnmetric and antisymmetric (antisymmetric asymmetric!) Anther way to define antisymmetry: aRb ab (bRa) 14 Slide 15 Transitive Relations 15 Consider the following relations on the set {1,2,3,4} {(2,2),(2,3),(2,4),(3,2),(3,3),(3,4)} {(1,1),(1,2),(2,1),(2,2),(3,3),(4,4)} {(2,4),(4,2)} {(1,2),(2,3),(3,4)} {(1,1),(2,2),(3,3),(4,4)} {(1,3),(1,4),(2,3),(2,4),(3,1),(3,4)} Slide 16 Composition of Relations 16 Slide 17 RnRn 17 Slide 18 Transitivity 18 Slide 19 Section 9.2 19 n-ary Relations Slide 20 Definition Let A 1, A 2, , A n be sets. An n-ary relation on these sets is a subset of A 1 x A 2 x x A n. The sets A 1, A 2, , A n are called the domains of the relation, and n is called its degree. 20 Slide 21 Databases and Relations Students StudentNameIDnumberMajorGPA Ackermann Adams Chou Goodfriend Rao Stevens 231455 888323 102147 453876 678543 786576 Computer Science Physics Computer Science Mathematics Psychology 3.88 3.45 3.49 3.45 3.90 2.99 21 Slide 22 Operations Selection Chooses tuples (rows) that satisfy a condition Projection Chooses slots (columns) by position number or name Join Combines tuples from two tables with a common key 22 Slide 23 Relational Algebra Demo reldb.py 4 Tables: parts suppliers projects spj (a table that connects the other 3 tables) 23 Slide 24 Section 9.3 24 Representing Relations Slide 25 Using Matrices 25 Let A = {1,2,3} and B={1,2}. Let the relation from A to B containing (a,b) if a>b. R would then equal {(2,1), (3,1), (3,2)} 123123 1 2 Slide 26 Reflexive Relations 26 Slide 27 Symmetric 27 SYMMETRICNOT SYMMETRIC Slide 28 Antisymmetric 28 Slide 29 Boolean Matrices 29 Join of Matrices Union of Relations Meet of Matrices Intersection of Relations Boolean Product of Matrices Composite Relations Slide 30 Digraph Example 30 ab cd V = {a,b,c,d} E = { (a,b), (a,d), (c,b), (c,a), (b,b),(d,b), (b,d)} Slide 31 Spotting Properties via Graphs How can you spot the following properties from a graph? Reflexive Symmetric Antisymmetric Transitive 31 Slide 32 Slide 33 33 Section 9.4 Closures of Relations Slide 34 Reflexive Closure The reflexive closure of a relation, R, is the minimal relation containing R that is reflexive If R is already reflexive, it is its own reflexive closure Otherwise, add pairs (a,a) for all elements a, that are not already related to themselves. For example, if R = {(1,1), (1,2), (2,1), (3,2)}, the we add the following pairs: (2,2), (3,3). 34 Slide 35 Symmetric Closure The symmetric closure of a relation, R, is the minimal relation containing R that is symmetric If R is already symmetric, it is its own symmetric closure Otherwise, add pairs (b,a) for all pairs (a,b) in R that are not already there. For example, if R = {(1,1), (1,2), (2,1), (3,2)}, the we add the following pair: (2,3). 35 Slide 36 Can You Get There From Here? Often we want to know if there is a path from a to b (meaning a chain of pairs (a,x 1 ), (x 1,x 2 ), (x n,b)). We say that b is reachable in R from a. Forming this relation is harder than it appears 36 Slide 37 Transitive Closure The transitive closure of a relation, R, is the minimal relation containing R that is transitive. Theorem 1: There is a path from a to b in R of length n, if and only if (a,b) R n. (The composition of R with itself n- 1 times). R defines paths of length 1 R 2 adds paths of length 2, and so on 37 Slide 38 Computing the Transitive Closure R n computes paths of length n. This means that n+1 objects have been visited, forcing a repeat (cycle). So everything has been traversed! So all possible connected endpoints are computed by 38 Slide 39 Matrix Representation M* = M v M 2 v M 3 v v M n. See Example 7 in section 9.4, page 602-603. A more efficient algorithm (Warshalls algorithm) is on page 606 (Algorithm 2 in section 9.4). 39 Slide 40 40 Section 9.5 Equivalence Relations Slide 41 41 Equivalence Relations A relation on a set A is called an equivalence relation if it is reflexive, symmetric, and transitive. Slide 42 42 Examples: Let R be the relation on the set of strings such that a R b if and only if l (a) = l (b) where l (x) is the length of x. Let R be the relation on the set of real numbers such that a R b if and only if a-b is an integer. Slide 43 43 Is this an equivalence relation? Which elements are equivalent (related to each other)? Slide 44 44 Equivalence Classes Let R be an equivalence relation on a set A. The set of all elements that are related to an element a of A is called the equivalence class of a. The equivalence class of a with respect to R is denoted by [a] R. When only one relation is under consideration, we will delete the subscript R and write [a] for this equivalence class. Slide 45 45 [0] = {-14, -7, 0, 7, 14, 21,} [1] = {-13, -6, 1, 8, 15, 22,} [2] = {-12, -5, 2, 9, 16, 23,} [3] = {-11, -4, 3, 10, 17, 24,} [4] = {-10, -3, 4, 11, 18, 25,} [5] = {-9, -2, 5, 12, 19, 26,} [6] = {-8, -1, 6, 13, 20, 27,} Slide 46 Slide 47 47 Partition A partition of a set S is a collection of disjoint, nonempty subsets of S that have S as their union. Slide 48 48 Equivalence Relations and Partitions Let R be an equivalence relation on a set S. Then the equivalence classes of R form a partition of S. Conversely, given a partition of the set S, there is an equivalence relation R that has the sets as its equivalence classes. Slide 49 49 Section 9.6 Partial Orderings Slide 50 50 Partially Ordered Set (POSET) A relation R on a set S is called a partial ordering or partial order if it is reflexive, antisymmetric, and transitive. A set S together with a partial ordering R is called a partially ordered set, or poset, and is denoted by ( S, R ) Slide 51 51 Example (1) Let S = {1, 2, 3} and let R = {(1,1), (2,2), (3,3), (1, 2), (3,1), (3,2)} 1 2 3 Slide 52 52 In a poset, the notation a b denotes that This notation is used because the less than or equal to relation is a paradigm for a partial ordering. (Note that the symbol is used to denote the relation in any poset, not just the less than or equals relation.) The notation a b denotes that a b, but Slide 53 53 Example Let S = {1, 2, 3} and let R = {(1,1), (2,2), (3,3), (1, 2), (3,1), (3,2)} 1 2 3 2 3 2 Slide 54 54 Example (2) Consider the set of real numbers and the less than or equal to relation. ( R, ) Slide 55 55 Example (3) Consider the power set of { a, b, c } and the subset relation. ( P ({ a, b, c }), ) Slide 56 56 Comparable/Incomparable The elements a and b of a poset ( S, ) are called comparable if either a b or b a. When a and b are elements of S such that neither a b nor b a, a and b are called incomparable. Slide 57 57 Example Consider the power set of { a, b, c } and the subset relation. ( P ({ a, b, c }), ) So, { a, c } and { a, b } are incomparable Slide 58 58 ( S, ) If is a poset and every two elements of S are comparable, S is called totally ordered or linearly ordered set, and is called a total order or a linear order on S. A totally ordered set is also called a chain. Total Orders Slide 59 59 Example Consider again, the set of real numbers and the less than or equal to relation. ( R, ) Slide 60 60 Hasse Diagrams Given any partial order relation defined on a finite set, it is possible to draw the directed graph that depicts the ordering. This makes it possible to associate a somewhat simpler graph, called a Hasse diagram, with a partial order relation defined on a finite set. Slide 61 Slide 62 62 Hasse Diagrams (continued) Start with a directed graph of the relation in which all arrows point upward. Then eliminate: 1.the loops at all the vertices, 2.all arrows whose existence is implied by the transitive property, 3.the direction indicators on the arrows. Slide 63 63 Prerequisites TaskPrerequisite TasksTime Needed to Perform Task 123456789123456789 1 2 2, 3 4 2, 3 4, 5 6, 7, 8 7 hours 6 hours 3 hours 6 hours 3 hours 1 hour 2 hours 5 hours Slide 64 64 Example Job Scheduling (PERT chart) Task 1 7 hours Task 2 6 hours Task 3 3 hours Task 4 6 hours Task 5 3 hours Task 7 1 hour Task 6 1 hour Task 8 2 hour Task 9 5 hour Slide 65 65 CNS2300 CNS2420 CNS3520CNS3060 CNS2600 CNS3690 CNS1510 CNS2810 CNS1410 CNS1400 CNS301R Example (Core CNS Classes) Slide 66 66 a is a maximal in the poset (S, ) if there is no such that a b. Similarly, an element of a poset is called minimal if it is not greater than any element of the poset. That is, a is minimal if there is no element such that b a. It is possible to have multiple minimals and maximals. Maximal and Minimal Elements Slide 67 Slide 68 68 a is the greatest element in the poset (S, ) if b a for all. Similarly, an element of a poset is called the least element if it is less than all other elements in the poset That is, a is least element if there is no such that b a. Greatest Element Least Element Slide 69 Slide 70 70 Job Scheduling (Again) Task 1 7 hours Task 2 6 hours Task 3 3 hours Task 4 6 hours Task 5 3 hours Task 7 1 hour Task 6 1 hour Task 8 2 hour Task 9 5 hour How should we schedule the tasks? Slide 71 71 Topological Sorting A total ordering is said to be compatible with the partial ordering R if a b whenever a R b. Constructing a total ordering from a partial ordering is called topological sorting. Slide 72 Slide 73 Slide 74