简体   繁体   English

有向非循环图中两个顶点之间的最大加权路径

[英]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的最长路径,我们首先需要找到从cz所有前驱的最长路径。 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,...,ukdist[z]为从cz的最长路径然后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边缘之前我们已经考虑了从cu所有路径?

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中从cu的最大利润。 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 一些边缘会降低您的利润,一些会增加您的利润

  • Edges - increase profit - positive value 边缘 - 增加利润 - 正值
  • Edges - decrease profit - negative value 边缘 - 减少利润 - 负值

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.

相关问题 检查有向无环图中两个顶点之间是否存在路径 - 查询 - Check if there exist a path between two vertices in directed acyclic graph - queries 在未知大小的加权有向图上,一个人如何遍历两个顶点之间从最短到最长的所有可能的非循环路径? - On a weighted directed graph of unknown size, how can one iterate over all possible acyclic paths between two vertices from shortest to longest? 拓扑排序的加权有向非循环图上的最长路径搜索,但有效路径的最大边数 - Longest path search on a topologically sorted weighted directed acyclic graph, but with a maximum edge count for valid paths 在有向无环加权图中找到前3条最长路径 - Finding top 3 longest path in directed acyclic weighted graph 正加权有向无环图中的k边最短路径 - k-Edge Shortest Path in Positive Weighted Directed Acyclic Graph 连接有向无环加权图中的两个随机节点 - Connecting Two Random Nodes in a Directed Acyclic Weighted Graph 在有向加权图中找到两个节点之间的最短路径 - Find shortest path between two nodes in directed weighted graph 具有所需顶点的有向加权图 - Directed Weighted Graph with required vertices 在有向完整加权图中访问所有顶点的最短路径 - Shortest path that visits all vertices in a directed complete weighted graph 最多使用 k 个顶点的有向加权图中的最短路径 - Shortest path in a directed weighted graph that uses at most k vertices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM