Skip to content

Instantly share code, notes, and snippets.

@MagallanesFito
Created May 4, 2017 07:39
Show Gist options
  • Save MagallanesFito/2518320f4c660c0d3c8c929e1e38fda0 to your computer and use it in GitHub Desktop.
Save MagallanesFito/2518320f4c660c0d3c8c929e1e38fda0 to your computer and use it in GitHub Desktop.
Floyd-Warshall Graph Algorithm C++
#include <iostream>
#define INF 0x3f3f3f3f
#define MAX_VERTICES 4
using namespace std;
int graph[MAX_VERTICES][MAX_VERTICES] = {
{0,5,INF,10},
{INF,0,3,INF},
{INF,INF,0,1},
{INF,INF,INF,0}
};
int P[MAX_VERTICES][MAX_VERTICES]; //Matriz de predecesores, utilizada para trazar las rutas
int D[MAX_VERTICES][MAX_VERTICES]; //Matriz de distancias, guarda la distancia mas corta entre todos los pares de vertices
void getPath(int a,int b){
if(P[a][b]==b){
cout << b << " ";
return;
}
getPath(a,P[a][b]);
cout << b << " ";
}
void printSolution(){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
cout << "from " << i << " to " << j << " : ";
if(D[i][j] == INF){
cout << "-" << endl;
continue;
}
else{
cout << D[i][j] << endl;
getPath(i,j);
cout << endl;
}
}
cout << endl;
}
}
void floydWarshall(){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
D[i][j] = graph[i][j];
P[i][j] = i;
}
}
//Programacion Dinamica
for(int k=0;k<V;k++){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(D[i][k] + D[k][j] < D[i][j]){
D[i][j] = D[i][k] + D[k][j];
P[i][j] = k;
}
}
}
}
printSolution();
}
int main(){
floydWarshall();
//getPath(1,3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment