简体   繁体   中英

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.

the first call for DFSvisited is DFSvisited(G<- as represented below , 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.

I think I'm missing something in the recursive calls of DFSvisited inside the method DFSvisied, any ideas?

Thanks!

The problem with your approach is that the order of the DFS traversal is not necessarily equivalent to the Euler path. While your DFS traversal will eventually traverse all edges, it may require backtracking instead of taking one continuous path. 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.

Consider the following example graph:

在此处输入图片说明

If the DFS traversal starts with a -> b -> c -> a , it would then become stuck at a . Therefore, the DFS traversal has to backtrack to the last vertex which has an untraversed edge. This would be vertex b . The DFS traversal can then continue with 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 ). Instead, we begin with constructing the temporary path 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. But instead of inserting at the end, we have to insert them after b , resulting in abdebca , which is a valid Eulerian circuit.

To fix your program, you could add a parameter offset to your DFSvisited method. The offset would be set to 0 for the initial call to DFSvisited . For each recursive call, the offset is set to offset+1 . The appending to the temporary path can then be replaced by inserting into the temporary path at the given 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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