Docsity

Transitive Closure and Warshall's Algorithm: Computing the Transitive Closure of a Graph -, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Transitive Closure and Warshall’s Algorithm
We now begin the study of a number of algorithms related to paths in graphs.
The first algorithm computes the transitive closure of a graph.
This algorithm can be applied to either directed or undirected graphs.
The textbook discusses it within the context of directed graphs.
This lecture has the following structure:
1. It begins with a review of basic graph theory.
2. It then presents Warshall’s Algorithm.
3. Lastly, it argues for the correctness of Warshall’s Algorithm;
that the algorithm does indeed compute what it claims to compute.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Transitive Closure and Warshall's Algorithm: Computing the Transitive Closure of a Graph - and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Transitive Closure and Warshall’s Algorithm

We now begin the study of a number of algorithms related to paths in graphs. The first algorithm computes the transitive closure of a graph. This algorithm can be applied to either directed or undirected graphs. The textbook discusses it within the context of directed graphs. This lecture has the following structure:

  1. It begins with a review of basic graph theory.
  2. It then presents Warshall’s Algorithm.
  3. Lastly, it argues for the correctness of Warshall’s Algorithm; that the algorithm does indeed compute what it claims to compute.

Basic Graph Theory

Definition: A graph G is a finite non-empty set of vertices, denoted V(G), together with a (possibly empty) finite set E  V(G)  V(G). If we define |V(G)| = N and |E(G)| = M, the graph G is called an (N, M)–graph. In the formal theory, we identify each vertex by an integer. If |V(G)| = N, we say that V(G) = {1, 2, 3, …, N}. E(G)  { (J, K) | 1 ) | 1  J  N and 1  K) | 1  N } There are a number of special classes of graph that we find interesting. When speaking of graphs, we normally mean “simple graphs” , formally defined as V(G) = {1, 2, 3, …, N} and E(G)  { (J, K) | 1 ) | 1  J  N, 1  K) | 1  N, and J  K) | 1 }. A directed graph is a simple graph in which the edge (J, K) | 1 ) is not the same as the edge (K) | 1 , J); (J, K) | 1 )  (K) | 1 , J). An undirected graph is a simple graph in which the edge (J, K) | 1 ) is defined to be the same as the edge (K) | 1 , J); (J, K) | 1 ) = (K) | 1 , J).

Adjacency and Incidence

Let G be a graph with vertex set V(G) and edge set E(G). We have a set of definitions for undirected graphs and a set for directed graphs. G is an undirected graph. If vertices J and K) | 1 are in V(G) and edge (J, K) | 1 )  E(G), then Vertices J and K) | 1 are said to be adjacent ; J is adjacent to K) | 1 and K) | 1 adjacent to J. Edge (J, K) | 1 ) is said to be incident on each of vertices J and K) | 1. G is an directed graph. If vertices J and K) | 1 are in V(G) and edge (J, K) | 1 )  E(G), then Vertex J is said to be adjacent to vertex K) | 1. Vertex K) | 1 is said to be adjacent from vertex J. Edge (J, K) | 1 ) is said to be incident from J Edge (J, K) | 1 ) is said to be incident to K) | 1. In a directed graph, an edge is often called an arc.

The Adjacency Matrix

One common way to store adjacency information for a graph is to use a data structure called an adjacency matrix. For an (N, M)–graph, the adjacency matrix is an N–by–N square matrix. The adjacency matrix of a simple graph will have zero elements on its diagonal. An element of the matrix is denoted A[J, K) | 1 ]; A[J, J] = 0 for 1  J  N. For a graph without edge weights, A[J, K) | 1 ] = 1 if and only if (J, K) | 1 )  E(G). A[J, K) | 1 ] = 0 if (J, K) | 1 ) is not an edge in the graph. For a weighted graph A[J, K) | 1 ] = w( e ) if e = (J, K) | 1 )  E(G). The value of A[J, K) | 1 ] when (J, K) | 1 ) is not an edge depends on the algorithm. If the graph is undirected, then the adjacency matrix is symmetric and A[J, K) | 1 ] = A[K) | 1 , J]. If the graph is directed, we do not have the requirement that A[J, K) | 1 ] = A[K) | 1 , J]. However, this might be the case for a large number of matrix elements.

Example Undirected Graph

Consider the following sample undirected graph. Here V(G) = {1, 2, 3, 4, 5} E(G) = { (1, 2), (1, 3), (2, 3), (2, 4), (2, 5), (3, 5), (4, 5) } The adjacency matrix                 0 1 1 1 0 0 1 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 1 0 0

Definition of Transitive Closure

Transitive closure is defined on a set of elements with a binary operation.

The binary operation is often denoted by “R” or “”. The latter is called “squiggle”. We build sets by transitivity. If a , b , c  S and both ab and bc , then ac. The transitive closure of a graph is built on the set of vertices with the binary relation being adjacency. The adjacency matrix of the graph G is defined as A[J, K) | 1 ] = 1 if and only if J is adjacent to K) | 1. The transitive closure of the graph G is defined as T[J, K) | 1 ] = 1 if and only if there is a nontrivial directed path from J to K) | 1. In this application, we are not interested in the number of edges in the path, but only in the existence of the path. For undirected graphs, we have T[J, K) | 1 ] = T[K) | 1 , J]. Each of the adjacency matrix and its transitive closure is best viewed as a Boolean matrix, with values 1 (True) and 0 (False).

Example Again

Consider the following sample undirected graph.

A  B = 0 if either A = 0 or B = 0 or both. Logical OR A  B = 1 if either A = 1 or B = 1 or both. A  B = 0 if and only if A = 0 and B = 0. There is an implementation issue with Warshall’s Algorithm that we address here. The key expression has an expression depending on the discipline. Pure mathematics will write Z = W  (X  Y)) The programming implementation more closely resembles the following sequence. Z = W If ( Z = = 0) Then Z = (X  Y))

Warshall’s Algorithm: The Key Idea

Warshall’s Algorithm is built on the construction of a sequence of Boolean matrices. There are (N + 1) matrices in this sequence. These are defined as follows. if and only if there is a path from I to J that includes vertices not numbered above K) | 1. The claim is that This seems plausible, as if and only if there is a path from vertex I to vertex J that involves vertices in [1 .. N]. But this is the complete set of vertices in V(G).

Consider the matrix R

2

Possibilities:

  1. (I, J)  E(G)
  2. (I, 1)  E(G) and (1, J)  E(G)
  3. (I, 2)  E(G) and (2, J)  E(G)
  4. (I, 1)  E(G), (1, 2)  E(G), and (2, J)  E(G) More than one of these can be true at the same time. All we need is one path. There can be quite a few.

Some Obvious Recurrences

In general, for 0  K) | 1 < N we have the following. for K) | 1  L  N. This follows from the fact that the integer set [1, K) | 1 ] is a proper subset of [1, L] when L > K) | 1. A logical corollary to the above is that for 0  K) | 1  L  N

Extending the Sequence of R Matrices

What do these equations say? The first says that if there is a path from vertex I to vertex J comprising only vertices in the set 1 to K) | 1 , then there obviously is a path involving only vertices 1 to (K) | 1 + 1). Otherwise, we add vertex (K) | 1 + 1) to the mix and ask if both of the following are true:

  1. There is a path from I to (K) | 1 + 1) involving only vertices in the set 1 to K) | 1 , and
  2. There is a path from (K) | 1 + 1) to J involving only vertices in the set 1 to K) | 1. If both are true, then we have our path from I to J with only vertices 1 to (K) | 1 + 1).

Compressing the Sequence

The last observation of importance to our algorithm is the fact that new matrix values can overwrite old ones. If R K) | 1 + is computed, then R K) | 1 is no longer important. The algorithm makes use of only one N–by–N matrix R[I, J]. For 1  K) | 1  N, the K) | 1 th iteration can change R[I, J] from 0 to 1. Once R[I, J] = = 1, it stays at that value for every further iteration.

Another Version of the Algorithm

Algorithm Warshall (A[1..N, 1..N], R[1..N, 1..N]) // // This implements the version of Warshall’s algorithm that uses // only two matrices – one for the input and one for the output. // Input: A – the N-by-N adjacency matrix of the graph. // Output: R – the N-by-N transitive closure of the graph. // For I = 1 to N Do // Set R = A For J = 1 to N Do R[I, J] = A[I, J] End Do // J End Do // I // For K = 1 to N Do For I = 1 to N Do For J = 1 to N Do If (0 == R[I,J]) Then R[I,J] = R[I,K]  R[K,J] End Do // J End Do // I End Do // K // Return R

Example Graph for Warshall’s Algorithm

Consider the following directed graph. Here is the adjacency matrix, from which the algorithm will work. This is clearly the adjacency matrix of a directed graph. Note that it is not symmetric. A[1, 4] = 0, but A[4, 1] = 1. A[2, 4] = 1, but A[4, 2] = 0.