Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.algorithms.traversal.edgedfs.edge_dfs

edge_dfs(G, source=None, orientation='original')[source]

A directed, depth-first traversal of edges in G, beginning at source.

Parameters:
  • G (graph) – A directed/undirected graph/multigraph.
  • source (node, list of nodes) – The node from which the traversal begins. If None, then a source is chosen arbitrarily and repeatedly until all edges from each node in the graph are searched.
  • orientation (‘original’ | ‘reverse’ | ‘ignore’) – For directed graphs and directed multigraphs, edge traversals need not respect the original orientation of the edges. When set to ‘reverse’, then every edge will be traversed in the reverse direction. When set to ‘ignore’, then each directed edge is treated as a single undirected edge that can be traversed in either direction. For undirected graphs and undirected multigraphs, this parameter is meaningless and is not consulted by the algorithm.
Yields:

edge (directed edge) – A directed edge indicating the path taken by the depth-first traversal. For graphs, edge is of the form (u, v) where u and v are the tail and head of the edge as determined by the traversal. For multigraphs, edge is of the form (u, v, key), where key is the key of the edge. When the graph is directed, then u and v are always in the order of the actual directed edge. If orientation is ‘reverse’ or ‘ignore’, then edge takes the form (u, v, key, direction) where direction is a string, ‘forward’ or ‘reverse’, that indicates if the edge was traversed in the forward (tail to head) or reverse (head to tail) direction, respectively.

Examples

>>> import networkx as nx
>>> nodes = [0, 1, 2, 3]
>>> edges = [(0, 1), (1, 0), (1, 0), (2, 1), (3, 1)]
>>> list(nx.edge_dfs(nx.Graph(edges), nodes))
[(0, 1), (1, 2), (1, 3)]
>>> list(nx.edge_dfs(nx.DiGraph(edges), nodes))
[(0, 1), (1, 0), (2, 1), (3, 1)]
>>> list(nx.edge_dfs(nx.MultiGraph(edges), nodes))
[(0, 1, 0), (1, 0, 1), (0, 1, 2), (1, 2, 0), (1, 3, 0)]
>>> list(nx.edge_dfs(nx.MultiDiGraph(edges), nodes))
[(0, 1, 0), (1, 0, 0), (1, 0, 1), (2, 1, 0), (3, 1, 0)]
>>> list(nx.edge_dfs(nx.DiGraph(edges), nodes, orientation='ignore'))
[(0, 1, 'forward'), (1, 0, 'forward'), (2, 1, 'reverse'), (3, 1, 'reverse')]
>>> list(nx.edge_dfs(nx.MultiDiGraph(edges), nodes, orientation='ignore'))
[(0, 1, 0, 'forward'), (1, 0, 0, 'forward'), (1, 0, 1, 'reverse'), (2, 1, 0, 'reverse'), (3, 1, 0, 'reverse')]

Notes

The goal of this function is to visit edges. It differs from the more familiar depth-first traversal of nodes, as provided by networkx.algorithms.traversal.depth_first_search.dfs_edges(), in that it does not stop once every node has been visited. In a directed graph with edges [(0, 1), (1, 2), (2, 1)], the edge (2, 1) would not be visited if not for the functionality provided by this function.

See also

dfs_edges()