Data structure: can be used to find the shortest path of the method to find the longest path? Why is it?
Here the general method of solving the shortest path has Dijkstra algorithm and Floyd-warshall algorithm, Dijkstra algorithm does not allow the edge of the weight is negative, also does not allow the loop, and the Floyd-warshall algorithm can allow the edge of the weight is negative, but does not allow negative edges to form a loop, That can solve a graph with a loop
They all have limitations, the idea of these two algorithms can be used to find the longest path??
Why can't you?
(Thanks to the kind people who gave me the answer: from http://www.zhihu.com/question/27201255 and CSDN)
Here's a detailed explanation:
1)No, the core is that the shortest path problem is the optimal sub-structure, that is, "the shortest path of the subpath or the shortest", and the longest paths do not exist this substructure. The longest path here is the longest simple path between two nodes (the path does not loop). Consider the following example of the algorithm introduction: There are two longest paths withQto theT:Q–> R–> Tand theQ–> S-> T. Unlike shortest paths, these paths do not have the best sub-attributes for the longest. For example, the longest pathq-> r-> Tnot byQ->rand ther->tthe combinationbecause the longest path is fromQtoRto beq-> s-> t->r
2) not to. The shortest path problem is p, but the longest path problem is Np-hard:
Proving that the longest path problem is np-hard .
Proof:if the longest path problem has a polynomial-time algorithmA, we proveHamiltonloop problems also have polynomial-time algorithms, which createTuringto the Covenant.
for any one diagramg=<v,e>, use the following algorithm to check if it hasHamiltonCircuit:
assigning weights to all sides1, setVa vertex inV
for allV '∈V,Loop:
if<v,V ' >∈Eandvto theV 'the longest path length of the=| V|-1 (withACalculation)
soGhave aHamiltonCircuit;
End Loop
If allV 'do not meet the above conditions, theGNoHamiltonCircuit;
obviously, if the algorithmAis polynomial time, then the above-mentionedHamiltonThe algorithm of loop problem is also polynomial time, so the longest path problem isNp-hardof the
3) It is certainly not possible to use the Dijkstra algorithm, which is because the idea of the Dijkstra algorithm is to add the closest node to the source point each time, and then update the distance from the other nodes to the source point until all points are added. When you choose the shortest route each time you choose the longest path, there is a problem, that is, there is no guarantee that the current join node will be updated to make the source point of the distance becomes longer, and once the point is selected will no longer be updated. For example, this time to join the node U, the longest road is 10, the next time it is possible to join a node V, so that u through V to the source of the distance greater than 10, but because U has been added to the collection before, can not be updated, resulting in incorrect results. If the inverse uses Dijkstra to find the shortest path, remember, Dijkstra can not calculate the case of negative edges ...
4) You cannot use the Floyd-warshall algorithm to find the longest path between each pair of nodes: Because if the diagram contains a loop (and the loop does not contain negative values), the Floyd algorithm does not determine that it contains a loop, and will find a wrong solution.
Do not shoot bricks, welcome to communicate.
Data structure: Can be used to find the shortest path of the method to find the longest path? Give a detailed answer.