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 animations such as (Pictures from the network):
The algorithm is implemented as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceDijkstra Algorithm {classProgram {Static intLength =6; Static string[] Shortedpath =New string[length]; Static intNopath = -; Static intMaxSize = +; Static int[,] G ={{Nopath, Nopath,Ten, Nopath, -, -}, {Nopath, Nopath,5, Nopath, Nopath, Nopath}, {Nopath, Nopath, Nopath, -, Nopath, Nopath}, {Nopath, Nopath, Nopath, Nopath, Nopath ,Ten}, {Nopath, Nopath, Nopath, -, Nopath, -}, {Nopath, Nopath, Nopath, Nopath, Nopath, Nopath}}; Static string[] Pathresult =New string[length]; Static int[] path1 =New int[length]; Static int[,] path2 =New int[length, length]; Static int[] Distance2 =New int[length]; Static voidMain (string[] args) { intDist1 = Getshortedpath (G,0,5, path1); Console.WriteLine ("Node 0 to 5:"); for(inti =0; I < path1. Length; i++) Console.Write (Path1[i]. ToString ()+" "); Console.WriteLine ("Length:"+dist1); int[] pathdist = Getshortedpath (G,0, path2); Console.WriteLine ("\nnode 0 to other:"); for(intj =0; J < Pathdist. Length; J + +) {Console.WriteLine ("Node 0 to"+ j +"Path:"); for(inti =0; i < length; i++) {Console.Write (path2[j, I]. ToString ()+" "); } Console.WriteLine ("Length:"+Pathdist[j]); } console.readkey (); } //from a source point, find the shortest path to a node. Static intGetshortedpath (int[,] GintStartintEndint[] path) { BOOL[] s =New BOOL[Length];//indicates the shortest path between the starting node and the current node is found intMin//Minimum Distance temp variable intCurnode =0;//temporary node that records the current positive compute node . int[] dist =New int[length]; int[] prev =New int[length]; //Initial node Information for(intv =0; v < length; v++) {S[v]=false; DIST[V]=G[start, v]; if(Dist[v] >MaxSize) Prev[v]=0; ElsePrev[v]=start; } path[0] =end; Dist[start]=0; S[start]=true; //Main Loop for(inti =1; i < length; i++) {min=MaxSize; for(intW =0; W < length; w++) { if(!s[w] && dist[w] <min) {Curnode=W; Min=Dist[w]; }} S[curnode]=true; for(intj =0; J < length; J + +) if(!s[j] && min + g[curnode, J] <Dist[j]) {Dist[j]= min +G[curnode, J]; PREV[J]=Curnode; } } //Output Path Node intE = end, step =0; while(E! =start) {Step++; Path[step]=Prev[e]; E=Prev[e]; } for(inti = step; I > Step/2; i--) { inttemp = Path[step-i]; Path[step-I] =Path[i]; Path[i]=temp; } returnDist[end]; } //from a source point, find the shortest path to all nodes Static int[] Getshortedpath (int[,] GintStartint[,] path) { int[] Pathid =New int[Length];//path (denoted by number) BOOL[] s =New BOOL[Length];//indicates the shortest path between the starting node and the current node is found intMin//Minimum Distance temp variable intCurnode =0;//temporary node that records the current positive compute node . int[] dist =New int[length]; int[] prev =New int[length]; //Initial node Information for(intv =0; v < length; v++) {S[v]=false; DIST[V]=G[start, v]; if(Dist[v] >MaxSize) Prev[v]=0; ElsePrev[v]=start; Path[v,0] =v; } Dist[start]=0; S[start]=true; //Main Loop for(inti =1; i < length; i++) {min=MaxSize; for(intW =0; W < length; w++) { if(!s[w] && dist[w] <min) {Curnode=W; Min=Dist[w]; }} S[curnode]=true; for(intj =0; J < length; J + +) { if(!s[j] && min + g[curnode, J] <Dist[j]) {Dist[j]= min +G[curnode, J]; PREV[J]=Curnode; } } } //Output Path Node for(intK =0; K < length; k++) { inte = k, step =0; while(E! =start) {Step++; Path[k, Step]=Prev[e]; E=Prev[e]; } for(inti = step; I > Step/2; i--) { inttemp = path[k, step-i]; Path[k, Step-I] =path[k, I]; Path[k, I]=temp; } } returnDist; } }}
Finding the shortest Path-dijkstra algorithm between two points