Shortest Path Algorithm
FAQs:
Find the Shortest Path of the Community, the shortest path of the subway, the shortest path from one point to another, and all the shortest paths.
Ideas:
(1) convert all vertices into Graph; (2) use the Floyd algorithm or Dijkstra algorithm to obtain the shortest path.
Algorithm Implementation:
(1) Floyd algorithm: http://www.cnblogs.com/skywang12345/p/3711523.html
// Adjacent matrix typedef struct _ graph {char vexs [MAX]; // vertex set int vexnum; // Number of vertices int edgnum; // Number of edges int matrix [MAX] [MAX]; // adjacent matrix} Graph, * PGraph;
/** Floyd shortest path. * That is, the shortest path between vertices in the Statistical Chart. ** Parameter description: * G -- Graph * path -- path. Path [I] [j] = k indicates that the shortest path from "vertex I" to "vertex j" passes through vertex k. * Dist -- length array. That is, dist [I] [j] = sum indicates that the shortest path length from "vertex I" to "vertex j" is sum. */Void floyd (Graph G, int path [] [MAX], int dist [] [MAX]) {int I, j, k; int tmp; // initialize for (I = 0; I <G. vexnum; I ++) {for (j = 0; j <G. vexnum; j ++) {dist [I] [j] = G. matrix [I] [j]; // The path length from vertex I to vertex j is "I to j weight ". Path [I] [j] = j; // the shortest path from "vertex I" to "vertex j" goes through vertex j.} // Calculate the shortest path for (k = 0; k <G. vexnum; k ++) {for (I = 0; I <G. vexnum; I ++) {for (j = 0; j <G. vexnum; j ++) {// if the path passing through the subscript k vertex is shorter than the path between the original two points, update dist [I] [j] and path [I] [j] tmp = (dist [I] [k] = INF | dist [k] [j] = = INF )? INF: (dist [I] [k] + dist [k] [j]); if (dist [I] [j]> tmp) {// set the value of "I to j Shortest Path" to a smaller dist [I] [j] = tmp; // The path corresponding to "I to j Shortest path", which goes through k path [I] [j] = path [I] [k] ;}}} // print the result of the floyd Shortest Path printf ("floyd: \ n"); for (I = 0; I <G. vexnum; I ++) {for (j = 0; j <G. vexnum; j ++) printf ("% 2d", dist [I] [j]); printf ("\ n ");}}
(2) Dijkstra Algorithm Implementation: http://www.cnblogs.com/skywang12345/p/3711512.html
// Adjacent matrix typedef struct _ graph {char vexs [MAX]; // vertex set int vexnum; // Number of vertices int edgnum; // Number of edges int matrix [MAX] [MAX]; // adjacent matrix} Graph, * PGraph; // edge struct typedef struct _ EdgeData {char start; // the start point of the edge char end; // the end point of the edge int weight; // the weight of the edge} EData;
/** Dijkstra shortest path. * That is, the shortest path from "vertex vs" in the statistical graph (G) to other vertices. ** Parameter description: * G -- Graph * vs -- start vertex ). Calculate the shortest path from "vertex vs" to other vertices. * Prev -- a precursor vertex array. That is, the value of prev [I] is the vertex located before "vertex I" in all vertices experienced by the shortest path from "vertex vs" to "vertex I. * Dist -- length array. That is, dist [I] is the length of the shortest path from "vertex vs" to "vertex I. */Void dijkstra (Graph G, int vs, int prev [], int dist []) {int I, j, k; int min; int tmp; int flag [MAX]; // flag [I] = 1 indicates that the shortest path from "vertex vs" to "vertex I" has been obtained successfully. // Initialize for (I = 0; I <G. vexnum; I ++) {flag [I] = 0; // the shortest path of vertex I has not been obtained yet. Prev [I] = 0; // The precursor vertex of vertex I is 0. Dist [I] = G. matrix [vs] [I]; // the shortest path of vertex I is "vertex vs" to "vertex I.} // Initialize the "vertex vs" itself. flag [vs] = 1; dist [vs] = 0; // traverse G. vexnum-1 times; find the shortest path of a vertex each time. For (I = 1; I <G. vexnum; I ++) {// find the smallest current path; // that is, find the closest vertex (k) To vs In the vertex without obtaining the shortest path ). Min = INF; for (j = 0; j <G. vexnum; j ++) {if (flag [j] = 0 & dist [j] <min) {min = dist [j]; k = j ;}} // mark "vertex k" as the obtained Shortest Path. flag [k] = 1; // correct the current shortest path and the precursor vertex. // that is, after "Shortest Path of vertex k" is completed, update "Shortest Path and precursor vertex of vertex without Shortest Path ". For (j = 0; j <G. vexnum; j ++) {tmp = (G. matrix [k] [j] = INF? INF: (min + G. matrix [k] [j]); // prevents overflow if (flag [j] = 0 & (tmp <dist [j]) {dist [j] = tmp; prev [j] = k ;}}// print the result of the dijkstra shortest path printf ("dijkstra (% c): \ n ", g. vexs [vs]); for (I = 0; I <G. vexnum; I ++) printf ("shortest (% c, % c) = % d \ n", G. vexs [vs], G. vexs [I], dist [I]);}