In this question, a direction graph G is given, each side of which has a nonnegative length (consuming) a [i] [j], and the length of the path is the sum of the lengths of the edges that the path passes through. For a given source vertex s, you need to find the shortest path from it to any other vertex in the diagram (called the destination). Fig. 13-10a gives a direction graph with five vertices, and the number on each side is length. Assuming the source vertex s is 1, the shortest path from Vertex 1 is listed in Figure 13-10b in the order of path length, and the number in front of each path is the length of the path.
Using E. The greedy algorithm invented by Dijkstra can solve the shortest path problem and find the shortest path by step-by-step method. Each step produces a shortest path to the vertex of the new destination. The desired vertex of the next step is chosen by the following greedy guidelines: in vertices that have not yet produced the shortest path, select the destination vertex with the shortest path length. In other words, the D i j K S T r a method produces the shortest path by path length order.
The first is the path from S to its own, which has no edge and has a length of 0. In each step of the greedy algorithm, the next shortest path is generated. One method is to add a feasible shortest edge to the shortest path that has been produced at present, and the resulting new path is the shortest path that was originally produced, plus an edge. This strategy does not always work. Another method is to consider adding a shortest edge in each of the shortest paths currently generated, and then selecting the shortest one from all of these edges, which is the D i j K S t r a algorithm.
When you can verify that the shortest path is generated in order of length, the next shortest path is always formed by an edge of the shortest path that has been generated. In fact, the next shortest path is always obtained from the shortest path that has been generated, and the shortest path reached by this path has not yet been generated. For example, in Figure 1 3-1 0, B in the second path is the first path to expand an edge formed; the third path is the second path that expands an edge; the fourth path is the first path to extend an edge; the fifth path is the third path that expands one edge.
This observation is an easy way to store the shortest path. You can use the array p,p [i] to give the vertex at the front of vertex i in the path from S to I. In this example p [1:5] = [0, 1, 1, 3, 4]. The path from S to vertex I can be created in reverse. Starting from I press P[i],p[p[i]],p[p[p[i]], the order of., until the vertex s or 0 is reached. In this case, if you start with i = 5, the vertex sequence is p[i]=4, p[4]=3, p[3]=1=s, so the path is 1, 3, 4, 5.
To produce the shortest path conveniently in ascending order of length, define d [i] to add a shortest edge length to the shortest path that has been produced, thus allowing the extended path to reach vertex i. Initially, there is only a 0-length path from S to S, at which point i,d [i] equals a [s] [i] (A is the length adjacency matrix of the direction graph). To produce the next path, you need to select the next node that does not have the shortest path, where the D value is the smallest and the end of the next path. When a new shortest path is obtained, the D value of some vertices may change because the new shortest path may produce a smaller D value.
To sum up, you can get the pseudo code shown in Figure 1 3-11, and 1 will initialize the p of all vertices adjacent to S to S, which is used to record the best information currently available. In other words, the shortest path from S to I, that is, from S to its own path to expand a side to get. When a shorter path is found, the P [i] value is updated. If you produce the next shortest path, you need to update the value of D based on the extension edge of the path.
1) Initialize D[i] =a[s] [i] (1≤i≤n),
For all vertices adjacent to S I, set p[i] =s, for the rest of the vertices p[i] = 0;
The L table is established for all vertices of the p[i]≠0.
2 if L is empty, terminate, or go to 3.
3 Remove the vertex with the smallest D value from L.
4) for all the vertex J adjacent to I that have not yet reached, update d[j] Value M i n{d[j], D[i] +a[i [j]}; if d[J] has changed and J has not
In L, then place p[j] = 1, and add J to L and go to 2.
Fig. 1-11 Description of Shortest path algorithm