0% found this document useful (0 votes)
8 views

Transitive-Closure-of-Graph

How to Find Transitive Closure of a graph Warshall's Algorithm

Uploaded by

hodpgcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Transitive-Closure-of-Graph

How to Find Transitive Closure of a graph Warshall's Algorithm

Uploaded by

hodpgcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

208 DIRECTED GRAPHS [CHAP.

Suppose now that there is a path from vertex vi to vertex vj in a graph G with m vertices. Then there must
be a simple path from vi to vj when vi  = vj , or there must be a cycle from vi to vj when vi = vj . Since G has
m vertices, such a simple path must have length m − 1 or less, or such a cycle must have length m or less. This
means that there is a nonzero ij entry in the matrix Bm (defined above) where A is the adjacency matrix of G.
Accordingly, the path matrix P and Bm have the same nonzero entries. We state this result formally.
Proposition 9.5: Let A be the adjacency matrix of a graph G with m vertices. Then the path matrix P and Bm
have the same nonzero entries where

Bm = A + A2 + A3 + · · · + Am

Recall that a directed graph G is said to be strongly connected if, for any pair of vertices u and v in G, there
is a path from u to v and from v to u. Accordingly, G is strongly connected if and only if the path matrix P of G
has no zero entries. This fact together with Proposition 9.5 gives the following result.
Proposition 9.6: Let A be the adjacency matrix of a graph G with m vertices. Then G is strongly connected if
and only if Bm has no zero entries where

Bm = A + A2 + A3 + · · · + Am

EXAMPLE 9.8 Consider the graph G and its adjacency matrix A appearing in Fig. 9-4. Here G has m = 4
vertices. Adding the matrix A and matrices A2 , A3 , A4 in Example 9.7, we obtain the following matrix B4 and
also path (reachability) matrix P by replacing the nonzero entries in B4 by 1:
⎡ ⎤ ⎡ ⎤
4 0 3 4 1 0 1 1
⎢ 11 0 7 11 ⎥ ⎢ 1 0 1 1 ⎥
B4 = ⎢ ⎥
⎣ 7 0 4 7 ⎦ and P = ⎣ 1 0 1 1 ⎦
⎢ ⎥

7 0 4 7 1 0 1 1

Examining the matrix B4 or P, we see zero entries; hence G is not strongly connected. In particular, we see that
the vertex v2 is not reachable from any of the other vertices.
Remark: The adjacency matrix A and the path matrix P of a graph G may be viewed as logical (Boolean)
matrices where 0 represents “false” and 1 represents “true.” Thus the logical operations of ∧ (AND) and
∨ (OR) may be applied to the entries of A and P where these operations, used in the next section, are defined
in Fig. 9-5.

Fig. 9-5

Transitive Closure and the Path Matrix


Let R be a relation on a finite set V with m elements. As noted above, the relation R may be identified with
the simple directed graph G = G (V , R). We note that the composition relation R 2 = R × R consists of all pairs
(u, v) such that there is a path of length 2 from u to v. Similarly:

R K = {(u, v)| there is a path of length K from u to v}.


CHAP. 9] DIRECTED GRAPHS 209

The transitive closure R* of the relation R on V may now be viewed as the set of ordered pairs (u, v) such that
there is a path from u to v in the graph G. Furthermore, by the above discussion, we need only look at simple
paths of length m − 1 or less and cycles of length m or less. Accordingly, we have the following result which
characterizes the transitive closure R* of R.
Theorem 9.7: Let R be a relation on a set V with m elements. Then:

(i) R ∗ = R ∪ R 2 ∪ . . . ∪ R m is the transitive closure of R.

(ii) The path matrix P of G(V, R) is the adjacency matrix of G (V, R*).

9.6 WARSHALL’S ALGORITHM, SHORTEST PATHS


Let G be a directed graph with m vertices, v1 , v2 ,. . . , vm . Suppose we want to find the path matrix P of the
graph G. Warshall gave an algorithm which is much more efficient than calculating the powers of the adjacency
matrix A. This algorithm is defined in this section, and a similar algorithm is used to find shortest paths in G when
G is weighted.

Warshall’s Algorithm
First we define m-square Boolean matrices P0 , P1 , . . ., Pm where Pk [i, j ] denotes the ij entry of the
matrix Pk : ⎧
⎨ 1 if there is a simple path from vi to vj which does not use any
Pk [i, j ] = other vertices except possibly v1 , v2 , …, vk,

0 otherwise.
For example,

P3 [i, j ] = 1 if there is a simple path from vi to vj which does not use any
other vertices except possibly v1 , v2 , v3 .

Observe that the first matrix P0 = A, the adjacency matrix of G. Furthermore, since G has only m vertices, the
last matrix Pm = P , the path matrix of G.
Warshall observed that Pk [i, j ] = 1 can occur only if one of the following two cases occurs:

(1) There is a simple path from vi to vj which does not use any other vertices except possibly v1 , v2 ,…,vk−1 ;
hence
Pk−1 [i, j ] = 1

(2) There is a simple path from vi to vk and a simple path from vk to vj where each simple path does not use
any other vertices except possibly v1 , v2 ,…, vk−1 ; hence

Pk−1 [i, k] = 1 and Pk−1 [k, j ] = 1

These two cases are pictured as follows:

(1) vi → · · · → vj ; (2) vi → · · · → vk → · · · vj

where → · · · → denotes part of a simple path which does not use any other vertices except possibly v1 ,v2 ,…,
vk−1 . Accordingly, the elements of Pk can be obtained by:

Pk [i, j ] = Pk−1 [i, j ] ∨ (Pk−1 [i, k] ∧ Pk−1 [k, j ])

where we use the logical operations of a ∧ (AND) and ∨ (OR). In other words we can obtain each entry in the
matrix Pk by looking at only three entries in the matrix Pk−1 . Warshall’s algorithm appears in Fig. 9-6.
210 DIRECTED GRAPHS [CHAP. 9

Fig. 9-6

Shortest-path Algorithm
Let G be a simple directed graph with m vertices, v1 , v2 , . . ., vm . Suppose G is weighted; that is, suppose each
edge e of G is assigned a nonnegative number w(e) called the weight or length of e. Then G may be maintained
in memory by its weight matrix W = [wij ] defined as follows:

w(e) if there is an edge e from vi to vj
wij =
0 if there is no edge from vi to vj

The path matrix P tells us whether or not there are paths between the vertices. Now we want to find a matrix Q
which tells us the lengths of the shortest paths between the vertices or, more exactly, a matrix Q = [qij ] where

qij = length of the shortest path from vi to vj

Next we describe a modification of Warshall’s algorithm which efficiently finds us the matrix Q.
Here we define a sequence of matrices Q0 , Q1 , . . ., Qm (analogous to the above matrices P0 , P1 , . . ., Pm )
where Qk [i, j ], the ij entry of Qk , is defined as follows:
Qk [i, j ] = the smaller of the length of the preceding path from vi to vj or the sum of the lengths of the
preceding paths from vi to vk and from vk to vj .
More exactly,
Qk [i, j ] = MIN(Qk−1 [i, j ], Qk−1 [i, k] + Qk−1 [k, j ])
The initial matrix Q0 is the same as the weight matrix W except that each 0 in w is replaced by ∞ (or a very,
very large number). The final matrix Qm will be the desired matrix Q.

EXAMPLE 9.9 Figure 9-7 shows a weighted graph G and its weight matrix W where we assume that v1 = R,
v2 = S, v3 = T, v4 = U.
Suppose we apply the modified Warshall’s algorithm to our weighted graph G in Fig. 9-7. We will obtain
the matrices Q0 , Q1 , Q3 , and Q4 in Fig. 9-8. (To the right of each matrix Qk in Fig. 9-8, we show the matrix of
CHAP. 9] DIRECTED GRAPHS 211

Fig. 9-7

paths which correspond to the lengths in the matrix Qk .) The entries in the matrix Q0 are the same as the weight
matrix W except each 0 in W is replaced by ∞ (a very large number). We indicate how the circled entries are
obtained:
Q1 [4, 2] = MIN (Q0 [4, 2], Q0 [4, 1] + Q0 [1, 2]) = MIN(∞, 4 + 5) = 9
Q2 [1, 3] = MIN (Q1 [1, 3], Q1 [1, 2] + Q1 [2, 3]) = MIN(∞, 5 + ∞) = ∞
Q3 [4, 2] = MIN (Q2 [4, 2], Q2 [4, 3] + Q2 [3, 2]) = MIN(9, 3 + 1) = 4
Q4 [3, 1] = MIN (Q3 [3, 1], Q3 [3, 4] + Q3 [4, 1]) = MIN(10, 5 + 4) = 9
The last matrix Q4 = Q, the desired shortest-path matrix.

Fig. 9-8

9.7 LINKED REPRESENTATION OF DIRECTED GRAPHS


Let G be a directed graph with m vertices. Suppose the number of edges of G is O(m) or even O(m log m),
that is, suppose G is sparse. Then the adjacency matrix A of G will contain many zeros; hence a great deal of
212 DIRECTED GRAPHS [CHAP. 9

memory space will be wasted. Accordingly, when G is sparse, G is usually represented in memory by some type
of linked representation, also called an adjacency structure, which is described below by means of an example.

Fig. 9-9

Consider the directed graph G in Fig. 9-9(a). Observe that G may be equivalently defined by the table in
Fig. 9-9(b), which shows each vertex in G followed by its adjacency list, also called its successors or neighbors.
Here the symbol ⭋ denotes an empty list. Observe that each edge of G corresponds to a unique vertex in an
adjacency list and vice versa. Here G has seven edges and there are seven vertices in the adjacency lists. This
table may also be presented in the following compact form where a colon “:” separates a vertex from its list of
neighbors, and a semicolon “;” separates the different lists:

G = [A : B, C, D; B : C; C : ⭋; D : C, E; E : C]

The linked representation of a directed graph G maintains G in memory by using linked lists for its adjacency
lists. Specifically, the linked representation will normally contain two files (sets of records), one called the Vertex
File and the other called the Edge File, as follows.

(a) Vertex File: The Vertex File will contain the list of vertices of the graph G usually maintained by an array or
by a linked list. Each record of the Vertex File will have the form

VERTEX NEXT-V PTR

Here VERTEX will be the name of the vertex, NEXT-V points to the next vertex in the list of vertices in the
Vertex File, and PTR will point to the first element in the adjacency list of the vertex appearing in the Edge
File. The shaded area indicates that there may be other information in the record corresponding to the vertex.

(b) Edge File: The Edge File contains the edges of G and also contains all the adjacency lists of G where each
list is maintained in memory by a linked list. Each record of the Edge File will represent a unique edge in G
and hence will correspond to a unique vertex in an adjacency list. The record will usually have the form

EDGE BEG-V END-V NEXT-E

Here:

(1) EDGE will be the name of the edge (if it has a name).
(2) BEG-V- points to location in the Vertex File of the initial (beginning) vertex of the edge.
(3) END-V points to the location in the Vertex File of the terminal (ending) vertex of the edge. The adjacency
lists appear in this field.
(4) NEXT-E points to the location in the Edge File of the next vertex in the adjacency list.

We emphasize that the adjacency lists consist of terminal vertices and hence are maintained by the END-V
field. The shaded area indicates that there may be other information in the record corresponding to the edge. We
note that the order of the vertices in any adjacency list does depend on the order in which the edges (pairs of
vertices) appear in the input.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy