![](/img/trans.png)
[英]How to check if all nodes in a graph are reachable from all other nodes?
[英]How to choose node closest to all other nodes in a graph?
一群人需要见面。 从一个人的房子到聚会的房子有一定的距离。 聚会的房子可以是任何人的房子。 作为聚会场所选择的最佳房屋是什么? 我们正在减少总距离。
我想到了一个天真的解决方案,你去每个房子,并采取每个人必须前往该位置的距离。
这个问题的最佳解决方案是什么?
选择具有最低输入权重总和的节点。
O(V ^ 2)时间复杂度,其中V是节点数。
O(1)内存复杂性。
伪代码:
min_dist = INF
min_node = null
for node in graph: // O(V) loops
sum = 0
for neighbor in neighbors(node): // O(V) loops
sum += dist(node, neighbor)
if min_dist <= sum: // small optimization
break
if min_dist > sum:
min_dist = sum
min_node = node
return min_node
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.