对于网图来说,最短路径,是指两顶点之间经过的边上权值之和最少的路径,并且我们称路径上的第一个顶点是源点,最后一个顶点是终点。
这是一个按路径长度递增的次序产生最短路径的算法。它的思路大体是这样的:并不是一下子就求出v0到v8的最短路径,而是一步步求出它们之间顶点的最短路径,过程中都是基于已经求出的最短路径的基础上,求得更远顶点的最短路径,最终得到你要的结果。
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXEDGE 20
#define MAXVEX 20
#define INFINITY 65535
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef struct
{
int vexs[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
typedef int Patharc[MAXVEX]; /* 用于存储最短路径下标的数组 */
typedef int ShortPathTable[MAXVEX];/* 用于存储到各点最短路径的权值和 */
/* 构件图 */
void CreateMGraph(MGraph *G)
{
int i, j;
/* printf("请输入边数和顶点数:"); */
G->numEdges = 16;
G->numVertexes = 9;
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
G->vexs[i] = i;
}
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
for (j = 0; j < G->numVertexes; j++)
{
if (i == j)
G->arc[i][j] = 0;
else
G->arc[i][j] = G->arc[j][i] = INFINITY;
}
}
G->arc[0][1] = 1;
G->arc[0][2] = 5;
G->arc[1][2] = 3;
G->arc[1][3] = 7;
G->arc[1][4] = 5;
G->arc[2][4] = 1;
G->arc[2][5] = 7;
G->arc[3][4] = 2;
G->arc[3][6] = 3;
G->arc[4][5] = 3;
G->arc[4][6] = 6;
G->arc[4][7] = 9;
G->arc[5][7] = 5;
G->arc[6][7] = 2;
G->arc[6][8] = 7;
G->arc[7][8] = 4;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
/* Dijkstra算法,求有向网G的v0顶点到其余顶点v的最短路径P[v]及带权长度D[v] */
/* P[v]的值为前驱顶点下标,D[v]表示v0到v的最短路径长度和 */
void ShortestPath_Dijkstra(MGraph G, int v0, Patharc *P, ShortPathTable *D)
{
int v, w, k, min;
int final[MAXVEX];/* final[w]=1表示求得顶点v0至vw的最短路径 */
for (v = 0; v<G.numVertexes; v++) /* 初始化数据 */
{
final[v] = 0; /* 全部顶点初始化为未知最短路径状态 */
(*D)[v] = G.arc[v0][v];/* 将与v0点有连线的顶点加上权值 */
(*P)[v] = -1; /* 初始化路径数组P为-1 */
}
(*D)[v0] = 0; /* v0至v0路径为0 */
final[v0] = 1; /* v0至v0不需要求路径 */
/* 开始主循环,每次求得v0到某个v顶点的最短路径 */
for (v = 1; v<G.numVertexes; v++)
{
min = INFINITY; /* 当前所知离v0顶点的最近距离 */
for (w = 0; w<G.numVertexes; w++) /* 寻找离v0最近的顶点 */
{
if (!final[w] && (*D)[w]<min)
{
k = w;
min = (*D)[w]; /* w顶点离v0顶点更近 */
}
}
final[k] = 1; /* 将目前找到的最近的顶点置为1 */
for (w = 0; w<G.numVertexes; w++) /* 修正当前最短路径及距离 */
{
/* 如果经过v顶点的路径比现在这条路径的长度短的话 */
if (!final[w] && (min + G.arc[k][w]<(*D)[w]))
{ /* 说明找到了更短的路径,修改D[w]和P[w] */
(*D)[w] = min + G.arc[k][w]; /* 修改当前路径长度 */
(*P)[w] = k;
}
}
}
}
int main(void)
{
int i, j, v0;
MGraph G;
Patharc P;
ShortPathTable D; /* 求某点到其余各点的最短路径 */
v0 = 0;
CreateMGraph(&G);
ShortestPath_Dijkstra(G, v0, &P, &D);
printf("最短路径倒序如下:\n");
for (i = 1; i<G.numVertexes; ++i)
{
printf("v%d - v%d : ", v0, i);
j = i;
while (P[j] != -1)
{
printf("%d ", P[j]);
j = P[j];
}
printf("\n");
}
printf("\n源点到各顶点的最短路径长度为:\n");
for (i = 1; i<G.numVertexes; ++i)
printf("v%d - v%d : %d \n", G.vexs[0], G.vexs[i], D[i]);
system("pause");
return 0;
}
运行结果为:
最短路径倒序如下:
v0 - v1 :
v0 - v2 : 1
v0 - v3 : 4 2 1
v0 - v4 : 2 1
v0 - v5 : 4 2 1
v0 - v6 : 3 4 2 1
v0 - v7 : 6 3 4 2 1
v0 - v8 : 7 6 3 4 2 1
源点到各顶点的最短路径长度为:
v0 - v1 : 1
v0 - v2 : 4
v0 - v3 : 7
v0 - v4 : 5
v0 - v5 : 8
v0 - v6 : 10
v0 - v7 : 12
v0 - v8 : 16
先定义两个二维数组 D [ 3 ] [ 3 ] D[3][3] D[3][3]和 P [ 3 ] [ 3 ] P[3][3] P[3][3], D D D代表顶点到顶点的最短路径权值和的矩阵,初始化为 D − 1 D^{-1} D−1,其实就是初始的图的邻接矩阵。将 P P P命名为 P − 1 P^{-1} P−1,初始化为图中矩阵。
首先分析所有的顶点经过v0后到达另一个顶点的最短路径。因为只有三个顶点,因此需要查看v1->v0->v2,得到路径为3。而v1->v2权值为5,所以就有了 D 0 D^0 D0矩阵,同时修改 P 0 P^0 P0矩阵。接下来,再在 D 0 和 P 0 D^0和P^0 D0和P0的基础上计算经过v1和v2的后到达另一个顶点的最短路径,得到 D 1 、 P 1 和 D 2 、 P 2 D^1、P^1和D^2、P^2 D1、P1和D2、P2。
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXEDGE 20
#define MAXVEX 20
#define INFINITY 65535
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef struct
{
int vexs[MAXVEX];
int arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
typedef int Patharc[MAXVEX][MAXVEX];
typedef int ShortPathTable[MAXVEX][MAXVEX];
/* 构件图 */
void CreateMGraph(MGraph *G)
{
int i, j;
/* printf("请输入边数和顶点数:"); */
G->numEdges = 16;
G->numVertexes = 9;
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
G->vexs[i] = i;
}
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
for (j = 0; j < G->numVertexes; j++)
{
if (i == j)
G->arc[i][j] = 0;
else
G->arc[i][j] = G->arc[j][i] = INFINITY;
}
}
G->arc[0][1] = 1;
G->arc[0][2] = 5;
G->arc[1][2] = 3;
G->arc[1][3] = 7;
G->arc[1][4] = 5;
G->arc[2][4] = 1;
G->arc[2][5] = 7;
G->arc[3][4] = 2;
G->arc[3][6] = 3;
G->arc[4][5] = 3;
G->arc[4][6] = 6;
G->arc[4][7] = 9;
G->arc[5][7] = 5;
G->arc[6][7] = 2;
G->arc[6][8] = 7;
G->arc[7][8] = 4;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
/* Floyd算法,求网图G中各顶点v到其余顶点w的最短路径P[v][w]及带权长度D[v][w]。 */
void ShortestPath_Floyd(MGraph G, Patharc *P, ShortPathTable *D)
{
int v, w, k;
for (v = 0; v<G.numVertexes; ++v) /* 初始化D与P */
{
for (w = 0; w<G.numVertexes; ++w)
{
(*D)[v][w] = G.arc[v][w]; /* D[v][w]值即为对应点间的权值 */
(*P)[v][w] = w; /* 初始化P */
}
}
for (k = 0; k<G.numVertexes; ++k)
{
for (v = 0; v<G.numVertexes; ++v)
{
for (w = 0; w<G.numVertexes; ++w)
{
if ((*D)[v][w]>(*D)[v][k] + (*D)[k][w])
{/* 如果经过下标为k顶点路径比原两点间路径更短 */
(*D)[v][w] = (*D)[v][k] + (*D)[k][w];/* 将当前两点间权值设为更小的一个 */
(*P)[v][w] = (*P)[v][k];/* 路径设置为经过下标为k的顶点 */
}
}
}
}
}
int main(void)
{
int v, w, k;
MGraph G;
Patharc P;
ShortPathTable D; /* 求某点到其余各点的最短路径 */
CreateMGraph(&G);
ShortestPath_Floyd(G, &P, &D);
printf("各顶点间最短路径如下:\n");
for (v = 0; v<G.numVertexes; ++v)
{
for (w = v + 1; w<G.numVertexes; w++)
{
printf("v%d-v%d weight: %d ", v, w, D[v][w]);
k = P[v][w]; /* 获得第一个路径顶点下标 */
printf(" path: %d", v); /* 打印源点 */
while (k != w) /* 如果路径顶点下标不是终点 */
{
printf(" -> %d", k); /* 打印路径顶点 */
k = P[k][w]; /* 获得下一个路径顶点下标 */
}
printf(" -> %d\n", w); /* 打印终点 */
}
printf("\n");
}
printf("最短路径D\n");
for (v = 0; v<G.numVertexes; ++v)
{
for (w = 0; w<G.numVertexes; ++w)
{
printf("%d\t", D[v][w]);
}
printf("\n");
}
printf("最短路径P\n");
for (v = 0; v<G.numVertexes; ++v)
{
for (w = 0; w<G.numVertexes; ++w)
{
printf("%d ", P[v][w]);
}
printf("\n");
}
system("pause");
return 0;
}
运行结果:
各顶点间最短路径如下:
v0-v1 weight: 1 path: 0 -> 1
v0-v2 weight: 4 path: 0 -> 1 -> 2
v0-v3 weight: 7 path: 0 -> 1 -> 2 -> 4 -> 3
v0-v4 weight: 5 path: 0 -> 1 -> 2 -> 4
v0-v5 weight: 8 path: 0 -> 1 -> 2 -> 4 -> 5
v0-v6 weight: 10 path: 0 -> 1 -> 2 -> 4 -> 3 -> 6
v0-v7 weight: 12 path: 0 -> 1 -> 2 -> 4 -> 3 -> 6 -> 7
v0-v8 weight: 16 path: 0 -> 1 -> 2 -> 4 -> 3 -> 6 -> 7 -> 8
v1-v2 weight: 3 path: 1 -> 2
v1-v3 weight: 6 path: 1 -> 2 -> 4 -> 3
v1-v4 weight: 4 path: 1 -> 2 -> 4
v1-v5 weight: 7 path: 1 -> 2 -> 4 -> 5
v1-v6 weight: 9 path: 1 -> 2 -> 4 -> 3 -> 6
v1-v7 weight: 11 path: 1 -> 2 -> 4 -> 3 -> 6 -> 7
v1-v8 weight: 15 path: 1 -> 2 -> 4 -> 3 -> 6 -> 7 -> 8
v2-v3 weight: 3 path: 2 -> 4 -> 3
v2-v4 weight: 1 path: 2 -> 4
v2-v5 weight: 4 path: 2 -> 4 -> 5
v2-v6 weight: 6 path: 2 -> 4 -> 3 -> 6
v2-v7 weight: 8 path: 2 -> 4 -> 3 -> 6 -> 7
v2-v8 weight: 12 path: 2 -> 4 -> 3 -> 6 -> 7 -> 8
v3-v4 weight: 2 path: 3 -> 4
v3-v5 weight: 5 path: 3 -> 4 -> 5
v3-v6 weight: 3 path: 3 -> 6
v3-v7 weight: 5 path: 3 -> 6 -> 7
v3-v8 weight: 9 path: 3 -> 6 -> 7 -> 8
v4-v5 weight: 3 path: 4 -> 5
v4-v6 weight: 5 path: 4 -> 3 -> 6
v4-v7 weight: 7 path: 4 -> 3 -> 6 -> 7
v4-v8 weight: 11 path: 4 -> 3 -> 6 -> 7 -> 8
v5-v6 weight: 7 path: 5 -> 7 -> 6
v5-v7 weight: 5 path: 5 -> 7
v5-v8 weight: 9 path: 5 -> 7 -> 8
v6-v7 weight: 2 path: 6 -> 7
v6-v8 weight: 6 path: 6 -> 7 -> 8
v7-v8 weight: 4 path: 7 -> 8
最短路径D
0 1 4 7 5 8 10 12 16
1 0 3 6 4 7 9 11 15
4 3 0 3 1 4 6 8 12
7 6 3 0 2 5 3 5 9
5 4 1 2 0 3 5 7 11
8 7 4 5 3 0 7 5 9
10 9 6 3 5 7 0 2 6
12 11 8 5 7 5 2 0 4
16 15 12 9 11 9 6 4 0
最短路径P
0 1 1 1 1 1 1 1 1
0 1 2 2 2 2 2 2 2
1 1 2 4 4 4 4 4 4
4 4 4 3 4 4 6 6 6
2 2 2 3 4 5 3 3 3
4 4 4 4 4 5 7 7 7
3 3 3 3 3 7 6 7 7
6 6 6 6 6 5 6 7 8
7 7 7 7 7 7 7 7 8