2010-11-11 46 views
2

关于递归问题(和切线图库libraryx):我有一个带有节点的有向图,该节点具有可以是0或1(有效边权重)的属性[“值” 。递归搜索networkx图

我希望能够递归检查一个节点的邻居,直到邻居的节点失败一定的阈值。例如:

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但它似乎并不特别相关?)

谢谢!

回答

3

所载之条款:我不知道任何关于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) 
+0

我不知道我的执行没有工作,但还是谢谢您!希望这可以帮助其他人。 – Rio 2010-11-11 21:43:41

+0

@Rio:我看到你从有用的人中删除了这个答案,如果它没有帮助你,请让我知道:) – mouad 2010-11-12 11:09:30