网络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/