Floyd-Warshall Algorithm
An All-Pairs Shortest Paths algorithm
• The Floyd-Warshall algorithm is a powerful tool
used in computer science to find the shortest
paths between all pairs of nodes in a weighted
graph.
• This algorithm is particularly useful when you
need to know the shortest path for every
possible pair of nodes, making it a key technique
in solving all-pairs shortest path problems.
Thursday, April 17, 2025
Floyd-Warshall Algorithm
• The algorithm works by checking every
possible path between the nodes and
updating the shortest known distance
between them.
• It repeats this process for each node in the
graph until it has considered all possible paths,
• ensuring that the shortest path is found for
every pair of nodes.
Thursday, April 17, 2025
Applications of Floyd-Warshall’s Algorithm
• Network Routing: Finds the shortest paths between all pairs of nodes in
communication networks to optimize routing protocols.
• Urban Planning: Helps in determining the most efficient routes between multiple
locations in a city for infrastructure development.
• Traffic Management: Assists in calculating the shortest travel paths between
intersections in road networks.
• Social Network Analysis: Used to determine the shortest connection paths between
individuals in social networks.
• Computer Graphics: Computes shortest paths in 3D meshes for rendering and
animation.
• Telecommunications: Optimizes signal transmission paths across various nodes in
telecommunication networks.
• Flight Scheduling: Determines the shortest flight routes between multiple airports for
airlines.
• Game Development: Used in pathfinding algorithms to determine the shortest route
between points in a game environment.
Thursday, April 17, 2025
Key Concepts in Floyd-Warshall Algorithm
• Dynamic Programming:
• This is a method used to solve problems by breaking them down into
smaller, simpler subproblems.
• In the Floyd-Warshall algorithm, dynamic programming is used to
update and keep track of the shortest paths between nodes by
gradually improving the known distances as more paths are
considered.
• All-Pairs Shortest Paths:
• The Floyd-Warshall algorithm finds the shortest paths for all pairs of
nodes in the graph, not just from one node to all others (single
source).
• This means that after running the algorithm, you know the shortest
path between any two nodes in the graph.
Thursday, April 17, 2025
Key Concepts in Floyd-Warshall cont…
• Matrix Representation:
• The algorithm uses a matrix (a table with rows and columns) to
represent the graph and store the distances between nodes.
[Remember The graph can be represented as an adjacency matrix]
• Here, each cell in the matrix represents the distance between two
nodes.
• Where ,infinity symbol "∞" represents that there is no direct path
between the nodes.
• As the algorithm runs, it updates the matrix to reflect the shortest
known distances between all pairs of nodes.
• The final matrix represents the shortest paths between all pairs of
nodes.
• The time complexity of Floyd-Warshall algorithm is O(V3).
Thursday, April 17, 2025
Operation of Floyd-Warshall
• Given a weighted graph….
• First remove all the self loops and parallel edges( keep the
lowest weight edge ) from the graph.
• Write the initial matrix
• This represents the distance btwn every pair of vertices in the
form of given weights.
• For diagonal elements( representing self loops), distance value =
0.
• For vertices having a direct edge btwn them, distance value=
weight of that edge.
• For vertices having no direct edge btwn them distance value
=infinity
Thursday, April 17, 2025
Here's a more detailed explanation:
• Initialization:
• The algorithm starts with a matrix dist representing the
distances between all pairs of vertices, initialized with the
direct edge weights (or infinity if no direct edge exists).
• Iteration:
• The algorithm then iterates through all possible intermediate
vertices k (from 1 to the number of vertices).
• Update:
• For each pair of vertices i and j, it checks if going
from i to j through k is shorter than the current shortest path
between i and j.
Thursday, April 17, 2025
Here's a more detailed explanation:
• Formula:
• If dist[i][k] + dist[k][j] (the path from i to j through k) is shorter
than dist[i][j] (the current shortest path), then dist[i][j] is
updated to the new shorter path: dist[i][j] = dist[i][k] + dist[k]
[j].
• Result:
• After iterating through all possible intermediate vertices,
the dist matrix will contain the shortest distances between all
pairs of vertices.
Thursday, April 17, 2025
In summary:
• Consider a graph, G = {V, E} where V is the set of all vertices
present in the graph and E is the set of all the edges in the graph.
• The graph, G, is represented in the form of an adjacency
matrix, A, that contains all the weights of every edge connecting
two vertices.
• The Algorithm
• Step 1 − Construct an adjacency matrix A with all the costs of
edges present in the graph. If there is no path between two
vertices, mark the value as ∞.
Thursday, April 17, 2025
In summary cont…:
• Step 2 − Derive another adjacency matrix A1 from A keeping the first
row and first column of the original adjacency matrix intact in A1.
• And for the remaining values, say A1[i,j], if A[i,j]>A[i,k]+A[k,j] then
replace A1[i,j] with A[i,k]+A[k,j].
• Otherwise, do not change the values. Here, in this step, k = 1 (first
vertex acting as pivot).
• Step 3 − Repeat Step 2 for all the vertices in the graph by changing
the k value for every pivot vertex until the final matrix is achieved.
• Step 4 − The final adjacency matrix obtained is the final solution
with all the shortest paths.
Thursday, April 17, 2025