## Warshall's AlgorithmWarshall's algorithm is used to determine the transitive closure of a directed graph or all paths in a directed graph by using the adjacency matrix. For this, it generates a sequence of n matrices. Where, n is used to describe the number of vertices. R A sequence of vertices is used to define a path in a simple graph. In the k The R Now we will assume a case in which r v The above case will be occur only if r The r So in summary, we can say that r Now we will describe the algorithm of Warshall's Algorithm for computing transitive closure Warshall(A[1...n, 1...n]) // A is the adjacency matrix Here, - Time efficiency of this algorithm is (n
^{3}) - In the Space efficiency of this algorithm, the matrices can be written over their predecessors.
- Θ(
*n*^{3}) is the worst-case cost. We should know that the brute force algorithm is better than Warshall's algorithm. In fact, the brute force algorithm is also faster for a space graph.
## Example of Transitive closureIn this example, we will consider two graphs. The first graph is described as follows: The matrix of this graph is described as follows: The second graph is described as follows: The matrix of this graph is described as follows: The main idea of these graphs is described as follows: - The vertices i, j will be contained a path if
- The graph contains an edge from i to j; or
- The graph contains a path from i to j with the help of vertex 1; or
- The graph contains a path from i to j with the help of vertex 1 and/or vertex 2; or
- The graph contains a path from i to j with the help of vertex 1, 2, and/or vertex 3; or
- The graph contains a path from i to j with the help of any other vertices.
On the kth iteration, the algorithm will use the vertices among 1, …., k, known as the intermediate, and find out that there is a path exists between i and j vertices or not In a directed graph, the The A ## Warshall's Algorithm (Matrix generation)Recurrence relating elements R(k) to elements of R(k-1) can be described as follows: R(k)[i, j] = R(k-1)[i, j] or (R(k-1)[i, k] and R(k-1)[k, j]) In order to generate R(k) from R(k-1), the following rules will be implemented:
## Application of Warshall's algorithm to the directed graphIn order to understand this, we will use a graph, which is described as follow: For this graph R(0) will be looked like this: Here R(0) shows the adjacency matrix. In R(0), we can see the existence of a path, which has no intermediate vertices. We will get R(1) with the help of a boxed row and column in R(0). In R(1), we can see the existence of a path, which has intermediate vertices. The number of the vertices is not greater than 1, which means just a vertex. It contains a new path from d to b. We will get R(2) with the help of a boxed row and column in R(1). In R(2), we can see the existence of a path, which has intermediate vertices. The number of the vertices is not greater than 2, which means a and b. It contains two new paths. We will get R(3) with the help of a boxed row and column in R(2). In R(3), we can see the existence of a path, which has intermediate vertices. The number of the vertices is not greater than 3, which means a, b and c. It does not contain any new paths. We will get R(4) with the help of a boxed row and column in R(3). In R(4), we can see the existence of a path, which has intermediate vertices. The number of the vertices is not greater than 4, which means a, b, c and d. It contains five new paths.
In this example, we will assume A = {1, 2, 3, 4} and let R be the relation on A whose matrix is described as follows: Now we will use Warshall's algorithm to compute M
Here, matrix is the starting point. So matrix is described as follows: Now we will assume W
In this step, we will transfer all 1's in W
Now we will make of list separately of all the columns that have 1's in row k and all the rows that have 1's in column k. After separation, we will get columns 1, 2, and 4, and rows 1, 2, and 3.
In this step, we will pair each of the row numbers with each of the column numbers. If there is not 1 in the paired position, then we will add 1 in that position of W The pairs which we get are described as follow: (1, 1), (1, 2), (1, 4), (2, 1), (2, 2), (2, 4), (3, 1), (3, 2) and (3, 4). The matrix with the help of these pairs is described as follows: Now we will repeat the same process for k = 2. The first step will provide us the following matrix: Using the second step, we will get the row numbers 1, 2, 3, 4 and column numbers 1, 2, 3, 4. With the help of third step, we will get all possible pairs of row numbers and column numbers for k = 2, which are described as follows: (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), and (4, 4). The matrix with the help of these pairs is described as follows: In this example, we will also have to calculate W Thus And the transition closure R can be defined as the total relation A*A, which contains every possible ordered pair of elements of A. So it can specify that everything in A is related by R Now we will use the Boolean operation ∧ and ∨ on {0, 1} so that we can understand this algorithm more formally. If Wij(k) is the (i, j) entry of Wk, then on the basis of the above-described steps, we can say that w According to the second step of this algorithm, if i exists in the row list and j exists in the column list, then w(k?1)ik∧w(k?1)kj=1will be 1. If the third step of this algorithm either shows w Next TopicMade In India Mobile Phones |