2010-11-11 49 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