## Dijkstra algorithm

### 1. Definition overview

The Dijkstra algorithm is a typical single-source shortest path algorithm that calculates the shortest path from one node to all other nodes.The main feature is to extend from the starting point to the outer layer until it reaches the end point.Dijkstra's algorithm is a very representative shortest path algorithm. It has been introduced as a basic content in many professional courses, such as data structure, graph theory, operations research and so on.Note that this algorithm requires that there are no negative edges in the graph. Problem Description: In the undirected graph G=(V,E), assuming that the length of each edge E[i] is w[i], find the shortest path from the vertex V0 to the remaining points.(single source shortest path)

### 2. Algorithm description

1) Algorithm idea: Let G = (V, E) be a weighted directed graph. Divide the vertex set V in the graph into two groups. The first group is the set of vertices for which the shortest path has been obtained (indicated by S, initially There is only one source point in S. After each shortest path is obtained, it will be added to the set S until all the vertices are added to S. The algorithm is complete. The second set is the set of vertices whose other undefined shortest paths are determined. (Indicated by U), add the vertices of the second group to S in ascending order of the length of the shortest path.In the joining process, the length of the shortest path from the source point v to the vertices in S is always not greater than the shortest path length from any source point v to any vertex in U.In addition, each vertex corresponds to a distance. The distance of the vertex in S is the shortest path length from v to this vertex, and the distance of the vertex in U is from v to this vertex. Only the vertices in S are intermediate vertices. The shortest path length. 2) Algorithm steps: a. Initially, S contains only the source point, ie S={v}, and the distance of v is 0.U contains other vertices besides v, that is: U={other vertices}. If v has edges with vertex u in U, then <u,v> has normal weight, if u is not an adjacency point of v, then The <u,v> weight is ∞. b. Select a vertex k with the smallest distance v from U, and add k to S (the selected distance is the shortest path length from v to k). c. Using k as the midpoint of new consideration, modify the distance of each vertex in U; if the distance from source v to vertex u (passing vertex k) is shorter than the original distance (not passing vertex k), then modify the vertex u The distance value, the distance of the vertex k of the modified distance value plus the weight on the edge. d. Repeat steps b and c until all vertices are included in S. Perform the animation process as shown below

### 3 algorithm code to achieve:

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 |
Const int MAXINT = 32767; Const int MAXNUM = 10; Int dist[MAXNUM]; Int prev[MAXNUM]; Int A[MAXUNM][MAXNUM]; Void Dijkstra(int v0) { Bool S[MAXNUM]; // Determine if this point has been stored in the S collection Int n=MAXNUM; For(int i=1; i<=n; ++i) { Dist[i] = A[v0][i]; S[i] = false; // This point was not used initially If(dist[i] == MAXINT) Prev[i] = -1; Else Prev[i] = v0; } Dist[v0] = 0; S[v0] = true; For(int i=2; i<=n; i++) { Int mindist = MAXINT; Int u = v0; // Finding the dist[j] minimum of the currently unused point j For(int j=1; j<=n; ++j) If((!S[j]) && dist[j]<mindist) { u = j; // u saves the number of the point with the smallest distance in the current adjacency Mindist = dist[j]; } S[u] = true; For(int j=1; j<=n; j++) If((!S[j]) && A[u][j]<MAXINT) { If(dist[u] + A[u][j] <dist[j]) // Finding a path shorter than v0 in the newly added u point path { Dist[j] = dist[u] + A[u][j]; //update dist Prev[j] = u; // Record the predecessor vertex } } } } |

### 4. Algorithm example

First give an undirected graph Find the single-source shortest path starting from A using the Dijkstra algorithm

## Floyd algorithm

### 1. Definition overview

The Floyd-Warshall algorithm (Floyd-Warshall algorithm) is an algorithm that solves the shortest path between any two points, can correctly handle the shortest path problem of directed graphs or negative weights, and is also used to calculate the transitive closure of directed graphs. package.The time complexity of the Floyd-Warshall algorithm is O(N3) and the spatial complexity is O(N2).

### 2. Algorithm description

1) The idea of the algorithm: The Floyd algorithm is a classic dynamic programming algorithm.To describe it in popular language, our goal first is to find the shortest path from point i to point j.From the point of view of dynamic planning, we need to re-interpret this goal. (This interpretation is the essence of the most creative of dynamic planning.) The shortest path from any node i to any node j is nothing more than 2 possibilities. 1 is directly from i to j, 2 is from i through several nodes k to j.Therefore, we assume that Dis(i,j) is the distance of the shortest path from node u to node v. For each node k, we check Dis(i,k) + Dis(k,j) < Dis(i,j) If it is true, if it is true, prove that the path from i to k to j is shorter than the path of i directly to j, we set Dis(i,j) = Dis(i,k) + Dis(k,j), For one thing, when we traverse all nodes k, the distance recorded in Dis(i,j) is the shortest path from i to j. 2). Algorithm Description: a. Start from any single-sided path.The distance between all two points is the weight of the edge. If there is no edge connection between the two points, the weight is infinite. b. For each pair of vertices u and v, see if there is a vertex w that makes the path from u to w to v shorter than the known path.If it is updated it. 3). Floyd algorithm process matrix calculation ---- Cross method method: two lines, starting from the upper left corner to the lower right corner as given by the matrix, where matrix A is the adjacency matrix, and the matrix Path records u The corresponding calculation method for the point that the shortest path between the two points must pass is as follows: The last A3 is the result

### 3 algorithm code to achieve

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 |
Typedef struct { Char vertex[VertexNum]; //Vertex table Int edges[VertexNum][VertexNum]; //adjacency matrix, can be seen as side table Int n,e; //The current number of vertices and edges in the graph }MGraph; Void Floyd(MGraph g) { Int A[MAXV][MAXV]; Int path[MAXV][MAXV]; Int i,j,k,n=g.n; For(i=0;i<n;i++) For(j=0;j<n;j++) { A[i][j]=g.edges[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(A[i][j]>(A[i][k]+A[k][j])) {<br> A[i][j]=A[i][k]+A[k][j]; Path[i][j]=k; } } } |

Algorithm time complexity: O(n3)

This article is from the blog park: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html

## No Comment