Rio
Rio

Reputation: 14892

Recursively search networkx graph

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!

Upvotes: 3

Views: 1771

Answers (1)

mouad
mouad

Reputation: 70059

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)

Upvotes: 3

Related Questions