This documents the development version of NetworkX. Documentation for the current release can be found here.


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

Iterate over edges in a depth-first-search (DFS).

Perform a depth-first-search over the nodes of G and yield the edges in order. This may not generate all edges in G (see edge_dfs).

  • G (NetworkX graph)

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

  • depth_limit (int, optional (default=len(G))) – Specify the maximum search depth.


edges – A generator of edges in the depth-first-search.

Return type



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


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”.

See also

dfs_preorder_nodes(), dfs_postorder_nodes(), dfs_labeled_edges(), edge_dfs(), bfs_edges()