-1

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?

2 Answers2

0

This line of code here might cause the problem:

return [user_A] + path_to_friend(network,conn,user_B)

Basically this code is run as soon as any connection is found that hasn't been visited yet. Thus the code will search for a connection at the first non-visited path and no path else.

         c ---- e
        /
a ---- b
        \
         d

If you're starting at a and target-node is e, you're code will only reach e, if c appears before d in get_connections(network , b), Otherwise the code will end at d. This behavior isn't even defined/the execution-path doesn't end with a return-statement.

The simplest solution would be to simply return None, if a path ends without finding the node:

for conn in get_connections(network,user_A) :
     if conn in traversed:
         continue
     else:
         traversed.append(conn)
         tmp = path_to_friend(network , conn , user_B)
         if (tmp is not None):
             return [user_A] + tmp//a matching path was found

//no matching path was found -> return none
return None

Next problem with the code: you don't keep the traversed-list from one call to the next, thus each time the function is called, traversed is None holds. Use the traversed-list of each call as parameter for the subsequent call:

path_to_friend(network,conn,user_B, traversed)
  • I have updated the the code. But still going into a infinite loop. Seems like the traversed list is being created at every function call. How do i fix this. – Kartik Kapoor Oct 18 '15 at 11:08
  • @KartikKapoor i've updated the answer, should fix that issue aswell. –  Oct 18 '15 at 12:05
0
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 network[user_A][0] :
        return [user_A] + [user_B]
    else:
        for conn in network[user_A][0] :
            if conn in traversed:
                continue
            else:
                traversed.append(conn)
                temp = path_to_friend(network,conn,user_B,traversed)
                if (temp is not None):
                    return [user_A] + temp
        return None
else:
   return None


network = {'Bob': [['Carol'], []], 'Alice': [['Bob'], []], 'Carol': [['Bob'], []]}
path_to_friend(network,'Bob','Alice')