Comparison of several Shortest Path Algorithms

Source: Internet
Author: User

Shortest PathThe problem is a classic algorithm problem in graph theory research. It aims to find the shortest path between two nodes in a graph composed of nodes and paths.

The specific algorithm formats include:

Determine the Shortest Path of the start point: Find the shortest path when the Start Node is known.

The shortest path for determining the end point: opposite to the problem of determining the start point, this problem is the problem of finding the shortest path by knowing the end node. In an undirected graph, this problem is exactly the same as the problem of determining the starting point. In a directed graph, this problem is equivalent to the problem of determining the starting point when all paths are reversed.

Determine the Shortest Path of the start and end points: Find the shortest path between the two nodes.

Global shortest path problem: Find all shortest paths in the graph.

Floyd

Find the shortest path of multiple sources without negative weight edges. Use a matrix to record graphs. Poor timeliness, time complexity O (V ^ 3 ).

Floyd-Warshall algorithm) is an algorithm for solving the shortest path between any two points. It can correctly handle the shortest path problem of Directed Graphs or negative weights.

The time complexity of the Floyd-Warshall algorithm is O (N ^ 3) and the space complexity is O (N ^ 2 ).

The principle of Floyd-Warshall is dynamic planning:

Set Di, j, and k to the shortest path length of the intermediate node from I to j.

If the shortest path goes through k, Di, j, k = Di, k, K-1 + Dk, j, K-1;

If the shortest path does not pass through k, Di, j, k = Di, j, K-1.

Therefore, Di, j, k = min (Di, k, K-1 + Dk, j, K-1, Di, j, k-1 ).

In actual algorithms, iteration can be performed directly in the original space to save space, so that the space can be reduced to two-dimensional.

The Floyd-Warshall algorithm is described as follows:

 
 
  1. for k ← 1 to n do 
  2. for i ← 1 to n do 
  3. for j ← 1 to n do 
  4. if (Di,k + Dk,j < Di,j) then  
  5. Di,j ← Di,k + Dk,j; 

Where Di and j represent the cost from point I to Point j. When Di and j are ∞, there is no connection between two points.

Dijkstra

The shortest path of a single source with no negative weight is used. Good timeliness, with the time complexity OV * V + E ).

If the source is reachable, OV * lgV + E * lgV) => OE * lgV ).

When it is a sparse graph, E = V * V/lgV, so the time complexity of the algorithm can be OV ^ 2 ). If the Fibonacci heap is used as the priority queue, the algorithm time complexity is OV * lgV + E ).

Bellman-Ford

To find the shortest path of a single source, you can determine whether there is a negative weight loop. If yes, there is no Shortest Path.) The timeliness is good, and the time complexity is OVE ).

The Bellman-Ford algorithm is an algorithm used to solve the single-source shortest path problem.

The shortest path of a Single Source Vertex refers to finding the shortest path from s to v for any vertex v in the graph G given a weighted directed graph G and the Source Vertex s.

Unlike the Dijkstra algorithm, In the Bellman-Ford algorithm, the edge weight can be negative. Suppose we can find a loop, that is, starting from v and returning to v after several points) and the sum of the weights of all edges in the loop is negative. Through this loop, the shortest path of any two points in the loop can be infinitely small. If this negative loop is not processed, the program will run forever. The Bellman-Ford algorithm is capable of distinguishing this negative loop.

SPFA

It is the queue Optimization of Bellman-Ford, with relatively good timeliness and time complexity ). K <V ).

Similar to the Bellman-ford algorithm, the SPFA algorithm uses a series of relaxation operations to get the shortest path from a node to all other nodes in the graph. The difference is that by maintaining a queue, the SPFA algorithm makes it unnecessary to update other nodes immediately after the current shortest path of a node is updated, thus greatly reducing the number of repeated operations.

The SPFA algorithm can be used for graphs with negative edge weights, which is different from the dijkstra algorithm.

Unlike the Dijkstra algorithm and the Bellman-ford algorithm, the time efficiency of the SPFA algorithm is unstable, that is, the time required for different graphs varies greatly.

In the best case, if each node only enters the queue once, the algorithm actually changes to breadth-first traversal, and the time complexity is only O (E ). On the other hand, there is such an example that every node is queued (V-1) times, at this time the algorithm degrades to the Bellman-ford algorithm, its time complexity is O (VE ).

The SPFA algorithm can completely replace the Bellman-ford algorithm in the negative edge weight graph. It also performs well in the sparse graph. However, in the non-negative edge weight graph, to avoid the worst case, the more efficient Dijkstra Algorithm and Its heap optimized version are usually used. The performance of common SPFA algorithms in a class of grid graphs is not satisfactory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.