edge_dfs¶

edge_dfs
(G, source=None, orientation='original')[source]¶ A directed, depthfirst traversal of edges in
G
, beginning atsource
.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 depthfirst traversal. For graphs,
edge
is of the form(u, v)
whereu
andv
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, thenu
andv
are always in the order of the actual directed edge. If orientation is ‘reverse’ or ‘ignore’, thenedge
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 depthfirst 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()