简体   繁体   English

恢复时间最短的路径

[英]Path with the shortest recovery time

The problem can be described in the following way: 问题可以通过以下方式描述:

A network of nodes has crashed, and each connection (edge) has a certain recovery time until it's back online and the two nodes are connected again. 节点网络已崩溃,每个连接(边缘)都有一定的恢复时间,直到它重新联机并再次连接两个节点。 Our goal is to find the path between nodes 1 to n that is up and running the earliest, and return the longest recovery time on that path. 我们的目标是找到最早运行的节点1到n之间的路径,并在该路径上返回最长的恢复时间。

The network can be represented as a graph with undirected edges. 网络可以表示为具有无向边的图。

We have three arrays: 我们有三个数组:

  1. First one contains the origin vertices 第一个包含原点顶点
  2. Second one contains the destination vertices 第二个包含目标顶点
  3. Third one contains the recovery time of each connection (edge) 第三个包含每个连接的恢复时间(边缘)

Example: 例:

{1,2,2,3}, {2,3,4,4}, {1,5,10,2} {1,2,2,3},{2,3,4,4},{1,5,10,2}

where the recovery time of the connection between nodes 1 and 2 is 1, etc.. 其中节点1和2之间的连接恢复时间为1,等等。

The optimal path from 1 to n = 4 is 1-2-3-4, since the longest recovery time on this path is 5, in comparison to the path 1-2-4, where the longest recovery time is 10. 从1到n = 4的最佳路径是1-2-3-4,因为与路径1-2-4相比,该路径上的最长恢复时间是5,其中最长恢复时间是10。

The important thing here is to notice, that only the longest recovery time on each path is what matters, ie the "length" of the path is not the sum of the waiting periods, but the length of the longest time one has to wait for the connection between two nodes to recover. 这里重要的是要注意,每条路径上最长的恢复时间才是最重要的,即路径的“长度”不是等待时间的总和,而是需要等待的最长时间的长度。两个节点之间的连接要恢复。 Each recovery time is calculated from t = 0, so the recovery times are independent, and the order does not matter. 每个恢复时间从t = 0计算,因此恢复时间是独立的,顺序无关紧要。

So what we have to do here is to find the path that has the minimum recovery time out of the maximum recovery times on each path, and return that time. 所以我们要做的就是找到每个路径上恢复时间最短的路径,并返回该时间。

I have approached this problem using both Dijkstra's and Bellman-Ford -algorithm, but can't really wrap my head around on how to modify the algorithms to get the desired outcome. 我已经使用Dijkstra和Bellman-Ford算法解决了这个问题,但是无法真正解决如何修改算法以获得理想结果的问题。 There can be at most 10^5 connections. 最多可以有10 ^ 5个连接。

This is easy using DSU ( https://en.wikipedia.org/wiki/Disjoint-set_data_structure ): 使用DSU( https://en.wikipedia.org/wiki/Disjoint-set_data_structure )很容易:

  1. Sort all edges by weight, increasing 按重量对所有边缘进行排序,增加
  2. Loop throught edges, if nodes u and v are in different disjoint sets, merge them into one disjoint set 循环遍历边,如果节点u和v在不同的不相交集中,则将它们合并为一个不相交集
  3. If after merge nodes 1 and N are in same set, asnwer is weight of current edge and we can break out of loop. 如果合并后节点1和N在同一组中,则asnwer是当前边缘的权重,我们可以突破循环。

Complexity: O(E log E + V log V) 复杂性:O(E log E + V log V)

DSU is the most beautiful solution, as described by Photon. 如Photon所述,DSU是最美丽的解决方案。

Another possible solution uses binary-search + dfs/bfs/Dijkstra/Bellman-Ford/ 另一种可能的解决方案是使用二进制搜索+ dfs / bfs / Dijkstra / Bellman-Ford /

This algorithm will run DFS/BFS at most log( max possible edge cost ). 该算法将以最多log(最大可能的边缘成本)运行DFS / BFS。

The algorithm would work as follows: 该算法将如下工作:

lo = 0, hi = largest cost from any edge from a graph
mid = dummy_value

while ( lo < hi ):
    mid = (lo + hi) / 2
    check if there is a path from source to destination using only edges with cost <= mid
    if there is a path:
        hi = mid
    else:
        lo = mid + 1

return mid

The solution using DSU has a better runtime complexity, but this introduces the idea of doing binary search on the answer which is a classical idea in problem solving. 使用DSU的解决方案具有更好的运行时复杂性,但这引入了对答案进行二元搜索的想法,这是解决问题的经典理念。 In some problems, doing DSU is not a possibility. 在某些问题中,做DSU是不可能的。

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

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