# Floyd(弗洛伊德)算法(C语言)

## 实例说明

1.计算metrixD矩阵(两顶点之间的最短距离)和P矩阵(两顶点的中介点)

#include <stdio.h>
#include <stdlib.h>

void Create_metrixD_P(int** metrixD, int **P ,int VerNum, int EdgNum)
{
int x, y, Weight, edg_count = 0;
int i, j, k;

for (i = 0; i < VerNum; ++i) {
for (j = 0; j < VerNum; ++j) {
metrixD[i][j] = INT_MAX;
P[i][j] = j;
}
}

while (edg_count < EdgNum) {
scanf("%d%d%d", &x, &y, &Weight);
metrixD[x - 1][y - 1] = Weight;
edg_count++;
}
}

//Floyd algorithm
void Floyd(int **metirxD, int **P, int VerNum) {
int n, x, y, temp = 0;
//The triple loop looks for shortest paths and weights
for (n = 0; n < VerNum; ++n) {
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);
if (temp < metirxD[x][y]) {
//Update matrix information
metirxD[x][y] = temp;
P[x][y] = n;
}
}
}
}
}

void Show_metrixD_P(int** metrixD, int **P, int VerNum)
{
int x, y;
printf("metrixD:\n");
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
if (metrixD[x][y] == INT_MAX) {
printf("∞ ");
}
else {
printf("%d ", metrixD[x][y]);
}
}
printf("\n");
}
printf("P:\n");
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
printf("%d ", P[x][y]);
}
printf("\n");
}
}

int main(void)
{
int VerNum, EdgNum, i;
int** metrixD, ** P;

printf("Enter the number of vertices and edges:");
scanf("%d%d", &VerNum, &EdgNum);

metrixD = (int**)malloc(VerNum * sizeof(int));
P = (int**)malloc(VerNum * sizeof(int));

for (i = 0; i < VerNum; ++i) {
metrixD[i] = (int*)malloc(VerNum * sizeof(int));
P[i] = (int*)malloc(VerNum * sizeof(int));
}

printf("Input vertices and weights:");
Create_metrixD_P(metrixD, P, VerNum, EdgNum);
Floyd(metrixD, P, VerNum);
Show_metrixD_P(metrixD, P, VerNum);

for (i = 0; i < VerNum; ++i) {
free(metrixD[i]);
free(P[i]);
}
free(metrixD);
free(P);

return 0;
}

2.输出顶点之间的最短距离与路径

#include <stdio.h>
#include <stdlib.h>

#define VEXNUM 5

//Adjacency matrix: shows the distance between vertices
int metirxD[VEXNUM][VEXNUM] = {
INT_MAX,10,        5,        INT_MAX,INT_MAX,
INT_MAX,INT_MAX,2,        1,        INT_MAX,
INT_MAX,3,        INT_MAX,9,        2,
INT_MAX,INT_MAX,INT_MAX,INT_MAX,4,
7,        INT_MAX,INT_MAX,5,        INT_MAX
};

//Path: passing vertex between two vertices
int P[VEXNUM][VEXNUM] = {
0,1,2,3,4,
0,1,2,3,4,
0,1,2,3,4,
0,1,2,3,4,
0,1,2,3,4
};

//Floyd algorithm
void Floyd() {
int n, x, y, temp = 0;
//The triple loop looks for shortest paths and weights
for (n = 0; n < VEXNUM; ++n) {
for (x = 0; x < VEXNUM; ++x) {
for (y = 0; y < VEXNUM; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);
if (temp < metirxD[x][y]) {
//Update matrix information
metirxD[x][y] = temp;
P[x][y] = n;
}
}
}
}
}

void Show_Path() {
int x, y, temp = 0;
//Output the shortest path between two vertices
for (x = 0; x < VEXNUM - 1; ++x) {
for (y = x + 1; y < VEXNUM; ++y) {
printf("V%d-->V%d  weight:%d  path:V%d", x, y, metirxD[x][y], x);
temp = P[x][y];
while (temp != y) {
printf("-->V%d", temp);
temp = P[temp][y];
}
printf("-->V%d", y);
printf("\n");
}
}
}

int main(void)
{
Floyd();
Show_Path();

return 0;
}

## 完整代码

#include <stdio.h>
#include <stdlib.h>

void Create_metrixD_P(int** metrixD, int **P ,int VerNum, int EdgNum)
{
int x, y, Weight, edg_count = 0;
int i, j, k;

for (i = 0; i < VerNum; ++i) {
for (j = 0; j < VerNum; ++j) {
metrixD[i][j] = INT_MAX;
P[i][j] = j;
}
}

while (edg_count < EdgNum) {
scanf("%d%d%d", &x, &y, &Weight);
metrixD[x - 1][y - 1] = Weight;
edg_count++;
}
}

//Floyd algorithm
void Floyd(int **metirxD, int **P, int VerNum) {
int n, x, y, temp = 0;
//The triple loop looks for shortest paths and weights
for (n = 0; n < VerNum; ++n) {
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);
if (temp < metirxD[x][y]) {
//Update matrix information
metirxD[x][y] = temp;
P[x][y] = n;
}
}
}
}
}

void Show_metrixD_P(int** metrixD, int **P, int VerNum)
{
int x, y;
printf("metrixD:\n");
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
if (metrixD[x][y] == INT_MAX) {
printf("∞ ");
}
else {
printf("%d ", metrixD[x][y]);
}
}
printf("\n");
}
printf("P:\n");
for (x = 0; x < VerNum; ++x) {
for (y = 0; y < VerNum; ++y) {
printf("%d ", P[x][y]);
}
printf("\n");
}
}

void Show_Path(int **metirxD, int **P, int VerNum) {
int x, y, temp = 0;
//Output the shortest path between two vertices
for (x = 0; x < VerNum - 1; ++x) {
for (y = x + 1; y < VerNum; ++y) {
printf("V%d-->V%d  weight:%d  path:V%d", x, y, metirxD[x][y], x);
temp = P[x][y];
while (temp != y) {
printf("-->V%d", temp);
temp = P[temp][y];
}
printf("-->V%d", y);
printf("\n");
}
}
}

int main(void)
{
int VerNum, EdgNum, i;
int** metrixD, ** P;

printf("Enter the number of vertices and edges:");
scanf("%d%d", &VerNum, &EdgNum);

metrixD = (int**)malloc(VerNum * sizeof(int));
P = (int**)malloc(VerNum * sizeof(int));

for (i = 0; i < VerNum; ++i) {
metrixD[i] = (int*)malloc(VerNum * sizeof(int));
P[i] = (int*)malloc(VerNum * sizeof(int));
}

printf("Input vertices and weights:");
Create_metrixD_P(metrixD, P, VerNum, EdgNum);
Floyd(metrixD, P, VerNum);
Show_metrixD_P(metrixD, P, VerNum);
Show_Path(metrixD, P, VerNum);

for (i = 0; i < VerNum; ++i) {
free(metrixD[i]);
free(P[i]);
}
free(metrixD);
free(P);

return 0;
}

posted @ 2019-10-09 00:02  Hk_Mayfly  阅读(374)  评论(0编辑  收藏