For a network chart, the shortest path refers to the path with the least sum of edge weights between two vertices. In addition, we call the first Vertex on the path as the Source Vertex and the last vertex as the destination vertex. The shortest path algorithms mainly include the Dijkstra algorithm and the Floyd algorithm. This article first introduces the first type of shortest path from a source point to other vertices.
This is an algorithm that generates the Shortest Path in ascending order of path length. Its general idea is as follows.
For example, the shortest path from vertex V0 to V1 in Figure 7-7-3 is obviously 1. Because vertex V1 is also connected to V2, V3, and V4, we also obtain V0-> V1-> v2 = 1 + 3 = 4, v0-> V1-> V3 = 1 + 7 = 8, V0-> V1-> V4 = 1 + 5 = 6.
Now we can know that the shortest distance from V0 to V2 is 4 instead of 5, 7-7-4 of V0-> V2. Vertex V2 is also connected to V4 and V5, at the same time, we obtained that V0-> v2-> V4 is actually V0-> V1-> v2-> V4 = 4 + 1 = 5, v0-> v2-> V5 = 4 + 7 = 11. Here we use V0-> V2, which is a small 4 we just calculated. In this case, we also find that V0-> V1-> v2-> V4 = 5 is smaller than V0-> V1-> V4 = 6, therefore, the shortest distance from V0 to V4 is currently 5, 7-7-5.
When we require the shortest path from V0 to V3, the three sides that lead to V3, except that V6 has not been studied, V0-> V1-> V3 = 8, v0-> V4-> V3 = 5 + 2 = 7, so the shortest path from V0 to V3 is 7-7-6.
As shown above, this algorithm does not find the shortest path from V0 to V8 at once, but is used to find the shortest distance between the vertices step by step. The process is based on the obtained shortest path, obtain the Shortest Path of the farther vertex and finally obtain the desired result.
The program code is as follows: (adapted from "big talk Data Structure")
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
# Include <iostream> Using namespace STD;# Define maxedge 20 # Define maxvex 20 # Define infinity 65535 Typedef struct { Int vexs [maxvex]; Int arc [maxvex] [maxvex]; Int numvertexes, numedges; } Mgraph; Typedef int patharc [maxvex]; Typedef int implements pathtable [maxvex]; /* Build a chart */ Void createmgraph (mgraph * g) { Int I, J; /* Printf ("Enter the number of edges and the number of vertices :");*/ G-> numedges = 16; G-> numvertexes = 9; For (I = 0; I <G-> numvertexes; I ++)/* initialization diagram */ { G-> vexs [I] = I; } For (I = 0; I <G-> numvertexes; I ++)/* initialization diagram */ { 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 algorithm, which is used to obtain the shortest path from the POs vertex of The Directed Network G to the other vertex v P [v] and the weighted length d [v] */ /* The value of P [v] is the subscript of the precursor vertex. d [v] indicates the shortest path length from POs to V and */ /* The POS value ranges from 0 ~ MG. numVertexs-1 */ Void shortestpath_dijkstra (mgraph mg, int POs, patharc P, shortpathtable D) { Int V, W, K, min; Int final [maxvex];/* final [w] = 1 indicates the shortest path from POs to W */ For (V = 0; v <mg. numvertexes; V ++) { Final [v] = 0;/* Initialize all vertices to unknown Shortest Path status */ D [v] = Mg. Arc [POS] [v];/* Add the weight value to the vertex connected to the POs point */ P [v] = 0;/* the initialization path array P is 0 */ } D [POS] = 0;/* indicates that the source point POS does not reach its own path */ P [POS] =-1;/*-1 indicates that no precursor vertex exists */ Final [POS] = 1;/* path not required for POs to POS */ /* Start the main loop and obtain the shortest path from POs to a certain v vertex each time */ For (V = 1; v <mg. numvertexes; V ++) { Min = infinity;/* the closest known distance to the POs vertex */ For (W = 0; W <mg. numvertexes; W ++)/* Find the vertex closest to the POs */ { If (! Final [w] & D [w] <min) { K = W; Min = d [w];/* w vertices are closer to POS vertices */ } } Final [k] = 1;/* set the closest vertex found to 1 */ For (W = 0; W <mg. numvertexes; W ++)/* modifies the current Shortest Path and distance */ { If (! Final [w] & (min + Mg. Arc [k] [W] <D [w]) { /* Indicates that a shorter path is found. Modify d [w] and P [w] */ D [w] = min + Mg. Arc [k] [W];/* modify the current path length */ P [w] = K; } } } /* End the cycle. If P [w] = 0, the precursor of vertex W is POS */ } Int main (void) { Mgraph mg; Patharc P; Shortpathtable D; Int I, j, Pos = 2; Createmgraph (& Mg ); Shortestpath_dijkstra (Mg, POs, P, d ); Cout <"reverse Shortest Path:" <Endl; For (I = 8; I> = 0; I --) { J = I; While (P [J]! =-1 & P [J]! = 0) { Cout <"v" <j <"<-" <"v" <p [J] <""; J = P [J]; } Cout <"v" <j <"<-" <"v" <POS <""; Cout <Endl; } Cout <Endl; Return 0; } |
Output:
The createmgraph function is used to create an adjacent matrix 7-7.
I believe that after the above analysis, you can analyze the loop program by yourself. After the loop ends, final = {1, 1, 1, 1, 1, 1, 1, 1} indicates that all vertices have completed the Shortest Path search. In this case, D = {4, 3, 0, 3, 1, 4, 6, 8, 12 }, note that we have mentioned earlier that the Dijkstra algorithm can find the shortest path from a source point to other vertices. Now the POs given in the above program is 2,
That is, if the source point is V2, d [2] = 0 indicates that the path is not in its own path. The D array represents the shortest path length from V2 to each vertex, for example, d [8] = 1 + 2 + 3 + 2 + 4 = 12. In this case, P = {1, 0,-1, 4, 0, 4, 3, 6, 7, P [2] =-1 indicates that v2 does not have a precursor vertex. P [1] = P [4] = 0 indicates that V1 and V4 are the Source Vertex V2. For example, P [8] = 7 indicates that the V8 precursor is V7, and P [7] = 6 indicates that the V7 precursor is V6; P [6] = 3 indicates that the V6 precursor is V3.
The shortest path to V8 is v2-> V4-> V3-> V6-> V7-> V8. The above program output can also verify our speculation.
In fact, the final returned array D and array P can obtain the shortest path and path length from V2 to any vertex, that is to say, the Dijkstra algorithm solves the shortest path problem from a source point to other vertices. From nested loops, we can see that the time complexity of this algorithm is O (n ^ 2). What if we want to obtain the shortest path from any vertex to other vertices? The simplest way is to perform a Dijkstra Algorithm for each vertex as the Source Vertex, which is equivalent to a loop based on the original algorithm, the time complexity of the entire algorithm is O (n ^ 3 ).