0% found this document useful (0 votes)
7 views6 pages

Floyds Program

Uploaded by

sv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Floyds Program

Uploaded by

sv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using

Floyd's algorithm.
FLOYD'S ALGORITHM

Theory and Concept of 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:

• Infinity indicates no direct edge between vertices.


• The graph has 4 vertices and the initial distance matrix is the same as the weight
matrix.

Initialization (Given Cost Matrix)

Iteration k=0 (Using Node 0 as an Intermediate)


For each i,j
D[i][j]=min(D[i][j],D[i][0]+D[0][j])
Updated Matrix D(1)

(No changes since node 0 doesn't provide a shorter path.)

Iteration k=1 (Using Node 1 as an Intermediate)


D[i][j]=min(D[i][j],D[i][1]+D[1][j])
Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.

New shortest paths found:


• Node 2 to Node 0: D[2][0]=D[2][1]+D[1][0]=1+2=3

Updated Matrix D(2)

Iteration k=2 (Using Node 2 as an Intermediate)


D[i][j]=min(D[i][j],D[i][2]+D[2][j])

New shortest paths found:


• Node 3 to Node 0: D[3][0]=D[3][2]+D[2][0]=3+2=5
• Node 3 to Node 1: D[3][1]=D[3][2]+D[2][1]=2+1=3

Updated Matrix D(3)

Iteration k=3 (Using Node 3 as an Intermediate)


D[i][j]=min(D[i][j],D[i][3]+D[3][j])

New shortest paths found:


• Node 0 to Node 2: D[0][2]=D[0][3]+D[3][2]=5+2=7.
• Node 1 to Node 2: D[1][2]=D[1][3]+D[3][2]=4+2=6.

Final Shortest Path Matrix D(4):


Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
• Each entry D[i][j]D[i][j]D[i][j] represents the shortest distance from vertex i to vertex
j.
• If no path exists, the value remains as INF.

Time and Space Complexity:


• Time Complexity: O(n3)O(n^3)O(n3) due to the triple nested loop.
• Space Complexity: O(n2)O(n^2)O(n2) for storing the distance matrix.

Advantages and Disadvantages:


• Advantages:
o Handles negative weights (as long as there are no negative cycles).
o Simple to implement using dynamic programming.
o Computes shortest paths between all pairs of vertices.
• Disadvantages:
o Inefficient for sparse graphs due to O(n3)O(n^3)O(n3) complexity.
o Not suitable for graphs with negative cycles.

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;

const int INF = 1e9; // Use a large value to represent infinity

// Function to implement Floyd's Algorithm


vector<vector<int>> floydWarshall(vector<vector<int>> &W) {
int n = W.size();
vector<vector<int>> D = W; // Initialize distance matrix with weight matrix

// 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;
}

// Function to display the matrix


void displayMatrix(const vector<vector<int>> &matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == INF)
cout << "INF\t";
else
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}

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);

// Apply Floyd's Algorithm


vector<vector<int>> shortestPaths = floydWarshall(W);

cout << "\nAll Pair Shortest Path Matrix" << endl;


displayMatrix(shortestPaths);

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.

All Pair Shortest Path Matrix


0 3 7 5
2 0 6 4
3 1 0 5
5 3 2 0

You might also like