UNIT 4: Dynamic Programming
Warshall’s and Floyd’s Algorithms
Warshall - Floyd’s Algorithms
• Also known as Floyd's algorithm, the Roy–Warshall algorithm, the
Roy–Floyd algorithm, or the WFI algorithm
• Robert Floyd in 1962, essentially the same as algorithms previously
published by individuals - Bernard Roy in 1959, and also by Stephen
Warshall in 1962
• algorithm for finding shortest paths in a directed weighted graph with
positive or negative edge weights (but with no negative cycles)
Interesting fact!!
In Season 4 episode "Black Swan" of the television crime
drama, mathematical genius Charles Eppes proposed using
the Floyd-Warshall algorithm to analyze the most recent
destinations of a bombing suspect.
Warshall’s algorithm
• for computing the transitive closure of a directed
graph (undirected graph)
• Is an application of the dynamic programming
technique.
• Transitive closure is the reachability matrix to reach
from vertex u to vertex v of a graph.
• transitive closure of a digraph can be computed by
applying DFS/BFS on every vertex – less efficient
Warshall’s algorithm: Idea
• constructs the transitive closure of a given digraph
with n vertices through a series of n-by-n boolean
matrices
Time efficiency of Warshall's algorithm is cubic
Example
Let’s check our understanding…
Apply Warshall’s algorithm to find the transitive
closure of the digraph defined by the following
adjacency matrix
Floyd’s algorithm
• for computing all-pairs shortest-paths problem.
• Is an application of the dynamic programming
technique.
• It is applicable to both undirected and directed
weighted graphs provided that they do not contain
a cycle of a negative length
Floyd’s algorithm: Idea
• computes the distance matrix of a weighted graph
with n vertices through a series of n-by-n matrices
Time efficiency of Floyd's algorithm is cubic
Let’s check our understanding…
Solve the all-pairs shortest-path problem for the
digraph with the weight matrix
Comparison with other shortest path algorithms
• Floyd–Warshall algorithm is a good choice for computing
paths between all pairs of vertices in dense graphs, in which
most or all pairs of vertices are connected by edges.
• For sparse graphs with non-negative edge weights, lower
asymptotic complexity can be obtained by running Dijkstra's
algorithm from each possible starting vertex
• For sparse graphs with negative edges but no negative
cycles, Johnson's algorithm can be used, with the same
asymptotic running time as the repeated Dijkstra approach.
Warshall – Floyd’s algorithm: Applications
• Software engineering: investigating data flow and control flow
dependencies as well as for inheritance testing of object-oriented
software.
• Optimal routing: finding the path with the maximum flow between two
vertices.
• Electronic engineering: for redundancy identification and test
generation for digital circuits
• Fast computation of Pathfinder networks.
• Widest paths/Maximum bandwidth paths
• Computing canonical form of difference bound matrices (DBMs)
• Computing the similarity between graphs
• Finding a regular expression denoting the regular language accepted by
a finite automaton
• Inversion of real matrices