We have learned how to use the Dijkstra algorithm to find the shortest path in a non-negative weight graph. Even if there is a negative weight edge in the figure, we know that the Bellman-Ford algorithm is used to find the shortest path from the given source point to all other nodes. Now we will see an algorithm that runs faster in linear time. It can find the shortest path from a given source point to all other reachable vertices in a directed acyclic graph, also known as directed acyclic graph (DAG ).
As a directed acyclic graph without loops, we do not have to worry about negative loops. Just as we already know that it is meaningless to discuss the Shortest Path in negative loops, because we can continuously "loop" in these loops ", but in fact, the path we get will become shorter and shorter (forming a negative loop with an endless loop ).
The existence of negative loops makes it meaningless to try to find the shortest path!
Therefore, we need to solve two problems in the Dijkstra algorithm and the Bellman-Ford algorithm. First, we only need a non-negative weight. Second, the graph cannot have a ring. Okay, so that we can use this algorithm to handle the two cases.
Overview
We first need to know about Directed Acyclic graphs (dags) that can be easily sorted by topology. Topology Sorting has a wide range of applications. The most common application is to arrange dependency tasks (dependent tasks are the same entities that belong to the same task in a job, and these entities guarantee interconnection, they solve common problems ).
Topological sorting is usually used to "sort" tasks!
After topological sorting, we will finally get a list of Dag vertices. We are sure that if an edge (u, v) exists in the topological sorting list ), then the vertex u will go to the list before vertex v.
If an edge (u, v) exists, the vertex u must be in front of the vertex v. This image makes the result easier to understand. There is no edge between B and D, but in the list, B is in front of D!
This information is very important. The only thing we need to do is to use this sort list to calculate the shortest path, which is similar to the Dijkstra algorithm.
Let's summarize this algorithm:
-First, we must sort the directed acyclic graph topology;
-Second, we initialize the distance to the source point to 0 and set the distance to all other vertices to infinity;
-Finally, for each vertex in the list, we find the vertex of the shortest path from all its adjacent nodes;
This is similar to Dijkstra algorithm, but the main difference is that we use a priority queue, and this time we use a list sorted by topology.
Code
This code is actually a pseudo code. Although all examples have been written in PHP so far, pseudocode is easier to understand and will not bind you with a specific language implementation. In addition, if you are hard to understand the given language, it is more difficult for you to read the code than to read the pseudo code.
1234567 |
Topologically sort G into L; Set the distance to the source to 0; Set the distances to all other vertices to infinity; For each vertex u in L - Walk through all neighbors v of u; - If dist(v) > dist(u) + w(u, v) - Set dist(v) <- dist(u) + w(u, v); |
Application
The reason we must use this algorithm is obvious. The only noteworthy problem is that we must ensure that this graph has no loops. However, if we know how to create a graph, we will have more or less additional information to confirm whether there is a ring in the graph-then the algorithm in this linear time will be very suitable.
Reproduced http://www.importnew.com/9690.html