【问题标题】:Minimal time to notify all nodes of network通知网络所有节点的最短时间
【发布时间】:2018-12-09 23:33:14
【问题描述】:

问题:输入是无向图,边上有权重,它表示计算机网络,其中边的权重意味着在两台计算机之间发送消息所需的最短时间。我们选择该图的一个顶点并向连接的顶点发送消息。当顶点收到消息时,它只会将其重新发送给邻居一次。需要找到最短的时间来通知图中的每个顶点。

我已经实现了蛮力算法来解决这个问题,但它太慢了(N^2)。首先,我认为这可以通过最小生成树的权重来解决,但它需要一些东西。

我认为这个问题有一些现有的算法......

【问题讨论】:

标签: algorithm graph


【解决方案1】:

应为此目的修改dijkstras algorithm。但是,该算法不是搜索到特定节点的最短路径,而是在访问完所有节点后终止。完成上述操作后,问题就归结为从遍历图时建立的表中提取与源节点距离最大的节点。

longest_path(node):
    # dijkstras algo
    path_len = map()
    queue = priority_queue()  # priority-queue sorted by distance to source-node
    visited = set() 

    path_len[node] = 0
    queue.put(node)       

    while queue not empty
        n = queue.pop()

        if n in visited
            continue

        for neighbor in n.neighbors()
            dist = path_len[n] + n.edge_length(neighbor)
            if neighbor not in path_len or path_len[neighbor] > dist
                queue.offer(neighbor)
                path_len[neighbor] = dist

         visited.add(n)

     # find the node with the maximum distance to the source-node
     max = -1
     res = NULL
     for n in path_len.keys()
         if path_len[n] > max
             max = path_len[n]
             res = n

     return res

上述算法在O(|E| + |V| log |V|)最坏的情况下工作。

【讨论】:

  • 您对该算法的复杂度估计是否包括您在此处使用的地图、集合和优先级队列上的操作复杂度?
  • @MishKotelkov 当然。这只是dijkstras算法的复杂性。使用这些数据结构的标准复杂性。您可以在我在答案中提供的链接的维基百科文章中查找它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2015-05-17
相关资源
最近更新 更多