From e3a8e58e9b78dc2e2cdd5b9a12ff9405260f86fa Mon Sep 17 00:00:00 2001 From: takshaygoyal <73619444+takshaygoyal@users.noreply.github.com> Date: Fri, 30 Oct 2020 03:14:14 +0530 Subject: [PATCH] Create Dynamic PrograFloydWarshallAlgorithm.cpp --- Dynamic PrograFloydWarshallAlgorithm.cpp | 65 ++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Dynamic PrograFloydWarshallAlgorithm.cpp diff --git a/Dynamic PrograFloydWarshallAlgorithm.cpp b/Dynamic PrograFloydWarshallAlgorithm.cpp new file mode 100644 index 0000000..d7055cd --- /dev/null +++ b/Dynamic PrograFloydWarshallAlgorithm.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +#define V 4 +#define INF 99999 + + +void printSolution(int dist[][V]); + +void floydWarshall (int graph[][V]) +{ int dist[V][V], i, j, k; + + for (i = 0; i < V; i++) + for (j = 0; j < V; j++) + dist[i][j] = graph[i][j]; + + + for (k = 0; k < V; k++) + { for (i = 0; i < V; i++) + { + for (j = 0; j < V; j++) + { + if (dist[i][k] + dist[k][j] < dist[i][j]) + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + + + printSolution(dist); +} + + +void printSolution(int dist[][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<