single-source Shortest path problem is that given a graph g= (v,e), we want to find the shortest path from the given source node s to each node V. Single source Shortest path problem can be used to solve many of the shortest path variants.
single-Destination Shortest path problem : Find the shortest path from each node V to the given destination node T. Flipping each edge of the graph, this problem can be converted to the single source shortest path problem.
single node to shortest path problem : Find the shortest path from the given node u to the given node v. If you have resolved the cell shortest path problem with u, the problem has been resolved.
In the single source shortest path problem, there are different solutions depending on the nature of the diagram. The main properties are: There is a direction or no map, is the weight of the map or unit, there is no negative weight of the edge, whether there is a ring.
Dijkstra algorithm
The Dijkstra algorithm solves the single-source shortest path problem on a non-negative weighted graph. The algorithm logic is to maintain a node set S, the shortest path between each node in the collection has been found, repeat from the set v-s Select the shortest path between the source node R and the least estimated node U join to S, Then the distance from the edge check R to u and the distance of the edge is less than the original shortest path estimate for the other node of the edge.
(a) Initial state, S is the source node and is added to the set S.
(b) Check the path of S departure (S,t) and (s,y) to update the shortest path estimation for Y and T
(c) Select the shortest path in the remaining node to estimate the minimum node Y, and then check the path from Y (y,t) + (s,y) =8<10, the distance of update T is 8, also for X and z do the same, after the end of y added to S
(d) Next select Z node, (z,s) +7=14>s, so s remains unchanged, and (z,x) +6=13<14,x value is updated to 13, then Z is added to S.
(e)-(f) The same operation as above, no longer described in detail. Eventually all points are added to S.
In order to complete the algorithm, we need to store the shortest path estimation of each node and their precursor nodes to output the final path, and need a minimum priority queue to save the shortest path estimation of the nodes in the v-s, otherwise it is necessary to traverse the nodes in the v-s to select the shortest one. The algorithm complexity of the direct traversal of all edges is O (v^2), and the complexity can be reduced to O ((v+e) LgV) If the minimum binary heap is used to store the priority queue.
1#include <stdio.h>2 using namespacestd;3 #defineSIZE 104 #defineINFI 100005 6 intG[size][size];//adjacency Matrix, parameter initialization slightly7 intDist[size];//distance estimation from the root8 BOOLVisit[size];//have you ever been visited ?9 Ten voidDijkstraintroot) { One inti,j; A intmin; - for(i =0; i < SIZE; i++){ -Dist[i] =infi; theVisit[i] =false; - } -Dist[root] =0; - for(i =0; I <SIZE; i++){ +Min =0;//find the nearest point in the remaining point in the distance root - for(j =1; J < size;j++){ + if(!visit[j] && Dist[j] <Dist[min]) { AMin =J; at } - } - for(j =0; J < SIZE; J + +){ - if(!visit[j] && g[min][j] + Dist[min] <Dist[j]) -DIST[J] = G[min][j] + dist[min];//Check if you need to update the shortest path - } inVisit[min] =true; - } to}Bellman-ford algorithm
The algorithm can solve the negative weighted edge of the graph, the algorithm returns a Boolean value, indicating whether there is a source node can reach a negative weight of the loop, if the existence of the algorithm will tell us that there is no solution, because the existence of this loop will lead to a point where the distance is negative infinity.
Algorithms need to be | V|-1 cycles, each loop relaxes all edges in the same order, and checks for the existence of a negative-weighted loop after the end. The algorithm complexity is O (VE), so the algorithm can reduce the efficiency when dealing with dense graphs, the overall efficiency is inferior to the Dijkstra algorithm.
The order of each relaxation edge is (t,x) (t,y) (t,z) (y,x) (y,z) (z,x) (z,s). The source node is s, and the shaded edge represents the precursor path.
(a) The distance from the update source node is 0
(b) In order, only (s,t) (s,y) can update the value of the node, t=6 y=7
(c) (T,z) (y,x) can update values for Z and x
(d) (x,t) can update the value of T
(e) There is no updatable value for this cycle, check that a ring with a negative weight value returns True
1#include <stdio.h>2 using namespacestd;3 #defineSIZE 104 #defineINFI 100005 6 intG[size][size];//adjacency Matrix, parameter initialization slightly7 intDist[size];//distance estimation from the root8 intP[size];//precursor junction.9 Ten voidBellmanford (introot) { One intI, J, K; A for(i =0; i < SIZE; i++){ -Dist[i] =infi; - } theDist[root] =0; - for(i =0; I < SIZE-1; i++){ - for(j =0; J < SIZE; J + +){ - if(Dist[j] = =infi) + Continue;//nodes cannot be reached from the source node before they are skipped - for(k =0; K < SIZE; k++){ + if(G[j][k]! =0&& G[j][k] + Dist[j] <Dist[k]) ADIST[K] = G[j][k] + dist[j];//Slack Path at } - } - } - for(i =0; i < SIZE; i++){ - for(j =0; J < SIZE; J + +){ - if(G[i][j]! =0&& Dist[i] > Dist[j] +G[i][j]) in return false;//Check for a ring with no weight or negative - } to } +}Differential constraints and shortest paths
In the linear programming problem, it is usually given a m*n matrix A, a m-dimensional vector B and an n-dimensional vector C, in the hope of finding an n-dimensional vector x, so that the objective function can be optimized under the condition of ax≤b given m constraints.
Maximizes the value of the target function. In a differential constrained system, each row of a linear programming matrix A consists of a 1 and a-1, all the other items are 0, and each constraint becomes an inequality xj-xi≤bk.
Matrices and vectors can be represented as 8 simple inequalities, which require a set of feasible solutions. With X as the node and the following constraint as the path weight, you can draw a constraint graph.
A set of feasible solutions can be obtained by setting a source node for the constraint graph, such as v0 to find the shortest path solution. Because of the negative weight, it needs to be solved by the Bellman-ford algorithm.
Introduction to the algorithm--the shortest path of the unit