Download Presentation
## Chapter 4 Relations and Digraphs

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Chapter 4 Relations and Digraphs**Weiqi Luo (骆伟祺) School of Software Sun Yat-Sen University Email：weiqi.luo@yahoo.com Office：A309**Chapter Four: Relations and Digraphs**• 4.1 Product Sets and Partitions • 4.2 Relations and Digraphs • 4.3 Paths in Relations and Digraphs • 4.4 Properties of Relations • 4.5 Equivalence Relations • 4.6 Data structures for Relations and Digraphs • 4.7 Operations on Relations • 4.8 Transitive Closure and Warshall’s Algorithm**4.1. Product Sets and Partitions**• Ordered pair An order pair (a, b) is a listing of objects a and b in a prescribed order, which a appearing first and b appearing second (a,b) = (c,d) <=> a=c and b=d • Product set If A and B are two nonempty sets, we define the product set or Cartesian product A × B as the set of all ordered pairs (a, b) with a ∈ A and b ∈ B. Thus A × B = {(a, b) | a ∈ A and b ∈ B}**4.1. Product Sets and Partitions**• Example 1 & 2 Let A={1, 2, 3} and B={r, s} then A × B = {(1, r), (1, s), (2, r), (2, s), (3, r), (3, s)} B × A = {(r, 1), (s, 1), (r, 2), (s, 2), (r, 3), (s, 3)} Note: A × B is not equal to B × A**4.1. Product Sets and Partitions**• Theorem 1: For any two finite, nonempty sets A and B, |A×B| = |A||B| Proof: Suppose |A| = m, |B|=n. To form an ordered pair (a, b), a ∈ A, and b∈B, we must perform two successive tasks: 1) choose a first element from A (m ways) 2) choose a second element from B (n ways) By multiplication principle (Section 3.1). There are totally m×n ways to form an ordered pair (a, b), which means that |A×B| = |A||B| = m×n**4.1. Product Sets and Partitions**• Example 3 If A=B=R, the set of all real numbers, then R×R, also denoted by R2, is the set of all points in the plane. The ordered pair (a, b) gives the coordinates of a point in the plane Y (a, b) O X**4.1. Product Sets and Partitions**• Cartesian product A1 × A2 × … × Am = {(a1,a2, …,am) | ai ∈ Ai} where A1, A2 , … , Am are non-empty sets. • | A1× A2× … × Am | = |A1| |A2| … |Am| where A1, A2 , … , Am are finite, nonempty sets**4.1. Product Sets and Partitions**• Example 5 A manufacture offers the following options for its refrigerators Doors: side-by-side(s) over-under(u) , three (t) Icemaker: freezer(f), door(d) Finish: standard(r), metallic(m), custom(c) Let D={s,u,t} I={f,d} and F={r, m, c} then the Cartesian product D ×I ×F contains all the categories that describe refigerator options. There are 3 ×2 ×3=18 categories.**4.1. Product Sets and Partitions**• Employees • Select D[ai1=required1, ai2=required2,…, aik=requiredk]**4.1. Product Sets and Partitions**• Partition A partition or quotient set of a nonempty set A is a collection B of nonempty subsets of A such that 1. Each element of A belongs to one of the set in B 2. If A1 and A2 are distinct elements of B, then A1∩ A2 = { } A B={A1, A2, A3, A4, A5, A6} A1 A2 A3 A4 A6 A5 Blocks or cells**4.1. Product Sets and Partitions**• Example 6 Let A={a, b, c, d, e, f, g, h}, consider the following subsets of A A1= {a, b, c, d} A2={a, c, e, f, g, h} A3= {a, c, e, g} A4={b, d} A5={f, h} Then {A1, A2} is not a partition, since A1 ∩ A2 is not an empty set. Also {A1 , A5} is not a partition since e is not in A1 and A5 . The collection B={A3 A4 A5} is a partition of A**4.1. Product Sets and Partitions**• Homework Ex. 2, Ex. 10, Ex. 17, Ex. 32, Ex. 36, Ex. 37**4.2 Relations and Digraphs**• Relation Let A and B be nonempty sets. A relation R from A to B is a subset of A × B. If R ⊆ A× B and (a, b) ∈ R, we say that a is related to b by R, write a R b, if a is not related to b by R, we write a b. If A and B are equal. We say that If R ⊆ A× A is a related on A, instead of a relation from A to A.**4.2 Relations and Digraphs**• Example 1 Let A ={1, 2, 3} and B={r, s}. Then we define R={(1, r), (2, s), (3, r)} is a relation from A to B. • Example 2 Let A and B be sets of real numbers. We define the following relation R(equals) from A to B Y O X**4.2 Relations and Digraphs**• Example 3 Let A={1, 2, 3, 4}. Define the following relation R (less than) on A a R b if and only if a<b Then R= { (1,2) (1,3) (1,4) (2, 3) (2,4) (3,4) } • Example 4 Let A=Z+, the set of all positive integers. Define the following relation R on A: a R b if and only if a divides b Then 4 R 12, 6 R 30, but 5 7 .**4.2 Relations and Digraphs**• Example 5 Let A be the set of all people in the world. We define the following relation R on A: a R b if any only if there is a sequence a0, a1, a2, …,an of people such that a0=a, an=b and ai-1 knows ai, i=1,2,…,n (n will depend on a and b) Small-world network http://en.wikipedia.org/wiki/Small-world_network**4.2 Relations and Digraphs**• Example 6 Let A=R, the set of real numbers. We define the following relation R on A x R y if and only if x and y satisfy the equation x2/4+y2/9=1 Y (0,3) O (2,0) X**4.2 Relations and Digraphs**• Example 9 Ci R Cj if and only if the cost of going from Ci to Cj defined and less than or equal to $180.**4.2 Relations and Digraphs**• Sets arising from relations Let R ⊆ A× B be a relation from A to B. a) The domain of R Denoted by Dom(R), is the set of all first elements in the pairs that make up R. (Dom(R) ⊆ A ) b) The range of R Denoted by Ran(R), is the set of all second elements in the pairs that make up R. (Ran(R) ⊆ B )**4.2 Relations and Digraphs**• Example 12 Let R be the relation of Example 6 then Dom(R) = [-2, +2] Ran(R) = [-3, +3] Y (0,3) O (2,0) X**4.2 Relations and Digraphs**• R-relative set of x If R is a relation from A to B and x ∈ A, we define R(x), the R-relative set of x, to be the set of all y in B with the property that x is R-related to y. R(x) = {y ∈ B | x R y} If A1 ⊆A, then R(A1), the R-relative set of A1, is the set of all y in B with the property that x is R-related to y for some x in A1. R(A1) = {y ∈ B | x R y for some x in A1}**4.2 Relations and Digraphs**• Example 13 Let A={a, b, c, d} and let R={(a,a), (a,b), (b,c), (c,a), (d,c), (c,b)}. Then R(a) = {a, b} R(b)={c} If A1={c, d}, Then R(A1)= {a, b, c}**4.2 Relations and Digraphs**• Let R be the relation of Example 6 A1=[-∞, -2) U (+2, +∞], A2=[-2, +2] then R(A1)= ф , R(A2)= [-3, +3] R(-2)={0}, R(+2)={0} , R(0) = {-3, +3} Y (0,3) O (2,0) X**4.2 Relations and Digraphs**• Theorem 1 Let R be a relation from A to B, and let A1 and A2 be subsets of A, then (a) If A1⊆ A2, then R(A1) ⊆ R(A2) (b) R(A1 U A2 ) = R(A1) U R(A2) (c) R (A1 ∩ A2 ) ⊆R(A1 ) ∩ R(A2)**4.2 Relations and Digraphs**• (a) If A1⊆ A2, then R(A1) ⊆ R(A2) Proof: If y ∈ R(A1), then x R y for some x in A1, since A1⊆ A2, x ∈A2, thus y ∈ R(A2) and therefore R(A1) ⊆ R(A2)**4.2 Relations and Digraphs**• (b) R(A1 U A2 ) = R(A1) U R(A2) Proof : (1) R(A1 U A2 ) ⊆ R(A1) U R(A2) If y ∈ R(A1 U A2 ), then x R y for some x in A1 U A2, then x in A1 or A2. If x in A1 , y ∈ R(A1); if x in A2 . y ∈ R(A2). In either cases, y ∈ R(A1) U R(A2) , therefore R(A1 U A2 ) ⊆ R(A1) U R(A2) (2) R(A1) U R(A2) ⊆ R(A1 U A2 ) A1⊆ A1 U A2 , then R(A1) ⊆ R(A1 U A2 ) A2⊆ A1 U A2 , then R(A2) ⊆ R(A1 U A2 ) Thus R(A1) U R(A2) ⊆ R(A1 U A2 ) Therefore, we obtain R(A1 U A2 ) = R(A1) U R(A2)**4.2 Relations and Digraphs**• (c) R (A1 ∩ A2) ⊆ R(A1) ∩ R(A2) Proof: If y ∈ R (A1 ∩ A2) , then x R y for some x in A1 ∩ A2, since x is in both A1 and A2, it follows that y is in both R(A1) and R(A2) ; that is, y ∈ R(A1 ) ∩ R(A2). Therefore R (A1 ∩ A2) ⊆ R(A1) ∩ R(A2) Note: R(A1) ∩ R(A2) ⊆ R (A1 ∩ A2)**4.2 Relations and Digraphs**• Example 15 Let A=Z, R be “≤” A1={0,1,2} and A2={9,13}. Then R(A1) consists of all integers n such that 0 ≤n, or 1 ≤n, or 2 ≤ n, thus R(A1)={0, 1, 2,…}. Similarly R(A2)={9, 10, 11,…}, so R(A1) ∩ R(A2)= R(A2)={9,10,11,…} On the other hand, A1 ∩ A2 = ф , therefore, R (A1 ∩ A2 ) = ф This shows that R(A1 ) ∩ R(A2) ⊆ R (A1 ∩ A2 )**4.2 Relations and Digraphs**• Theorem 2 Let R and S be relations from A to B, If R(a) =S(a) for all a in A, then R=S Proof: If a R b, then b ∈ R(a). Therefore, b ∈ S(a) and a S b. A completely similar argument shows that, if a S b, then a R b. Thus R=S.**4.2 Relations and Digraphs**• The matrix of a Relation If A={a1,a2, …am} and B={b1,b2,…bn} are finite sets containing m and n elements, respectively, and R is a relation from A to B, we represent R by the m×n matrix MR =[mij], which is defined by mij = 1 if (ai bj) ∈ R = 0 otherwise The matrix MR is called the matrix of R.**4.2 Relations and Digraphs**• Example 17 Let A ={1, 2, 3} and B={r, s}. Then we define R={(1, r), (2, s), (3, r)} is a relation from A to B. Then the matrix of R is**4.2 Relations and Digraphs**• Consider the matrix Since M is 3×4, we let A={a1,a2,a3} and B={b1,b2,b3,b4} Then (ai,bj) in R if and only if mij=1, thus R={(a1,b1),(a1,b4),(a2,b2),(a2,b3),(a3,b1),(a3,b3}**4.2 Relations and Digraphs**• The Digraph of a Relation If A is a finite set, R is a relation on A (from A to A) 1) Draw a small circle for each element of A and label the circle with the corresponding element of A (Vertices) 2) Draw an arrow from vertex ai to vertex aj if and only if ai R aj(Edge) The resulting pictorial representation of R is called a directed graph or digraphy of R.**4.2 Relations and Digraphs**• Example 19 Let A={1, 2, 3, 4} R={(1,1), (1,2), (2,1), (2,2), (2,3), (2,4), (3,4), (4,1)}**4.2 Relations and Digraphs**• Example 20 Find the relation determined by the following Figure. R={ (1,1), (1,3), (2,3), (3,3), (3,2), (4,3) }**4.2 Relations and Digraphs**• In-/Out- Degree If R is a relation on a set A, and a in A, then The in-degree of a relative to the relation R is the number of b in A such that (b,a) in R. The out-degree of a is the number of b in A such that (a,b) in R**4.2 Relations and Digraphs**• Example 22 Let A={a, b, c, d}, and R be the relation on A that has the matrix Construct the digraph of R, and list the in-degrees and out-degrees of all vertices In-degree Out-degree**4.2 Relations and Digraphs**• If R is a relation on a set A, and B is a subset of A, the restriction of R to B is R ∩ (B×B) • Example 24 Let A={a,b,c,d,e,f}, R={(a,a), (a,c), (b,c), (a,e), (b,e), (c,e)}. Let B={a,b,c}, then B×B={(a,a), (a,b), (a,c), (b,a), (b,b), (b,c), (c,a), (c,b), (c,c) } and the restriction of R to B is {(a, a), (a, c), (b, c)}**4.2 Relations and Digraphs**• Homework Ex. 2, Ex. 8, Ex.12, Ex.20 Ex.24, Ex. 28, Ex. 32**4.3 Paths in Relations and Digraphs**• Suppose that R is a relation on a set A. A path of length n in R from a to b is a finite sequence п : a, x1, x2, …, xn-1, b beginning with a and ending with b, such that a R x1, x1 R x2, …,xn-1 R b Note: A path of length n involves n+1 elements of A, although they are not necessarily distinct.**4.3 Paths in Relations and Digraphs**• Example 1 Consider the digraph in the following figure. Then п1: 1, 2, 5, 4, 3 is a path of length 4 from vertex 1 to 3 п2: 1, 2, 5, 1 is a path of length 3 from vertex 1 to itself п3: 2, 2 is a path of length 1 from vertex 2 to itself Cycle: a path that begins and ends at the same vertex (п2 п3 )**4.3 Paths in Relations and Digraphs**• If n is a fixed positive integer, we define a relation Rn on A as follows: x Rn y means that there is a path of length n from x to y in R • Define a relation R∞ on A, by letting x R y mean that there is some path in R from x to y. The length of such a path will depend on x and y. The relation R∞ is sometimes called the connectivity relation for R. • Rn(x) consists of all verities that can be reached form x by means of a path in R of length n. The set R∞ (x) consists of all vertices that can be reached from x by some path in R**4.3 Paths in Relations and Digraphs**• Example 2 Let A be the set of all living human beings R be the relation of mutual acquaintance a R b means that a and b know one another a R2 b means that a and b have an acquaintance in common a Rn b if a knows someone x1, who knows x2, …, who knows xn-1, who knows b. a R∞ b means that some chain of acquaintances exists that begins at a and ends at b. Q: It is interesting (and unknown) whether every two Americans, say, are related by R∞**4.3 Paths in Relations and Digraphs**• Example 3 Let A be a set of cities x R y if there is a direct flight from x to y on at least one airline. x Rn y if one can book a flight from x to y having exactly n-1 intermediate stops x R∞ y if one can get from x to y by plane**4.3 Paths in Relations and Digraphs**• Example 5 Let A={a,b,c,d,e} and R={(a,a), (a,b), (b,c), (c,e), (c,d), (d,e)} Compute (a) R2 (b) R ∞ R**4.3 Paths in Relations and Digraphs**• Compute R2 a R2 a since a R a and a R a a R2 b since a R a and a R b a R2 c since a R b and b R c b R2 e since b R c and c R e b R2 d since b R c and c R d c R2 e since c R d and d R e Hence R2 ={(a,a), (a,b), (a,c), (b,e), (b,d), (c,e)}**4.3 Paths in Relations and Digraphs**• Compute R ∞ To compute R ∞ , we need all ordered pairs of vertices for which there is a path of any length from the first vertex to the second. We can see that from the figure R∞ = { (a,a), (a,b), (a,c), (a,d), (a,e), (b,c),(b,d), (b,e), (c,d), (c,e), (d,e) }. R Note: If |R| is large, it can be tedious and perhaps difficult to compute R ∞**4.3 Paths in Relations and Digraphs**• Boolean Matrix (p.37) A Boolean Matrix is an m×n matrix whose entries are either 0 or 1. Let A=[aij] and B=[bij] be m×n matrix Boolean matrix. The Join of A and B : A V B = C = [cij] cij=1 if aij=1 or bij=1 cij=0 otherwise The meet of A and B: A ^ B = D = [dij] dij=1 if aij and bij are both 1 dij=0 otherwise**4.3 Paths in Relations and Digraphs**• Boolean Product (p.38) A = [aij] is an m×p Boolean matrix and B = [bij] is a p×n Boolean matrix. The Boolean product of A and B, denoted A⊙B, is the m×n Boolean matrix C=[cij] defined by**4.3 Paths in Relations and Digraphs**• Example Let then