I am doing some practice problems on udacity and have to write some recursive code to find path to friends in a node. And i came up with this. However the recursive definition is missing a stopping condition i think where the connection is not found. How do i fix it?
def path_to_friend(network, user_A, user_B,traversed = None):
if traversed is None:
traversed = []
if (user_B in network and user_A in network):
if user_B in get_connections(network,user_A):
return [user_A] + [user_B]
else:
for conn in get_connections(network,user_A) :
if conn in traversed:
continue
else:
traversed.append(conn)
return [user_A] + path_to_friend(network,conn,user_B)
else:
return None
data structure of network: {'Bob': [['Carol'], []], 'Alice': [['Bob'], []], 'Carol': [['Bob'], []]}
To find : path_to_friend(network,'Bob','Alice')
Result: Infinite recursion. How do i fix it?