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
  • 14,182
  • 21
  • 67
  • 107

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,752
  • 35
  • 31
  • 35
mouad
  • 67,571
  • 18
  • 114
  • 106
  • @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