python - networkx是否支持按标签进行dfs遍历

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

网络x dfs_edges()函数将迭代子节点。据我所知,http://networkx.lanl.gov/文档未在 dfs_edges() 中指定参数仅当边具有特定标签时才遍历。

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

最佳答案

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

如果这不起作用,那么修改 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()

关于python - networkx是否支持按标签进行dfs遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574660/

相关文章:

python - 使用 .grid() 定位时,如何将小部件放置在 tkinter 窗口的最底部?

python - 使用 SWIG 编码(marshal) Python PIL 图像

python - Unicode解码错误: 'utf8' codec can't decode

python - 为在 Python 中使用 ipdb 进行调试而定义的便捷函数

python - 在 Python 中检测 Windows 8.1?

algorithm - 为什么依赖图不表示为双向无环图?

c++ - 是否可以检查两个二叉树在线性时间内是否同构?

graph - 如何获取所有连接的节点,排除特定关系

python - 用于大规模持久化图形的 NoSQL 解决方案

python - Dato-Graphlab 检查 Edge 是否存在