pythonrecursiongraphbreadth-first-searchmicrosoft-distributed-file-system

Python DFS recursive function retaining values from previous call


I'm sorry if the title is misleading, but I could not put it in any other way.
I am trying to implement bfs and dfs in order to remember some concepts, but and odd behavior is going on with the recursive versions of the codes.

This is what is happening:

def rec_dfs(g, start_node, visited=[]):
    visited.append(start_node)
    for next_node in g[start_node]:
        if next_node not in visited:
            rec_dfs(g, next_node, visited)
    return visited

graph2={'A': ['B', 'C', 'D'],
       'B': ['A', 'E', 'F'],
       'C': ['A', 'F'],
       'D': ['A'],
       'E': ['B'],
       'F': ['B', 'C']}

rec_dfs(graph2, "A") #['A', 'B', 'E', 'F', 'C', 'D']               OK
rec_dfs(graph2, "A") #['A', 'B', 'E', 'F', 'C', 'D', 'A']          NOK
rec_dfs(graph2, "A") #['A', 'B', 'E', 'F', 'C', 'D', 'A', 'A']     NOK

It should always return the first case, but when I investigated I could see that the second call already had "visited" populated.

If I call the function like:

rec_dfs(graph2, "A", []) #['A', 'B', 'E', 'F', 'C', 'D']  OK
rec_dfs(graph2, "A", []) #['A', 'B', 'E', 'F', 'C', 'D']  OK
rec_dfs(graph2, "A", []) #['A', 'B', 'E', 'F', 'C', 'D']  OK

it works just fine...
I would really appreciate if someone could explain why this behavior is happening, and if there is a way to avoid it.

Thanks!


Solution

  • You're using visited array as a mutable default argument which is essentially initialized to an empty array only once at definition according to http://code.activestate.com/recipes/577786-smarter-default-arguments/.

    During each subsequent call to rec_dfs(), if visited array is not explicitly re-initialized, it will maintain its state during each subsequent function call.