Floyd Warshall
Floyd Warshall Algorithm Definition: The Floyd Warshall algorithm is a greedy algorithm used to determine the shortest path between all pairs of vertice...
Floyd Warshall Algorithm Definition: The Floyd Warshall algorithm is a greedy algorithm used to determine the shortest path between all pairs of vertice...
Floyd Warshall Algorithm
Definition:
The Floyd Warshall algorithm is a greedy algorithm used to determine the shortest path between all pairs of vertices in a weighted graph.
Algorithm:
Set up a matrix of distances, where d[i, j] represents the shortest distance between vertices i and j.
Set d[i, i] to 0, as the shortest path to itself is always 0.
For each pair of vertices i and j, if d[i, j] is not equal to d[i, i], update d[i, j] to the minimum of its current value and the distance from i to j via j.
This process continues until all vertices have been processed.
d[i, j] represents the shortest path between vertices i and j.Example:
Consider a graph with the following edges:
A -> B (weight 5)
A -> C (weight 3)
B -> C (weight 4)
C -> A (weight 2)
C -> D (weight 1)
The Floyd Warshall algorithm would work as follows:
| i | j | d[i, j] |
|---|---|---|
| A | A | 0 |
| A | B | 5 |
| A | C | 3 |
| B | C | 4 |
| C | A | 2 |
| C | D | 1 |
After the algorithm finishes, d[i, j] represents the shortest path between vertices i and j. In this case, the shortest path is A -> B -> C -> A, with a total distance of 10.
Applications:
The Floyd Warshall algorithm has numerous applications in computer science, including:
Finding shortest paths: Given a graph and the weight of edges between each pair of vertices, the algorithm can determine the shortest path between all pairs of vertices.
Matching algorithms: The algorithm can be used to find matching pairs of vertices in a graph.
Topological sorting: By sorting the vertices based on their distance from a starting vertex, the algorithm can be used to perform topological sorting of a graph