dfs_labeled_edges

dfs_labeled_edges(G, source=None)[source]

Produce edges in a depth-first-search (DFS) labeled by type.

Parameters:
  • G (NetworkX graph)
  • source (node, optional) – Specify starting node for depth-first search and return edges in the component reachable from source.
Returns:

edges – 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’, or ‘reverse’. 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 on in which both u and v have been visited and the edge is in the DFS tree.

Return type:

generator

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')]

Notes

Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py by D. Eppstein, July 2004.

If a source is not specified then a source is chosen arbitrarily and repeatedly until all components in the graph are searched.