Skip to content
This repository was archived by the owner on Oct 5, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@
| Ankita Sinha Ray |<a href="https://github.com/ankitasray">ankitasray</a>|
| Aditya Pandey |<a href="https://github.com/Aditya7pandey">Aditya7pandey</a>|
| Yash Bandal |<a href="https://github.com/Yash-Bandal">Yash Bandal</a>|
| Harshit Jaiswal |<a href="https://github.com/harshu84190">Harshit Jaiswal</a>|
| Harshit Jaiswal |<a href="https://github.com/harshu84190">Harshit Jaiswal</a>|
| mani-252 |<a href="https://github.com/mani-353">mani-353</a>|
58 changes: 58 additions & 0 deletions DSA_C++/Floyd-Warshall-algo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
using namespace std;

#define INF 99999
#define V 4

// A function to print the solution matrix
void printSolution(int dist[V][V]) {
cout << "The following matrix shows the shortest distances between every pair of vertices:\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

// All-pairs shortest path algorithm (Floyd-Warshall)
void floydWarshall(int graph[V][V]) {
int dist[V][V];

// Initialize the solution matrix same as input graph matrix
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

// Update the solution matrix with shortest paths
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

// Print the shortest path matrix
printSolution(dist);
}

int main() {
/* Input graph represented as adjacency matrix.
INF means there is no edge between those vertices */
int graph[V][V] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};

// Run Floyd-Warshall algorithm
floydWarshall(graph);

return 0;
}