From 763821cae0cca5fc04920514a6d45f0bcd7cc392 Mon Sep 17 00:00:00 2001 From: Manikanta <168364481+mani-353@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:20:58 +0530 Subject: [PATCH 1/2] Create Floyd-Warshall-algo.cpp --- DSA_C++/Floyd-Warshall-algo.cpp | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 DSA_C++/Floyd-Warshall-algo.cpp diff --git a/DSA_C++/Floyd-Warshall-algo.cpp b/DSA_C++/Floyd-Warshall-algo.cpp new file mode 100644 index 00000000..8dd0a268 --- /dev/null +++ b/DSA_C++/Floyd-Warshall-algo.cpp @@ -0,0 +1,58 @@ +#include +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; +} From 951db2fd02a5c42f44455bca6019c1b32b711a54 Mon Sep 17 00:00:00 2001 From: Manikanta <168364481+mani-353@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:22:25 +0530 Subject: [PATCH 2/2] Update CONTRIBUTORS.md --- CONTRIBUTORS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c6c487a0..1214b5e2 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -52,4 +52,5 @@ | Ankita Sinha Ray |ankitasray| | Aditya Pandey |Aditya7pandey| | Yash Bandal |Yash Bandal| -| Harshit Jaiswal |Harshit Jaiswal| \ No newline at end of file +| Harshit Jaiswal |Harshit Jaiswal| +| mani-252 |mani-353|