Floyd-Warshall algorithm

From Wikipedia, the free encyclopedia

Graph search algorithms
Search

In computer science, the Floyd–Warshall algorithm (sometimes known as the Roy–Floyd algorithm, since Bernard Roy described this algorithm in 1959) is an algorithm for solving the all-pairs shortest path problem on weighted, directed graphs in cubic time.

Contents

The Floyd–Warshall algorithm takes as input an adjacency matrix representation of a weighted, directed graph (V, E). The weight of a path between two vertices is the sum of the weights of the edges along that path. The edges E of the graph may have negative weights, but the graph must not have any negative weight cycles. The algorithm computes, for each pair of vertices, the minimum weight among all paths between the two vertices. The running time complexity is Θ(n3) (see the Analysis below for details). The algorithm is based on the following observation:

  • Assuming the vertices of a directed graph G are V = {1, 2, 3, 4, ..., n}, consider a subset {1, 2, 3, ..., k}.
  • For any pair of vertices i, j in V, consider all paths from i to j whose intermediate vertices are all taken from {1, 2, ..., k}, and p is a minimum weight path from among them.
  • The algorithm exploits a relationship between path p and shortest paths from i to j with all intermediate vertices in the set {1, 2, ..., k−1}.
  • The relationship depends on whether or not k is an intermediate vertex of path p.

Let \mathcal{W}_\mathit{k} = [ \mathcal{W}_{\mathit{i}\mathit{j}}^{[k]} ] be the zero-one matrix that has a "1" in its (i,j) position \Leftrightarrow there is a path from vi to vj with interior vertices from the set {v1,v2,...,vk}. Then \mathcal{W}_{ij}^{[k]} = \mathcal{W}_{ij}^{[k-1]} \lor ( \mathcal{W}_{ik}^{[k-1]} \land \mathcal{W}_{kj}^{[k-1]} ), whenever i, j, and k are positive integers not exceeding n. Therefore, we can efficiently compute the matrices \mathcal{W}_k, k = {12...n}. Example psuedocode assumes the input graph is an adjacency matrix representation of a directed graph, with the value ∞ (infinity) as the weight between vertices which have no edge that directly connects them and 0 on the diagonal (distance from a vertex to itself)

procedure Warshall (\mathcal{M}_\mathcal{R} : \mathit{n} \times \mathit{n} zero-one matrix)
\mathcal{W} := \mathcal{M}_\mathcal{R}
for k: = 1 to n
begin
    for i: = 1 to n
    begin
        for j: = 1 to n
        \mathit{w}_{\mathit{i}\mathit{j}} := \mathit{w}_{\mathit{i}\mathit{j}} \lor ( \mathit{w}_{\mathit{i}\mathit{k}} \land \mathit{w}_{\mathit{k}\mathit{j}})
   end
end

To find all n2 of \mathcal{W}_k from those of \mathcal{W}_{\mathit{k}-1} requires 2n2 bit operations. Since we begin with \mathcal{W}_0 = \mathcal{W}_\mathcal{R} and compute the sequence of n zero-one matrices \mathcal{W}_1, \mathcal{W}_2, ..., \mathcal{W}_\mathit{n} = \mathcal{M}_{\mathcal{R}^*}, the total number of bit operations used is \mathit{n} \times 2\mathit{n}^2 = 2\mathit{n}^3.

The Floyd-Warshall algorithm can be used to solve the following problems, among others:

  • Shortest paths in directed graphs (Floyd's algorithm).
  • Transitive closure of directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm, the graph is unweighted and represented by a Boolean adjacency matrix. Then the addition operation is replaced by logical conjunction (AND) and the minimum operation by logical disjunction (OR).
  • Finding a regular expression denoting the regular language accepted by a finite automaton (Kleene's algorithm)
  • Inversion of real matrices (Gauss-Jordan algorithm).
  • Optimal routing. In this application one is interested in finding the path with the maximum flow between two vertices. This means that, rather than taking minima as in the pseudocode above, one instead takes maxima. The edge weights represent fixed constraints on flow. Path weights represent bottlenecks; so the addition operation above is replaced by the minimum operation.
  • Testing whether an undirected graph is bipartite.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.