Warshall Algorithm: Algorithm, or The WFI Algorithm
Warshall Algorithm: Algorithm, or The WFI Algorithm
Warshall Algorithm: Algorithm, or The WFI Algorithm
INTRODUCTION
In computer science, the Floyd–Warshall algorithm (also known
as Floyd's algorithm, Roy–Warshall algorithm, Roy–Floyd
algorithm, or the WFI algorithm[clarification needed]) is
a graphanalysis algorithm for finding shortest paths in a weighted
graph (with positive or negative edge weights). A single execution of the
algorithm will find the lengths (summed weights) of the shortest paths
between all pairs of vertices though it does not return details of the paths
themselves. The algorithm is an example of dynamic programming. It
was published in its currently recognized form by Robert Floyd in 1962.
However, it is essentially the same as algorithms previously published
by Bernard Roy in 1959 and also by Stephen Warshall in 1962 for
finding the transitive closure of a graph.
ALGORITHM
The Floyd–Warshall algorithm compares all possible paths through the
graph between each pair of vertices. It is able to do this with only Θ(|V|3)
comparisons in a graph. This is remarkable considering that there may
be up to Ω(|V|2) edges in the graph, and every combination of edges is
tested. It does so by incrementally improving an estimate on the shortest
path between two vertices, until the estimate is optimal.
Consider a graph G with vertices V, each numbered 1 through N. Further
consider a function shortestPath(i, j, k) that returns the shortest possible
path from i to j using vertices only from the set {1,2,...,k} as
intermediate points along the way. Now, given this function, our goal is
to find the shortest path from each i to each j using only vertices 1
to k + 1.
There are two candidates for each of these paths: either the true shortest
path only uses vertices in the set {1, ..., k}; or there exists some path that
goes from i to k + 1, then from k + 1 to j that is better. We know that the
best path from i to j that only uses vertices 1 through k is defined by
shortestPath(i, j, k), and it is clear that if there were a better path
from i to k + 1 to j, then the length of this path would be the
concatenation of the shortest path from i to k + 1 (using vertices in
{1, ..., k}) and the shortest path from k + 1 to j (also using vertices
in {1, ..., k}).
Therefore, we can define shortestPath(i, j, k) in terms of the
following recursive formula:
DESCRIPTION
Prim's algorithm has many applications, such as in the generation of this
maze, which involves a randomized variant of Prim's algorithm.
The only spanning tree of the empty graph (with an empty vertex set) is
again the empty graph. The following description assumes that this
special case is handled separately.
The algorithm continuously increases the size of a tree, one edge at a
time, starting with a tree consisting of a single vertex, until it spans all
vertices.Input: A non-empty connected weighted graph with
vertices V and edges E (the weights can be negative).
TIME COMPLEXITY
Image Description
DESCRIPTION
create a forest F (a set of trees), where each vertex in the graph is a
separate tree
create a set S containing all the edges in the graph
while S is nonempty and F is not yet spanning
remove an edge with minimum weight from S
if that edge connects two different trees, then add it to the
forest, combining two trees into a single tree
otherwise discard that edge.
At the termination of the algorithm, the forest has only one component
and forms a minimum spanning tree of the graph.
PERFORMANCE
Where E is the number of edges in the graph and V is the number of
vertices, Kruskal's algorithm can be shown to run in O(E log E) time, or
equivalently, O(E log V) time, all with simple data structures. These
running times are equivalent because:
Image Description