[英]Maximum weighted path between two vertices in a directed acyclic Graph
Love some guidance on this problem: 喜欢这个问题的一些指导:
G is a directed acyclic graph. G是有向无环图。 You want to move from vertex c to vertex z.
您想要从顶点c移动到顶点z。 Some edges reduce your profit and some increase your profit.
一些边缘会降低您的利润,一些会增加您的利润。 How do you get from c to z while maximizing your profit.
如何在最大化利润的同时从c到z。 What is the time complexity?
什么是时间复杂度?
Thanks! 谢谢!
The problem has an optimal substructure. 该问题具有最佳子结构。 To find the longest path from vertex
c
to vertex z
, we first need to find the longest path from c
to all the predecessors of z
. 为了找到从顶点
c
到顶点z
的最长路径,我们首先需要找到从c
到z
所有前驱的最长路径。 Each problem of these is another smaller subproblem (longest path from c
to a specific predecessor). 这些问题的另一个问题是另一个较小的子问题(从
c
到特定前任的最长路径)。
Lets denote the predecessors of z
as u1,u2,...,uk
and dist[z]
to be the longest path from c
to z
then dist[z]=max(dist[ui]+w(ui,z))
.. 让我们将
z
的前身表示为u1,u2,...,uk
和dist[z]
为从c
到z
的最长路径然后dist[z]=max(dist[ui]+w(ui,z))
..
Here is an illustration with 3 predecessors omitting the edge set weights: 下面是3个前辈省略边集权重的说明:
So to find the longest path to z
we first need to find the longest path to its predecessors and take the maximum over (their values plus their edges weights to z
). 因此,要找到
z
的最长路径,我们首先需要找到其前任的最长路径并取最大值(它们的值加上它们的边缘权重到z
)。
This requires whenever we visit a vertex u
, all of u
's predecessors must have been analyzed and computed. 这要求每当我们访问顶点
u
,必须对所有u
的前辈进行分析和计算。
So the question is: for any vertex u
, how to make sure that once we set dist[u]
, dist[u]
will never be changed later on? 所以问题是:对于任何顶点
u
,如何确保一旦我们设置dist[u]
, dist[u]
以后永远不会改变? Put it in another way: how to make sure that we have considered all paths from c
to u
before considering any edge originating at u
? 换句话说:如何确保在考虑任何来自
u
边缘之前我们已经考虑了从c
到u
所有路径?
Since the graph is acyclic, we can guarantee this condition by finding a topological sort over the graph. 由于图是非循环的,我们可以通过在图上找到拓扑排序来保证这种情况。 topological sort is like a chain of vertices where all edges point left to right.
拓扑排序就像一个顶点链,所有边都从左到右指向。 So if we are at vertex
vi
then we have considered all paths leading to vi
and have the final value of dist[vi]
. 因此,如果我们在顶点
vi
那么我们已经考虑了所有通向vi
路径并且具有dist[vi]
的最终值。
The time complexity: topological sort takes O(V+E)
. 时间复杂度:拓扑排序需要
O(V+E)
。 In the worst case where z
is a leaf and all other vertices point to it, we will visit all the graph edges which gives O(V+E)
. 在最糟糕的情况下,
z
是一个叶子而所有其他顶点都指向它,我们将访问所有图形边缘,给出O(V+E)
。
Let f(u) be the maximum profit you can get going from c to u in your DAG. 设f(u)是您在DAG中从c到u的最大利润。 Then you want to compute f(z) .
然后你想要计算f(z) 。 This can be easily computed in linear time using dynamic programming/topological sorting.
这可以使用动态编程/拓扑排序在线性时间内轻松计算。
Initialize f(u) = -infinity for every u other than c , and f(c) = 0 . 对c以外的每个u初始化f(u)= -infinity,并且f(c)= 0 。 Then, proceed computing the values of f in some topological order of your DAG.
然后,继续以DAG的某些拓扑顺序计算f的值。 Thus, as the order is topological, for every incoming edge of the node being computed, the other endpoints are calculated, so just pick the maximum possible value for this node, ie f(u) = max( f(v) + cost(v, u) ) for each incoming edge (v, u) .
因此,由于顺序是拓扑的,对于正在计算的节点的每个进入边缘,计算其他端点,因此只需选择该节点的最大可能值,即f(u)= max( f(v)+ cost( v,u) )每个进入边缘(v,u) 。
Its better to use Topological Sorting instead of Bellman Ford since its DAG. 自DAG以来,最好使用Topological Sorting而不是Bellman Ford 。
Source:- http://www.utdallas.edu/~sizheng/CS4349.d/l-notes.d/L17.pdf 资料来源: - http://www.utdallas.edu/~sizheng/CS4349.d/l-notes.d/L17.pdf
EDIT:- 编辑:-
G is a DAG with negative edges. G是具有负边缘的DAG。
Some edges reduce your profit and some increase your profit
一些边缘会降低您的利润,一些会增加您的利润
After TS, for each vertex U in TS order - relax each outgoing edge. 在TS之后,对于TS顺序中的每个顶点U,放松每个输出边缘。
dist[] = {-INF, -INF, ….}
dist[c] = 0 // source
for every vertex u in topological order
if (u == z) break; // dest vertex
for every adjacent vertex v of u
if (dist[v] < (dist[u] + weight(u, v))) // < for longest path = max profit
dist[v] = dist[u] + weight(u, v)
ans = dist[z];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.