Single-Source Shortest Path

Source: Internet
Author: User

The most short-circuit problem is the most fundamental problem in graph theory. There are also many times of appearance in the interview questions, and many problems such as the least number of steps can be converted to the most short-circuit problem, this article introduces two algorithms for single-source shortest path. The single-source shortest path is fixed with a starting point, and the shortest path from the single-source shortest path to all other points. It seems much easier to find the shortest path between two fixed points, however, the complexity is the same, so we have extensively discussed the single-source shortest path.

1Bellman-Ford algorithm we define the distance from point s to the destination point I to d [I], so we need I = 0 to I = E-1 (E is the vertex) all d [I]. Divide the problem into subproblems. Assume that the vertex j and I have an edge connection. D [I] = min {d [j] + (weight of the edge from j to I )}. Note that: If there is a circle in the graph, for example, if I to j is 3 and j to I is 4, we do not know whether to calculate d [I] Or d [j] First, at this time, we only need to initialize all d [I] to INF (I is not s), so it does not matter which one is calculated first. Even if the value of d [j] + (the edge weight from j to I) is smaller than that of d [I], we update the value of d [I. In this way, we can directly use this recursive link to write the code as follows:
Struct edge {int from, to, cost} // structure of the edge: costedge es from vertex to vertex [MAX_E]; // edge array int d [MAX_V]; // distance array int V, E; // Number of vertices and number of edges void shortest_path (int s) {for (int I = 0; I
 
  
D [e. form] + e. cost) {// find the d [e. to] = d [e. from] + e. cost; update = true ;}} if (! Update) break; // after all vertices are updated, end }}
 
Complexity: while can be recycled at most | V |-1 times, so it is O (| V | * | E | ).

Note:: If there is a negative circle in the graph (for example, I to j is-1, and j to I is-2), the shortest path will keep an infinite loop along the negative circle... We can determine that if the while th | V | th loop is also updated, there will be a negative circle.
The Dijkstra algorithm of 2Dijkstra algorithm is a special case, that is, when there is no negative edge in the figure, this situation is very suitable for the actual situation, such as the distance between two travel destinations. Since they are all positive numbers, we can think about it. If d [j] is not sleeping at the shortest distance, even if d [I] = d [j] + (weight) is updated, d [I] is not the shortest distance. In addition, in each cycle of vertex V in the code above, it is a waste of time to check all the edges connected to the vertex. Therefore, the greedy algorithm is modified as follows: (1) locate the vertex determined by the shortest distance and update the distance between adjacent vertices from it. (2) You don't have to worry about the points that have been determined. This definite vertex is the vertex with the smallest distance among unused vertices. Because there is no negative edge, this definite vertex will not become smaller in subsequent updates.
In this figure, the order of vertices is as follows: A, C, B, D, E, and F:
Int cost [MAX_V] [MAX_E] int d [MAX_V]; bool used [MAX_V]; int V; void dijkstra (int s) {fill (d, d + V, INF); fill (used, used + V, false); d [s] = 0; while (true) {int v =-1; for (int u = 0; u
 
  
The complexity of this writing method is O (| V | ^ 2). Most of the time in the Code is spent searching for the next vertex. We can use a good data structure to protect this vertex, to obtain the minimum value in the set when a value is inserted, the best data structure is heap. The element in the heap is known as O (| V |), and O (| E |) needs to be updated) so the complexity is O (| E | log | V | ).
  

The following uses the priority queue (priority_queue) in STL ):
Struct edge {int to, cost} typedef pair
   
    
P; // first is the shortest distance, and second is the vertex number int d [MAX_V]; vector
    
     
G [MAX_V]; // The int V of the adjacent table; void dijkstra (int s) {priority_queue
     
      
, Greater
      

> Que; // greater indicates fill (d, d + V, INF) from small to large; d [s] = 0; que. push (P (0, s); while (! Que. empty () {P p = que. top (); // retrieve the exact vertex que. pop (); int v = p. second; if (d [v] D [v] + e. cost;) {d [e. to] = d [v] + e. cost; que. push (P (d [e. to], e. to); // puts the update distance and points into the heap }}}}

If the Fibonacci heap is used, the performance can be improved, which is a little complicated. In short, the performance of the Dijkstra algorithm is superior to that of the Bellman-Ford Algorithm without negative edges. Therefore, this method is very important and requires careful research on its principles.



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.