【问题标题】:Does networkx support dfs traversal by labelnetworkx 是否支持按标签遍历 dfs
【发布时间】:2013-05-15 20:45:03
【问题描述】:

networkx dfs_edges() 函数将遍历子节点。据我所知,http://networkx.lanl.gov/ 文档没有在dfs_edges() 中指定参数,仅在边缘具有特定标签时才进行遍历。

另外,我查看了dfs_labeled_edges(),但这仅告诉您在使用 DFS 迭代图形时的遍历方向。

【问题讨论】:

    标签: python graph networkx depth-first-search microsoft-distributed-file-system


    【解决方案1】:

    没有选项只能遍历具有给定标签的边。如果您不介意制作图表的副本,您可以构建一个新图表,其中仅包含带有您想要的特定标签的边。

    如果这不起作用,那么修改 dfs_edges() 的源代码来做到这一点并不难。例如

    if source is None:
        # produce edges for all components
        nodes=G
    else:
        # produce edges for components with source
        nodes=[source]
    visited=set()
    for start in nodes:
        if start in visited:
            continue
        visited.add(start)
        stack = [(start,iter(G[start]))] <- edit here
        while stack:
            parent,children = stack[-1]
            try:
                child = next(children)
                if child not in visited:
                    yield parent,child
                    visited.add(child)
                    stack.append((child,iter(G[child]))) <- and edit here
            except StopIteration:
                stack.pop()
    

    【讨论】:

    • 阿里克,谢谢。我在networkx fork上提出了答案。
    【解决方案2】:

    我有一个适合我的方法。感谢@Aric 的启发。

    https://github.com/namoopsoo/networkx/blob/master/networkx/algorithms/traversal/depth_first_search.py

    这是一个名为 dfs_edges_by_label() 的新函数。并且给定一个标签作为输入,它只遍历与标签匹配的边。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 2014-10-04
      • 2014-07-14
      相关资源
      最近更新 更多