I have a function that I need to optimize:
def search(graph, node, maxdepth = 10, depth = 0):
nodes = []
for neighbor in graph.neighbors_iter(node):
if graph.node[neighbor].get('station', False):
return neighbor
nodes.append(neighbor)
for i in nodes:
if depth+1 > maxdepth:
return False
if search(graph, i, maxdepth, depth+1):
return i
return False
graph
should be a networkx graph object. How can I optimize this? This should find the closest node in the network with 'station'
attribute to True
.
if search(graph, i, ...): return i
==> the search() function might return something other than i, yet you are returning i. \$\endgroup\$