Question

The networkx dfs_edges() function will iterate over child nodes. As far as I can tell, the http://networkx.lanl.gov/ documentation does not specify a parameter into dfs_edges() to only traverse if edges have a specific label.

Also, I looked at dfs_labeled_edges() but that only tells you the traversal direction while iterating over a graph with DFS.

Was it helpful?

Solution 2

I have an approach which is working for me. Thanks @Aric for the inspiration.

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

It is a new function called dfs_edges_by_label() . And given a label as an input, it only traverses edges matching the label.

OTHER TIPS

There is no option to only traverse edges with a given label. If you don't mind making a copy of the graph you can build a new graph with only the edges with the specific label you want.

If that doesn't work it wouldn't be that hard to modify the source code of dfs_edges() to do that. e.g.

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top