递归搜索networkx图

发布于 2024-10-02 17:38:11 字数 560 浏览 3 评论 0原文

关于递归(以及与图形库networkx相切)的问题:我有一个有向图,其节点的边具有可以为0或1(有效地边权重)的属性[“值”]。

我希望能够递归地检查节点的邻居,直到邻居的节点未达到某个阈值。例如:

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”循环的完成方式。我可以寻求帮助吗? (我查看了 这个其他帖子 但似乎不是特别相关?)

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-10-09 17:38:11

免责声明:我对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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文