简体   繁体   中英

Find path with maximum cost difference of edges in directed graph in linear-time

Having a directed graph G with edges having positive weights, we define the cost of a path the difference between the edge with minimum weight and maximum weight. The goal is to find a path with maximum cost among other paths. We don't need to return the path itself just the maximum cost.

If x is the number of vertices and y is the number of edges then x < 10^5 and y < min(x^2,10^5) . The time limit is 1 second.

Based on the bounds I think this problem has to be solved in O(x+y) time.

So based on the problems properties ans some studying of other algorithms I thought that on possible answer may contain multiple DFS 's (like the SCC problem). I also find a similar problem with Dijkstra's algorithm that can be modified for this problem but I'm not very sure that the complexity would be OK. Also there are some other questions similar to mine however the answers were of O(m^2) or higher which were not very efficient.

Other than what was mentioned above I have no idea to solve the problem and some insight would be very helpful. Thanks

Solution: Due to the fact that our graph can have cycles, this complicates things for us. Let's build a new graph in which we "compress" all the components of a strong connection into a single vertex, so new graph will be acyclic. During this process let's memorize maximum( maximumWeight[v] ) and minimum( minimumWeigh[v] ) weights of the edges for each strongly connected components. We spent O(|V| + |E|) time for it, where |V| is count of vertexes and |E| is count if edges.

After that let's make a topological sorting of given graph in O(|V|+|E|) time complexity. Then let's calculate simple DP.

dpMax[v] - the maximum weight of the edge to which there is a path from this vertex. dpMin[v] - the minimum weight of the edge to which there is a path from this vertex.

Default value: If vertex v is strongly connected component, then maximum and minimum weights already calculates in maximumWeight[v] and minimumWeigh[v] , so dpMax[v] = maximumWeight[v] and dpMin[v] = minimumWeight[v] . Otherwise maximumWeight[v] = -Infinity and minimumWeigh[v] = Infinity .

In topological order let's improve our DP for each vertex:

int answer = -Infinity;
for(auto v : vertexesInTopologicalOrder) {
    int newMinDp = dpMin[v], newMaxDp = dpMax[v];
    for(auto e : v.outgoingEdges) {
        int l = dpMin[v];
        int r = dpMax[v];
        l = min(l, e.weight);
        r = max(r, e.weight);
        l = min(l, dpMin[e.target]);
        r = max(r, dpMax[e.target]);
        answer = max(answer, e.weight - l);
        answer = max(answer, r - e.weight);
        newMinDp = min(newMinDp, l);
        newMaxDp = max(newMaxDp, r);
    }
    dpMin[v] = newMinDp;
    dpMax[v] = newMaxDp;
}

Since we calculating DP in topological order, the DP at achievable vertexes have already been calculated.

For a better understanding, let's look at an example.

Let's say we have such an initial graph. 假设我们有这样一个初始图。

After compression, the graph will look something like this. 压缩后,图形将如下所示。

After topological sorting, we get this order of vertices. (Green numbers) 经过拓扑排序后,我们得到了这个顶点顺序。 (绿色数字)

Sequentially calculate all DP values, along with updating the answer. 依次计算所有 DP 值,并更新答案。

Final answer 6 will be achieved during calculating top vertex DP.

I hope it turned out to be quite detailed and clear. Final time complexity is O(|V|+|E|) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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