Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 8 Relations 歐亞書局
8.1 Relations and Their Properties 8.2 n-ary Relations and Their Applications 8.3 Representing Relations 8.4 Closures of Relations 8.5 Equivalence Relations 8.6 Partial Orderings 歐亞書局 P. 519
8.1 Relations and Their Properties –To represent relationships between elements of sets Ordered pairs: binary relations Definition 1: Let A and B be sets. A binary relation from A to B is a subset of A B. –a R b: (a,b) R a is related to b by R –a b: (a,b) R –Ex.1-3 Functions as relations –Relations are generalization of functions
FIGURE 1 (8.1) FIGURE 1 Displaying the Ordered Pairs in the Relation R from Example 3. 歐亞書局 P. 520
Relations on a Set Definition 2: a relation on the set A is a relation from A to A. –Ex.4 –Ex.5 –Ex.6: How many relations are there on a set with n elements?
FIGURE 2 (8.1) FIGURE 2 Displaying the Ordered Pairs in the Relation R from Example 4. 歐亞書局 P. 521
Properties of Relations Definition 3: A relation R on a set A is called reflexive if (a,a) R for every element a A. – a((a,a) R) –Ex.7-9 Definition 4: A relation R on a set A is called symmetric if (b,a) R whenever (a,b) R, for all a,b A. A relation R on a set A such that for all a,b A, if (a,b) R and (b,a) R, then a=b is called antisymmetric. – a b((a,b) R →(b,a) R) – a b(((a,b) R (b,a) R) →(a=b)) –Not opposites –Ex
Definition 5: A relation R on a set A is called transitive if whenever (a,b) R and (b,c) R, then (a,c) R, for all a,b,c A. – a b c(((a,b) R (b,c) R) → (a,c) R) –Ex
Combining Relations Two relations can be combined in any way two sets can be combined –Ex Definition 6: R: relation from A to B, S: relation from B to C. The composite of R and S is the relation consisting of ordered pairs (a,c), where a A, c C, and there exists an element b B such that (a,b) R and (b,c) S. (denoted by S○R) –Ex.20-21
Definition 7: Let R be a relation on the set A. The powers R n, n=1,2,3,…, are defined recursively by R 1 =R, and R n+1 =R n ○R. –Ex. 22 Theorem 1: The relation R on a set A is transitive iff R n R for n=1,2,3,… –Proof.
8.2 n-ary Relations and Their Applications To represent computer databases Definition 1: Let A 1, A 2, …, A n be sets. An n-ary relations on these sets is a subset of A 1 A 2 … A n. The sets A 1, A 2, …, A n are called the domains of the relation, and n is called its degree. –Ex.1-4
Databases and Relations Relational data model –Records –Fields –Tables –Primary key –Ex.5-6
TABLE 1 (8.2) 歐亞書局 P. 531
Operations on n-ary Relations Definition 2: Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the selection operator S C maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C. –Ex.7 Definition 3: The projection P i1i2…im where 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. –Ex.8-10
TABLE 2 (8.2) 歐亞書局 P. 534
TABLE 3 (8.2) 歐亞書局 P. 534
TABLE 4 (8.2) 歐亞書局 P. 534
Definition 4: 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 ) belongs to R and the n -tuple ( c 1,c 2,…,c p,b 1,b 2,…,b n-p ) belongs to S. –Ex.11
TABLE 5 (8.2) 歐亞書局 P. 534
TABLE 6 (8.2) 歐亞書局 P. 534
TABLE 7 (8.2) 歐亞書局 P. 535
SQL Structured Query Language –Ex.12 SELECT Departure_time FROM Flights WHERE Destination=‘Detroit’ –Ex.13 SELECT Professor, Time FROM Teaching_assignments, Class_schedule WHERE Department=‘Mathematics’
TABLE 8 (8.2) 歐亞書局 P. 535
TABLE 9 (8.2) 歐亞書局 P. 537
TABLE 10 (8.2) 歐亞書局 P. 537
8.3 Representing Relations To represent binary relations –Ordered pairs –Tables –Zero-one matrices –Directed graphs
Representing Relations Using Matrices Suppose that 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 the matrix M R =[m ij ], where m ij =1 if (a i,b j ) R, or 0 if (a i,b j ) R. –Ex.1-6
FIGURE 1 (8.3) FIGURE 1 The Zero-One Matrix for a Reflexive Relation. 歐亞書局 P. 538
FIGURE 2 (8.3) FIGURE 2 The Zero-One Matrices for Symmetric and Antisymmetric Relations. 歐亞書局 P. 539
Representing Relations Using Digraphs Definition 1: A directed graph ( digraph ) consists of a set V of vertices (or nodes ) and a set E of ordered pairs of elements of V called edges (or arcs ). The vertex a is called the initial vertex of the edge (a,b), and the vertex b is called the terminal vertex of this edge. –Ex.7-10
FIGURE 3 (8.3) FIGURE 3 The Directed Graph. 歐亞書局 P. 541
FIGURE 4 (8.3) FIGURE 4 The Directed Graph of the Relations R. 歐亞書局 P. 542
FIGURE 5 (8.3) FIGURE 5 The Directed Graph of the Relations R. 歐亞書局 P. 542
FIGURE 6 (8.3) FIGURE 6 The Directed Graph of the Relations R and S. 歐亞書局 P. 542
8.4 Closures of Relations Ex. Telephone line connections Let R be a relation on a set A. R may or may not have some property P. If there is a relation S with property P containing R such that S is a subset of every relation with property P containing R, then S is called the closure of R with respect to P. –S: the smallest relation that contains R
Closures Reflexive closure –Ex: R={(1,1),(1,2),(2,1),(3,2)} on the set A={1,2,3} The smallest relation contains R? –The reflexive closure of R=R ∆, where ∆={(a,a)|a A} Ex.1 Symmetric closure –The symmetric closure of R=R R -1, where R -1 ={(b,a)|(a,b) R} Ex.2 Transitive closure –More complex
Paths in Directed Graphs Definition 1: A path from a to b in the directed graph G is a sequence of edges (x0,x1), (x1,x2), …,(xn-1,xn) in G, where n is a nonnegative integer, and x0=a and xn = b. –The path denoted by x0,x1,…,xn-1, xn has length n. –A path of length n >=1 that begins and ends at the same vertex is called a circuit or cycle. –Ex.3 –There is a path from a to b in R if there is a sequence of elements a, x1, x2, …, xn-1, b with (a,x1) R, (x1,x2) R, …, and (xn-1,b) R.
FIGURE 1 (8.4) FIGURE 1 A Directed Graph. 歐亞書局 P. 546
Theorem 1: Let R be a relation on a set A. There is a path of length n, where n is a positive integer, from a to b iff (a,b) R n. –Proof. (by mathematical induction)
Transitive Closures Definition 2: Let R be a relation on a set A. The connectivity relation R* consists of pairs (a,b) such that there is a path of length at least one from a to b in R. –R*= n=1.. R n. –Ex.4 –Ex.5 –Ex.6
Theorem 2: The transitive closure of a relation R equals the connectivity relation R*. –Proof. Lemma 1: Let A be a set with n elements, and R be a relation on A. If there is a path of length at least one in R from a to b, then there is such as path with length not exceeding n. –When a b, if there is a path of length at least one in R from a to b, then there is such as path with length not exceeding n-1. –Proof.
FIGURE 2 (8.4) FIGURE 2 Producing a Path with Length Not Exceeding n. 歐亞書局 P. 549
Theorem 3: Let M R be the zero-one matrix of the relation R on a set with n elements. Then the zero-one matrix of the transitive closure R* is M R* = M R M R [2] M R [3] … M R [n]. –Ex.7
Algorithm 1: A procedure for computing the transitive closure. –Procedure transitive closure ( M R : zero-one n*n matrix) A:= M R B:=A for i:=2 to n begin A:=A ๏ M R B:=B A end –O(n 4 ) bit operations
Warshall’s Algorithm Named after Stephen Warshall in 1960 –2n 3 bit operation –Also called Roy-Warshall algorithm, Bernard Roy in 1959 Interior vertices W0, W1, …, Wn –Wk=[w ij (k) ], where w ij (k) =1 if there is a path from v i to v j such that all interior vertices of this path are in the set [v1, v2, …, vk]. –Wn= M R* –Ex.8 (Fig. 3)
FIGURE 3 (8.4) FIGURE 3 The Directed Graph of the Relations R. 歐亞書局 P. 551
Observation: we can compute Wk directly from Wk-1 –Two cases (Fig. 4) There is a path from vi to vj with its interior vertices among the first k-1 vertices –w ij (k-1) =1 There are paths from vi to vk and from vk to vj that have interior vertices only among the first k-1 vertices –w ik (k-1) =1 and w kj (k-1) =1
FIGURE 4 (8.4) FIGURE 4 Adding v k to the Set of Allowable Interior Vertices. 歐亞書局 P. 553
Lemma 2: Let Wk=w ij (k) be the zero-one matrix that has a 1 in its (i,j)th position iff there is a path from vi to vj with interior vertices from the set {v1, v2, …, vk}. Then w ij (k) =w ij (k-1) (w ik (k-1) w kj (k-1) ), whenever I, j, and k are positive integers not exceeding n.
Algorithm 2: Warshall Algorithm –Procedure Warshall( M R : n*n zero-one matrix) W:= M R for k:=1 to n begin for i:=1 to n begin for j:=1 to n wij:=wij (wik wkj) end end
8.5 Equivalence Relations Definition 1: A relation on a set A is called an equivalence relation if it is reflexive, symmetric, and transitive. Definition 2: Two elements a and b are related by an equivalence relation are called equivalent. –The notation a~b : to denote that a and b are equivalent elements with respect to a particular equivalence relation –Ex. 1-5: equivalence relations –Ex. 6-7: not equivalence relations
Equivalence Classes Definition 3: 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. –Denoted by [a] R or [a] –[a] R = {s|(a,s) R} –If b [a] R, then b is called a representative of the equivalence class –Ex. 8-11
Equivalence Classes and Partitions Theorem 1: Let R be an equivalence relation on a set A. These statements for elements a and b of A are equivalent: –a R b –[a]=[b] –[a] [b] –Proof.
Observations – a A [a] R =A –[a] [b]= when [a] R [b] R The equivalence classes form a partition of A –A partition of a set S is a collection of disjoint nonempty subsets of S that have S as their union Ai for i I, Ai Aj= when i j i I Ai=S Ex.12
FIGURE 1 (8.5) FIGURE 1 A Partition of a Set. 歐亞書局 P. 561
Theorem 2: 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 {Ai|i I} of the set S, there is an equivalence relation R that has the sets Ai, i I, as its equivalence classes. –Ex.13-15
8.6 Partial Orderings Definition 1: 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 its partial ordering R is called a partially ordered set, or poset, (S, R). –Proof. –Ex.1-3: ≥, |, –Ex.4: older than –Notation: : a ≤ b, but a b
Definition 2: The elements a and b of a poset (S, ) are called comparable if either a b or b a. –If neither a b nor b a, a and b are incomparable. (“partial”) –Ex. 5 –When every two elements in the set are comparable, the relation is called a total ordering
Definition 3: If (S, ) is a poset and every two elements of S are comparable, S is called a totally ordered or linearly ordered set (or a chain ), and is called a total order or a linear order. –Ex.6: ( Z, ≤) –Ex.7: ( Z +, |) Definition 4: (S, ) is a well-ordered set if it is a poset such that is a total ordering and every nonempty subset of S has a least element. –Ex.8
Theorem 1: (The Principle of Well- Ordered Induction) Suppose that S is a well-ordered set. Then P(x) is true for all x S, if (inductive step): For every y S, if P(x) is true for all x S with x y, then P(y) is true. –Proof.
Lexicographic Order (A 1, 1 ), (A 2, 2 ) –(a 1, a 2 ) (b 1, b 2 ) Either if a 1 1 b 1 Or if a 1 =b 1 and a 2 2 b 2 –Ex.9-10 –n-tuples (Fig. 1) –Ex.11
FIGURE 1 (8.6) FIGURE 1 The Ordered Pairs Less Than (3,4) in Lexicographic Order. 歐亞書局 P. 570
Hasse Diagrams Hasse Diagrams: some edges do not have to be shown –Reflexive: loops can be removed –Transitive: if (a,b) and (b,c), then (a,c) can be removed –Remove all the arrows –Ex. 12: (Fig. 3) –Ex. 13: (Fig. 4)
FIGURE 2 (8.6) FIGURE 2 Constructing the Hasse Diagram for ({1,2,3,4}, ≦ ). 歐亞書局 P. 571
FIGURE 3 (8.6) FIGURE 3 Constructing the Hasse Diagram of ({1,2,3,4,6,8,12}, | ). 歐亞書局 P. 572
FIGURE 4 (8.6) 歐亞書局 P. 573 FIGURE 4 The Hasse Diagram of (P({a,b,c}), ).
Maximal and Minimal Elements a is maximal in the poset (S, ) if there is no b S such that a b. a is minimal in the poset (S, ) if there is no b S such that b a. –Ex. 14: (Fig. 5) –Ex. 15: (Fig. 6) –Ex
FIGURE 5 (8.6) FIGURE 5 The Hasse Diagram of a Poset. 歐亞書局 P. 573
FIGURE 6 (8.6) FIGURE 6 Hasse Diagrams of Four Posets. 歐亞書局 P. 573
In a subset A of a poset (S, ), if u is an element of S such that a u for all a A, then u is an upper bound of A. In a subset A of a poset (S, ), if l is an element of S such that l a for all a A, then l is an lower bound of A. –Ex. 18: (Fig. 7) –Ex. 19: (Fig. 7) –Ex. 20
FIGURE 7 (8.6) FIGURE 7 The Hasse Diagram of a Poset. 歐亞書局 P. 574
Lattices A partially ordered set in which every pair of elements has both a least upper bound and a greatest lower bound is called a lattice –Ex. 21: (Fig. 8) –Ex
FIGURE 8 (8.6) FIGURE 8 Hasse Diagrams of Three Posets. 歐亞書局 P. 575
Topological Sorting A total ordering is said to be compatible with the partial ordering R if a b whenever a R b. –Topological sorting: constructing a compatible total ordering from a partial ordering Lemma 1: Every finite nonempty poset (S, ) has at lease one minimal element.
Algorithm 1: topological sorting –Procedure topological sort(S, : finite poset) k:=1 while S begin ak:=a minimal element of S S:=S-{ak} k:=k+1 end –Ex.26: (Fig. 9) –Ex. 27: (Fig. 10, 11)
FIGURE 9 (8.6) FIGURE 9 A Topological Sort of ({1,2,4,5,12,20}, | ). 歐亞書局 P. 577
FIGURE 10 (8.6) FIGURE 10 The Hasse Diagram for Seven Tasks. 歐亞書局 P. 578
FIGURE 11 (8.6) FIGURE 11 A Topological Sort of the Tasks. 歐亞書局 P. 578
Thanks for Your Attention!