I. Optimal substructure properties of Shortest path
This property is described as: if P (i,j) ={vi .... Vk. Vs... VJ} is the shortest path from vertex i to J, and K and S is an intermediate vertex on this path, so P (k,s) must be the shortest path from K to S. The correctness of the property is shown below.
Suppose P (i,j) ={vi .... Vk. Vs... VJ} is the shortest path from vertex i to J, with P (i,j) =p (i,k) +p (k,s) +p (s,j). and P (k,s) is not the shortest distance from K to S, then there must be another shortest path from K to s P ' (k,s), then P ' (i,j) =p (i,k) +p ' (k,s) +p (s,j) <p (i,j). The shortest path from I to J is contradicted by P (i,j). Therefore, the nature of the evidence.
two. Dijkstra algorithm
Dijkstra (Dijkstra) algorithm is a typical shortest path routing algorithm that calculates the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. Dijkstra algorithm
First, give an image without a direction.
Using the Dijkstra algorithm to find the single-source shortest path with a as the starting point is as follows
Three. Code
Java
Static int[] testdijkstra (int n,int v,int[] dist,int[] prev,int[][] c) { int maxnum = 100; int maxint = 999999; boolean[] s = new boolean[maxnum]; // Determine if the point has been deposited into the S collection for (int i=1; i<=n; ++i) { dist[i] = c[v][i]; s[i] = false; // the point was not used initially if (Dist[i] == maxint) prev[i] = 0; else prev[i] = v; } dist[v] = 0; s[v] = true; // a node that is not placed in the S collection, taking dist[] the minimum value, and putting it into the //of the Union s. Once s contains all of the V vertices, Dist records the shortest path length from the source point to all other vertices // Note that starting from the second node, the first one is the source point for (Int i=2; i<=n; ++i) { int tmp = maxint; int u = v; // find the Dist[j] minimum value of the currently unused point J for (int &NBSP;J=1;&NBSP;J<=N;&NBSP;++J) if ((!s[j]) && dist[j]<tmp) { u = j; // u the number of points with the smallest distance in the current adjacency point tmp = dist[j]; } s[u] = true; means u point has been deposited in S collection // update dist for (int j=1; j<= N;&NBSP;++J) if ((!s[j]) && c[u][j]<maxint) { int newdist = dist[u] + c[u][j]; if (Newdist&nbsP;<&NBSP;DIST[J]) { dist[j] = newdist; prev[j] = u; } } } return dist; }
Math: Dijkstra algorithm