**Note: The following code is just a description of the idea, not tested!!**

**Dijkstra algorithm**

1. Definition Overview

The Dijkstra (Dijkstra) algorithm is a typical single-source shortest path 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 is a very representative shortest path algorithm, in many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on. Note that the algorithm requires no negative weight edges in the graph.

Problem Description: In the g= graph (v,e), assume that each edge e[i] length 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: Set g= (V,e) is a weighted to the graph, the figure of the vertex set V into two groups, the first set of the shortest path of the vertex set (s), the initial s only one source point, each subsequent to a shortest path, will be added to the set S, until all the vertices are added to the S, The algorithm ends, and the second set is the vertex set (denoted by u) of the remaining indeterminate shortest paths, and the second set of vertices is added to s in order of ascending shortest path length. During the join process, the shortest path length that is always maintained from the source Point V to the vertices in S is not longer than the shortest path length from the source point V to any vertex in U. In addition, each vertex corresponds to a distance, the distance from the vertex in S is the shortest path length from V to the vertex, and the distance from the vertex in U is the current shortest path length from V to the vertex that includes only the vertex in S as the middle vertex.

2) Algorithm steps:

A. Initially, S contains only the source point, that is, the distance of S={v},v is 0. U contains other vertices other than V, that is: u={remaining vertices}, if V and u in vertex u have edges, then <u,v> normal right value, if U is not V's out edge adjacency point, then <u,v> weight is ∞.

B. Select the smallest vertex k from U, and add K to S (the selected distance is the shortest path length of V to K).

C. Change the distance of each vertex in the U with K as the new consideration; If the distance from the source point v to the vertex u (through vertex K) is shorter than the original distance (without the vertex K), the distance value of the vertex U is modified, the distance of the vertex k of the modified distance value and the right of the edge are added.

D. Repeat steps b and c until all vertices are contained in S.

Perform an animation process such as

3. Algorithm code implementation:

Const intMAXINT =32767;Const intMaxnum =Ten;intDist[maxnum];intPrev[maxnum];intA[maxunm][maxnum];voidDijkstra (intv0) {BOOLS[maxnum];//determine if the point has been deposited into the S collection intn=Maxnum; for(intI=1; i<=n; ++i) {Dist[i]=A[v0][i]; S[i]=false;//the point has not been used initially if(Dist[i] = =MAXINT) Prev[i]= -1; ElsePrev[i]=V0; } Dist[v0]=0; S[v0]=true; for(intI=2; i<=n; i++) {intMindist =MAXINT; intu = v0;//find the Dist[j] minimum of the point J currently unused for(intj=1; j<=n; ++j)if((! S[J]) && dist[j]<mindist) {u= J;//U Save the number of the point with the smallest distance in the current adjacency pointMindist =Dist[j]; } S[u]=true; for(intj=1; j<=n; J + +)if((! S[J]) && a[u][j]<MAXINT) {if(Dist[u] + a[u][j] < Dist[j])//find a shorter path from the V0 point by the newly added U-point path{Dist[j]= Dist[u] + a[u][j];//Update DistPREV[J] = u;//record precursor vertices} }}}

4. Algorithm examples

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

Transferred from: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html

Shortest Path-dijkstra algorithm (reprint)