Introduction:The Dijkstra algorithm is the most representative of the shortest path algorithm. The idea of Dijkstra algorithm is based on greedy strategy. An overview of the process is to augment the collection by setting the vertex collection s and constantly making greedy choices. The standard of greedy selection is to select the shortest path length from the source node to the node each time.
Difficulty:Most of the shortest path algorithms written by most bloggers on the web are only looking for the shortest path. but most of the time there may be the shortest path in parallel, and you need to find all the shortest paths at the same time. here I would like to share with you a time complexity is probably O (N2) algorithm, to find the parallel shortest path algorithm.
the algorithm steps for finding a shortest path are as follows:Suppose G={v,e}1. Initial seasonal s={v0},t=v-s={remaining vertices},dist[current node] = V0 to the current node weights, pre[the current node]=v02. Select a vertex w with the least weight associated with the vertex in S from T and add to s 3. Modify the distance values for vertices in the rest of T: If you add a W as the middle vertex and the distance value from V0 to Vi is shortened, the distance value dist[vi]= the new value, Pre[vi]=w4. Repeat steps 2 and 3 above until all vertices are included in s, i.e. W=vi
Code implementation:int arcs[10][10];//adjacency matrix int d[10];//Save Shortest Path length int p[10][10];//path int final[10];//if final[i] = 1 indicates that vertex VI has int n = 0;//vertex number int v0 = 0;//source point in set S int v,w; Void shortestpath_dij () { for (v = 0; v < n; v++) //cycle initialization { final[v] = 0; D[v] = arcs[v0][v]; for (w = 0; w < n; w++) p[v] = v0 ;//Set Empty path } d[v0] = 0; final[v0]=0; //Initialize v0 vertex belongs to collection s //start main loop Each time the shortest path of v0 to a vertex v is obtained plus V to the set S for (int i = 1; i < n; i++) { int min = max; for (w = 0; w < n; w++) { //I think the core process--point if (!final[w]) //If the W vertex is in V-s { //The final point of the process It should be to select the current v-s with the S-Associated edge //and the least weighted vertex book described as current closest point to V0 if (d[w] < min) {v = w; min = &NBSP;D[W];} } } final[v ] = 1; //Select this point and add it to the collection S for (w = 0; w < n; w++)//update the current shortest path and distance { / * v in this loop is the point in the collection s currently selected The point V is the middle point investigate d0v+dvw is less than D[w] if less than the update For example, add in point 3 to see if &NBSP;D[5] is to be updated to determine D (V0-V3) + d (V3-V5) And whether it is less than D[5]&Nbsp; */ if (!final[w] && (Min+arcs[v][w]<d[w]) { D[w] = min + arcs[v][w]; p[w] = v; } } }}
the algorithm steps for finding multiple parallel shortest paths are as follows:Suppose G={v,e}, set the pre array to a two-dimensional array.
Code implementation:function Shortestpath (SourceIndex, Targetindex, Nodesnumber) {var V,w;var prev = [];var dist = [];var MAX = infinity;//Initialize D IST and Prevfor (v=0;v<nodesnumber;v++) {visited[v] = 0; DIST[V] = Arcs[sourceindex][v]?arcs[sourceindex][v]:max; PREV[V] = [];} for (v=0;v<nodesnumber;v++) {for (w=0;w<nodesnumber;w++) {if (V==sourceindex) {Prev[sourceindex][w] = Arcs[sou Rceindex][w]==1?1:-1; }else{Prev[v][w] =-1; }}}dist[sourceindex] = 0;visited[sourceindex] = 1;//Select a vertex from T that is associated with the vertex in S and the least weighted value wfor (var i=0;i<nodesnumber;i++) {var min = MAX; for (w=0;w<nodesnumber;w++) {if (!visited[w] && dist[w]<min) {v = w; min = dist[w]; }} var W = []; for (w=0;w<nodesnumber;w++) {if (!visited[w] && dist[w]==min) {W.push (w); VISITED[W] = 1; }}//The distance value from V0 to VI is shortened, this distance value is modified for (w=0;w<nodesnumber;w++) {for (Var j=0;j<w.length;j++) {v = w[j]; if (!visited[w] && min+arcs[v][w]<=dist[w]) {Dist[w] = min + ARCS[V][W]; PREV[V][W] = 1; }}}}}//search path functions function SearchPath (v,u) {var isconnect = false; for (Var i=0;i<prev[v].length;i++) {if (prev[v][i] = = 1) {if (I==targetindex) {Path.push (targetindex); Paths.push (path); Path = [SourceIndex]; return true; }else{Path.push (i); } isconnect = SearchPath (i,u); }} if (!connect) {path.pop (); return false; }}
Graph theory's shortest path algorithm