depth_first_search.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. """Basic algorithms for depth-first searching the nodes of a graph."""
  2. from collections import defaultdict
  3. import networkx as nx
  4. __all__ = [
  5. "dfs_edges",
  6. "dfs_tree",
  7. "dfs_predecessors",
  8. "dfs_successors",
  9. "dfs_preorder_nodes",
  10. "dfs_postorder_nodes",
  11. "dfs_labeled_edges",
  12. ]
  13. def dfs_edges(G, source=None, depth_limit=None):
  14. """Iterate over edges in a depth-first-search (DFS).
  15. Perform a depth-first-search over the nodes of `G` and yield
  16. the edges in order. This may not generate all edges in `G`
  17. (see `~networkx.algorithms.traversal.edgedfs.edge_dfs`).
  18. Parameters
  19. ----------
  20. G : NetworkX graph
  21. source : node, optional
  22. Specify starting node for depth-first search and yield edges in
  23. the component reachable from source.
  24. depth_limit : int, optional (default=len(G))
  25. Specify the maximum search depth.
  26. Yields
  27. ------
  28. edge: 2-tuple of nodes
  29. Yields edges resulting from the depth-first-search.
  30. Examples
  31. --------
  32. >>> G = nx.path_graph(5)
  33. >>> list(nx.dfs_edges(G, source=0))
  34. [(0, 1), (1, 2), (2, 3), (3, 4)]
  35. >>> list(nx.dfs_edges(G, source=0, depth_limit=2))
  36. [(0, 1), (1, 2)]
  37. Notes
  38. -----
  39. If a source is not specified then a source is chosen arbitrarily and
  40. repeatedly until all components in the graph are searched.
  41. The implementation of this function is adapted from David Eppstein's
  42. depth-first search function in PADS [1]_, with modifications
  43. to allow depth limits based on the Wikipedia article
  44. "Depth-limited search" [2]_.
  45. See Also
  46. --------
  47. dfs_preorder_nodes
  48. dfs_postorder_nodes
  49. dfs_labeled_edges
  50. :func:`~networkx.algorithms.traversal.edgedfs.edge_dfs`
  51. :func:`~networkx.algorithms.traversal.breadth_first_search.bfs_edges`
  52. References
  53. ----------
  54. .. [1] http://www.ics.uci.edu/~eppstein/PADS
  55. .. [2] https://en.wikipedia.org/wiki/Depth-limited_search
  56. """
  57. if source is None:
  58. # edges for all components
  59. nodes = G
  60. else:
  61. # edges for components with source
  62. nodes = [source]
  63. visited = set()
  64. if depth_limit is None:
  65. depth_limit = len(G)
  66. for start in nodes:
  67. if start in visited:
  68. continue
  69. visited.add(start)
  70. stack = [(start, depth_limit, iter(G[start]))]
  71. while stack:
  72. parent, depth_now, children = stack[-1]
  73. try:
  74. child = next(children)
  75. if child not in visited:
  76. yield parent, child
  77. visited.add(child)
  78. if depth_now > 1:
  79. stack.append((child, depth_now - 1, iter(G[child])))
  80. except StopIteration:
  81. stack.pop()
  82. def dfs_tree(G, source=None, depth_limit=None):
  83. """Returns oriented tree constructed from a depth-first-search from source.
  84. Parameters
  85. ----------
  86. G : NetworkX graph
  87. source : node, optional
  88. Specify starting node for depth-first search.
  89. depth_limit : int, optional (default=len(G))
  90. Specify the maximum search depth.
  91. Returns
  92. -------
  93. T : NetworkX DiGraph
  94. An oriented tree
  95. Examples
  96. --------
  97. >>> G = nx.path_graph(5)
  98. >>> T = nx.dfs_tree(G, source=0, depth_limit=2)
  99. >>> list(T.edges())
  100. [(0, 1), (1, 2)]
  101. >>> T = nx.dfs_tree(G, source=0)
  102. >>> list(T.edges())
  103. [(0, 1), (1, 2), (2, 3), (3, 4)]
  104. See Also
  105. --------
  106. dfs_preorder_nodes
  107. dfs_postorder_nodes
  108. dfs_labeled_edges
  109. edge_dfs
  110. bfs_tree
  111. """
  112. T = nx.DiGraph()
  113. if source is None:
  114. T.add_nodes_from(G)
  115. else:
  116. T.add_node(source)
  117. T.add_edges_from(dfs_edges(G, source, depth_limit))
  118. return T
  119. def dfs_predecessors(G, source=None, depth_limit=None):
  120. """Returns dictionary of predecessors in depth-first-search from source.
  121. Parameters
  122. ----------
  123. G : NetworkX graph
  124. source : node, optional
  125. Specify starting node for depth-first search.
  126. depth_limit : int, optional (default=len(G))
  127. Specify the maximum search depth.
  128. Returns
  129. -------
  130. pred: dict
  131. A dictionary with nodes as keys and predecessor nodes as values.
  132. Examples
  133. --------
  134. >>> G = nx.path_graph(4)
  135. >>> nx.dfs_predecessors(G, source=0)
  136. {1: 0, 2: 1, 3: 2}
  137. >>> nx.dfs_predecessors(G, source=0, depth_limit=2)
  138. {1: 0, 2: 1}
  139. Notes
  140. -----
  141. If a source is not specified then a source is chosen arbitrarily and
  142. repeatedly until all components in the graph are searched.
  143. The implementation of this function is adapted from David Eppstein's
  144. depth-first search function in `PADS`_, with modifications
  145. to allow depth limits based on the Wikipedia article
  146. "`Depth-limited search`_".
  147. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS
  148. .. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
  149. See Also
  150. --------
  151. dfs_preorder_nodes
  152. dfs_postorder_nodes
  153. dfs_labeled_edges
  154. edge_dfs
  155. bfs_tree
  156. """
  157. return {t: s for s, t in dfs_edges(G, source, depth_limit)}
  158. def dfs_successors(G, source=None, depth_limit=None):
  159. """Returns dictionary of successors in depth-first-search from source.
  160. Parameters
  161. ----------
  162. G : NetworkX graph
  163. source : node, optional
  164. Specify starting node for depth-first search.
  165. depth_limit : int, optional (default=len(G))
  166. Specify the maximum search depth.
  167. Returns
  168. -------
  169. succ: dict
  170. A dictionary with nodes as keys and list of successor nodes as values.
  171. Examples
  172. --------
  173. >>> G = nx.path_graph(5)
  174. >>> nx.dfs_successors(G, source=0)
  175. {0: [1], 1: [2], 2: [3], 3: [4]}
  176. >>> nx.dfs_successors(G, source=0, depth_limit=2)
  177. {0: [1], 1: [2]}
  178. Notes
  179. -----
  180. If a source is not specified then a source is chosen arbitrarily and
  181. repeatedly until all components in the graph are searched.
  182. The implementation of this function is adapted from David Eppstein's
  183. depth-first search function in `PADS`_, with modifications
  184. to allow depth limits based on the Wikipedia article
  185. "`Depth-limited search`_".
  186. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS
  187. .. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
  188. See Also
  189. --------
  190. dfs_preorder_nodes
  191. dfs_postorder_nodes
  192. dfs_labeled_edges
  193. edge_dfs
  194. bfs_tree
  195. """
  196. d = defaultdict(list)
  197. for s, t in dfs_edges(G, source=source, depth_limit=depth_limit):
  198. d[s].append(t)
  199. return dict(d)
  200. def dfs_postorder_nodes(G, source=None, depth_limit=None):
  201. """Generate nodes in a depth-first-search post-ordering starting at source.
  202. Parameters
  203. ----------
  204. G : NetworkX graph
  205. source : node, optional
  206. Specify starting node for depth-first search.
  207. depth_limit : int, optional (default=len(G))
  208. Specify the maximum search depth.
  209. Returns
  210. -------
  211. nodes: generator
  212. A generator of nodes in a depth-first-search post-ordering.
  213. Examples
  214. --------
  215. >>> G = nx.path_graph(5)
  216. >>> list(nx.dfs_postorder_nodes(G, source=0))
  217. [4, 3, 2, 1, 0]
  218. >>> list(nx.dfs_postorder_nodes(G, source=0, depth_limit=2))
  219. [1, 0]
  220. Notes
  221. -----
  222. If a source is not specified then a source is chosen arbitrarily and
  223. repeatedly until all components in the graph are searched.
  224. The implementation of this function is adapted from David Eppstein's
  225. depth-first search function in `PADS`_, with modifications
  226. to allow depth limits based on the Wikipedia article
  227. "`Depth-limited search`_".
  228. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS
  229. .. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
  230. See Also
  231. --------
  232. dfs_edges
  233. dfs_preorder_nodes
  234. dfs_labeled_edges
  235. edge_dfs
  236. bfs_tree
  237. """
  238. edges = nx.dfs_labeled_edges(G, source=source, depth_limit=depth_limit)
  239. return (v for u, v, d in edges if d == "reverse")
  240. def dfs_preorder_nodes(G, source=None, depth_limit=None):
  241. """Generate nodes in a depth-first-search pre-ordering starting at source.
  242. Parameters
  243. ----------
  244. G : NetworkX graph
  245. source : node, optional
  246. Specify starting node for depth-first search and return nodes in
  247. the component reachable from source.
  248. depth_limit : int, optional (default=len(G))
  249. Specify the maximum search depth.
  250. Returns
  251. -------
  252. nodes: generator
  253. A generator of nodes in a depth-first-search pre-ordering.
  254. Examples
  255. --------
  256. >>> G = nx.path_graph(5)
  257. >>> list(nx.dfs_preorder_nodes(G, source=0))
  258. [0, 1, 2, 3, 4]
  259. >>> list(nx.dfs_preorder_nodes(G, source=0, depth_limit=2))
  260. [0, 1, 2]
  261. Notes
  262. -----
  263. If a source is not specified then a source is chosen arbitrarily and
  264. repeatedly until all components in the graph are searched.
  265. The implementation of this function is adapted from David Eppstein's
  266. depth-first search function in `PADS`_, with modifications
  267. to allow depth limits based on the Wikipedia article
  268. "`Depth-limited search`_".
  269. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS
  270. .. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
  271. See Also
  272. --------
  273. dfs_edges
  274. dfs_postorder_nodes
  275. dfs_labeled_edges
  276. bfs_edges
  277. """
  278. edges = nx.dfs_labeled_edges(G, source=source, depth_limit=depth_limit)
  279. return (v for u, v, d in edges if d == "forward")
  280. def dfs_labeled_edges(G, source=None, depth_limit=None):
  281. """Iterate over edges in a depth-first-search (DFS) labeled by type.
  282. Parameters
  283. ----------
  284. G : NetworkX graph
  285. source : node, optional
  286. Specify starting node for depth-first search and return edges in
  287. the component reachable from source.
  288. depth_limit : int, optional (default=len(G))
  289. Specify the maximum search depth.
  290. Returns
  291. -------
  292. edges: generator
  293. A generator of triples of the form (*u*, *v*, *d*), where (*u*,
  294. *v*) is the edge being explored in the depth-first search and *d*
  295. is one of the strings 'forward', 'nontree', 'reverse', or 'reverse-depth_limit'.
  296. A 'forward' edge is one in which *u* has been visited but *v* has
  297. not. A 'nontree' edge is one in which both *u* and *v* have been
  298. visited but the edge is not in the DFS tree. A 'reverse' edge is
  299. one in which both *u* and *v* have been visited and the edge is in
  300. the DFS tree. When the `depth_limit` is reached via a 'forward' edge,
  301. a 'reverse' edge is immediately generated rather than the subtree
  302. being explored. To indicate this flavor of 'reverse' edge, the string
  303. yielded is 'reverse-depth_limit'.
  304. Examples
  305. --------
  306. The labels reveal the complete transcript of the depth-first search
  307. algorithm in more detail than, for example, :func:`dfs_edges`::
  308. >>> from pprint import pprint
  309. >>>
  310. >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 1)])
  311. >>> pprint(list(nx.dfs_labeled_edges(G, source=0)))
  312. [(0, 0, 'forward'),
  313. (0, 1, 'forward'),
  314. (1, 2, 'forward'),
  315. (2, 1, 'nontree'),
  316. (1, 2, 'reverse'),
  317. (0, 1, 'reverse'),
  318. (0, 0, 'reverse')]
  319. Notes
  320. -----
  321. If a source is not specified then a source is chosen arbitrarily and
  322. repeatedly until all components in the graph are searched.
  323. The implementation of this function is adapted from David Eppstein's
  324. depth-first search function in `PADS`_, with modifications
  325. to allow depth limits based on the Wikipedia article
  326. "`Depth-limited search`_".
  327. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS
  328. .. _Depth-limited search: https://en.wikipedia.org/wiki/Depth-limited_search
  329. See Also
  330. --------
  331. dfs_edges
  332. dfs_preorder_nodes
  333. dfs_postorder_nodes
  334. """
  335. # Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
  336. # by D. Eppstein, July 2004.
  337. if source is None:
  338. # edges for all components
  339. nodes = G
  340. else:
  341. # edges for components with source
  342. nodes = [source]
  343. visited = set()
  344. if depth_limit is None:
  345. depth_limit = len(G)
  346. for start in nodes:
  347. if start in visited:
  348. continue
  349. yield start, start, "forward"
  350. visited.add(start)
  351. stack = [(start, depth_limit, iter(G[start]))]
  352. while stack:
  353. parent, depth_now, children = stack[-1]
  354. try:
  355. child = next(children)
  356. if child in visited:
  357. yield parent, child, "nontree"
  358. else:
  359. yield parent, child, "forward"
  360. visited.add(child)
  361. if depth_now > 1:
  362. stack.append((child, depth_now - 1, iter(G[child])))
  363. else:
  364. yield parent, child, "reverse-depth_limit"
  365. except StopIteration:
  366. stack.pop()
  367. if stack:
  368. yield stack[-1][0], parent, "reverse"
  369. yield start, start, "reverse"