Floyds Program
Floyds Program
Floyd's algorithm.
FLOYD'S ALGORITHM
Introduction:
Floyd's Algorithm (also known as the Floyd-Warshall Algorithm) is a dynamic programming
approach used to solve the All-Pairs Shortest Paths problem in a weighted graph. It computes
the shortest paths between all pairs of vertices, ensuring optimal solutions for each pair.
Problem Statement:
Given a directed graph with n vertices represented by a weight matrix W[1..n,1..n], where:
• W[i][j] = weight of the edge from vertex i to vertex j.
• W[i][j] = ∞ (or a large value) if there is no direct edge from i to j.
• W[i][i]= 0 for all i (distance from a vertex to itself is zero).
The goal is to find a distance matrix D where:
• D[i][j] = shortest path length from vertex i to vertex j.
Key Concepts:
1. All-Pairs Shortest Paths:
o The objective is to find the shortest path between every pair of vertices in the
graph.
2. Dynamic Programming Approach:
o The algorithm systematically solves overlapping subproblems and builds up
solutions to larger problems using optimal substructure property.
3. Optimal Substructure Property:
o The shortest path from i to j using vertices 1 to k as intermediate vertices is
calculated as: D[i][j]=min(D[i][j],D[i][k]+D[k][j])
▪ This checks if the path from i to j through k is shorter than the current
known path.
4. No Negative-Length Cycles:
o The algorithm does not work if the graph has negative-length cycles, as the
shortest path would be infinitely decreasing.
Algorithm Explanation:
1. Initialization:
o Initialize the distance matrix D as a copy of the weight matrix W: D←W
o D[i][j] is set to W[i][j], representing the direct edge weight.
2. Dynamic Programming Update:
o The algorithm uses a series of intermediate vertices k to find shorter paths.
o For each intermediate vertex k, it updates the shortest path between all pairs (i,
j) using the recurrence relation: D[i][j]=min(D[i][j],D[i][k]+D[k][j])
o This checks if the shortest path from i to j can be improved by going through
vertex k.
3. Triple Nested Loop:
Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
o The algorithm uses three nested loops:
▪ k loop: Intermediate vertex ranging from 1 to n.
▪ i loop: Source vertex ranging from 1 to n.
▪ j loop: Destination vertex ranging from 1 to n.
o The order of the loops ensures that all pairs of vertices are checked for shortest
paths using each intermediate vertex.
4. Final Distance Matrix:
o After processing all intermediate vertices, D[i][j] contains the shortest path from
vertex i to vertex j.
Example:
Consider the following graph represented by the weight matrix:
Applications:
• Network Routing Protocols: Finding shortest paths in communication networks.
• Graph Analysis: Computing transitive closure and reachability matrices.
• Social Network Analysis: Calculating shortest paths between users.
• Dynamic Programming Problems: Used in multi-stage decision processes.
Summary:
• Floyd's Algorithm is an efficient solution to the All-Pairs Shortest Paths problem
using dynamic programming.
• It checks all possible paths by considering each vertex as an intermediate point,
ensuring optimal solutions.
• The algorithm is suitable for dense graphs and provides comprehensive shortest path
information between all pairs of vertices.
Source code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Floyd's Algorithm
for (int k = 0; k < n; k++) { // Intermediate vertex
for (int i = 0; i < n; i++) { // Source vertex
for (int j = 0; j < n; j++) { // Destination vertex
// Update shortest path using vertex k as an intermediate point
Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
if (D[i][k] < INF && D[k][j] < INF) {
D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
}
}
}
}
return D;
}
int main() {
// Input Graph as a weight matrix (Using INF to represent no direct path)
vector<vector<int>> W = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
cout << "Input Graph: The Cost Adjacency Matrix" << endl;
displayMatrix(W);
return 0;
}
Output:
Input Graph: The Cost Adjacency Matrix
0 3 INF 5
2 0 INF 4
INF 1 0 INF
INF INF 2 0
Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.