简体   繁体   English

给定有一个负边(u,v)的有向加权图,找到最短路径(s,t)

[英]Given directed weighted graph that has one negeative edge (u,v), find shortest path (s,t)

Given: Directed, weighted graph G(V,E) and s,t are vertices of V , all edges are positive except the edge (u,v) which is negative. 给定:有向加权图G(V,E)和s,tV顶点,除边(u,v)为负之外,所有边均为正。

Problem: Find the shortest path ( Meaning: with the least weight ) from s to t. 问题:找到从s到t的最短路径( 含义:权重最小 )。

My solution: Well, because we have a negative edge we should use Bellman-Ford's algorithm. 我的解决方案:嗯,因为我们有一个消极的边缘,所以我们应该使用Bellman-Ford算法。 Doing n-1 "relax" on the edges, and if there's a problem in the n 'th iteration there's a cycle - thus return false. 在边缘执行n-1 “松弛”,如果在第n个迭代中存在问题,则存在一个循环-因此返回false。 otherwise, return the shortest path from s to t . 否则,返回从st的最短路径。

Another solution: Using Dijkstra, and changing it a bit by saving d(u) and passing on the negative edge (u,v) and doing "relax", then we go again on all the edges without the negative edge, if the distances were changed, we have a cycle, otherwise all good and return the shortest path. 另一个解决方案:使用Dijkstra,并通过保存d(u)并在负边缘(u,v)上传递并进行“松弛”来对其进行一些更改,然后在没有负边缘的情况下再次在所有边缘上移动被改变,我们有一个循环,否则一切都好,返回最短的路径。

Note: I'm obviously not sure about my solution, the fact that there is one negative edge is tricky, any ideas? 注意:我显然不确定我的解决方案,因为存在一个负面影响这一事实很棘手,有什么想法吗?

Note2: To make it interesting, you cannot use Bellman-Ford . 注意2:为了使它有趣, 您不能使用Bellman-Ford

You can use the information that there is a single negative edge in order to create an efficient algorithm. 您可以使用存在单个负边缘的信息来创建有效的算法。

Let's denote as a the source node of the negative edge, and b the destination node, so we have the negative edge a -> b . 让我们表示作为a下降沿的源节点,和b目的节点,所以我们有下降沿a -> b

There are two kinds of paths from node s to node t : 从节点s到节点t的路径有两种:

  1. the edge a->b is not part of the path; 边缘a->b不是路径的一部分;
  2. the edge a->b is part of the path. 边缘a->b是路径的一部分。

We intend to find the shortest path from s to t , and we will do so by finding the shortest path of each of the above two types. 我们打算找到从st的最短路径,我们将通过找到上述两种类型中的每一种的最短路径来做到这一点。


The minimum path along the ones of type (1) can be found by simply applying Dijkstra's algorithm on the modified graph where the negative edge is removed. 沿着类型(1)的最小路径可以通过简单地将Dijkstra算法应用到删除了负边缘的修改图形上来找到。


For type (2), we are now interested in the shortest path from s to t that contains the edge a->b . 对于类型(2),我们现在对从st的最短路径感兴趣,该路径包含边a->b This path has to be of this form: s -> ... -> a -> b -> ... -> t . 该路径必须采用以下形式: s -> ... -> a -> b -> ... -> t

If we do not have negative cycles in the graph, then the edge a->b should appear only once in the shortest path (please see the final part of this answer for a discussion regarding negative cycles). 如果图中没有负循环,则边a->b应该在最短路径中仅出现一次(请参见此答案的最后部分,以讨论负循环)。

In this situation (no negative cycles), we can apply Dijkstra's algorithm twice, first to find the shortest path from s to a ; 在这种情况下(没有负循环),我们可以应用Dijkstra算法两次,第一次找到最短路径sa ; and then to find the shortest path from b to t . 然后找到从bt的最短路径。 In both cases Dijkstra's algorithm should be applied on the graph modified by removing the negative edge a->b . 在这两种情况下,都应将Dijkstra算法应用于通过删除负边a->b修改的图。


Regarding negative cycles. 关于负循环。 If there is a negative cycle, ie a path that starts and ends in the same node and has negative cost, and that node is on a path from s to t , then a shortest path from s to t does not exist. 如果有开始和在同一节点中结束,并且具有负成本的负周期, 路径,该节点是从路径上st ,然后从最短路径st不存在。 Indeed, the cost from s to t can be made arbitrarily small in this case, by including the negative cost cycle multiple times. 实际上,在这种情况下,通过多次包含负成本周期,可以使st的成本任意减小。

However, it is possible to determine if there is such a cycle in the graph, using again Dijkstra's algorithm. 但是,可以再次使用Dijkstra算法确定图形中是否存在这样的循环。 Note that since there is a single negative edge, a->b , the negative cycle needs to contain it. 请注意,由于存在单个负边缘a->b ,因此负循环需要包含它。 Thus we need to have a path from b to a whose total cost is smaller than the absolute value of the weight of a->b . 因此,我们需要一条从ba的路径,其总成本小于a->b的权重的绝对值。 We can check whether there is such a path by applying Dijkstra's algorithm, with starting node b and destination a . 我们可以通过应用Dijkstra的算法(起始节点为b ,目的地为a来检查是否存在这样的路径。 Again, Dijkstra's algorithm should be applied on the graph with the a->b edge removed (we are not interested in having it in the path), thus all edge weights are positive. 同样,Dijkstra的算法应在删除了a->b边的情况下应用于图形(我们对将其放在路径中不感兴趣),因此所有边权重均为正。

暂无
暂无

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

相关问题 为了找到最短路径,如何在有向加权图中将一个边精确设置为零? - How to set exactly one edge to zero in directed weighted graph in order to find shortest path? 直径为k &lt;| V |的连通加权有向图,找到最短路径 - connected weighted directed graph with diameter k< |V|, find the shortest path 如何在至少访问有向图的一个目标的同时找到从顶点 u 到 v 的最短路径? - How to find the shortest path from vertex u to v while at least visiting one target for directed graph? 正加权有向无环图中的k边最短路径 - k-Edge Shortest Path in Positive Weighted Directed Acyclic Graph 加权有向图中的最短路径 - Shortest path in weighted directed graph 给定无向加权连通图,s,t。 找到从s到t的路径,其最大加权边缘尽可能低 - Given undirected weighted connected graph, s,t. Find path from s to t that its most weighted edge is low as possible 在有向加权图中找到两个节点之间的最短路径 - Find shortest path between two nodes in directed weighted graph 在有向图中,最短路径 st 是否总是包含图的最小边 - In a directed graph, will the shortest path s-t always contain the minimum edge of the graph 如何在线性时间内边缘权重为0或1的有向图中找到最短路径? - How to find shortest path in a directed graph that has edge weights either 0 or 1 in linear time? 找到有向非负加权图的最短路径,避免给定子集顶点的任何顶点彼此相邻? - Find the shortest path of a directed non-negative weighted graph avoiding any vertices of a given subset vertices to be next to each other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM