Warning

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

networkx.algorithms.traversal.depth_first_search.dfs_tree

dfs_tree(G, source=None, depth_limit=None)[source]

Returns oriented tree constructed from a depth-first-search from source.

Parameters:
  • G (NetworkX graph)
  • source (node, optional) – Specify starting node for depth-first search.
  • depth_limit (int, optional (default=len(G))) – Specify the maximum search depth.
Returns:

T – An oriented tree

Return type:

NetworkX DiGraph

Examples

>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]