【发布时间】:2020-06-12 14:15:26
【问题描述】:
我已经实现了这个algorithm 来为每个多边形生成一个相邻多边形的列表。实现工作正常。
现在我打算生成一个多边形簇列表。每个集群都包含共同的邻居:
我有点困惑,试图想出一种算法来将共同的邻居合并到集群中。是否有任何标准算法可以做到这一点?
【问题讨论】:
我已经实现了这个algorithm 来为每个多边形生成一个相邻多边形的列表。实现工作正常。
现在我打算生成一个多边形簇列表。每个集群都包含共同的邻居:
我有点困惑,试图想出一种算法来将共同的邻居合并到集群中。是否有任何标准算法可以做到这一点?
【问题讨论】:
这是一个经典的不相交集union-find 问题。 union-find算法及其相关数据结构支持三种操作:
现在执行以下算法:
for each polygon p
MakeSet(p)
for each polygon p
for each polygon q that's a neighbor of p
Union(p, q)
Let m be a map from polygons to lists of polygons, intitially empty
for each polygon p
append p to map[Find(p)]
现在地图中的值(多边形列表)就是您的答案。
Union-Find 算法与按秩并和和折叠查找基本上是常数时间(请阅读 Wikipedia 文章了解反阿克曼函数的理论细节),在实践中非常快,并且易于实现。所有的地图操作也是常数时间。
因此,该算法的运行速度(基本上)与输入的多边形列表的总和成正比;尽可能快。
【讨论】:
这通常使用depth-first search 或breadth-first search 来完成。
对于 BFS,您可以执行以下操作:
Create an empty queue, and assign all polygons to group -1. Set the current group to 0
While there are any polygons in group -1:
Randomly grab a polygon which is in group -1 and add it to the queue
While the queue is not empty:
Grab the first polygon in the queue and assign it to the current group
Find all of that polygon's neighbors
For all of the neighbors, if they are in group -1, add them to the queue
Remove the selected polygon from the queue
Increment the current group
当这个算法完成时,每个多边形将被分配给一个连接组件的集群。
【讨论】: