Home     Blog

Warshall’s Algorithm

The former method discussed is quite inefficient. Let us see if a more efficient method to compute path can be produced. Let us define the matrix path k such that path k [i][j] is true if and only if there is a path from node i to node j that does not pass through any nodes numbered higher than k (except, possibly, for i and j themselves). How can the value of path k+ J [i][j] be obtained from path k? Clearly for any i and j such that path k [i][j] = true, path k+I [i][j] must be true (why?). The only situation in which path k+1 [i][j] can be true while path k [i][j] equals false is if there is a path from i to j passing through node k + I, but there is no path from i to j passing through nodes i through k. But this means that there must be a path from i to k + 1 passing through nodes i through k and a similar path from k + I to j. Thus path k+ I [i][j] equals true if and only if one of the following two conditions holds:

path k[i][j] == true path k[i][k+1] == true and path k[k+1][j] == true

This means that path k+1[i][j] equals pathk[i][j] || (path k[i][k+1] && path k[k+1][j]). An algorithm to obtain the matrix path k from the matrix path k-1 based on this observation follows

This may be logically simplified and made more efficient as follows,

Clearly, path 0[i][j] = adj, since the only way to go from node i to node j without passing through any other node is to go directly from i to j. Further, path MAXNODES-1[i][j] = path[i][j], since if a path may pass through any node numbered from 0 to MAXNODES-1, any path from node i to node j may be selected.

This technique increases the efficiency of finding the transitive closure to O(n). The method is often called Warshall's Algorithm, after its discoverer.

warshall algorithm clip image002 Warshalls Algorithm

warshall algorithm clip image004 Warshalls Algorithm

Output:

warshall algorithm clip image006 Warshalls Algorithm

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Prim Algorithm

    Given a connected weighted graph G, it is often desired to create a spanning tree T for G such that the sum of the weights of the tree edges in T is as small as possible. Such a tree is called as minimum spanning tree and represents the most inexpensive way of connecting all the [...]...


  • Dijkstra Algorithm

    Dijkstra algorithm gets its name from the Dutch computer scientist Edsger Dijkstra who invented this algorithm in 1959. It is used to find the shortest path between two non-negative nodes in a graph. Dijkstra algorithm is widely used for the purpose of routing. For any giving node in a graph the algorithm finds the path [...]...


  • Breadth First Search Algorithm

    A breadth first search traversal method, visits all the successors of a visited node before visiting any successor of any of its child nodes. This is a contradiction to depth first traversal method; which visits the successor of a visited node before visiting any of its brothers, i.e., children of the same parent. A depth [...]...


  • Kruskal’s Algorithm

    An algorithm which is used to create a minimum spanning tree is attributed to Kruskal’s algorithm. The nodes of the graph are initially considered as n distinct partial trees with one node each. At each step of the algorithm, two distinct partial trees are connected into a single partial tree by an edge of the [...]...


  • Depth First Search Algorithm

    A DFS algorithm, as the name implies, is used to search deeper in the graph, whenever possible. The edges are explored, out of the most recently discovered vertex v that still has unexplored edges leaving it. When all of v's edges have been explored, the search backtracks to explore edges leaving the vertex from which [...]...