简体   繁体   English

在python中使用dfs的欧拉电路

[英]euler circuit using dfs in python

I'm trying to write a program that checks if a given matrix has an Euler circuit or not, I'm using DFS for checking but there is some problem in my recursive calls.我正在尝试编写一个程序来检查给定的矩阵是否具有欧拉电路,我正在使用 DFS 进行检查,但是我的递归调用存在一些问题。

the first call for DFSvisited is DFSvisited(G<- as represented below , 0, 1, temp_path =0) DFSvisited 的第一个调用是 DFSvisited(G<- 如下所示 , 0, 1, temp_path =0)

def DFSvisited(G,i,j,temp_path):
    G[i][j]=0
    G[j][i]=0
    temp_path.append(j)
    for k in range(0,n):
        if G[j][k]==1:
            print 'j+++',j,"#### k",k
            DFSvisited(G,j,k,temp_path)

I pass a matrix that looks like this:我传递了一个看起来像这样的矩阵:

   0   1   0   0   0   1   0
   1   0   1   0   0   1   1
   0   1   0   1   1   0   1
   0   0   1   0   1   0   0
   0   0   1   1   0   1   1
   1   1   0   0   1   0   1
   0   1   1   0   1   1   0

but its returning a temp_path of [0, 1, 2, 3, 4, 5, 6, 6, 4, 6, 5, 6] instead of [0,1,2,3,4,2,6,1,5,0] in its first iteration.但它返回[0, 1, 2, 3, 4, 5, 6, 6, 4, 6, 5, 6]而不是 [0,1,2,3,4,2,6,1, 5,0] 在它的第一次迭代中。

I think I'm missing something in the recursive calls of DFSvisited inside the method DFSvisied, any ideas?我想我在 DFSvisied 方法中的 DFSvisited 递归调用中遗漏了一些东西,有什么想法吗?

Thanks!谢谢!

The problem with your approach is that the order of the DFS traversal is not necessarily equivalent to the Euler path.你的方法的问题是 DFS 遍历的顺序不一定等同于欧拉路径。 While your DFS traversal will eventually traverse all edges, it may require backtracking instead of taking one continuous path.虽然您的 DFS 遍历最终会遍历所有边,但它可能需要回溯而不是采用一条连续路径。 Therefore, instead of always appending to the end of your temporary path, you have to insert into the temporary path according to how far the DFS backtracked.因此,您不必总是附加到临时路径的末尾,而是必须根据 DFS 回溯的距离插入临时路径。

Consider the following example graph:考虑以下示例图:

在此处输入图片说明

If the DFS traversal starts with a -> b -> c -> a , it would then become stuck at a .如果 DFS 遍历从a -> b -> c -> a ,那么它会卡在a Therefore, the DFS traversal has to backtrack to the last vertex which has an untraversed edge.因此,DFS 遍历必须回溯到具有未遍历边的最后一个顶点。 This would be vertex b .这将是顶点b The DFS traversal can then continue with b -> d -> e -> b . DFS 遍历然后可以继续b -> d -> e -> b After this, no untraversed edges can be found, so the search ends.此后,找不到未遍历的边,因此搜索结束。 To construct the Euler path, we cannot simply concatenate the vertices in the order in which they were visited (which would be abcabdeb ).为了构造欧拉路径,我们不能简单地按照访问顺序(即abcabdeb )连接顶点。 Instead, we begin with constructing the temporary path abca .相反,我们从构建临时路径abca Then, when we backtrack to b after getting stuck at a we have to insert the vertices visited from b in the temporary path.然后,当我们回溯到b陷在后a我们必须插入来自走访了顶点b在临时路径。 But instead of inserting at the end, we have to insert them after b , resulting in abdebca , which is a valid Eulerian circuit.但是不是在最后插入,我们必须在b之后插入它们,从而产生abdebca ,这是一个有效的欧拉回路。

To fix your program, you could add a parameter offset to your DFSvisited method.要修复您的程序,您可以向DFSvisited方法添加一个参数offset The offset would be set to 0 for the initial call to DFSvisited .对于DFSvisited的初始调用,偏移量将设置为0 For each recursive call, the offset is set to offset+1 .对于每个递归调用,偏移量设置为offset+1 The appending to the temporary path can then be replaced by inserting into the temporary path at the given offset :然后可以通过在给定offset处插入临时路径来替换临时路径的附加:

def DFSvisited(G,i,j,offset,temp_path):
    G[i][j]=0
    G[j][i]=0
    n = len(G)
    temp_path.insert(offset, j)
    for k in range(0,n):
        if G[j][k]==1:
            DFSvisited(G,j,k,offset+1,temp_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM