26.8 KB · commit a9602da
A computer network has data centers in Boston, Chicago, Denver, Detroit, New York, and San Diego. There are direct, one-way telephone lines from Boston to Chicago, from Boston to Detroit, from Chicago to Detroit, from Detroit to Denver, and from New York to San Diego. Let R be the relation containing (a,b) if there is a telephone line from the data center in a to that in b. How can we determine if there is some (possibly indirect) link composed of one or more telephone lines from one center to another? Because not all links are direct, such as the link from Boston to Denver that goes through Detroit, R cannot be used directly to answer this. In the language of relations, R is not transitive, so it does not contain all the pairs that can be linked. As we will show in this section, we can find all pairs of data centers that have a link by constructing a transitive relation S containing R such that S is a subset of every transitive relation containing R. Here, S is the smallest transitive relation that contains R. This relation is called the transitive closure of R.
In general, let R be a relation on a set A. R may or may not have some property P, such as reflexivity, symmetry, or transitivity. 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. (Note that the closure of a relation with respect to a property may not exist; see Exercises 15 and 35.) We will show how reflexive, symmetric, and transitive closures of relations can be found.
The relation R={(1,1),(1,2),(2,1),(3,2)} on the set A={1,2,3} is not reflexive. How can we produce a reflexive relation containing R that is as small as possible? This can be done by adding (2,2) and (3,3) to R, because these are the only pairs of the form (a,a) that are not in R. Clearly, this new relation contains R. Furthermore, any reflexive relation that contains R must also contain (2,2) and (3,3). Because this relation contains R, is reflexive, and is contained within every reflexive relation that contains R, it is called the reflexive closure of R.
As this example illustrates, given a relation R on a set A, the reflexive closure of R can be formed by adding to R all pairs of the form (a,a) with a∈A, not already in R. The addition of these pairs produces a new relation that is reflexive, contains R, and is contained within any reflexive relation containing R. We see that the reflexive closure of R equals R∪Δ, where Δ={(a,a)∣a∈A} is the diagonal relation on A. (The reader should verify this.)
What is the reflexive closure of the relation R={(a,b)∣a<b} on the set of integers?
Solution: The reflexive closure of R is
R∪Δ={(a,b)∣a<b}∪{(a,a)∣a∈Z}={(a,b)∣a≤b}.◀
The relation {(1,1),(1,2),(2,2),(2,3),(3,1),(3,2)} on {1,2,3} is not symmetric. How can we produce a symmetric relation that is as small as possible and contains R? To do this, we need only add (2,1) and (1,3), because these are the only pairs of the form (b,a) with (a,b)∈R that are not in R. This new relation is symmetric and contains R. Furthermore, any symmetric relation that contains R must contain this new relation, because a symmetric relation that contains R must contain (2,1) and (1,3). Consequently, this new relation is called the symmetric closure of R.
As this example illustrates, the symmetric closure of a relation R can be constructed by adding all ordered pairs of the form (b,a), where (a,b) is in the relation, that are not already present in R. Adding these pairs produces a relation that is symmetric, that contains R, and that is contained in any symmetric relation that contains R. The symmetric closure of a relation can be constructed by taking the union of a relation with its inverse (defined in the preamble of Exercise 26 in Section 9.1); that is, R∪R−1 is the symmetric closure of R, where R−1={(b,a)∣(a,b)∈R}. The reader should verify this statement.
What is the symmetric closure of the relation R={(a,b)∣a>b} on the set of positive integers?
Solution: The symmetric closure of R is the relation
R∪R−1={(a,b)∣a>b}∪{(b,a)∣a>b}={(a,b)∣a=b}.
This last equality follows because R contains all ordered pairs of positive integers where the first element is greater than the second element and R−1 contains all ordered pairs of positive integers where the first element is less than the second. ◀
Suppose that a relation R is not transitive. How can we produce a transitive relation that contains R such that this new relation is contained within any transitive relation that contains R? Can the transitive closure of a relation R be produced by adding all the pairs of the form (a,c), where (a,b) and (b,c) are already in the relation? Consider the relation
R={(1,3),(1,4),(2,1),(3,2)} on the set {1,2,3,4}. This relation is not transitive because it does not contain all pairs of the form (a,c) where (a,b) and (b,c) are in R. The pairs of this form not in R are (1,2), (2,3), (2,4), and (3,1). Adding these pairs does not produce a transitive relation, because the resulting relation contains (3,1) and (1,4) but does not contain (3,4). This shows that constructing the transitive closure of a relation is more complicated than constructing either the reflexive or symmetric closure. The rest of this section develops algorithms for constructing transitive closures. As will be shown later in this section, the transitive closure of a relation can be found by adding new ordered pairs that must be present and then repeating this process until no new ordered pairs are needed.
We will see that representing relations by directed graphs helps in the construction of transitive closures. We now introduce some terminology that we will use for this purpose.
A path in a directed graph is obtained by traversing along edges (in the same direction as indicated by the arrow on the edge).
A path in a directed graph can pass through a vertex more than once. Moreover, an edge in a directed graph can occur more than once in a path.
Which of the following are paths in the directed graph shown in Figure 1: a,b,e,d; a,e,c,d,b; b,a,c,b,a,a,b; d,c; c,b,a; e,b,a,b,a,b,e? What are the lengths of those that are paths? Which of the paths in this list are circuits?
Figure 1 — A Directed Graph.
Solution: Because each of (a,b), (b,e), and (e,d) is an edge, a,b,e,d is a path of length three. Because (c,d) is not an edge, a,e,c,d,b is not a path. Also, b,a,c,b,a,a,b is a path of length six because (b,a), (a,c), (c,b), (b,a), (a,a), and (a,b) are all edges. We see that d,c is a path of length one, because (d,c) is an edge. Also c,b,a is a path of length two, because (c,b) and (b,a) are edges. All of (e,b), (b,a), (a,b), (b,a), (a,b), and (b,e) are edges, so e,b,a,b,a,b,e is a path of length six.
The two paths b,a,c,b,a,a,b and e,b,a,b,a,b,e are circuits because they begin and end at the same vertex. The paths a,b,e,d; c,b,a; and d,c are not circuits. ◀
The term path also applies to relations. Carrying over the definition from directed graphs to relations, 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. Theorem 1 can be obtained from the definition of a path in a relation.
Proof: We will use mathematical induction. By definition, there is a path from a to b of length one if and only if (a,b)∈R, so the theorem is true when n=1.
Assume that the theorem is true for the positive integer n. This is the inductive hypothesis. There is a path of length n+1 from a to b if and only if there is an element c∈A such that there is a path of length one from a to c, so (a,c)∈R, and a path of length n from c to b, that is, (c,b)∈Rn. Consequently, by the inductive hypothesis, there is a path of length n+1 from a to b if and only if there is an element c with (a,c)∈R and (c,b)∈Rn. But there is such an element if and only if (a,b)∈Rn+1. Therefore, there is a path of length n+1 from a to b if and only if (a,b)∈Rn+1. This completes the proof. ■
We now show that finding the transitive closure of a relation is equivalent to determining which pairs of vertices in the associated directed graph are connected by a path. With this in mind, we define a new relation.
Because Rn consists of the pairs (a,b) such that there is a path of length n from a to b, it follows that R∗ is the union of all the sets Rn. In other words,
R∗=n=1⋃∞Rn.
The connectivity relation is useful in many models.
Let R be the relation on the set of all people in the world that contains (a,b) if a has met b. What is Rn, where n is a positive integer greater than one? What is R∗?
Solution: The relation R2 contains (a,b) if there is a person c such that (a,c)∈R and (c,b)∈R, that is, if there is a person c such that a has met c and c has met b. Similarly, Rn consists of those pairs (a,b) such that there are people x1,x2,…,xn−1 such that a has met x1, x1 has met x2, …, and xn−1 has met b.
The relation R∗ contains (a,b) if there is a sequence of people, starting with a and ending with b, such that each person in the sequence has met the next person in the sequence. (There are many interesting conjectures about R∗. Do you think that this connectivity relation includes the pair with you as the first element and the president of Mongolia as the second element? We will use graphs to model this application in Chapter 10.) ◀
Let R be the relation on the set of all subway stops in New York City that contains (a,b) if it is possible to travel from stop a to stop b without changing trains. What is Rn when n is a positive integer? What is R∗?
Solution: The relation Rn contains (a,b) if it is possible to travel from stop a to stop b by making at most n−1 changes of trains. The relation R∗ consists of the ordered pairs (a,b) where it is possible to travel from stop a to stop b making as many changes of trains as necessary. (The reader should verify these statements.) ◀
Let R be the relation on the set of all states in the United States that contains (a,b) if state a and state b have a common border. What is Rn, where n is a positive integer? What is R∗?
Solution: The relation Rn consists of the pairs (a,b), where it is possible to go from state a to state b by crossing exactly n state borders. R∗ consists of the ordered pairs (a,b), where it is possible to go from state a to state b crossing as many borders as necessary. (The reader should verify these statements.) The only ordered pairs not in R∗ are those containing states that are not connected to the continental United States (i.e., those pairs containing Alaska or Hawaii). ◀
Theorem 2 shows that the transitive closure of a relation and the associated connectivity relation are the same.
Proof: Note that R∗ contains R by definition. To show that R∗ is the transitive closure of R we must also show that R∗ is transitive and that R∗⊆S whenever S is a transitive relation that contains R.
First, we show that R∗ is transitive. If (a,b)∈R∗ and (b,c)∈R∗, then there are paths from a to b and from b to c in R. We obtain a path from a to c by starting with the path from a to b and following it with the path from b to c. Hence, (a,c)∈R∗. It follows that R∗ is transitive.
Now suppose that S is a transitive relation containing R. Because S is transitive, Sn also is transitive (the reader should verify this) and Sn⊆S (by Theorem 1 of Section 9.1). Furthermore, because
S∗=k=1⋃∞Sk
and Sk⊆S, it follows that S∗⊆S. Now note that if R⊆S, then R∗⊆S∗, because any path in R is also a path in S. Consequently, R∗⊆S∗⊆S. Thus, any transitive relation that contains R must also contain R∗. Therefore, R∗ is the transitive closure of R. ■
Now that we know that the transitive closure equals the connectivity relation, we turn our attention to the problem of computing this relation. We do not need to examine arbitrarily long paths to determine whether there is a path between two vertices in a finite directed graph. As Lemma 1 shows, it is sufficient to examine paths containing no more than n edges, where n is the number of elements in the set.
Figure 2 — Producing a Path with Length Not Exceeding n.
Proof: Suppose there is a path from a to b in R. Let m be the length of the shortest such path. Suppose that x0,x1,x2,…,xm−1,xm, where x0=a and xm=b, is such a path.
Suppose that a=b and that m>n, so that m≥n+1. By the pigeonhole principle, because there are n vertices in A, among the m vertices x0,x1,…,xm−1, at least two are equal (see Figure 2).
Suppose that xi=xj with 0≤i<j≤m−1. Then the path contains a circuit from xi to itself. This circuit can be deleted from the path from a to b, leaving a path, namely, x0,x1,…,xi,xj+1,…,xm−1,xm, from a to b of shorter length. Hence, the path of shortest length must have length less than or equal to n.
The case where a=b is left as an exercise for the reader. ■
From Lemma 1, we see that the transitive closure of R is the union of R,R2,R3,…, and Rn. This follows because there is a path in R∗ between two vertices if and only if there is a path between these vertices in Ri, for some positive integer i with i≤n. Because
R∗=R∪R2∪R3∪⋯∪Rn
and the zero–one matrix representing a union of relations is the join of the zero–one matrices of these relations, the zero–one matrix for the transitive closure is the join of the zero–one matrices of the first n powers of the zero–one matrix of R.
Find the zero–one matrix of the transitive closure of the relation R where
MR=101011100.
Solution: By Theorem 3, it follows that the zero–one matrix of R∗ is
MR∗=MR∨MR[2]∨MR[3].
Because
MR[2]=101111101andMR[3]=101111101,
it follows that
MR∗=101011100∨101111101∨101111101=101111101.◀
Theorem 3 can be used as a basis for an algorithm for computing the matrix of the relation R∗. To find this matrix, the successive Boolean powers of MR, up to the nth power, are computed. As each power is calculated, its join with the join of all smaller powers is formed. When this is done with the nth power, the matrix for R∗ has been found. This procedure is displayed as Algorithm 1.
ALGORITHM 1 A Procedure for Computing the Transitive Closure.
procedure transitive closure (MR : zero–one n × n matrix)
A := MR
B := A
for i := 2 to n
A := A ⊙ MR
B := B ∨ A
return B {B is the zero–one matrix for R*}
We can easily find the number of bit operations used by Algorithm 1 to determine the transitive closure of a relation. Computing the Boolean powers MR,MR[2],…,MR[n] requires that n−1 Boolean products of n×n zero–one matrices be found. Each of these Boolean products can be found using n2(2n−1) bit operations. Hence, these products can be computed using n2(2n−1)(n−1) bit operations.
To find MR∗ from the n Boolean powers of MR, n−1 joins of zero–one matrices need to be found. Computing each of these joins uses n2 bit operations. Hence, (n−1)n2 bit operations are used in this part of the computation. Therefore, when Algorithm 1 is used, the matrix of the transitive closure of a relation on a set with n elements can be found using n2(2n−1)(n−1)+(n−1)n2=2n3(n−1), which is O(n4) bit operations. The remainder of this section describes a more efficient algorithm for finding transitive closures.
Warshall's algorithm, named after Stephen Warshall, who described this algorithm in 1960, is an efficient method for computing the transitive closure of a relation. Algorithm 1 can find the transitive closure of a relation on a set with n elements using 2n3(n−1) bit operations. However, the transitive closure can be found by Warshall's algorithm using only 2n3 bit operations.
Suppose that R is a relation on a set with n elements. Let v1,v2,…,vn be an arbitrary listing of these n elements. The concept of the interior vertices of a path is used in Warshall's algorithm. If a,x1,x2,…,xm−1,b is a path, its interior vertices are x1,x2,…,xm−1, that is, all the vertices of the path that occur somewhere other than as the first and last vertices in the path. For instance, the interior vertices of a path a,c,d,f,g,h,b,j in a directed graph
are c,d,f,g,h, and b. The interior vertices of a,c,d,a,f,b are c,d,a, and f. (Note that the first vertex in the path is not an interior vertex unless it is visited again by the path, except as the last vertex. Similarly, the last vertex in the path is not an interior vertex unless it was visited previously by the path, except as the first vertex.)
Warshall's algorithm is based on the construction of a sequence of zero–one matrices. These matrices are W0,W1,…,Wn, where W0=MR is the zero–one matrix of this relation, and Wk=[wij[k]], where wij[k]=1 if there is a path from vi to vj such that all the interior vertices of this path are in the set {v1,v2,…,vk} (the first k vertices in the list) and is 0 otherwise. (The first and last vertices in the path may be outside the set of the first k vertices in the list.) Note that Wn=MR∗, because the (i,j)th entry of MR∗ is 1 if and only if there is a path from vi to vj, with all interior vertices in the set {v1,v2,…,vn} (but these are the only vertices in the directed graph). Example 8 illustrates what the matrix Wk represents.
Let R be the relation with directed graph shown in Figure 3. Let a,b,c,d be a listing of the elements of the set. Find the matrices W0,W1,W2,W3, and W4. The matrix W4 is the transitive closure of R.
Figure 3 — The Directed Graph of the Relation R.
Solution: Let v1=a,v2=b,v3=c, and v4=d. W0 is the matrix of the relation. Hence,
W0=0110000001011010.
W1 has 1 as its (i,j)th entry if there is a path from vi to vj that has only v1=a as an interior vertex. Note that all paths of length one can still be used because they have no interior vertices. Also, there is now an allowable path from b to d, namely, b,a,d. Hence,
W1=0110000001011110.
W2 has 1 as its (i,j)th entry if there is a path from vi to vj that has only v1=a and/or v2=b as its interior vertices, if any. Because there are no edges that have b as a terminal vertex, no new paths are obtained when we permit b to be an interior vertex. Hence, W2=W1.
W3 has 1 as its (i,j)th entry if there is a path from vi to vj that has only v1=a, v2=b, and/or v3=c as its interior vertices, if any. We now have paths from d to a, namely, d,c,a, and from d to d, namely, d,c,d. Hence,
W3=0111000001011111.
Finally, W4 has 1 as its (i,j)th entry if there is a path from vi to vj that has v1=a, v2=b, v3=c, and/or v4=d as interior vertices, if any. Because these are all the vertices of the graph, this entry is 1 if and only if there is a path from vi to vj. Hence,
W4=1111000011111111.
This last matrix, W4, is the matrix of the transitive closure. ◀
Warshall's algorithm computes MR∗ by efficiently computing W0=MR,W1,W2,…,Wn=MR∗. This observation shows that we can compute Wk directly from Wk−1: There is a path from vi to vj with no vertices other than v1,v2,…,vk as interior vertices if and only if either there is a path from vi to vj with its interior vertices among the first k−1 vertices in the list, or there are paths from vi to vk and from vk to vj that have interior vertices only among the first k−1 vertices in the list. That is, either a path from vi to vj already existed before vk was permitted as an interior vertex, or allowing vk as an interior vertex produces a path that goes from vi to vk and then from vk to vj. These two cases are shown in Figure 4.
The first type of path exists if and only if wij[k−1]=1, and the second type of path exists if and only if both wik[k−1] and wkj[k−1] are 1. Hence, wij[k] is 1 if and only if either wij[k−1] is 1 or both wik[k−1] and wkj[k−1] are 1. This gives us Lemma 2.
Figure 4 — Adding vk to the Set of Allowable Interior Vertices.
Lemma 2 gives us the means to compute efficiently the matrices Wk,k=1,2,…,n. We display the pseudocode for Warshall's algorithm, using Lemma 2, as Algorithm 2.
ALGORITHM 2 Warshall Algorithm.
procedure Warshall (MR : n × n zero–one matrix)
W := MR
for k := 1 to n
for i := 1 to n
for j := 1 to n
wij := wij ∨ (wik ∧ wkj)
return W {W = [wij] is MR*}
The computational complexity of Warshall's algorithm can easily be computed in terms of bit operations. To find the entry wij[k] from the entries wij[k−1], wik[k−1], and wkj[k−1] using Lemma 2 requires two bit operations. To find all n2 entries of Wk from those of Wk−1 requires 2n2 bit operations. Because Warshall's algorithm begins with W0=MR and computes the sequence of n zero–one matrices W1,W2,…,Wn=MR∗, the total number of bit operations used is n⋅2n2=2n3.