有关递归的问题(以及与切线图库networkx相切的问题):我有一个有向图,该图的节点的边的属性[“ value”]可以为0或1(有效的边权重)。
我希望能够递归地检查节点的邻居,直到邻居的节点失败某个阈值。例如:
def checkAll(x):
for neighbor in graph.neighbors(x):
if neighbor is bad:
fail
else:
checkAll(neighbor)
#add all good neighbors here? This isn't working!
基本上,我在递归上失败了,因为“ for”循环的完成方式,我认为。我可以帮忙吗? (我查看了this other SO post,但这似乎并不特别相关?)
谢谢!
最佳答案
免责声明:我对networkx一无所知,但是据我对您的问题的了解,这可能会有所帮助:
def examine(node, neighbors_list)
for neighbor in graph.neighbors(node):
if graph[x]["neighbor"]["value"] = 1:
return
else:
neighbors_list.append(neighbor)
examine(neighbor, neighbors_list)
x = parent_node
neighbors = []
examine(x, neighbors)