简体   繁体   English

在动态图中找到最短路径

[英]Find shortest path in dynamic graph

Let's see our example input: 让我们看一下示例输入:

10.01.2013 CREATE road between 1 and 2 with maximum speed 90 and distance 120 25.03.2013 CREATE road between 1 and 4 with maximum speed 110 and distance 25 13.07.2013 CREATE road between 2 and 3 with maximum speed 160 and distance 320 19.07.2013 MODIFY road between 1 and 4 with maximum speed 120 01.11.2013 CREATE road between 1 and 3 with maximum speed 30 and distance 34 21.11.2013 MODIFY road between 2 and 3 with maximum speed 130 30.12.2013 CREATE road between 2 and 4 with maximum speed 80 and distance 120

When we MODIFY graph, the speed can be only improved. 当我们修改图时,只能提高速度。

Ok, and I need to answer for questions. 好的,我需要回答问题。 For instance: 例如:
When time travel between 1 and 4 will be shorter than 20 minutes When time travel between 2 and 4 will be shorter than 70 minutes

Maximum number of queries is 10. 最多查询数量为10。

This is example, random data only for explain my problem. 这是示例,随机数据仅用于解释我的问题。

I answer for this question using dijkstra algorithm, but I'm running Dijkstra for every query in every modification of graph. 我使用dijkstra算法回答了这个问题,但是我正在对图的每次修改中的每个查询运行Dijkstra。 So if I have 10 000 modifications and 10 queries my solution will run 100 000 times Dijkstra algorithm. 因此,如果我有10 000修改和10查询,我的解决方案将运行100 000次Dijkstra算法。 I know that's bad, but for this time I can't get better solution, so I'm writing here for help. 我知道那很糟糕,但是这次我无法获得更好的解决方案,因此我在这里写信寻求帮助。 I can add that maximum number of vertex is 10 000 and maximum number of edges is 100 000. 我可以补充一点,最大顶点数为10 000,最大边数为100 000。

Since graph modifications can only improve the time of travel from point A to point B, the following observation is true: 由于图形修改只能改善从A点到B点的旅行时间,因此以下观察是正确的:

For modifications m 1 and m 2 such that m 1 is earlier than m 2 and travel times between the same points A and B, T 1 and T 2 , it is true that T 1 >= T 2 对于修改m 1m 2以使m 1早于m 2以及在相同点A和B, T 1T 2之间的传播时间,确实T 1 > = T 2

This observation lets us use divide and conquer strategy (essentially, a binary search) for answering queries: pick a midpoint, find a travel time using Dijkstra's algorithm, and go right or left of the midpoint depending on the result. 该观察结果使我们能够使用分而治之的策略 (本质上是二进制搜索)来回答查询:选择一个中点,使用Dijkstra的算法找到行进时间,然后根据结果在中点的右边或左边移动。

Now you need to solve a problem of finding a path in a graph constructed up to a certain modification point. 现在,您需要解决一个问题,即在直到特定修改点的图形中查找路径。 You can do it by augmenting each edge with a list of {time, speed} pairs, and get the speed by lookup in a sorted map attached to that edge. 您可以通过添加{time, speed}对列表来扩展每个边缘,并通过在连接到该边缘的排序映射中查找来获得速度。

This gives you O(q * log 2 m) runs of Dijkstra algorithm, where m is the number of modifications, and q is the number of queries. 这使您获得Dijkstra算法的O(q * log 2 m)次运行,其中m是修改的数量,q是查询的数量。

You are almost there, you need just a few optimizations. 您快要准备好了,您只需要进行一些优化即可。 The main point here I think is that the paths can only get shorter. 我认为这里的要点是路径只会变短。 So for a query the answer is the first time when it's satisified. 因此,对于查询来说,答案是第一次满足。 So here's the modification I suggest. 所以这是我建议的修改。

  1. You run Dijkstra on the initial graph like before. 您像以前一样在初始图上运行Dijkstra。
  2. Sort the updates chronologically. 按时间顺序对更新进行排序。
  3. Check if the condition is satisfied and stop if it is. 检查条件是否满足,如果满足则停止。
  4. Pick the next update, see if you can use it to improve the minimum distance to either of the nodes using the new path. 选择下一个更新,看看是否可以使用它来使用新路径改善到两个节点的最小距离。
  5. If no improvement go to 4 如果没有改善,请执行4
  6. If there's an improvement, push the updated node into the Dijkstra priority queue and continue running dijkstra until you empty the queue again. 如果有改进,请将更新的节点推入Dijkstra优先级队列,然后继续运行dijkstra,直到再次清空队列。
  7. Go to 3 and continue. 转到3并继续。

Imagine that nodes of your graph is not just numbers, but numbers with dates and links also have dates. 想象一下,图形的节点不仅是数字,而且带有日期和链接的数字也具有日期。 Then search thru this modified graph. 然后通过此修改后的图进行搜索。

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

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