本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师、何钦铭老师的《数据结构》
1.使用Dijkstra算法对每个顶点运行一次运算,可以得到每个顶点到最图所有顶点的最小值,时间复杂度为:T = O( |V| 3 + |E||V|)。该算法对稀疏图比较好
2.使用Floyd算法,时间复杂度为:T = O( |V| 3 ),该算法对稠密图比较好
下面介绍Floyd算法
D k [i][j] = 路径{ i --> { l --> k } --> j }的最小长度
D 0 , D 1 , ..., D |V|-1 [i][j]即给出了i到j的真正最短距离
最初的D -1 是什么?
当D k-1 已经完成,递推到D k 时:
或者k 属于最短路径{ i --> { l --> k } --> j },则D k = D k-1
或者k 属于最短路径{ i --> { l --> k } --> j },则该路径必定由两
段最短路径组成: D k [i][j]=D k-1 [i][k]+D k-1 [k][j]
void Floyd()
{ for ( i = 0; i < N; i++ )
for( j = 0; j < N; j++ ) {
D[i][j] = G[i][j];
path[i][j] = -1;
}
for( k = 0; k < N; k++ )
for( i = 0; i < N; i++ )
for( j = 0; j < N; j++ )
if( D[i][k] + D[k][j] < D[i][j] ) {
D[i][j] = D[i][k] + D[k][j];
path[i][j] = k;
}
}
1 /* 2 * floyd.c 3 * 4 * Created on: 2017年5月14日 5 * Author: ygh 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #define MAX_VERTEX_NUM 100 /*define the max number of the vertex*/ 11 #define INFINITY 65535 /*define double byte no negitive integer max number is 65535*/ 12 #define ERROR -1 13 14 typedef int vertex; /*define the data type of the vertex*/ 15 typedef int weightType; /*define the data type of the weight*/ 16 typedef char dataType; /*define the data type of the vertex value*/ 17 18 /*define the data structure of the Edge*/ 19 typedef struct eNode *ptrToENode; 20 typedef struct eNode { 21 vertex v1, v2; /*two vertex between the edge <v1,v2>*/ 22 weightType weight; /*the value of the edge's weight */ 23 }; 24 typedef ptrToENode edge; 25 26 /*define the data structure of the graph*/ 27 typedef struct gNode *ptrToGNode; 28 typedef struct gNode { 29 int vertex_number; /*the number of the vertex*/ 30 int edge_nunber; /*the number of the edge*/ 31 weightType g[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; /*define the adjacent matrix weight of graph*/ 32 dataType data[MAX_VERTEX_NUM]; /*define the dataType array to store the value of vertex*/ 33 }; 34 typedef ptrToGNode adjacentMatrixGraph; /*a graph show by adjacent matrix*/ 35 36 /* 37 create a graph given the vertex number. 38 @param vertexNum The verter number of the graph 39 @return a graph with vertex but no any egdgs 40 */ 41 adjacentMatrixGraph createGraph(int vertexNum) { 42 vertex v, w; 43 adjacentMatrixGraph graph; 44 graph = (adjacentMatrixGraph) malloc(sizeof(struct gNode)); 45 graph->vertex_number = vertexNum; 46 graph->edge_nunber = 0; 47 /*initialize the adjacent matrix*/ 48 for (v = 0; v < graph->vertex_number; v++) { 49 for (w = 0; w < graph->vertex_number; w++) { 50 graph->g[v][w] = INFINITY; 51 } 52 } 53 54 return graph; 55 } 56 57 /* 58 insert a edge to graph.We will distinct oriented graph and undirected graph 59 @param graph The graph you want to insert edge 60 @param e The edge you want to insert the graph 61 @param isOriented Whether the graph is oriented graph.If the graph is oriented 62 we will set adjacent matrix [n][m]=[m][n]=edge's weight,else we only set 63 the adjacent matrix [n][m]=edge's weight 64 */ 65 void inserEdge(adjacentMatrixGraph graph, edge e, int isOriented) { 66 graph->g[e->v1][e->v2] = e->weight; 67 if (!isOriented) { 68 graph->g[e->v2][e->v1] = e->weight; 69 } 70 } 71 72 /* 73 construct a graph according user's input 74 75 @return a graph has been filled good 76 */ 77 adjacentMatrixGraph buildGraph(int isOrdered) { 78 adjacentMatrixGraph graph; 79 edge e; 80 vertex i; 81 int vertex_num; 82 scanf("%d", &vertex_num); 83 graph = createGraph(vertex_num); 84 scanf("%d", &(graph->edge_nunber)); 85 if (graph->edge_nunber) { 86 e = (edge) malloc(sizeof(struct eNode)); 87 for (i = 0; i < graph->edge_nunber; i++) { 88 scanf("%d %d %d", &e->v1, &e->v2, &e->weight); 89 e->v1--; 90 e->v2--; 91 inserEdge(graph, e, isOrdered); 92 } 93 } 94 return graph; 95 96 } 97 98 /* 99 * Floyd algorithms: 100 1.D k [i][j] = 路径{ i --> { l --> k } --> j }的最小长度 101 2.D 0 , D 1 , ..., D |V|-1 [i][j]即给出了i到j的真正最短距离 102 3.最初的D -1 是什么? 103 4.当D k-1 已经完成,递推到D k 时: 104 或者k 属于最短路径{ i --> { l --> k } --> j },则D k = D k-1 105 或者k 属于最短路径{ i --> { l --> k } --> j },则该路径必定由两 106 段最短路径组成: D k [i][j]=D k1 [i][k]+D k1 [k][j] 107 *@param graph A graph store by adjacent matrix 108 *@param d A two-dimensional array to store the distance value 109 *@param path A two-dimensional array to store the path 110 */ 111 int floyd(adjacentMatrixGraph graph, weightType d[][MAX_VERTEX_NUM], 112 vertex path[][MAX_VERTEX_NUM]) { 113 vertex i, j, k; 114 /* 115 * Initialize array 116 */ 117 for (i = 0; i < graph->vertex_number; i++) { 118 for (j = 0; j < graph->vertex_number; j++) { 119 d[i][j] = graph->g[i][j]; 120 path[i][j] = -1; 121 } 122 } 123 for (k = 0; k < graph->vertex_number; k++) { 124 for (i = 0; i < graph->vertex_number; i++) { 125 for (j = 0; j < graph->vertex_number; j++) { 126 if (d[i][k] + d[k][j] < d[i][j]) { 127 d[i][j] = d[i][k] + d[k][j]; 128 /* 129 * Find negative circle 130 */ 131 if (i == j && d[i][j] < 0) { 132 return 0; 133 } 134 path[i][j] = k; 135 } 136 } 137 } 138 } 139 return 1; 140 } 141 142 /* 143 * Print the distance matrix 144 */ 145 void toStringDistance(int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int length) { 146 vertex i, j; 147 for (i = 0; i < length; i++) { 148 printf("%d:", i); 149 for (j = 0; j < length; j++) { 150 printf("%d ", d[i][j]); 151 } 152 printf("\n"); 153 } 154 } 155 156 /* 157 * Print the path from source to destination 158 * we will recursive method to print the path 159 * 1.find the k between source and destination 160 * 2.find point between source and k and recursive this method until there no points between them 161 * 3.find the point between k and destination and recursive this method until there no points between them 162 * @param source The index of the source point 163 * @param destination The index of the destination 164 * @param path A two-dimensional array to store the path 165 */ 166 void toStringPath(int source, int destination, vertex path[][MAX_VERTEX_NUM]) { 167 if (destination == -1 || source == -1) { 168 return; 169 } else { 170 toStringPath(source, path[source][destination], path); 171 if (path[source][destination] != -1) { 172 printf("%d ", path[source][destination]); 173 } 174 toStringPath(path[source][destination], destination, path); 175 } 176 177 } 178 179 /* 180 * A test method 181 */ 182 int main() { 183 adjacentMatrixGraph graph = buildGraph(1); 184 int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 185 int path[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 186 int source = 1; 187 int destination = 5; 188 floyd(graph, d, path); 189 printf("toDistance\n"); 190 toStringDistance(d, graph->vertex_number); 191 printf("toPath:"); 192 printf("%d ", source); 193 toStringPath(source, destination, path); 194 printf("%d", destination); 195 return 0; 196 }