我已经实现了algorithm来为每个多边形生成一个相邻多边形的列表。实现效果很好。
现在,我打算生成一个多边形簇列表。每个群集包含共同的邻居:
我有点困惑,想出一种算法将常见的邻居合并到集群中。是否有任何标准算法可以做到这一点?
答案 0 :(得分:1)
这是经典的不相交集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)]
现在地图中的值(多边形列表)就是您的答案。
具有按等级和折叠查找的并集的并集查找算法本质上是恒定时间(请参阅Wikipedia文章以了解Ackermann逆函数的理论细节),在实践中非常快,并且易于实现。所有的地图操作也是固定时间。
因此,该算法的运行速度(基本上)与多边形输入列表的总和成正比;尽可能快。答案 1 :(得分:0)
通常使用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
此算法完成后,每个多边形将分配给一组连接的组件。