简体   繁体   English

有向加权边缘图及其权重的算法

[英]Algorithm for directed weighted edge graphs and their weights

I have a directed graph with non-negative weights on the edges. 我有一个有向图,边缘上有非负权重。

My algorithm should do the following: 我的算法应执行以下操作:

  • Get all paths from vertex u to vertex v. 获取从顶点u到顶点v的所有路径。
  • Compute the minimum weighted edge on each path from u to v. 计算从u到v的每条路径上的最小加权边。
  • Compute the Maximum of the minimum weighted edges I computed from above. 计算我从上面计算出的最小加权边的最大值。

What algorithm is good for this? 什么算法对此有好处? I ask this because I can just go naively and implement the steps above as I've stated them (brute force). 我之所以这么问,是因为我可以天真地执行上述步骤(蛮力)。

I have a feeling it's a slight modification of Dijkstra's algorithm, but I'm not sure. 我感觉这是对Dijkstra算法的略微修改,但我不确定。 Also, what would the time complexity be? 另外,时间复杂度是多少?

I think we don't need dijkstra here. 我认为我们这里不需要dijkstra。 Suppose we picked the path with desired minmax edge. 假设我们选择了具有所需的最小最大边缘的路径。 It obviously doesn't include edges with lesser weights. 它显然不包括权重较小的边。 So the algorithm is 所以算法是

  • pick K most heavy edges 选择K最重的边缘
  • use dfs/bfs to check if v is reachable from u through this edges ( f(K) is true ) 使用dfs / bfs来检查u是否可以通过此边缘到达vf(K) is true
  • if K1>K2 and f(K2) is true , then obviously f(K1) is true 如果K1>K2并且f(K2) is true ,则显然f(K1) is true
  • so we can just run binary search over K 这样我们就可以对K执行二进制搜索

time complexity is O((N+M) log M) where N - number of vertices and M - number of edges 时间复杂度为O((N+M) log M) ,其中N顶点数和M边数

Any algorithm that actually considers all paths from u to v will take exponential time, because there is an exponential number of such paths - consider a ladder, or a grid of 2xN nodes - to go from one end or the other in the long dimension you have at least N independent choices, so at least 2^N possible paths. 任何真正考虑从u到v的所有路径的算法都将花费指数时间,因为这种路径的数量是指数的-考虑一个梯子或2xN节点的网格-从一端到另一端沿着您的长维度具有至少N个独立的选择,因此至少2 ^ N个可能的路径。

I think you can modify Dijkstra's algorithm to do this. 我认为您可以修改Dijkstra的算法来做到这一点。 There is psuedocode at https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode . https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode中有伪代码。 As a first modification, change line 6 to set all initial distances to zero. 作为第一个修改,更改第6行以将所有初始距离设置为零。 We need to think about invariants, and our first invariant can be that dist[v] is the maximum value for the minimum edge in a path to v, for all paths seen so far. 我们需要考虑不变量,我们的第一个不变量可以是dist [v]是至v的路径中的最小边的最大值,对于到目前为止已看到的所有路径。 As before we set prev[v] to undefined and add v to Q, the queue holding all nodes currently under consideration. 和之前一样,我们将prev [v]设置为undefined并将v添加到Q,该队列保存当前正在考虑的所有节点。 As a final part of the initialisation we set dist[source] = infinity - not 0. You can think of this as a hack, or as a way of defining the minimum edge length in a path of length 0 from v to v that doesn't have any edges. 作为初始化的最后一部分,我们设置dist [source] = infinity-而不是0。您可以将其视为hack,也可以将其定义为从v到v的长度为0的路径中的最小边长度,没有任何优势。

Now we have that Q contains all nodes where the maximum value for the minimum edge is still under consideration, and that this value for all such nodes will only ever increase. 现在,我们拥有Q包含所有仍在考虑最小边的最大值的节点,并且所有此类节点的该值只会不断增加。 Now we remove from Q the node u with the maximum value of dist[u] - this is step 13 modified with min changed to max. 现在我们从Q中删除具有dist [u]最大值的节点u-这是第13步,将min更改为max。 For each neighbor v of u we compute min(dist[v], length(u, v)). 对于u的每个邻居v,我们计算min(dist [v],length(u,v))。 This is a new candidate for the maximum possible minimum edge length along any path from source to u, so if this is larger than dist[u], we set dist[u] to this value. 这是沿从源到u的任何路径上最大可能的最小边长的新候选者,因此,如果它大于dist [u],则将dist [u]设置为该值。 Note that this means that no element u of Q will ever have dist[u] set to a value greater than the value of dist[v] for the element v of Q we have just removed, which was a maximum such value in the queue at that time. 请注意,这意味着Q的元素u都不会将dist [u]的值设置为大于我们刚刚删除的Q的元素v的dist [v]的值,这是队列中的最大值那时候。 This means that once we have removed an element v from the queue its value dist[v] has been calculated for good and cannot be increased by any subsequent calculation. 这意味着,一旦我们从队列中删除了元素v,它的值dist [v]就已经确定好了,以后的任何计算都不能增加。 Beware that we have just changed dist[u] for a node in our queue Q. If Q is implemented with a data structure such as a priority queue, this probably means that the implementation will have to remove u from the priority queue under the old value of dist[u] and then re-insert it under the new value of dist[u]. 请注意,我们刚刚更改了队列Q中某个节点的dist [u]。如果Q是使用诸如优先级队列之类的数据结构实现的,则这可能意味着该实现将必须在旧版本的优先级队列中删除u值dist [u],然后在dist [u]的新值下重新插入。

Correctness follows from the invariants - at each time we have dist[v] the maximum value of minimum edge along every path considered so far, and when we remove v from the queue we know - because it has a maximum value for everything still in the queue - that none of the other paths we could consider can possibly change that value. 正确性来自于不变量-每次使dist [v]沿到目前为止所考虑的每条路径的最小边的最大值dist,并且当我们从队列中删除v时,我们都知道-因为它对于仍然存在于队列中的所有对象都具有最大值队列-我们可以考虑的其他路径都无法更改该值。

The time taken to do this is the same as Dijkstra, and for the same reason. 出于相同的原因,执行此操作所需的时间与Dijkstra相同。 We enter every element into the queue once, in the beginning, and we remove every element from the queue once. 首先,我们将每个元素输入队列一次,然后将每个元素从队列中删除一次。 This tells us how many times we execute the remove operation in line 14. 这告诉我们在第14行中执行了多少次删除操作。

(Maximum of the minimum weighted edges seems an odd thing to calculate - is there an interesting application here, or is this an artificial exercise to get you to think about Dijkstra's algorithm?) (要计算最小加权边的最大值似乎很奇怪-这里是否有有趣的应用程序,还是让您考虑Dijkstra算法的人工练习?)

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

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