Shortest Path Algorithm weighted graph (Dijkstra, Floyd, Bellman_ford)

Dijkstra algorithm - single-source shortest path calculation nonnegative weights

ALGORITHM

  Based on greedy strategy, always choose the closest source point S and have not yet confirmed the sink shortest path D, considered the shortest path distance is the final current SD SD, since the distance to other points of S are greater than SD, so S after reaching the other points of the path and then bound to the point D is greater than SD more, therefore, compared with the current SD S D is the shortest path. Notice that the SD is not necessarily the length of the SD side, is likely to be some other way has confirmed the length of the shortest path to the point. After determining the shortest path S to D, S to update the values ​​of other points such as the distance undetermined point E, is less than the SDE is determined after SE, if less than the SE is updated. After all updates, continue to search for the nearest distance S and the sink unacknowledged D ', the same as points D, this time SD' is the shortest distance from S to D ', and then continues to update the rest of the sink until all shortest path acknowledgment .

  The point is that each algorithm to determine the shortest path of a point, will be updated again from the source to the other unrecognized point, in order to ensure the shortest path to find the next point of the path is the shortest path, which is the greedy strategy key to success. However, the algorithm can not deal with negative weights FIG greedy not the optimal solution.

Bellman-ford algorithm - negative weights single-source shortest path

ALGORITHM

  Initially, the source S to the distance D (x) other points set to infinity, then one for each side of FIG. <U, v>, relaxation treatment as follows: D (v) = min (D (v), D (u) + uv). The above-described traversing operation up to n - 1 times, the shortest path can be obtained at all points. After once again traverse, if more D (v) appears> D (u) + uv situation, it only shows that there is a negative weight loop diagram, the shortest path can not be found, only the shorter relaxation.

  As to why for most n - 1 relaxation times can be determined traversal S reaches the other n - 1 points of the shortest path, because the completion of each traverse, at least the shortest path will be a point, for the following reasons (too lazy drawing, brain Consider the like):

  S can be assumed that there is a direct neighbor nodes ABC three (may be more, but the same principle), apparently SA, SB, SC is at least one of the shortest path, then after a relaxed, D (A), D (B ), D (C) at least one of the shortest path, assuming that D (a) is the shortest path, that is SA, the following two cases,

  (1) S BC shortest path to go through all the points of the point A, then the shortest path to the node A is directly adjacent to the DEF certainly be through a point A, and the ABC case three analog S, SAD, SAE, SAF also at least one of the shortest path, so after the second relaxation, but also to determine the at least one point, it is assumed as D. It is possible to point D in the first slack has been determined, which is determined by the order of traversal of edge.

  (2) S shortest path B or C does not require A, then the first pass when it has been determined that a certain point in the A and BC.

  Relaxation may be determined each time point is at least a shortest path, and only S, A, D ..... has a similar situation when (1) take up to n - 1 relaxation times.

Floyd algorithm - calculating the shortest path between any two points in FIG.

ALGORITHM

  When the dynamic programming algorithm, for any two points m, n, m to the shortest path through 1,2,3,4 ... n may be arbitrary points, thus calculated first may be subjected to a first point, mn the shortest path is the number: dp [m] [n] = min (dp [m] [1] + dp [1] [n], dp [m] [n]), when, after calculating any two points, can then determine the time elapsed before the two points, the shortest path mn: dp [m] [n] = min (dp [m] [2] + dp [2] [n], dp [m] [n]), If after the second point, dp [m] [n] the same, if the shortest path to go through the second point, dp [m] [n] = dp [m] [2] + dp [2] [n-], without passing through this point as to the first point, or go through after no need to consider, because dp [m] [2] and dp [2] [n] is considered the representative of a first point the shortest path in a case where, when the first only consider the first point has been calculated before. After the first continues to calculate the following three points, four points may be the case until ...... n points.

  Dynamic programming optimal substructure that: in the can before the elapsed points k-1, the shortest path between any two points mn is known, then the time may pass before the k points, mn shortest path by:

dp[m][n] = min ( dp[m][k] + dp[k][n], dp[m][n] )

 

Guess you like

Origin www.cnblogs.com/zz-zhang/p/12584839.html