在闭合图中查找唯一循环

时间:2013-11-08 15:11:10

标签: python algorithm graph-algorithm

我终于设法编写了一个代码来识别当前配置可能的所有循环。例如,对于下面的图像,以下是我的程序的输入。

Graph Problem

network2=Pipes.NetworkManager(vertices=[1,2,3,4],
                 nodes=[(1,2),(1,3),(1,4),(2,1),(2,3),
                        (2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)])

network2.search_loop()

现在,我很难过滤输出中的数据以找到唯一的循环。

结果如下:

starting search from 1
--------------------------------------------------------
the loop is complete [1, 2, 3, 1]
the loop is complete [1, 2, 3, 4, 1]
the loop is complete [1, 2, 4, 1]
the loop is complete [1, 2, 4, 3, 1]
the loop is complete [1, 3, 2, 1]
the loop is complete [1, 3, 2, 4, 1]
the loop is complete [1, 3, 4, 1]
the loop is complete [1, 3, 4, 2, 1]
the loop is complete [1, 4, 2, 1]
the loop is complete [1, 4, 2, 3, 1]
the loop is complete [1, 4, 3, 1]
the loop is complete [1, 4, 3, 2, 1]
--------------------------------------------------------
starting search from 2
--------------------------------------------------------
the loop is complete [2, 1, 3, 2]
the loop is complete [2, 1, 3, 4, 2]
the loop is complete [2, 1, 4, 2]
the loop is complete [2, 1, 4, 3, 2]
the loop is complete [2, 3, 1, 2]
the loop is complete [2, 3, 1, 4, 2]
the loop is complete [2, 3, 4, 1, 2]
the loop is complete [2, 3, 4, 2]
the loop is complete [2, 4, 1, 2]
the loop is complete [2, 4, 1, 3, 2]
the loop is complete [2, 4, 3, 1, 2]
the loop is complete [2, 4, 3, 2]
--------------------------------------------------------
starting search from 3
--------------------------------------------------------
the loop is complete [3, 1, 2, 3]
the loop is complete [3, 1, 2, 4, 3]
the loop is complete [3, 1, 4, 2, 3]
the loop is complete [3, 1, 4, 3]
the loop is complete [3, 2, 1, 3]
the loop is complete [3, 2, 1, 4, 3]
the loop is complete [3, 2, 4, 1, 3]
the loop is complete [3, 2, 4, 3]
the loop is complete [3, 4, 1, 2, 3]
the loop is complete [3, 4, 1, 3]
the loop is complete [3, 4, 2, 1, 3]
the loop is complete [3, 4, 2, 3]
--------------------------------------------------------
starting search from 4
--------------------------------------------------------
the loop is complete [4, 1, 2, 3, 4]
the loop is complete [4, 1, 2, 4]
the loop is complete [4, 1, 3, 2, 4]
the loop is complete [4, 1, 3, 4]
the loop is complete [4, 2, 1, 3, 4]
the loop is complete [4, 2, 1, 4]
the loop is complete [4, 2, 3, 1, 4]
the loop is complete [4, 2, 3, 4]
the loop is complete [4, 3, 1, 2, 4]
the loop is complete [4, 3, 1, 4]
the loop is complete [4, 3, 2, 1, 4]
the loop is complete [4, 3, 2, 4]
--------------------------------------------------------

我使用递归(还有什么可能是更好的选择?)来解决问题。现在,在我获得结果后,我发现很难过滤这些结果并找到独特的循环。我对图论的理解是有限的(我刚刚开始阅读它)。从这个已识别的循环中找到唯一循环的有效方法是什么?

感谢您的一个答案,表明重复循环具有在反转时保持不变的属性。例如:

[1,2,3,1]
[1,3,2,1]
[2,3,1,2]

在上面的情况下,它从与第一个和第二个相同的顶点开始,反转将指示那些是相同的循环,但在第三种情况下,尽管它与前两个循环相同,但情况有点棘手。现在反过来应该通过该循环中的第三个顶点完成。当形成环的顶点数量增加时,这种复杂性将增加。因此,任何有效简化此问题的算法?我在这里看到了一些递归模式,但它仍然有点复杂,并且会知道是否有人可以提出简单的解决方案。

2 个答案:

答案 0 :(得分:1)

请注意,循环复制具有以下属性:如果您颠倒它的顺序,您将获得原始循环。

为了让您的生活更轻松,您可以决定将具有较小词典索引的循环带到您的解决方案中。这意味着,如果您找到了循环1->3->2->11->2->3->1(根据您的定义相同),您可以将1->2->3->1带到解决方案中。

从这一点开始的最佳方法是反转您找到的每个循环,如果反向模式的词典索引低于原始循环,则忽略循环。否则将其添加到解决方案中。

您可以通过一种非常简单的方法检查词典索引,只需创建一个顶点顺序的数字。

例如:将1->2->3->1翻译为1231,将1->3->2->1翻译为13211231小于1321,因此1->2->3->1将被用于解决方案,1->3->2->1将被忽略。

修改

为了消除不共享相同起点的重复循环(如示例1->3->2->12->1->3->2,您可以忽略第一个顶点索引不是最小的循环循环中的索引。这里2->1->3->2可以忽略,因为索引2不是循环中的最小值。

答案 1 :(得分:1)

在确保循环之后,一个简单的方法就是创建一组frozensets。

>>> loop1 = [1,2,3,4,1]
>>> loop2 = [1,2,4,3,1]
>>> loop3 = [1,2,3,1]
>>> loops = [loop1,loop2,loop3]
>>> loops = set([frozenset(loop) for loop in loops])
>>> loops
set([frozenset([1, 2, 3, 4]), frozenset([1, 2, 3])])

这当然会迫使你假设冻结集中的第一项是起点和终点。