
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























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:
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).
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.
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.
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
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 a b and b c , then a c. 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).
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 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).
2
Possibilities:
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
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:
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.
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
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.