Read the graph algorithm, Robert Sedgewick note-Shortest Path

Source: Internet
Author: User
Read the graph algorithm, Robert Sedgewick note-Shortest Path


Shortest Path (Shortest Path) is a very useful tool in practical applications. It can be divided into point-to-point Shortest Path (source-sink ), the shortest path of a single-source vertex (single-source). The shortest path of all vertices (all-pairs) and all vertices with negative edges.

To simplify our problem, set the following limits:

  • Directed Graph. an undirected graph can be seen as directed edges corresponding to two edges.
  • As you can imagine, if a negative ring exists, the path value can be continuously reduced. The shortest path containing a negative ring is NP. We will not discuss it for the moment.

Simple Algorithm for the shortest path of a single source node
The shortest path (without negative edges) of a single source node can be better solved using the Dijkstra algorithm. Its idea is similar to the prim algorithm for finding the smallest spanning tree (MST. Only Dijkstra changed the priority queue right from two points to the path from the source point to the next point:
Prim: Priority = edge. Weight () // weight from V to W
Dijkstra: Priority = wt [v] + edge. Weight () // the value from the source point to the W point. Wt [v] indicates the value from the source point to the V point.
Note that WT stores priority.

Similar to Prim, the complexity of the Dijkstra algorithm mainly depends on the implementation of the priority queue. The ordinary Dijkstra algorithm can solve the shortest path problem of a single source node without negative edge in linear time, that is, the complexity is V2. After the data structure of the priority queue is adopted, the problem can be solved between elgdv-elg2v (D indicates that D-heap is used ).

Point-to-Point shortest path without negative edge can also be solved using Dijkstra. Starting from the source point, the search will stop when the target point is found. It's very easy to understand, so I won't be arrogant.

The shortest path of All-pairs
As mentioned in most textbooks, Dijkstra is used for finding the shortest path of a Single Source Vertex without negative edge, and Floyd is used for finding the shortest path of all vertices. Indeed, we will use the Floyd algorithm, but it does not mean that Floyd is the best choice in all cases.

For those who have not studied Floyd, the first possible reaction to the All-pairs shortest path problem after learning Dijkstra may be: Calculate the single-source shortest path, can we get the shortest path of all vertices. A simple description of the algorithm is to execute the V Dijkstra algorithm. Naturally, its complexity can be velgdv in the optimal case.

Floyd can be said to be an extension of the warshall algorithm. Three for loops can solve a complicated problem. It should be said that they are very classic. From its layer-3 Loop, we can see that its complexity is V3. In addition to adding a point judgment in the layer-2 for, it can slightly improve the efficiency, and there is almost no other way to reduce its complexity.

Comparing the two algorithms, it is not difficult to draw the following conclusion: for sparse graphs, the use of V Dijkstra is superior. For dense graphs, the Floyd algorithm can be used. In addition, Floyd can process graphs with negative edges.

The shortest path of the loop-Free Network
Compared with a band-Ring Network (slightly different from a DAG), the positive and negative edges do not matter because of the no-ring network.
Remember that some vertices in the topology network may only have no inbound traffic for the Source Vertex, so we cannot find the vertex in the shortest path. We propose the multi-source shortest paths (multisource shortest paths) in this case, the shortest path is obtained. In fact, we can convert this problem to the shortest path of a single source node. We only need to add a dummy node, which points to all non-Inbound nodes to serve as a new source, all edges of the dummy node are 0.

Finding the longest path in a general graph (with a positive ring) is NP hard, similar to finding the shortest path in a negative ring graph. However, you can find the longest path in the ring-free network. Because the ring-free network has no positive ring, you can determine the longest path, and the longest path is meaningful to the Dag. Therefore, we first consider finding the longest path in the DAG (the shortest path algorithm is similar ):
Using the powerful Dag weapon-topological sorting can avoid the loss of the priority queue in Dijkstra, so the efficiency is higher than Dijkstra.
You only need to traverse each node based on the topological sequence, refresh each edge, and finally get the longest path.
The problem of the longest or shortest path of multiple source points can be solved in linear time, that is, complexity E.

For the shortest path of all vertices, we can certainly run the V algorithm in the previous section, so the complexity is ve. In addition, the shortest path of all vertices in a non-ring graph can be used to avoid multiple topological sorting.
Similar to the problem of passing closure of Dag, an effective algorithm can be obtained using DFS and DP methods. The pseudocode is as follows:

Two-dimensional array P for edge storage;
Store the double two-dimensional array D;

Recursive functions (graph, int traversal node v ){
Traverse all edges starting from the V node:
Edge e = this edge;
Int T = the other end of the edge;
Double W = edge weight;
If d [s] [T]> W, update d [s] [T] = W, P [s] [T] = E;
If t is not traversed, recursively traverse T;
Traverse all edges starting from the T node:
If d [s] [I]> W + d [T] [I]:
Update d [s] [I] = W + d [T] [I], p [s] [I] = E;
}
The complexity of this algorithm is ve (applicable to negative edges ).

The shortest path of Euclidean Networks
Euclidean networks refers to a network with many points on the plane. The weights between points are the Geometric Distance between the two points.
Generally, Euclidean networks is relatively large, that is, there are many points. Here, we only consider finding the point-to-point Shortest Path. Because the distance is not negative, we do not need to consider the negative side. For more information, see the method described earlier. Dijkstra is the best choice. PFS (priority queue) is used to save the nodes to be checked and then the shortest processing is selected.

Because the edges of Euclidean networks are determined by Geometric Distance, we use several special methods to accelerate algorithms and reduce the search range. First, consider the priority of PFS. In Dijkstra, W [v] + edge is used. weight () is the priority. In Euclidean networks, we can consider the distance from the checkpoint to the target point. This is a heuristic search idea. In this way, you can make the following changes to Dijkstra (S source point, d end point, DIST ry distance function, V is checking point, W is waiting for checkpoint ):

  1. Initialize wt [s] to dist (S, d)
  2. Change the priority to (wt [v] + edge. weight () + dist (W, d)-dist (v, D), that is, the distance from S to W plus W to D.

(If you are familiar with heuristic search, you can think of f' () = wt [v]-dist (v, d), g' () = edge. weight () + dist (W, d), H' () = f' () + G '())

The advantages of this modification can be understood through a simple example: Assume that node v is currently being checked, and W1 is to be checked (close to V, not connected to D) and W2 (connected to V to D ). If Dijkstra is used, traverse W1 first by priority, Then W2, and finally d, the v-w2-d is obtained. But if you modify the priority, you can avoid traversing W1 and get the result of the v-w2-d directly. If this method is used in large-scale data, you can search for the target solution quickly.

However, heuristic search only accelerates the target solution, without saving any time and space.
Euclidean heuristic search is an algorithm of a * algorithm. Of course, a boundary function is required to eliminate the possibility of better solution, that is, cutting. The simple method is to ignore all the tasks whose priority is greater than the checkpoint to be resolved when a possible solution is found.
The restriction of Euclidean heuristic search can be understood as a large circle with s as the origin, with S and D as the focus, and the shape is affected by the current solution. Dijkstra searches the entire circle, while heuristic searches only for the ellipse (because the focus is inside the circle and the solution is constantly updated and smaller, we can imagine that the ellipse is very small than the big circle). Therefore, heuristic search is much better than Dijkstra in space and time.

Another method to implement Euclidean heuristic functions is to re-authorize edges. Update each edge V-W, that is, add dist (W, d)-dist (v, d ), the initialization of WT [s] is still dist (S, D ). Then run the standard shortest path algorithm. Re-granting edge permissions will be used later.

Other problems

First, define a useful technical means-reduction)
Definition we say that a problem a reduces to another problem B if we can use an algorithm that solves B to develop an algorithm that solves a, in a total amount of time that is, in the worst case, no more than a constant times the worst-case running time of the algorithm that solves B. we say that two problems are equivalent if they reduce to each other.
Define if you can design an algorithm for solving problem a from the Algorithm for Solving Problem B, in the worst case, the total amount of time does not exceed the integer multiple of the running time of algorithm B in the worst case. We call the problem attenuation as B. When two problems can decay, It is equivalent.

For example, Floyd and warshall are so similar that we can say that the transfer closure problem reduces to the shortest path problem of all vertices (and vice versa ). Therefore, the passed closure can be evaluated using Floyd, And the complexity is the same as that of warshall.
In a network with unlimited edges, finding the longest and shortest edges is equivalent.

Job Scheduling (I have not translated the relevant question name). For a job, you need to complete several other jobs first. Each job has a time attribute, the minimum time required to complete all jobs under certain conditions. First, consider simple scheduling without any restrictions.
Difference constraints: It sets some restrictions on a series of variables from x0 to XN. Each restriction indicates that the difference between the two variables is not less than the given constant. For example, x1-x0> = 0.41 x7-x1 = 0.41 and so on.
Linear Programming and difference constraints are generalized.

The job-scheduling problem can be caused by CES to difference-constraints. When job a must begin after Job B is completed, there can be a-B> = Tb relationship.
If the constant in difference-constraints is a positive number, equivalent to the longest path of a single source point in a ring-free network. Therefore, you can use the longest path of a single source node in a non-circular network to solve the General unrestricted job-scheduling problem.
Considering the limitation of the deadline for job-scheduling, we have mentioned that job-scheduling can reduce to difference-constraints, in fact, if the job has a deadline, it is equivalent to the difference-constraints in which there is Xi-XJ <= Dj, that is, XJ-xi> =-DJ. That is, constants in difference-constraints can be negative.
For the job-Scheduling Problem with a fixed job duration, you can use CES to solve the shortest path problem with a negative edge and no negative ring.

Another purpose of the reduce method is to determine whether a certain type of problem is an NP-hard problem. If the NP-hard problem is reduced to another problem, the other problem is NP-hard.
Finding the shortest path for a network with negative edges is NP-hard.

Some functions from graph algorithms

A B a => B implication example
1 easy new B lower bound sorting => emst
2 easy tractable none Tc => APSP (+)
3 easy intractable none SSSP (DAG) => SSSP (±)
4 easy unknown none
5 tractable easy a easy
6 tractable new a solution DC (+) => SSSP (DAG)
7. tractable intractable none
8 tractable unknown none
9 intractable easy profound
10 intractable tractable profound
11 intractable same as 1 or 6 sslp () => SSSP (±)
12 intractable unknown B intractable HP => SSSP (±)
13 unknown easy a easy JS => SSSP (DAG)
14 unknown tractable A tractable
15 unknown intractable a solvable
16 unknown same as 1 or 6 jswd => SSSP (±)

Key:
Emst Euclidean Minimum Spanning Tree
TC Transitive Closure
APSP all-pairs shortest paths
SSSP single-source shortest paths
Sslp single-source longest paths
(+) (In networks with Nonnegative Weights)
(±) (In networks with weights that cocould be negative)
(DAG) (IN acyclic networks)
DC difference Constraints
HP Hamilton paths
JS (WD) Job Scheduling (with deadlines)

Shortest Path with negative edge
It is not terrible to have a negative edge. What is terrible is that there is a negative ring. Therefore, we add a restriction for the currently required Shortest Path-no negative ring. Raise the following three questions:

  • Finding the shortest path in a network without negative Loops
  • Negative ring Detection
  • Arbitrage transaction problems (PKU has two exercises: 1238 2240)

Looking back at the Dijkstra method, it cannot deal with the negative side, because Dijkstra, as greedy, lost the optimal sub-structure in the face of the negative side, leading to algorithm failure.
Floyd can still cope with the negative edge network, and Floyd can detect the negative ring in the complexity of V3.

The alternative to Dijkstra is the Bellman-Ford algorithm, which can detect the negative ring and find the shortest path of a single source point under the complexity ve. You can find specific algorithms by searching for them.

Reweighting does not affect the shortest path!
How exciting it is. If the re-assignment of edges does not affect the shortest path, we can eliminate all negative edges, so that we will not return to the network without negative edges.
First, we will think of a new edge weight algorithm that adds a constant value to each edge, but it turns out that this method does not work. A simple counterexample is that a and B have two paths, one passing through a vertex long A and the other passing through two long B (<). After the constant C is added to the edge, the total length of the previous path increases C, followed by 2C, if C> A-B, then the shortest path will change from the following to the previous one. The shortest path is significantly affected.

The correct re-authorization method is to add a difference between the points at both ends of each edge after Bellman-Ford of any vertex is calculated. The principle is to use the relativity of any point to maintain some of the features of the right. I will not be arrogant about the specifics. I will find out after careful derivation.
The re-authorization method is as follows. Wt is the shortest path of a Single-Source Vertex after a bellman-Ford operation, and edge. W is the other end of the edge:
For each vertex v:
For all edge edges starting from vertex v:
Edge. Weight = edge. Weight + wt [v]-wt [edge. W];

Because the edge weight is changed, all operations on comparing edge size will fail! The remedy is to change the permission back after calculating the shortest path. In addition, this method still cannot solve the negative ring problem.

Now that we have the method of re-granting values, we have a new algorithm (Johnson's algorithm) to solve the shortest path of all vertices ):

  • Run the Bellman-Ford algorithm on the source point (or any point) of the network.
  • If a negative ring is detected, it is stopped.
  • Network re-Authorization Value
  • Use Dijkstra algorithm to find the shortest path of all vertices.

The complexity of the Johnson algorithm is velogdv, D = E/V. If e is less than 2 V, D = 2.

Finally, for a network without negative edges, the shortest path of a single source node is simpler than that of a single source node, and the shortest path of a single source node is simpler than that of all vertices. In the case of a negative edge, the complexity of the three problems is the same in the worst case.

Link:

PKU 1238:
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1238

PKU 2240:
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2240

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.