0% found this document useful (0 votes)
14 views4 pages

Warshall's Algorithm for Transitive Closure

Uploaded by

akashnani1129
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)
14 views4 pages

Warshall's Algorithm for Transitive Closure

Uploaded by

akashnani1129
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

Warshal's Alqonthrn

* It s used to find transi tive closre

dynamie prmgranming
The branAitive Can be
cloßure of a digraph with n verices
defined by n boolen mtrix in uhich the element mth
(th yow
where' osísn and jtheotumn osi<n is 5
there is a tmvial
tivial path from ith vertex to h vertex,
otherwike it D

Algerithm Warshal
7 input i Adjaceny matix of
Houtput : Transitive digraph
closure of digraph
Ro) A
for k: 1 to n do
for iz 4 to n do

tor í= 1 to n do
Ck-) (k-)
(R Ci,kJ and R CK,iJ)
reurn Ro)
Trating p mabik

A
1

Initalize the
K= No.o Intermediate vertices
2 2

K= o
R

(a, a)= 1 (a,b) 1


d
> (d,6)= 1

(ab) =1 (6,d)=1
() ’(a,d)-1
R
(6,d)= 1

(2)
R
No pair
c d

)
>(6,4):/
(6,d)=1 with ( d,a):)
(d,)=/->():)

Cdd 1 with

(4,d)=l >d, d=1

(4)

R
b

d
Proqyam
#include < stdioihs
(int p[][o1 int n)
Vofd warshall

int i,j,kj
tor ( k=l j k<=nj ktt)
<njitt)
foy ( (2 1 ,
(i-1; iKenj j+t) PCKJLJJ
for [J[k]4f
PLiJLD- PLIJJliJI|P
3
int main()

int alieJ [ie] n,, i)


Enter the [Link] vertices"J;
Printt ("
/ \n
matrix :|»")
Printt ("\n Enter the
Adjaconty
for (i=1; ÍKen; itt)
for Gzl; 0<-n, itt)
Start ("%d", kaCiJjJ):
warshall(a,n);
sln;
Printt (" The Resultant path Matix
for li-1; i<-n;itt)
for (j=l;i K=n;0tt)
Printt (*%.d'", aciJ SJ);
Printt ("\n'))
yetun o)

Common questions

Powered by AI

In the provided implementation of Warshal's algorithm, the matrix R is initially set to be the same as the adjacency matrix of the graph. This means that an entry R[i][j] is true (1) if there is an edge directly connecting vertex i to vertex j, and false (0) otherwise. This serves as the base case for further iterations, where paths that incorporate intermediate vertices are explored .

The program handles input by prompting the user to enter the number of vertices and then the adjacency matrix representing the graph. The adjacency matrix is used as initial input for Warshal's algorithm. After the algorithm computes the transitive closure, the program outputs the resultant path matrix, which represents the transitive closure of the graph, thereby providing information about reachability between each pair of vertices .

Intermediate vertices are integrated into the pathfinding process in Warshal's algorithm through the use of the k loop. For each possible intermediate vertex k, the algorithm updates the matrix R such that for any vertices i and j, it checks if there's a path from i to j using k as an intermediate node. If either a direct path from i to j through k exists or can be created by using the current intermediate node, the matrix is updated to reflect this expanded connectivity. This systematic inclusion of each vertex as an intermediary ensures all potential paths are considered .

The primary purpose of Warshal's algorithm is to find the transitive closure of a directed graph. This involves determining if there is a path between every pair of vertices in a graph and representing this information in a boolean matrix, where the entry at row i and column j is true if there is a path from vertex i to vertex j, and false otherwise .

A boolean matrix is used in Warshal's algorithm to represent the transitive closure because it provides a simple way to encode the reachability information between the vertices of a graph. Each entry (i, j) is true if there is a path from vertex i to vertex j, and false otherwise, which is how the transitive relationships are typically represented in a compact and computationally efficient manner .

The final matrix outputted by Warshal's algorithm reflects the concept of transitive closure by indicating the reachability between every pair of vertices in the graph. Each entry in the matrix corresponds to whether a path exists from vertex i to vertex j, which includes direct as well as indirect paths. Thus, the matrix summarizes which vertices are directly or indirectly connected, effectively demonstrating the graph's connectivity structure after accounting for all possible intermediate paths .

Evaluating each pair (i, j) for transitive closure in Warshal's algorithm is significant because it ensures that the algorithm accounts for all possible paths in the graph. This thorough examination is crucial, as it identifies any indirect paths that might exist between vertices via intermediate nodes. By considering all vertex pairs, the algorithm ensures the transitive closure captures the complete connectivity of the graph, thereby correctly identifying all potential connections even if they utilize multiple edges .

Warshal’s algorithm has a computational complexity of O(n^3), where n is the number of vertices in the graph. This results from the three nested loops used to check and update the reachability for each pair of vertices with every possible intermediate vertex. While this cubic complexity is manageable for small to medium-sized graphs, it becomes computationally expensive for large graphs, potentially making it impractical for real-time applications or graphs with a very large number of vertices due to significant time and resource requirements .

The nested loops in the Warshal's algorithm implementation play a crucial role in iteratively updating the reachability matrix. The outermost loop iterates over each possible intermediate vertex (k), while the inner loops (i and j) iterate over all pairs of vertices. For each triplet (i, j, k), the algorithm checks if a vertex k can be used as an intermediate point to create a path from i to j, and, if so, updates R[i][j] to true (1) if either a direct path from i to j exists or if there is a path from i to k and from k to j. This stepwise expansion of paths ensures the complete transitive closure is achieved by the end of execution .

Dynamic programming facilitates the computation of transitive closure in Directed Graphs using Warshal's algorithm by breaking the problem into simple subproblems. The algorithm incrementally incorporates intermediate vertices into paths and updates the reachability matrix iteratively. This approach optimizes the computation by storing the results of subproblems, which are used to efficiently compute solutions for larger problems without redundant recalculations .

You might also like