Skip to content

Commit

Permalink
Create Floyd Warshall
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushiChakrabarty authored Nov 4, 2020
1 parent 336dea1 commit eee49c9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Floyd Warshall
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
#include<iomanip>
#define NODE 7
#define INF 999
using namespace std;

//Cost matrix of the graph
int costMat[NODE][NODE] = {
{0, 3, 6, INF, INF, INF, INF},
{3, 0, 2, 1, INF, INF, INF},
{6, 2, 0, 1, 4, 2, INF},
{INF, 1, 1, 0, 2, INF, 4},
{INF, INF, 4, 2, 0, 2, 1},
{INF, INF, 2, INF, 2, 0, 1},
{INF, INF, INF, 4, 1, 1, 0}
};

void floydWarshal() {
int cost[NODE][NODE]; //defind to store shortest distance from any node to any node
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
cost[i][j] = costMat[i][j]; //copy costMatrix to new matrix

for(int k = 0; k<NODE; k++) {
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
if(cost[i][k]+cost[k][j] < cost[i][j])
cost[i][j] = cost[i][k]+cost[k][j];
}

cout << "The matrix:" << endl;
for(int i = 0; i<NODE; i++) {
for(int j = 0; j<NODE; j++)
cout << setw(3) << cost[i][j];
cout << endl;
}
}

int main() {
floydWarshal();
}

0 comments on commit eee49c9

Please sign in to comment.