FLOYD’S ALGORITHM
WHAT IS DYNAMIC PROGRAMMING?
To find a complicated problem by breaking
down into simple sub-problem in recursive
manner.
Sequence of decision is taken at each steps.
FLOYD’S ALGORITHM
Floyd’s algorithm, also known as Floyd-Warshall Algorithm, is a
graph algorithm used for finding the shortest paths between all
pairs of vertices in a weighted graph.
It works for both directed and undirected graphs and can handle
negative weight edges, but not negative weight cycles(where the sum
of the edges in a cycle is negative).
The Floyd-Warshall algorithm follows a dynamic programming
approach to progressively improve the shortest path estimates
between all pairs of vertices.
NEGATVE CYCLE
FLOYD’S ALGORITHM
All pair shortest path problem.
Weighted Graph with both positive and negative
costs.
Dijikstra’s algorithm uses only positive costs.
TRY IT YOUESELF
PSEUDOCODE
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i, j ] ← min{D[i, j ], D[i, k] + D[k, j ]}
return D
TIME AND SPACE complexity
TIME COMPLEXITY : O(V3)
SPACE COMPLEXITY : O(V2)
ADVANTAGE
✔️Handles negative weight edges (but not negative cycles).
✔️Simpler to implement compared to Dijkstra’s Algorithm for all-pairs shortest
paths.
✔️Works well for dense graphs (where the number of edges is close to V²).
DISADVANTAGES
❌ Does not work with negative weight cycles (it will keep reducing
distances indefinitely).
❌ Less efficient than Dijkstra’s Algorithm for sparse graphs.
APPLICATION
In computer networking, the algorithm can be used to find the shortest path between all pairs of
nodes in a network. This is termed as network routing.
Flight Connectivity In the aviation industry to find the shortest path between the airports.
GIS(Geographic Information Systems) applications often involve analyzing spatial data, such as
road networks, to find the shortest paths between locations.
Why Floyd-Warshall Algorithm better for Dense
Graphs and not for Sparse Graphs?
Dense Graph: A graph in which the number of edges are
significantly much higher than the number of vertices.
Sparse Graph: A graph in which the number of edges are
very much low.
How is Floyd-warshall
algorithm different from Dijkstra’s algorithm?
Dijkstra’s algorithm is a single-source shortest path algorithm used to find
the shortest paths from a single source vertex to all other vertices in a
weighted graph.
Greedy approach,as it selects the vertex with the shortest distance
The Floyd-Warshall algorithm is an all-pairs shortest path algorithm used to find
the shortest paths between all pairs of vertices in a weighted graph.
It uses Dynamic programming, to compute all pair shortest paths.
DIFFERENCE BETWEEN FLOYD AND
WARSHALL ALGORITHM