dfs_labeled_edges#
- dfs_labeled_edges(G, source=None, depth_limit=None)[source]#
Iterate over edges in a depth-first-search (DFS) labeled by type.
- Parameters:
- GNetworkX graph
- sourcenode, optional
Specify starting node for depth-first search and return edges in the component reachable from source.
- depth_limitint, optional (default=len(G))
Specify the maximum search depth.
- Returns:
- edges: generator
A generator of triples of the form (u, v, d), where (u, v) is the edge being explored in the depth-first search and d is one of the strings ‘forward’, ‘nontree’, ‘reverse’, or ‘reverse-depth_limit’. A ‘forward’ edge is one in which u has been visited but v has not. A ‘nontree’ edge is one in which both u and v have been visited but the edge is not in the DFS tree. A ‘reverse’ edge is one in which both u and v have been visited and the edge is in the DFS tree. When the
depth_limit
is reached via a ‘forward’ edge, a ‘reverse’ edge is immediately generated rather than the subtree being explored. To indicate this flavor of ‘reverse’ edge, the string yielded is ‘reverse-depth_limit’.
Notes
If a source is not specified then a source is chosen arbitrarily and repeatedly until all components in the graph are searched.
The implementation of this function is adapted from David Eppstein’s depth-first search function in PADS, with modifications to allow depth limits based on the Wikipedia article “Depth-limited search”.
Examples
The labels reveal the complete transcript of the depth-first search algorithm in more detail than, for example,
dfs_edges()
:>>> from pprint import pprint >>> >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 1)]) >>> pprint(list(nx.dfs_labeled_edges(G, source=0))) [(0, 0, 'forward'), (0, 1, 'forward'), (1, 2, 'forward'), (2, 1, 'nontree'), (1, 2, 'reverse'), (0, 1, 'reverse'), (0, 0, 'reverse')]