Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Nov 16, 2017
1 parent b11d99c commit d7b97b6
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions 25.FloydWarshall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This program constructs a graph from user inputs and
* computes all pairs shortest paths between every pair of nodes using
* Floyd Warshall Algorithm
*
* Coded by: Abdurrezak Efe
* */

#include <iostream>
#include <limits.h>
using namespace std;

const int V = 1000; //assumed number of vertices
int edges[V+1][V+1] = {0}; //the graph itself

void floyd_warshall(int v)
{
int distance[v+1][v+1];


//initializing distance matrix
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
distance[i][j] = edges[i][j];


//updating distance
for(int k=0;k<v;k++)
for(int j=0;j<v;j++)
for(int i=0;i<v;i++)
distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j]);

//printing shortest ways between nodes
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
cout << distance[i][j] << " ";
cout << endl;
}
}

// driver program to test above function
int main()
{

int v,e;
cin >> v >> e;

for(int i=0;i<v+1;i++)
for(int j=0;j<v+1;j++)
if(i != j)
edges[i][j] = 999999999;

//constructing the graph
int x,y,w;
for(int i=0;i<e;i++)
cin >> x >> y >> w, edges[x][y] = edges[y][x] = w;

floyd_warshall(v);
}

0 comments on commit d7b97b6

Please sign in to comment.