3

A question about recursion (and tangentially the graphing library networkx): I have an directed graph with node that have edges that have an attribute ["value"] that can be 0 or 1 (effectively edge weights).

I want to be able to examine a node's neighbor recursively until a neighbor's node fails a certain threshold. For example:

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!

I'm failing at recursion, basically, I think because of the way the "for" loop is done. Can I get some help? (I looked at this other SO post but it didn't seem particularly relevant?)

Thank you!

Community
  • 1
  • 1
Rio
  • 13,052
  • 21
  • 63
  • 106

1 Answers1

3

disclaimer : I don't know anything about networkx, but from my understanding of your problem, maybe this can help:

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)
David Buck
  • 3,594
  • 33
  • 29
  • 34
mouad
  • 63,741
  • 17
  • 112
  • 105
  • I don't know what my implementation didn't work, but thank you! Hope this helps others as well. – Rio Nov 11 '10 at 21:43
  • @Rio: i saw that you removed this answer from useful one, if it's not helping you, please let me know :) – mouad Nov 12 '10 at 11:09