1.26k likes | 2.29k Views
歐亞書局. 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
E N D
歐亞書局 Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 8Relations
歐亞書局 • 8.1Relations 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 AB. • 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 aA. • 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,bA. A relation R on a set A such that for all a,bA, if (a,b)R and (b,a)R, then a=b is called antisymmetric. • ab((a,b)R →(b,a)R) • ab(((a,b)R (b,a)R) →(a=b)) • Not opposites • Ex. 10-12
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,cA. • abc(((a,b)R (b,c)R) → (a,c)R) • Ex. 13-16
Combining Relations • Two relations can be combined in any way two sets can be combined • Ex.17-19 • 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 aA, cC, and there exists an element bB 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 powersRn, n=1,2,3,…, are defined recursively by R1=R, and Rn+1=Rn○R. • Ex. 22 • Theorem 1: The relation R on a set A is transitive iff Rn R for n=1,2,3,… • Proof.
8.2 n-ary Relations and Their Applications • To represent computer databases • Definition 1: Let A1, A2, …, An be sets. An n-ary relations on these sets is a subset of A1 A2 … An. The sets A1, A2, …, An 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 SC 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 Pi1i2…im where i1<i2<…<im, maps the n-tuple (a1,a2,…,an) to the m-tuple (ai1,ai2,…,aim), 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 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) belongs to R and the n-tuple (c1,c2,…,cp,b1,b2,…,bn-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_timeFROM FlightsWHERE Destination=‘Detroit’ • Ex.13 • SELECT Professor, TimeFROM Teaching_assignments, Class_scheduleWHERE 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={a1,a2,…,am} to B={b1,b2,…,bn}. R can be represented by the matrix MR=[mij], wheremij=1 if (ai,bj)R, or 0 if (ai,bj)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)|aA} • Ex.1 • Symmetric closure • The symmetric closure of R=RR-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)Rn. • 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..Rn. • 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 ab, 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 MR 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* isMR*=MR MR[2]MR[3]… MR[n]. • Ex.7
Algorithm 1: A procedure for computing the transitive closure. • Procedure transitive closure (MR : zero-one n*n matrix)A:= MRB:=Afor i:=2 to nbegin A:=A ๏MR B:=B Aend • O(n4) bit operations
Warshall’s Algorithm • Named after Stephen Warshall in 1960 • 2n3 bit operation • Also called Roy-Warshall algorithm, Bernard Roy in 1959 • Interior vertices • W0, W1, …, Wn • Wk=[wij(k)], where wij(k)=1 if there is a path from vi to vj such that all interior vertices of this path are in the set [v1, v2, …, vk]. • Wn= MR* • 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 • wij(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 • wik(k-1)=1 and wkj(k-1)=1
歐亞書局 FIGURE 4 (8.4) FIGURE 4 Adding vk to the Set of Allowable Interior Vertices. P. 553
Lemma 2: Let Wk=wij(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 wij(k)=wij(k-1) (wik(k-1) wkj(k-1)), whenever I, j, and k are positive integers not exceeding n.
Algorithm 2: Warshall Algorithm • Procedure Warshall(MR: n*n zero-one matrix)W:= MRfor k:=1 to nbegin for i:=1 to n begin for j:=1 to n wij:=wij(wikwkj) endend