The shortest path algorithm and analysis: Dijkstra algorithm, Floyed, Bellman-Ford, SPFA algorithm;

Shortest Path Algorithm: shortest path algorithm is studied in graph theory, a classical algorithm problem; Figure aimed at finding the shortest path between two nodes (a node and a path consisting of).

Determine the starting point of the shortest path problem: the known starting point, the shortest path problem. Dijkstra's algorithm for use; (the single-source shortest path)

Global shortest path problem: find the shortest path to all the figures, suitable for Floyed-Warshall algorithm; (multi-source shortest path)

Single-source shortest path: Given a weighted directed graph G = V, E; wherein each edge is a real number. Further, a back vertex set V, designated source; source to calculate from the length of the shortest path to all other vertices. This means that each length of the right side and the road. This problem is usually referred to as single-source shortest path problem;

Dijkstra's algorithm : Dijkstra algorithm using greedy idea that solving the current choice is always the best solution in question; the algorithm used to solve the single-source shortest path problem, can not handle negative weights, can only be positive for the right figure ; algorithm using greedy strategy, from s0 start, select unvisited V [i] of from s0 nearest a point i, which is the smallest D [i]; and i is as an intermediate point, the update through i, can reach the point the shortest distance, greedy continue to find the nearest one point unvisited, after n greedy, all of the access points is completed, the algorithm is finished; the shortest distance between start and end points of output;

  • Initialization d [s0] = 0, else d [i] = INF;
  • After n greedy, find the shortest distance from the starting point s0 to other points;
  • greedy:
    • Identify a minimum unvisited d [K];
    • K is visited flag V [k];
    • K is as an intermediate point, to update the starting point s0, through the other of v k D [v]; array can update path tracing, the shortest recording current from [v] = k from which node;
  • The difference between Prim algorithm and greedy algorithm:
    • Prim's algorithm: updating the set of markers is not set to the distance between the marked;
    • Dijkstra algorithm: updating the source is set to a distance between the unmarked;
  • Dijkstra's algorithm can be used heap optimization: Heap optimization, core Dijkstra's algorithm is to first find the minimum distance, and then update; when not optimized, we are here to find the minimum distance through the cycle; we can use the priority queue to optimize ; heap priority queue are generally used to implement, it can be considered to optimize the heap; C ++ has std :: priority_queue container adapter can be used;

Floyed algorithm: Floyed algorithm, also known as the interpolation point method, utilizing idea of dynamic programming algorithm to find the shortest path to a given weighting between multiple source figure; the multi-source method can be obtained shortest, can handle negative the right side of the case , but not negative ring; the idea of using a dynamic programming algorithm;

  • Set D [i] [j] [k] represents the i to j after only 1,2, .. k when these nodes, the shortest path i to j. There will be the following two situations:
    • After point k: d [i] [j] [k] = d [i] [k] [k-1] + d [k] [j] [k-1];
    • Does not pass through point k: d [i] [j] [k] = d [i] [j] [k-1];
  • State transition equation is: d [i] [j] [k] = min {d [i] [j] [k-1], d [i] [k] [k-1] + d [k] [j ] [k-1]}
  • Boundary conditions: d [i] [j] [0] = w [i] [j]; (w [i] [j] denotes weights ij edges, edge weights represent the absence of a positive infinity)
  • Since k is incremented, d [i] [j] is saved state d [i] [j] [k-1], can be reduced by one-dimensional, two-dimensional array:
    • State transition equation: d [i] [j] = min {d [i] [k] + d [k] [j], d [i] [j]};
    • Boundary conditions: d [i] [j] = w [i] [j];
    • Enumeration k, k is updated using the midpoint shortest distance from i to j;

Bellman-Ford algorithm: Bellman Ford algorithm is a single-source shortest path algorithm; Dijkstra algorithm it is relatively negative weights may be treated for conditions: no negative ring; simple, but the time complexity is high; may be used to determine whether there is negative loop , each iteration updating the shortest path to the starting point of each node; (n-1 is present between the edges 6 points) If the iteration n-1 times, again there is a path update iteration, then there is a negative ring;

Algorithm thinking: any one of the shortest bar chart, neither contain negative cycles, it will not contain is the right loop, containing up to n-1 edges. Then, beginning from the source point s, the node can be reached, if there is the shortest, the shortest path to constitute a shortest s rooted tree. Thus, in accordance with the root of the distance s of the level, layer by layer generates the shortest reach each point (relaxation operation); therefore the process, is the shortest path tree creation process; requires an auxiliary array of d [n] and v [n] and the shortest distance to the recording track tracing; considered from the perspective of the edge, each edge to traverse each iteration; the cycle n-1 times, if the n-th cycle of all d [n] value is not updated, out of the loop ; if there is the n-th path update, indicates the presence of a negative ring; the Bellman-Ford algorithm to solve the longest path may be used to determine n and the ring, as long as the selected maximum update recursion relation like;

Algorithm implementation process:

  • Initial: dist [u] = INF; dist [s] = 0; s is the starting point;
  • Recursion: For each edge (u, v) relaxation operation; dist [v] = min (dist [v], dist [u] + w [u] [v]); (n-1 operates as a relaxation times ) 
  • Finally recirculation time, determines whether there is a negative ring;

SPFA algorithm: SPFA (Shortest the Path Faster Algorithm); Bellman-Ford algorithm described above, the time complexity of the algorithm is relatively high; Bellman-Ford algorithm requires recursive n times, each recursive necessary to scan all sides; however, each slack operations do not require all of the slack side, only need to find the most current side connected to short relaxation point; therefore the use of queues, each time point from the update does not enqueued in a queue; each taken from a queue vertex, all its adjacent nodes relaxation, relaxation success if a vertex, such as the return point is not in the queue, it is enqueued, such operations are repeated, up until the queue is empty; if the number of times a node enqueue more than n times, indicating the presence of negative cycles; can use a CNT [n] array for counting;

Algorithm implementation process:

  • Initialization: dis [s] = 0; dis [i] = INF; create a new queue enqueue the source node s, s numerals have been enqueued;
  • Remove a point from the first team u, u been dequeued flag, and will be connected to the edge point u v relaxation operation; and v is not successful if the slack in the queue, then v enqueued;
  • Repeat until the queue is empty;

Time complexity analysis:

  • Floyed Algorithm: seeking multi-source shortest possible processing load side; time complexity of O (n3);
  • Dijkstra's algorithm: seeking single source shortest, can not handle negative edge; time complexity of O (n2);
  • Bellman-Ford algorithm: seeking single source shortest possible processing load right side; the time complexity is O (NM);
  • SPFA algorithm: seeking single source shortest, Bellman-ford optimization algorithm version can handle negative right side; the time complexity is O (kM) ~ O (NM ); k is a small constant;

Please refer to the code to achieve: https://github.com/yaowenxu/codes/tree/master/ shortest path algorithm

Kept up to date, please indicate the source; for more information, please attention cnblogs.com/xuyaowen; 

references:

Shortest Path Problem  four most shortest path algorithm  dijkstra algorithm  Floyd algorithm   difference between Prim and Dijkstra Algorithm  Bellman-Ford algorithm  SPFA algorithm 

Guess you like

Origin www.cnblogs.com/xuyaowen/p/shortest-path-algos.html