简体   繁体   English

为什么在图中找到最长路径是 NP-hard

[英]Why finding the longest path in a graph is NP-hard

This link mentions:这个链接提到:

A longest path between two given vertices s and t in a weighted graph G is the same thing as a shortest path in a graph −G derived from G by changing every weight to its negation.加权图 G 中两个给定顶点 s 和 t 之间的最长路径与图 -G 中通过将每个权重变为其否定而从 G 导出的最短路径相同。 Therefore, if shortest paths can be found in −G, then longest paths can also be found in G.因此,如果在-G中可以找到最短路径,那么在G中也可以找到最长路径。

So why is finding the longest path an NP-hard problem if this transformation can reduce it to the shortest path problem?那么,如果这种转换可以将其简化为最短路径问题,那么为什么找到最长路径是一个 NP-hard 问题呢?

After the transformation, we have have these cases:改造后,我们有这些情况:

  • -G has a negative cycle, in which case the shortest path in -G cannot be found -G有一个负循环,在这种情况下-G的最短路径找不到
  • -G does not have a negative cycle, in which case Floy-Warshall or Bellman-Ford algorithm can find the shortest path in -G in polynomial time -G没有负循环,在这种情况下,Floy-Warshall 或 Bellman-Ford 算法可以在多项式时间内找到-G中的最短路径

Questions:问题:

  1. Is it correct to say if there is no negative cycle, the problem of finding longest path is not NP-hard?说如果没有负循环,找到最长路径的问题不是NP-hard的问题对吗?

  2. Is it correct to say in the presence of negative cycles, there is still a longest simple path between nodes which is NP-hard to be found?说在存在负循环的情况下,节点之间仍然存在最长的simple path是 NP 难找到的,是否正确?

  3. If so, is it more accurate to say finding the longest simple path in a graph is NP-hard?如果是这样,说在图中找到最长的简单路径是 NP-hard 是否更准确?

  4. If so, because of -G transformation is it also correct to say that finding the shortest simple path in a graph is also NP-hard?如果是这样,由于-G变换,说在图中找到最短的简单路径也是 NP-hard 也是正确的吗?

Edit编辑

This link explains the confusion about longest path problem in more details: https://hackernoon.com/shortest-and-longest-path-algorithms-job-interview-cheatsheet-2adc8e18869此链接更详细地解释了有关最长路径问题的混淆: https : //hackernoon.com/shortest-and-longest-path-algorithms-job-interview-cheatsheet-2adc8e18869

The confusion here is that the Longest Path Problem generally asks for the longest simple path, ie, the longest path without repeated vertices.这里的混淆是最长路径问题通常要求最长的简单路径,即没有重复顶点的最长路径。 For this reason, it can be reduced to the Hamiltonian Path problem, which is known to be NP-hard.出于这个原因,它可以简化为哈密顿路径问题,这是已知的 NP-hard。

Bellman-Ford and similar algorithms, on the other hand, compute the shortest path in a graph (note: without simple ), ie vertices can be repeated.另一方面,Bellman-Ford 和类似算法计算图中的最短路径(注意:没有simple ),即顶点可以重复。

So your four questions:所以你的四个问题:

  1. No. If there is a negative cycle in -G , a longest path does not exist at all in G , because you can just continue walking around the cycle forever.否。如果有一个负循环-G ,最长路径完全不存在中G ,因为你可以永远继续走动周期。 A longest simple path might still exist, but with or without negative cycle, the problem can be reduced to Hamiltonian Path and is therefore still NP-hard.最长的simple路径可能仍然存在,但无论有没有负循环,问题都可以简化为哈密顿路径,因此仍然是 NP-hard。
  2. Indeed!的确! (If the graph is undirected.) (如果图是无向图。)
  3. Yup是的
  4. Yup是的
  • First, Longest-path problem is in general NP-Hard (in fact NP-Complete) even when there is no negative edges.首先,即使没有负边缘,最长路径问题通常也是 NP-Hard(实际上是 NP-Complete)。 I have tried to prove it from basics, here ;我试图从基础来证明它,这里; also for the proof of NP-complete, you might like to check this one .也为NP完全的证明,你可能想检查这一个
  • Second, for various categories of Graphs, scientists have come up with polynomial time solution to this.其次,对于各种类型的图,科学家们已经提出了多项式时间解决方案。 DAG, and tree are two such kinds. DAG 和树就是这样的两种。
  • Third, for DAG, while one option is to find shortest distance in a transformed graph, -G (this is mentioned in Sedgewick book, 4th Ed), there is also another alternate, see here .第三,对于 DAG,虽然一种选择是在转换图中找到最短距离 -G(这在 Sedgewick 书籍,第 4 版中提到),但还有另一种替代方法,请参见此处 Both run in theta(E+V) time.两者都在 theta(E+V) 时间内运行。
  • Finally, for answers to your questions 1 thru 3, I'll stick to Berthur's answer .最后,对于您的问题 1 到 3的答案,我将坚持Berthur 的回答 But for your question 4, note that we do not do a transformation like -G, for any arbitrary graph G. If we at all do so, then it is a DAG.但是对于您的问题 4,请注意我们不会对任何任意图 G 进行类似 -G 的转换。如果我们这样做,那么它就是 DAG。 In fact, we often prove longest-path problem as NPC by referring to Hamiltonian path - which has nothing to do with weights, let alone negative weights.事实上,我们经常通过引用哈密顿路径来证明最长路径问题作为NPC——这与权重无关,更不用说负权重了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM