需要从成员可能连接的集合列表中创建集合列表
我在这里实时处理多边形数据,但问题非常简单。 我有一个包含数千组多边形不定数(整数)的巨大列表,我需要尽可能“快速”地将列表简化为“连接”不数集的列表。 即任何包含也在另一个集合中的整数的集合在结果中成为一个集合。我读过几种可能的解决方案,涉及集合和集合。我所追求的只是具有一定程度的共性的集合的最终列表。
我在这里处理大量数据,但为了简单起见,这里有一些示例数据:
setA = set([0,1,2])
setB = set([6,7,8,9])
setC = set([4,5,6])
setD = set([3,4,5,0])
setE = set([10,11,12])
setF = set([11,13,14,15])
setG = set([16,17,18,19])
listOfSets = [setA,setB,setC,setD,setE,setF,setG]
在这种情况下,我正在寻找一个结果如下的列表,尽管顺序无关:
connectedFacesListOfSets = [ set([0,1,2 ,3,4,5,6,7,8,9]), set([10,11,12,13,14,15]), set([16,17,18,19])]
我已经寻找类似的解决方案,但得票最高的解决方案在我的大型测试数据上给出了错误的结果。
I'm dealing with polygonal data in realtime here, but the problems quite simple.
I have a huge list containing thousands of sets of polygon Indecies (Integers) and I need to simplify the list as "fast" as possible into a list of sets of "connected" Indecies.
i.e. Any sets containing integers that are also in another set become one set in the result. I've read several possible solutions involving sets & graphs etc. All i'm after are a final list of sets which had any degree of commonality.
I'm dealing with lots of data here, but for simplicities sake here's some sample data:
setA = set([0,1,2])
setB = set([6,7,8,9])
setC = set([4,5,6])
setD = set([3,4,5,0])
setE = set([10,11,12])
setF = set([11,13,14,15])
setG = set([16,17,18,19])
listOfSets = [setA,setB,setC,setD,setE,setF,setG]
In this case I'm after a list with a result like this, although ordering is irrelevant:
connectedFacesListOfSets = [ set([0,1,2,3,4,5,6,7,8,9]), set([10,11,12,13,14,15]), set([16,17,18,19])]
I've looked for similar solutions, but the one with the highest votes gave incorrect results on my large test data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果没有足够大的集合,很难判断性能,但可以从以下一些基本代码开始:
这对所提供的数据进行 3 次迭代。输出如下:
It's hard to tell the performance without a sufficiently large set, but here is some basic code to start from:
This works in 3 iterations on the provided data. And the output is as follows:
这是一个联合查找问题。
虽然我没有使用过,但这个 Python 代码对我来说看起来不错。
http://code.activestate.com/recipes/577225-union-find/
This is a union find problem.
Though I haven't used it, this Python code looks good to me.
http://code.activestate.com/recipes/577225-union-find/
原谅乱七八糟的大写(自动更正......):
Forgive the messed up caps (autocorrect...):
所以..我想我明白了。这是一团乱,但我明白了。这就是我所做的:
然后在主函数中你会做这样的事情:
运行它后,我得到以下输出
希望这就是你正在寻找的。
编辑:添加代码以随机生成集合:
如果您确实愿意,最后 3 行可以压缩为一行。
So.. I think I got it. It's a mess but I got it. Here's what I did:
Then in the main function you'd do something like this:
After running it, I got the following output
Hope that's what you're looking for.
EDIT: Added code to randomly generate sets:
The last 3 lines can be condensed into one if you really wanted to.
我尝试做一些不同的事情:这个算法为每个集合循环一次,为每个元素循环一次:
它打印:
I tried to do something different: this algorithm loops once for each set and once for each element:
It prints: