0% found this document useful (0 votes)
351 views3 pages

Warshall's Algorithm for Reachability Matrix

Warshall's algorithm takes an adjacency matrix as input and outputs a reachability matrix that shows whether every pair of vertices in a graph are connected by a path. It works by iteratively updating the matrix, starting with the adjacency matrix A0. In each step i, the entry Ai[x,y] is set to 1 if there exists a path from x to y using at most i intermediate vertices. When the algorithm terminates, the final matrix An shows the reachability between all pairs of vertices. The example demonstrates computing the reachability matrix for a sample graph using Warshall's algorithm.

Uploaded by

achintyashri2205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
351 views3 pages

Warshall's Algorithm for Reachability Matrix

Warshall's algorithm takes an adjacency matrix as input and outputs a reachability matrix that shows whether every pair of vertices in a graph are connected by a path. It works by iteratively updating the matrix, starting with the adjacency matrix A0. In each step i, the entry Ai[x,y] is set to 1 if there exists a path from x to y using at most i intermediate vertices. When the algorithm terminates, the final matrix An shows the reachability between all pairs of vertices. The example demonstrates computing the reachability matrix for a sample graph using Warshall's algorithm.

Uploaded by

achintyashri2205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Step 6:

Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 0 1 0
Dist. 0 3 2 1 2 1 1
1 G Next * A B C D G E
A 0 2 2
E
C
1 F

Step 7:

Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 0 0 0
Dist. 0 3 2 1 2 1 1
2 E
A 0 2 1 G Next * A B C D G E

C 1 F

6.4. Reachability Matrix (Warshall’s Algorithm):

Warshall’s algorithm requires knowing which edges exist and which does not. It doesn’t
need to know the lengths of the edges in the given directed graph. This information is
conveniently displayed by adjacency matrix for the graph, in which a ‘1’ indicates the
existence of an edge and ‘0’ indicates non-existence.

A l l P a ir s Re c h a b i l it y
A d j ac e nc y M at r i x W a r s h a ll’ s A l g or it h m
M at r i x

It begins with the adjacency matrix for the given graph, which is called A0, and then
updates the matrix ‘n’ times, producing matrices called A1, A2, . . . . . , An and then
stops.

In warshall’s algorithm the matrix Ai contains information about the existence of


i–paths. A one entry in the matrix Ai will correspond to the existence of i–paths and
zero entry will correspond to non-existence. Thus when the algorithm stops, the final
matrix An, contains the desired connectivity information.

A one entry indicates a pair of vertices, which are connected and zero entry indicates a
pair, which are not. This matrix is called a reachability matrix or path matrix for the
graph. It is also called the transitive closure of the original adjacency matrix.

The update rule for computing Ai from Ai-1 in warshall’s algorithm is:

Ai [x, y] = Ai-1 [x, y] (Ai-1 [x, i] Ai-1 [i, y]) ---- (1)

Lecture Notes 196 Dept. of Information Technology


Example 1:

Use warshall’s algorithm to calculate the reachability matrix for the graph:
4
1 4
5 6
7 11

1
2 3
7

We begin with the adjacency matrix of the graph ‘A0’

1 0 1 1 0
2 0 0 1 1
A0
3 0 0 0 0
4 1 1 1 0

The first step is to compute ‘A1’ matrix. To do so we will use the updating rule – (1).

Before doing so, we notice that only one entry in A0 must remain one in A1, since in
Boolean algebra 1 + (anything) = 1. Since these are only nine zero entries in A0, there
are only nine entries in A0 that need to be updated.

A1[1, 1] = A0[1, 1] (A0[1, 1] A0[1, 1]) = 0 (0 0) = 0


A1[1, 4] = A0[1, 4] (A0[1, 1] A0[1, 4]) = 0 (0 0) = 0
A1[2, 1] = A0[2, 1] (A0[2, 1] A0[1, 1]) = 0 (0 0) = 0
A1[2, 2] = A0[2, 2] (A0[2, 1] A0[1, 2]) = 0 (0 1) = 0
A1[3, 1] = A0[3, 1] (A0[3, 1] A0[1, 1]) = 0 (0 0) = 0
A1[3, 2] = A0[3, 2] (A0[3, 1] A0[1, 2]) = 0 (0 1) = 0
A1[3, 3] = A0[3, 3] (A0[3, 1] A0[1, 3]) = 0 (0 1) = 0
A1[3, 4] = A0[3, 4] (A0[3, 1] A0[1, 4]) = 0 (0 0) = 0
A1[4, 4] = A0[4, 4] (A0[4, 1] A0[1, 4]) = 0 (1 0) = 0

1 0 1 1 0
2 0 0 1 1
A1
3 0 0 0 0
4 1 1 1 0

Next, A2 must be calculated from A1; but again we need to update the 0 entries,

A2[1, 1] = A1[1, 1] (A1[1, 2] A1[2, 1]) = 0 (1 0) = 0


A2[1, 4] = A1[1, 4] (A1[1, 2] A1[2, 4]) = 0 (1 1) = 1
A2[2, 1] = A1[2, 1] (A1[2, 2] A1[2, 1]) = 0 (0 0) = 0
A2[2, 2] = A1[2, 2] (A1[2, 2] A1[2, 2]) = 0 (0 0) = 0
A2[3, 1] = A1[3, 1] (A1[3, 2] A1[2, 1]) = 0 (0 0) = 0
A2[3, 2] = A1[3, 2] (A1[3, 2] A1[2, 2]) = 0 (0 0) = 0
Lecture Notes 197 Dept. of Information Technology
A2[3, 3] = A1[3, 3] (A1[3, 2] A1[2, 3]) = 0 (0 1) = 0
A2[3, 4] = A1[3, 4] (A1[3, 2] A1[2, 4]) = 0 (0 1) = 0
A2[4, 4] = A1[4, 4] (A1[4, 2] A1[2, 4]) = 0 (1 1) = 1

1 0 1 1 1
2 0 0 1 1
A2
3 0 0 0 0
4 1 1 1 1

This matrix has only seven 0 entries, and so to compute A3, we need to do only seven
computations.

A3[1, 1] = A2[1, 1] (A2[1, 3] A2[3, 1]) = 0 (1 0) = 0


A3[2, 1] = A2[2, 1] (A2[2, 3] A2[3, 1]) = 0 (1 0) = 0
A3[2, 2] = A2[2, 2] (A2[2, 3] A2[3, 2]) = 0 (1 0) = 0
A3[3, 1] = A2[3, 1] (A2[3, 3] A2[3, 1]) = 0 (0 0) = 0
A3[3, 2] = A2[3, 2] (A2[3, 3] A2[3, 2]) = 0 (0 0) = 0
A3[3, 3] = A2[3, 3] (A2[3, 3] A2[3, 3]) = 0 (0 0) = 0
A3[3, 4] = A2[3, 4] (A2[3, 3] A2[3, 4]) = 0 (0 0) = 0

1 0 1 1 1
2 0 0 1 1
A3
3 0 0 0 0
4 1 1 1 1

Once A3 is calculated, we use the update rule to calculate A4 and stop. This matrix is
the reachability matrix for the graph.

A4[1, 1] = A3 [1, 1] (A3 [1, 4] A3 [4, 1]) = 0 (1 1) = 0 1=1


A4[2, 1] = A3 [2, 1] (A3 [2, 4] A3 [4, 1]) = 0 (1 1) = 0 1=1
A4[2, 2] = A3 [2, 2] (A3 [2, 4] A3 [4, 2]) = 0 (1 1) = 0 1=1
A4[3, 1] = A3 [3, 1] (A3 [3, 4] A3 [4, 1]) = 0 (0 1) = 0 0=0
A4[3, 2] = A3 [3, 2] (A3 [3, 4] A3 [4, 2]) = 0 (0 1) = 0 0=0
A4[3, 3] = A3 [3, 3] (A3 [3, 4] A3 [4, 3]) = 0 (0 1) = 0 0=0
A4[3, 4] = A3 [3, 4] (A3 [3, 4] A3 [4, 4]) = 0 (0 1) = 0 0=0

1 1 1 1 1
2 1 1 1 1
A4
3 0 0 0 0
4 1 1 1 1

Note that according to the algorithm vertex 3 is not reachable from itself 1. This is
because as can be seen in the graph, there is no path from vertex 3 back to itself.

Lecture Notes 198 Dept. of Information Technology

Common questions

Powered by AI

Boolean algebra is crucial to Warshall's algorithm as it underpins the logic used to update the adjacency matrix. The operation Ai[x, y] = Ai-1[x, y] ∨ (Ai-1[x, i] ∧ Ai-1[i, y]) relies on Boolean logic where ∨ (logical OR) and ∧ (logical AND) determine the presence of paths between vertices. By treating the entries as Boolean variables, the algorithm effectively checks and records whether a path exists, with a 1 meaning a path exists and a 0 meaning there is no path. This logical framework allows the algorithm to systematically build up the reachability matrix without needing to track individual paths .

The update rule in Warshall's algorithm ensures that once a path is found between two vertices, it is preserved in subsequent matrices. The rule Ai[x, y] = Ai-1[x, y] ∨ (Ai-1[x, i] ∧ Ai-1[i, y]) means that any existing path between x and y in Ai-1 is retained in Ai. Additionally, the rule checks if an indirect path can be formed through an intermediate vertex i. If such a path is possible, the entry becomes 1 (indicating reachability). This method efficiently compiles the transitive closure of the graph as the algorithm progresses through iterations .

In transitioning from matrix A3 to A4 in Warshall's algorithm, the update rule is applied to the zero entries of A3. The rule Ai[x, y] = Ai-1[x, y] ∨ (Ai-1[x, i] ∧ Ai-1[i, y]) is employed to identify if an indirect path via other vertices can connect the nodes. For example, A4[1,1] is computed as A3[1,1] ∨ (A3[1,4] ∧ A3[4,1]) = 0 ∨ (1 ∧ 1) = 1, indicating a reachability path for this entry has been found. The process is repeated for each relevant zero entry in A3 to produce A4, the final reachability matrix .

In Warshall's algorithm, only the zero entries in the adjacency matrix are subject to updates in each iteration. The update rule Ai[x, y] = Ai-1[x, y] ∨ (Ai-1[x, i] ∧ Ai-1[i, y]) is applied to these entries to see if an indirect path through other vertices can connect them. Since the logic follows Boolean algebra, once an entry becomes 1, it remains unchanged in future iterations because 1 ∨ (anything) equals 1. This selective update reduces the computational work needed for each matrix iteration .

In the context of Warshall's algorithm, the term 'transitive closure' refers to the final reachability matrix, which indicates whether there's a path between any pair of vertices in the graph. Each entry in this matrix is set to '1' if a path exists between the i-th and j-th vertices, thus representing all possible vertices' direct and indirect connections. The importance of transitive closure lies in its ability to encapsulate the full connectivity information of the graph, serving as a fundamental basis for analysing network connectivity, accessibility, and optimizing paths .

The selective updating rule in Warshall's algorithm achieves computational efficiency by targeting only zero entries in the adjacency matrices. This strategy limits the number of computations needed at each step, focusing only on positions where new paths may emerge. By leveraging the property of Boolean addition, where 1 + (anything) = 1, once an entry is confirmed as reachable, no further updates are necessary. This reduces the computational burden significantly by avoiding redundant calculations and taking advantage of existing information about direct and indirect paths .

According to Warshall's algorithm, vertex 3 is not reachable from itself because in the given graph, there is no path that allows a traversal from vertex 3 back to vertex 3. The algorithm calculates reachability based on the paths available, and if no direct or indirect path (via other vertices) exists for a vertex to reach itself, the corresponding entry in the reachability matrix will remain 0 .

The adjacency matrix is significant in Warshall's algorithm as it serves as the starting point for computing the reachability matrix. It provides a snapshot of the direct connections between vertices in the graph, where '1' denotes an edge between vertices and '0' denotes no edge. This matrix, denoted as A0, informs the algorithm which pairs of vertices are initially connected, allowing it to systematically build towards the full reachability matrix through iterative updates .

Warshall's algorithm terminates after completing n iterations, where n is equal to the number of vertices in the graph. Each iteration involves updating the current matrix based on the reachability information deduced from the previous iteration. The final matrix, An, represents the reachability matrix or transitive closure, indicating all pairs of vertices that can be connected through any sequence of edges in the graph .

The key principle of Warshall's algorithm is to compute the reachability matrix or transitive closure of a graph by iteratively updating an adjacency matrix to determine which vertices are connected by paths of various lengths. It starts with the initial adjacency matrix A0, where a '1' indicates the existence of an edge and '0' indicates non-existence. The algorithm updates the matrix through multiple steps (A1, A2, ..., An) using the update rule: Ai[x, y] = Ai-1[x, y] ∨ (Ai-1[x, i] ∧ Ai-1[i, y]). If after n iterations a matrix An contains a '1' at entry (i, j), it signifies a path exists between vertices i and j. The algorithm stops when the reachability matrix, which indicates connectivity information for all vertex pairs, is produced .

You might also like