Floyd algorithm, also known as interpolation method, is an algorithm that uses the idea of dynamic programming to find the shortest path between multiple source points in a given weighted graph .
This paper presents a C ++ implementation of Floyd algorithm. This algorithm supports dynamic input of points and edges and provides interface descriptions
1 Data structure
- The storage structure of an undirected graph uses an adjacency matrix.
- The weight of each edge is the distance between two points on this edge.
int ** d = NULL; // Two-dimensional array, storing the sum of the weights of the shortest path of any two points
int ** path = NULL; // Two-dimensional array, store the shortest path between any two points
int matrixSize = 0; // The number of vertices in the figure
int arcSize = 0; // The number of edges in the figure
int isDigraph = 0; // The type of graph, whether it is a directed graph or an undirected graph
2 Interface definition
Define the following interface:
/ * Initialize the storage structure
vertexNum: the number of vertices
arcNum: number of edges * /
bool init(int vertexNum, int arcNum)
/ * Add an edge
start: the starting point of the edge
end: the end of the edge
weight: weight of the edge * /
bool addArc(int start, int end, int weight, bool isDigraph = false)
/ * Calculate the shortest path between all vertices * /
bool calculatePath()
/ * Return to the shortest path between two points
start: the starting point of the path
end: the end of the path * /
bool getShortestPath(int start, int end)
3 source code implementation
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#define MAX 10000000
int** d = NULL;
int** path = NULL;
int matrixSize = 0;
int arcSize = 0;
int isDigraph = 0;
bool init(int vertexNum, int arcNum)
{
matrixSize = vertexNum;
arcSize = arcNum;
d = (int**)malloc(vertexNum * sizeof(int*));
memset(d, 0, vertexNum * sizeof(int*));
for(int i = 0; i < vertexNum; i++)
{
d[i] = (int*)malloc(vertexNum * sizeof(int));
memset(d[i], 0, vertexNum * sizeof(int));
for(int j = 0; j < vertexNum; j++)
{
d[i][j] = MAX;
}
}
path = (int**)malloc(vertexNum * sizeof(int*));
memset(path, 0, vertexNum * sizeof(int*));
for(int i = 0; i < vertexNum; i++)
{
path[i] = (int*)malloc(vertexNum * sizeof(int));
memset(path[i], 0, vertexNum * sizeof(int));
for(int j = 0; j < vertexNum; j++)
{
path[i][j] = -1;
}
}
return true;
}
bool addArc(int start, int end, int weight, bool isDigraph = false)
{
if(!isDigraph)
{
d[start][end] = weight;
d[end][start] = weight;
path[start][end] = end;
path[end][start] = start;
}
else
{
d[start][end] = weight;
path[start][end] = end;
}
}
bool calculatePath()
{
for(int k=0;k<matrixSize;k++)
for(int i=0;i<matrixSize;i++)
for(int j=0;j<matrixSize;j++) {
if(d[i][k]+d[k][j]<d[i][j]) {
d[i][j]=d[i][k]+d[k][j];
path[i][j]=path[i][k];
}
}
return true;
}
bool getShortestPath(int start, int end)
{
if ( (start!=end) && (d[start][end] < MAX))
{
printf("%d->%d:%d ,",start,end,d[start][end]);
printf(" path:");
int f = start;
int en = end;
while (f!=en)
{
printf("%d->",f);
f=path[f][en];
}
printf("%d\n",en);
return true;
}
else if( (start!=end) && (d[start][end] >= MAX))
{
printf("%d->%d:NO PATH\n",start,end);
return false;
}
return false;
}
int main()
{
int i,j,m,n;
int x,y,z;
printf("input vertexNum arcNum isDigraph:\n");
scanf("%d%d%d",&n,&m,&isDigraph);
init(n, m);
for(i=0;i<m;i++) {
printf("input arc with startPoint endPoint weitht:\n");
scanf("%d%d%d",&x,&y,&z);
addArc(x,y,z, isDigraph);
}
calculatePath();
printf("List all shortest distance and path:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
getShortestPath(i, j);
}
}
return 0;
}