dag.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. """Algorithms for directed acyclic graphs (DAGs).
  2. Note that most of these functions are only guaranteed to work for DAGs.
  3. In general, these functions do not check for acyclic-ness, so it is up
  4. to the user to check for that.
  5. """
  6. import heapq
  7. from collections import deque
  8. from functools import partial
  9. from itertools import chain, combinations, product, starmap
  10. from math import gcd
  11. import networkx as nx
  12. from networkx.utils import arbitrary_element, not_implemented_for, pairwise
  13. __all__ = [
  14. "descendants",
  15. "ancestors",
  16. "topological_sort",
  17. "lexicographical_topological_sort",
  18. "all_topological_sorts",
  19. "topological_generations",
  20. "is_directed_acyclic_graph",
  21. "is_aperiodic",
  22. "transitive_closure",
  23. "transitive_closure_dag",
  24. "transitive_reduction",
  25. "antichains",
  26. "dag_longest_path",
  27. "dag_longest_path_length",
  28. "dag_to_branching",
  29. "compute_v_structures",
  30. ]
  31. chaini = chain.from_iterable
  32. @nx._dispatch
  33. def descendants(G, source):
  34. """Returns all nodes reachable from `source` in `G`.
  35. Parameters
  36. ----------
  37. G : NetworkX Graph
  38. source : node in `G`
  39. Returns
  40. -------
  41. set()
  42. The descendants of `source` in `G`
  43. Raises
  44. ------
  45. NetworkXError
  46. If node `source` is not in `G`.
  47. Examples
  48. --------
  49. >>> DG = nx.path_graph(5, create_using=nx.DiGraph)
  50. >>> sorted(nx.descendants(DG, 2))
  51. [3, 4]
  52. The `source` node is not a descendant of itself, but can be included manually:
  53. >>> sorted(nx.descendants(DG, 2) | {2})
  54. [2, 3, 4]
  55. See also
  56. --------
  57. ancestors
  58. """
  59. return {child for parent, child in nx.bfs_edges(G, source)}
  60. @nx._dispatch
  61. def ancestors(G, source):
  62. """Returns all nodes having a path to `source` in `G`.
  63. Parameters
  64. ----------
  65. G : NetworkX Graph
  66. source : node in `G`
  67. Returns
  68. -------
  69. set()
  70. The ancestors of `source` in `G`
  71. Raises
  72. ------
  73. NetworkXError
  74. If node `source` is not in `G`.
  75. Examples
  76. --------
  77. >>> DG = nx.path_graph(5, create_using=nx.DiGraph)
  78. >>> sorted(nx.ancestors(DG, 2))
  79. [0, 1]
  80. The `source` node is not an ancestor of itself, but can be included manually:
  81. >>> sorted(nx.ancestors(DG, 2) | {2})
  82. [0, 1, 2]
  83. See also
  84. --------
  85. descendants
  86. """
  87. return {child for parent, child in nx.bfs_edges(G, source, reverse=True)}
  88. def has_cycle(G):
  89. """Decides whether the directed graph has a cycle."""
  90. try:
  91. # Feed the entire iterator into a zero-length deque.
  92. deque(topological_sort(G), maxlen=0)
  93. except nx.NetworkXUnfeasible:
  94. return True
  95. else:
  96. return False
  97. def is_directed_acyclic_graph(G):
  98. """Returns True if the graph `G` is a directed acyclic graph (DAG) or
  99. False if not.
  100. Parameters
  101. ----------
  102. G : NetworkX graph
  103. Returns
  104. -------
  105. bool
  106. True if `G` is a DAG, False otherwise
  107. Examples
  108. --------
  109. Undirected graph::
  110. >>> G = nx.Graph([(1, 2), (2, 3)])
  111. >>> nx.is_directed_acyclic_graph(G)
  112. False
  113. Directed graph with cycle::
  114. >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
  115. >>> nx.is_directed_acyclic_graph(G)
  116. False
  117. Directed acyclic graph::
  118. >>> G = nx.DiGraph([(1, 2), (2, 3)])
  119. >>> nx.is_directed_acyclic_graph(G)
  120. True
  121. See also
  122. --------
  123. topological_sort
  124. """
  125. return G.is_directed() and not has_cycle(G)
  126. def topological_generations(G):
  127. """Stratifies a DAG into generations.
  128. A topological generation is node collection in which ancestors of a node in each
  129. generation are guaranteed to be in a previous generation, and any descendants of
  130. a node are guaranteed to be in a following generation. Nodes are guaranteed to
  131. be in the earliest possible generation that they can belong to.
  132. Parameters
  133. ----------
  134. G : NetworkX digraph
  135. A directed acyclic graph (DAG)
  136. Yields
  137. ------
  138. sets of nodes
  139. Yields sets of nodes representing each generation.
  140. Raises
  141. ------
  142. NetworkXError
  143. Generations are defined for directed graphs only. If the graph
  144. `G` is undirected, a :exc:`NetworkXError` is raised.
  145. NetworkXUnfeasible
  146. If `G` is not a directed acyclic graph (DAG) no topological generations
  147. exist and a :exc:`NetworkXUnfeasible` exception is raised. This can also
  148. be raised if `G` is changed while the returned iterator is being processed
  149. RuntimeError
  150. If `G` is changed while the returned iterator is being processed.
  151. Examples
  152. --------
  153. >>> DG = nx.DiGraph([(2, 1), (3, 1)])
  154. >>> [sorted(generation) for generation in nx.topological_generations(DG)]
  155. [[2, 3], [1]]
  156. Notes
  157. -----
  158. The generation in which a node resides can also be determined by taking the
  159. max-path-distance from the node to the farthest leaf node. That value can
  160. be obtained with this function using `enumerate(topological_generations(G))`.
  161. See also
  162. --------
  163. topological_sort
  164. """
  165. if not G.is_directed():
  166. raise nx.NetworkXError("Topological sort not defined on undirected graphs.")
  167. multigraph = G.is_multigraph()
  168. indegree_map = {v: d for v, d in G.in_degree() if d > 0}
  169. zero_indegree = [v for v, d in G.in_degree() if d == 0]
  170. while zero_indegree:
  171. this_generation = zero_indegree
  172. zero_indegree = []
  173. for node in this_generation:
  174. if node not in G:
  175. raise RuntimeError("Graph changed during iteration")
  176. for child in G.neighbors(node):
  177. try:
  178. indegree_map[child] -= len(G[node][child]) if multigraph else 1
  179. except KeyError as err:
  180. raise RuntimeError("Graph changed during iteration") from err
  181. if indegree_map[child] == 0:
  182. zero_indegree.append(child)
  183. del indegree_map[child]
  184. yield this_generation
  185. if indegree_map:
  186. raise nx.NetworkXUnfeasible(
  187. "Graph contains a cycle or graph changed during iteration"
  188. )
  189. def topological_sort(G):
  190. """Returns a generator of nodes in topologically sorted order.
  191. A topological sort is a nonunique permutation of the nodes of a
  192. directed graph such that an edge from u to v implies that u
  193. appears before v in the topological sort order. This ordering is
  194. valid only if the graph has no directed cycles.
  195. Parameters
  196. ----------
  197. G : NetworkX digraph
  198. A directed acyclic graph (DAG)
  199. Yields
  200. ------
  201. nodes
  202. Yields the nodes in topological sorted order.
  203. Raises
  204. ------
  205. NetworkXError
  206. Topological sort is defined for directed graphs only. If the graph `G`
  207. is undirected, a :exc:`NetworkXError` is raised.
  208. NetworkXUnfeasible
  209. If `G` is not a directed acyclic graph (DAG) no topological sort exists
  210. and a :exc:`NetworkXUnfeasible` exception is raised. This can also be
  211. raised if `G` is changed while the returned iterator is being processed
  212. RuntimeError
  213. If `G` is changed while the returned iterator is being processed.
  214. Examples
  215. --------
  216. To get the reverse order of the topological sort:
  217. >>> DG = nx.DiGraph([(1, 2), (2, 3)])
  218. >>> list(reversed(list(nx.topological_sort(DG))))
  219. [3, 2, 1]
  220. If your DiGraph naturally has the edges representing tasks/inputs
  221. and nodes representing people/processes that initiate tasks, then
  222. topological_sort is not quite what you need. You will have to change
  223. the tasks to nodes with dependence reflected by edges. The result is
  224. a kind of topological sort of the edges. This can be done
  225. with :func:`networkx.line_graph` as follows:
  226. >>> list(nx.topological_sort(nx.line_graph(DG)))
  227. [(1, 2), (2, 3)]
  228. Notes
  229. -----
  230. This algorithm is based on a description and proof in
  231. "Introduction to Algorithms: A Creative Approach" [1]_ .
  232. See also
  233. --------
  234. is_directed_acyclic_graph, lexicographical_topological_sort
  235. References
  236. ----------
  237. .. [1] Manber, U. (1989).
  238. *Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
  239. """
  240. for generation in nx.topological_generations(G):
  241. yield from generation
  242. def lexicographical_topological_sort(G, key=None):
  243. """Generate the nodes in the unique lexicographical topological sort order.
  244. Generates a unique ordering of nodes by first sorting topologically (for which there are often
  245. multiple valid orderings) and then additionally by sorting lexicographically.
  246. A topological sort arranges the nodes of a directed graph so that the
  247. upstream node of each directed edge precedes the downstream node.
  248. It is always possible to find a solution for directed graphs that have no cycles.
  249. There may be more than one valid solution.
  250. Lexicographical sorting is just sorting alphabetically. It is used here to break ties in the
  251. topological sort and to determine a single, unique ordering. This can be useful in comparing
  252. sort results.
  253. The lexicographical order can be customized by providing a function to the `key=` parameter.
  254. The definition of the key function is the same as used in python's built-in `sort()`.
  255. The function takes a single argument and returns a key to use for sorting purposes.
  256. Lexicographical sorting can fail if the node names are un-sortable. See the example below.
  257. The solution is to provide a function to the `key=` argument that returns sortable keys.
  258. Parameters
  259. ----------
  260. G : NetworkX digraph
  261. A directed acyclic graph (DAG)
  262. key : function, optional
  263. A function of one argument that converts a node name to a comparison key.
  264. It defines and resolves ambiguities in the sort order. Defaults to the identity function.
  265. Yields
  266. ------
  267. nodes
  268. Yields the nodes of G in lexicographical topological sort order.
  269. Raises
  270. ------
  271. NetworkXError
  272. Topological sort is defined for directed graphs only. If the graph `G`
  273. is undirected, a :exc:`NetworkXError` is raised.
  274. NetworkXUnfeasible
  275. If `G` is not a directed acyclic graph (DAG) no topological sort exists
  276. and a :exc:`NetworkXUnfeasible` exception is raised. This can also be
  277. raised if `G` is changed while the returned iterator is being processed
  278. RuntimeError
  279. If `G` is changed while the returned iterator is being processed.
  280. TypeError
  281. Results from un-sortable node names.
  282. Consider using `key=` parameter to resolve ambiguities in the sort order.
  283. Examples
  284. --------
  285. >>> DG = nx.DiGraph([(2, 1), (2, 5), (1, 3), (1, 4), (5, 4)])
  286. >>> list(nx.lexicographical_topological_sort(DG))
  287. [2, 1, 3, 5, 4]
  288. >>> list(nx.lexicographical_topological_sort(DG, key=lambda x: -x))
  289. [2, 5, 1, 4, 3]
  290. The sort will fail for any graph with integer and string nodes. Comparison of integer to strings
  291. is not defined in python. Is 3 greater or less than 'red'?
  292. >>> DG = nx.DiGraph([(1, 'red'), (3, 'red'), (1, 'green'), (2, 'blue')])
  293. >>> list(nx.lexicographical_topological_sort(DG))
  294. Traceback (most recent call last):
  295. ...
  296. TypeError: '<' not supported between instances of 'str' and 'int'
  297. ...
  298. Incomparable nodes can be resolved using a `key` function. This example function
  299. allows comparison of integers and strings by returning a tuple where the first
  300. element is True for `str`, False otherwise. The second element is the node name.
  301. This groups the strings and integers separately so they can be compared only among themselves.
  302. >>> key = lambda node: (isinstance(node, str), node)
  303. >>> list(nx.lexicographical_topological_sort(DG, key=key))
  304. [1, 2, 3, 'blue', 'green', 'red']
  305. Notes
  306. -----
  307. This algorithm is based on a description and proof in
  308. "Introduction to Algorithms: A Creative Approach" [1]_ .
  309. See also
  310. --------
  311. topological_sort
  312. References
  313. ----------
  314. .. [1] Manber, U. (1989).
  315. *Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
  316. """
  317. if not G.is_directed():
  318. msg = "Topological sort not defined on undirected graphs."
  319. raise nx.NetworkXError(msg)
  320. if key is None:
  321. def key(node):
  322. return node
  323. nodeid_map = {n: i for i, n in enumerate(G)}
  324. def create_tuple(node):
  325. return key(node), nodeid_map[node], node
  326. indegree_map = {v: d for v, d in G.in_degree() if d > 0}
  327. # These nodes have zero indegree and ready to be returned.
  328. zero_indegree = [create_tuple(v) for v, d in G.in_degree() if d == 0]
  329. heapq.heapify(zero_indegree)
  330. while zero_indegree:
  331. _, _, node = heapq.heappop(zero_indegree)
  332. if node not in G:
  333. raise RuntimeError("Graph changed during iteration")
  334. for _, child in G.edges(node):
  335. try:
  336. indegree_map[child] -= 1
  337. except KeyError as err:
  338. raise RuntimeError("Graph changed during iteration") from err
  339. if indegree_map[child] == 0:
  340. try:
  341. heapq.heappush(zero_indegree, create_tuple(child))
  342. except TypeError as err:
  343. raise TypeError(
  344. f"{err}\nConsider using `key=` parameter to resolve ambiguities in the sort order."
  345. )
  346. del indegree_map[child]
  347. yield node
  348. if indegree_map:
  349. msg = "Graph contains a cycle or graph changed during iteration"
  350. raise nx.NetworkXUnfeasible(msg)
  351. @not_implemented_for("undirected")
  352. def all_topological_sorts(G):
  353. """Returns a generator of _all_ topological sorts of the directed graph G.
  354. A topological sort is a nonunique permutation of the nodes such that an
  355. edge from u to v implies that u appears before v in the topological sort
  356. order.
  357. Parameters
  358. ----------
  359. G : NetworkX DiGraph
  360. A directed graph
  361. Yields
  362. ------
  363. topological_sort_order : list
  364. a list of nodes in `G`, representing one of the topological sort orders
  365. Raises
  366. ------
  367. NetworkXNotImplemented
  368. If `G` is not directed
  369. NetworkXUnfeasible
  370. If `G` is not acyclic
  371. Examples
  372. --------
  373. To enumerate all topological sorts of directed graph:
  374. >>> DG = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
  375. >>> list(nx.all_topological_sorts(DG))
  376. [[1, 2, 4, 3], [1, 2, 3, 4]]
  377. Notes
  378. -----
  379. Implements an iterative version of the algorithm given in [1].
  380. References
  381. ----------
  382. .. [1] Knuth, Donald E., Szwarcfiter, Jayme L. (1974).
  383. "A Structured Program to Generate All Topological Sorting Arrangements"
  384. Information Processing Letters, Volume 2, Issue 6, 1974, Pages 153-157,
  385. ISSN 0020-0190,
  386. https://doi.org/10.1016/0020-0190(74)90001-5.
  387. Elsevier (North-Holland), Amsterdam
  388. """
  389. if not G.is_directed():
  390. raise nx.NetworkXError("Topological sort not defined on undirected graphs.")
  391. # the names of count and D are chosen to match the global variables in [1]
  392. # number of edges originating in a vertex v
  393. count = dict(G.in_degree())
  394. # vertices with indegree 0
  395. D = deque([v for v, d in G.in_degree() if d == 0])
  396. # stack of first value chosen at a position k in the topological sort
  397. bases = []
  398. current_sort = []
  399. # do-while construct
  400. while True:
  401. assert all(count[v] == 0 for v in D)
  402. if len(current_sort) == len(G):
  403. yield list(current_sort)
  404. # clean-up stack
  405. while len(current_sort) > 0:
  406. assert len(bases) == len(current_sort)
  407. q = current_sort.pop()
  408. # "restores" all edges (q, x)
  409. # NOTE: it is important to iterate over edges instead
  410. # of successors, so count is updated correctly in multigraphs
  411. for _, j in G.out_edges(q):
  412. count[j] += 1
  413. assert count[j] >= 0
  414. # remove entries from D
  415. while len(D) > 0 and count[D[-1]] > 0:
  416. D.pop()
  417. # corresponds to a circular shift of the values in D
  418. # if the first value chosen (the base) is in the first
  419. # position of D again, we are done and need to consider the
  420. # previous condition
  421. D.appendleft(q)
  422. if D[-1] == bases[-1]:
  423. # all possible values have been chosen at current position
  424. # remove corresponding marker
  425. bases.pop()
  426. else:
  427. # there are still elements that have not been fixed
  428. # at the current position in the topological sort
  429. # stop removing elements, escape inner loop
  430. break
  431. else:
  432. if len(D) == 0:
  433. raise nx.NetworkXUnfeasible("Graph contains a cycle.")
  434. # choose next node
  435. q = D.pop()
  436. # "erase" all edges (q, x)
  437. # NOTE: it is important to iterate over edges instead
  438. # of successors, so count is updated correctly in multigraphs
  439. for _, j in G.out_edges(q):
  440. count[j] -= 1
  441. assert count[j] >= 0
  442. if count[j] == 0:
  443. D.append(j)
  444. current_sort.append(q)
  445. # base for current position might _not_ be fixed yet
  446. if len(bases) < len(current_sort):
  447. bases.append(q)
  448. if len(bases) == 0:
  449. break
  450. def is_aperiodic(G):
  451. """Returns True if `G` is aperiodic.
  452. A directed graph is aperiodic if there is no integer k > 1 that
  453. divides the length of every cycle in the graph.
  454. Parameters
  455. ----------
  456. G : NetworkX DiGraph
  457. A directed graph
  458. Returns
  459. -------
  460. bool
  461. True if the graph is aperiodic False otherwise
  462. Raises
  463. ------
  464. NetworkXError
  465. If `G` is not directed
  466. Examples
  467. --------
  468. A graph consisting of one cycle, the length of which is 2. Therefore ``k = 2``
  469. divides the length of every cycle in the graph and thus the graph
  470. is *not aperiodic*::
  471. >>> DG = nx.DiGraph([(1, 2), (2, 1)])
  472. >>> nx.is_aperiodic(DG)
  473. False
  474. A graph consisting of two cycles: one of length 2 and the other of length 3.
  475. The cycle lengths are coprime, so there is no single value of k where ``k > 1``
  476. that divides each cycle length and therefore the graph is *aperiodic*::
  477. >>> DG = nx.DiGraph([(1, 2), (2, 3), (3, 1), (1, 4), (4, 1)])
  478. >>> nx.is_aperiodic(DG)
  479. True
  480. A graph consisting of two cycles: one of length 2 and the other of length 4.
  481. The lengths of the cycles share a common factor ``k = 2``, and therefore
  482. the graph is *not aperiodic*::
  483. >>> DG = nx.DiGraph([(1, 2), (2, 1), (3, 4), (4, 5), (5, 6), (6, 3)])
  484. >>> nx.is_aperiodic(DG)
  485. False
  486. An acyclic graph, therefore the graph is *not aperiodic*::
  487. >>> DG = nx.DiGraph([(1, 2), (2, 3)])
  488. >>> nx.is_aperiodic(DG)
  489. False
  490. Notes
  491. -----
  492. This uses the method outlined in [1]_, which runs in $O(m)$ time
  493. given $m$ edges in `G`. Note that a graph is not aperiodic if it is
  494. acyclic as every integer trivial divides length 0 cycles.
  495. References
  496. ----------
  497. .. [1] Jarvis, J. P.; Shier, D. R. (1996),
  498. "Graph-theoretic analysis of finite Markov chains,"
  499. in Shier, D. R.; Wallenius, K. T., Applied Mathematical Modeling:
  500. A Multidisciplinary Approach, CRC Press.
  501. """
  502. if not G.is_directed():
  503. raise nx.NetworkXError("is_aperiodic not defined for undirected graphs")
  504. s = arbitrary_element(G)
  505. levels = {s: 0}
  506. this_level = [s]
  507. g = 0
  508. lev = 1
  509. while this_level:
  510. next_level = []
  511. for u in this_level:
  512. for v in G[u]:
  513. if v in levels: # Non-Tree Edge
  514. g = gcd(g, levels[u] - levels[v] + 1)
  515. else: # Tree Edge
  516. next_level.append(v)
  517. levels[v] = lev
  518. this_level = next_level
  519. lev += 1
  520. if len(levels) == len(G): # All nodes in tree
  521. return g == 1
  522. else:
  523. return g == 1 and nx.is_aperiodic(G.subgraph(set(G) - set(levels)))
  524. def transitive_closure(G, reflexive=False):
  525. """Returns transitive closure of a graph
  526. The transitive closure of G = (V,E) is a graph G+ = (V,E+) such that
  527. for all v, w in V there is an edge (v, w) in E+ if and only if there
  528. is a path from v to w in G.
  529. Handling of paths from v to v has some flexibility within this definition.
  530. A reflexive transitive closure creates a self-loop for the path
  531. from v to v of length 0. The usual transitive closure creates a
  532. self-loop only if a cycle exists (a path from v to v with length > 0).
  533. We also allow an option for no self-loops.
  534. Parameters
  535. ----------
  536. G : NetworkX Graph
  537. A directed/undirected graph/multigraph.
  538. reflexive : Bool or None, optional (default: False)
  539. Determines when cycles create self-loops in the Transitive Closure.
  540. If True, trivial cycles (length 0) create self-loops. The result
  541. is a reflexive transitive closure of G.
  542. If False (the default) non-trivial cycles create self-loops.
  543. If None, self-loops are not created.
  544. Returns
  545. -------
  546. NetworkX graph
  547. The transitive closure of `G`
  548. Raises
  549. ------
  550. NetworkXError
  551. If `reflexive` not in `{None, True, False}`
  552. Examples
  553. --------
  554. The treatment of trivial (i.e. length 0) cycles is controlled by the
  555. `reflexive` parameter.
  556. Trivial (i.e. length 0) cycles do not create self-loops when
  557. ``reflexive=False`` (the default)::
  558. >>> DG = nx.DiGraph([(1, 2), (2, 3)])
  559. >>> TC = nx.transitive_closure(DG, reflexive=False)
  560. >>> TC.edges()
  561. OutEdgeView([(1, 2), (1, 3), (2, 3)])
  562. However, nontrivial (i.e. length greater then 0) cycles create self-loops
  563. when ``reflexive=False`` (the default)::
  564. >>> DG = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
  565. >>> TC = nx.transitive_closure(DG, reflexive=False)
  566. >>> TC.edges()
  567. OutEdgeView([(1, 2), (1, 3), (1, 1), (2, 3), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3)])
  568. Trivial cycles (length 0) create self-loops when ``reflexive=True``::
  569. >>> DG = nx.DiGraph([(1, 2), (2, 3)])
  570. >>> TC = nx.transitive_closure(DG, reflexive=True)
  571. >>> TC.edges()
  572. OutEdgeView([(1, 2), (1, 1), (1, 3), (2, 3), (2, 2), (3, 3)])
  573. And the third option is not to create self-loops at all when ``reflexive=None``::
  574. >>> DG = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
  575. >>> TC = nx.transitive_closure(DG, reflexive=None)
  576. >>> TC.edges()
  577. OutEdgeView([(1, 2), (1, 3), (2, 3), (2, 1), (3, 1), (3, 2)])
  578. References
  579. ----------
  580. .. [1] https://www.ics.uci.edu/~eppstein/PADS/PartialOrder.py
  581. """
  582. TC = G.copy()
  583. if reflexive not in {None, True, False}:
  584. raise nx.NetworkXError("Incorrect value for the parameter `reflexive`")
  585. for v in G:
  586. if reflexive is None:
  587. TC.add_edges_from((v, u) for u in nx.descendants(G, v) if u not in TC[v])
  588. elif reflexive is True:
  589. TC.add_edges_from(
  590. (v, u) for u in nx.descendants(G, v) | {v} if u not in TC[v]
  591. )
  592. elif reflexive is False:
  593. TC.add_edges_from((v, e[1]) for e in nx.edge_bfs(G, v) if e[1] not in TC[v])
  594. return TC
  595. @not_implemented_for("undirected")
  596. def transitive_closure_dag(G, topo_order=None):
  597. """Returns the transitive closure of a directed acyclic graph.
  598. This function is faster than the function `transitive_closure`, but fails
  599. if the graph has a cycle.
  600. The transitive closure of G = (V,E) is a graph G+ = (V,E+) such that
  601. for all v, w in V there is an edge (v, w) in E+ if and only if there
  602. is a non-null path from v to w in G.
  603. Parameters
  604. ----------
  605. G : NetworkX DiGraph
  606. A directed acyclic graph (DAG)
  607. topo_order: list or tuple, optional
  608. A topological order for G (if None, the function will compute one)
  609. Returns
  610. -------
  611. NetworkX DiGraph
  612. The transitive closure of `G`
  613. Raises
  614. ------
  615. NetworkXNotImplemented
  616. If `G` is not directed
  617. NetworkXUnfeasible
  618. If `G` has a cycle
  619. Examples
  620. --------
  621. >>> DG = nx.DiGraph([(1, 2), (2, 3)])
  622. >>> TC = nx.transitive_closure_dag(DG)
  623. >>> TC.edges()
  624. OutEdgeView([(1, 2), (1, 3), (2, 3)])
  625. Notes
  626. -----
  627. This algorithm is probably simple enough to be well-known but I didn't find
  628. a mention in the literature.
  629. """
  630. if topo_order is None:
  631. topo_order = list(topological_sort(G))
  632. TC = G.copy()
  633. # idea: traverse vertices following a reverse topological order, connecting
  634. # each vertex to its descendants at distance 2 as we go
  635. for v in reversed(topo_order):
  636. TC.add_edges_from((v, u) for u in nx.descendants_at_distance(TC, v, 2))
  637. return TC
  638. @not_implemented_for("undirected")
  639. def transitive_reduction(G):
  640. """Returns transitive reduction of a directed graph
  641. The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
  642. for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
  643. in E and there is no path from v to w in G with length greater than 1.
  644. Parameters
  645. ----------
  646. G : NetworkX DiGraph
  647. A directed acyclic graph (DAG)
  648. Returns
  649. -------
  650. NetworkX DiGraph
  651. The transitive reduction of `G`
  652. Raises
  653. ------
  654. NetworkXError
  655. If `G` is not a directed acyclic graph (DAG) transitive reduction is
  656. not uniquely defined and a :exc:`NetworkXError` exception is raised.
  657. Examples
  658. --------
  659. To perform transitive reduction on a DiGraph:
  660. >>> DG = nx.DiGraph([(1, 2), (2, 3), (1, 3)])
  661. >>> TR = nx.transitive_reduction(DG)
  662. >>> list(TR.edges)
  663. [(1, 2), (2, 3)]
  664. To avoid unnecessary data copies, this implementation does not return a
  665. DiGraph with node/edge data.
  666. To perform transitive reduction on a DiGraph and transfer node/edge data:
  667. >>> DG = nx.DiGraph()
  668. >>> DG.add_edges_from([(1, 2), (2, 3), (1, 3)], color='red')
  669. >>> TR = nx.transitive_reduction(DG)
  670. >>> TR.add_nodes_from(DG.nodes(data=True))
  671. >>> TR.add_edges_from((u, v, DG.edges[u, v]) for u, v in TR.edges)
  672. >>> list(TR.edges(data=True))
  673. [(1, 2, {'color': 'red'}), (2, 3, {'color': 'red'})]
  674. References
  675. ----------
  676. https://en.wikipedia.org/wiki/Transitive_reduction
  677. """
  678. if not is_directed_acyclic_graph(G):
  679. msg = "Directed Acyclic Graph required for transitive_reduction"
  680. raise nx.NetworkXError(msg)
  681. TR = nx.DiGraph()
  682. TR.add_nodes_from(G.nodes())
  683. descendants = {}
  684. # count before removing set stored in descendants
  685. check_count = dict(G.in_degree)
  686. for u in G:
  687. u_nbrs = set(G[u])
  688. for v in G[u]:
  689. if v in u_nbrs:
  690. if v not in descendants:
  691. descendants[v] = {y for x, y in nx.dfs_edges(G, v)}
  692. u_nbrs -= descendants[v]
  693. check_count[v] -= 1
  694. if check_count[v] == 0:
  695. del descendants[v]
  696. TR.add_edges_from((u, v) for v in u_nbrs)
  697. return TR
  698. @not_implemented_for("undirected")
  699. def antichains(G, topo_order=None):
  700. """Generates antichains from a directed acyclic graph (DAG).
  701. An antichain is a subset of a partially ordered set such that any
  702. two elements in the subset are incomparable.
  703. Parameters
  704. ----------
  705. G : NetworkX DiGraph
  706. A directed acyclic graph (DAG)
  707. topo_order: list or tuple, optional
  708. A topological order for G (if None, the function will compute one)
  709. Yields
  710. ------
  711. antichain : list
  712. a list of nodes in `G` representing an antichain
  713. Raises
  714. ------
  715. NetworkXNotImplemented
  716. If `G` is not directed
  717. NetworkXUnfeasible
  718. If `G` contains a cycle
  719. Examples
  720. --------
  721. >>> DG = nx.DiGraph([(1, 2), (1, 3)])
  722. >>> list(nx.antichains(DG))
  723. [[], [3], [2], [2, 3], [1]]
  724. Notes
  725. -----
  726. This function was originally developed by Peter Jipsen and Franco Saliola
  727. for the SAGE project. It's included in NetworkX with permission from the
  728. authors. Original SAGE code at:
  729. https://github.com/sagemath/sage/blob/master/src/sage/combinat/posets/hasse_diagram.py
  730. References
  731. ----------
  732. .. [1] Free Lattices, by R. Freese, J. Jezek and J. B. Nation,
  733. AMS, Vol 42, 1995, p. 226.
  734. """
  735. if topo_order is None:
  736. topo_order = list(nx.topological_sort(G))
  737. TC = nx.transitive_closure_dag(G, topo_order)
  738. antichains_stacks = [([], list(reversed(topo_order)))]
  739. while antichains_stacks:
  740. (antichain, stack) = antichains_stacks.pop()
  741. # Invariant:
  742. # - the elements of antichain are independent
  743. # - the elements of stack are independent from those of antichain
  744. yield antichain
  745. while stack:
  746. x = stack.pop()
  747. new_antichain = antichain + [x]
  748. new_stack = [t for t in stack if not ((t in TC[x]) or (x in TC[t]))]
  749. antichains_stacks.append((new_antichain, new_stack))
  750. @not_implemented_for("undirected")
  751. def dag_longest_path(G, weight="weight", default_weight=1, topo_order=None):
  752. """Returns the longest path in a directed acyclic graph (DAG).
  753. If `G` has edges with `weight` attribute the edge data are used as
  754. weight values.
  755. Parameters
  756. ----------
  757. G : NetworkX DiGraph
  758. A directed acyclic graph (DAG)
  759. weight : str, optional
  760. Edge data key to use for weight
  761. default_weight : int, optional
  762. The weight of edges that do not have a weight attribute
  763. topo_order: list or tuple, optional
  764. A topological order for `G` (if None, the function will compute one)
  765. Returns
  766. -------
  767. list
  768. Longest path
  769. Raises
  770. ------
  771. NetworkXNotImplemented
  772. If `G` is not directed
  773. Examples
  774. --------
  775. >>> DG = nx.DiGraph([(0, 1, {'cost':1}), (1, 2, {'cost':1}), (0, 2, {'cost':42})])
  776. >>> list(nx.all_simple_paths(DG, 0, 2))
  777. [[0, 1, 2], [0, 2]]
  778. >>> nx.dag_longest_path(DG)
  779. [0, 1, 2]
  780. >>> nx.dag_longest_path(DG, weight="cost")
  781. [0, 2]
  782. In the case where multiple valid topological orderings exist, `topo_order`
  783. can be used to specify a specific ordering:
  784. >>> DG = nx.DiGraph([(0, 1), (0, 2)])
  785. >>> sorted(nx.all_topological_sorts(DG)) # Valid topological orderings
  786. [[0, 1, 2], [0, 2, 1]]
  787. >>> nx.dag_longest_path(DG, topo_order=[0, 1, 2])
  788. [0, 1]
  789. >>> nx.dag_longest_path(DG, topo_order=[0, 2, 1])
  790. [0, 2]
  791. See also
  792. --------
  793. dag_longest_path_length
  794. """
  795. if not G:
  796. return []
  797. if topo_order is None:
  798. topo_order = nx.topological_sort(G)
  799. dist = {} # stores {v : (length, u)}
  800. for v in topo_order:
  801. us = [
  802. (
  803. dist[u][0]
  804. + (
  805. max(data.values(), key=lambda x: x.get(weight, default_weight))
  806. if G.is_multigraph()
  807. else data
  808. ).get(weight, default_weight),
  809. u,
  810. )
  811. for u, data in G.pred[v].items()
  812. ]
  813. # Use the best predecessor if there is one and its distance is
  814. # non-negative, otherwise terminate.
  815. maxu = max(us, key=lambda x: x[0]) if us else (0, v)
  816. dist[v] = maxu if maxu[0] >= 0 else (0, v)
  817. u = None
  818. v = max(dist, key=lambda x: dist[x][0])
  819. path = []
  820. while u != v:
  821. path.append(v)
  822. u = v
  823. v = dist[v][1]
  824. path.reverse()
  825. return path
  826. @not_implemented_for("undirected")
  827. def dag_longest_path_length(G, weight="weight", default_weight=1):
  828. """Returns the longest path length in a DAG
  829. Parameters
  830. ----------
  831. G : NetworkX DiGraph
  832. A directed acyclic graph (DAG)
  833. weight : string, optional
  834. Edge data key to use for weight
  835. default_weight : int, optional
  836. The weight of edges that do not have a weight attribute
  837. Returns
  838. -------
  839. int
  840. Longest path length
  841. Raises
  842. ------
  843. NetworkXNotImplemented
  844. If `G` is not directed
  845. Examples
  846. --------
  847. >>> DG = nx.DiGraph([(0, 1, {'cost':1}), (1, 2, {'cost':1}), (0, 2, {'cost':42})])
  848. >>> list(nx.all_simple_paths(DG, 0, 2))
  849. [[0, 1, 2], [0, 2]]
  850. >>> nx.dag_longest_path_length(DG)
  851. 2
  852. >>> nx.dag_longest_path_length(DG, weight="cost")
  853. 42
  854. See also
  855. --------
  856. dag_longest_path
  857. """
  858. path = nx.dag_longest_path(G, weight, default_weight)
  859. path_length = 0
  860. if G.is_multigraph():
  861. for u, v in pairwise(path):
  862. i = max(G[u][v], key=lambda x: G[u][v][x].get(weight, default_weight))
  863. path_length += G[u][v][i].get(weight, default_weight)
  864. else:
  865. for u, v in pairwise(path):
  866. path_length += G[u][v].get(weight, default_weight)
  867. return path_length
  868. def root_to_leaf_paths(G):
  869. """Yields root-to-leaf paths in a directed acyclic graph.
  870. `G` must be a directed acyclic graph. If not, the behavior of this
  871. function is undefined. A "root" in this graph is a node of in-degree
  872. zero and a "leaf" a node of out-degree zero.
  873. When invoked, this function iterates over each path from any root to
  874. any leaf. A path is a list of nodes.
  875. """
  876. roots = (v for v, d in G.in_degree() if d == 0)
  877. leaves = (v for v, d in G.out_degree() if d == 0)
  878. all_paths = partial(nx.all_simple_paths, G)
  879. # TODO In Python 3, this would be better as `yield from ...`.
  880. return chaini(starmap(all_paths, product(roots, leaves)))
  881. @not_implemented_for("multigraph")
  882. @not_implemented_for("undirected")
  883. def dag_to_branching(G):
  884. """Returns a branching representing all (overlapping) paths from
  885. root nodes to leaf nodes in the given directed acyclic graph.
  886. As described in :mod:`networkx.algorithms.tree.recognition`, a
  887. *branching* is a directed forest in which each node has at most one
  888. parent. In other words, a branching is a disjoint union of
  889. *arborescences*. For this function, each node of in-degree zero in
  890. `G` becomes a root of one of the arborescences, and there will be
  891. one leaf node for each distinct path from that root to a leaf node
  892. in `G`.
  893. Each node `v` in `G` with *k* parents becomes *k* distinct nodes in
  894. the returned branching, one for each parent, and the sub-DAG rooted
  895. at `v` is duplicated for each copy. The algorithm then recurses on
  896. the children of each copy of `v`.
  897. Parameters
  898. ----------
  899. G : NetworkX graph
  900. A directed acyclic graph.
  901. Returns
  902. -------
  903. DiGraph
  904. The branching in which there is a bijection between root-to-leaf
  905. paths in `G` (in which multiple paths may share the same leaf)
  906. and root-to-leaf paths in the branching (in which there is a
  907. unique path from a root to a leaf).
  908. Each node has an attribute 'source' whose value is the original
  909. node to which this node corresponds. No other graph, node, or
  910. edge attributes are copied into this new graph.
  911. Raises
  912. ------
  913. NetworkXNotImplemented
  914. If `G` is not directed, or if `G` is a multigraph.
  915. HasACycle
  916. If `G` is not acyclic.
  917. Examples
  918. --------
  919. To examine which nodes in the returned branching were produced by
  920. which original node in the directed acyclic graph, we can collect
  921. the mapping from source node to new nodes into a dictionary. For
  922. example, consider the directed diamond graph::
  923. >>> from collections import defaultdict
  924. >>> from operator import itemgetter
  925. >>>
  926. >>> G = nx.DiGraph(nx.utils.pairwise("abd"))
  927. >>> G.add_edges_from(nx.utils.pairwise("acd"))
  928. >>> B = nx.dag_to_branching(G)
  929. >>>
  930. >>> sources = defaultdict(set)
  931. >>> for v, source in B.nodes(data="source"):
  932. ... sources[source].add(v)
  933. >>> len(sources["a"])
  934. 1
  935. >>> len(sources["d"])
  936. 2
  937. To copy node attributes from the original graph to the new graph,
  938. you can use a dictionary like the one constructed in the above
  939. example::
  940. >>> for source, nodes in sources.items():
  941. ... for v in nodes:
  942. ... B.nodes[v].update(G.nodes[source])
  943. Notes
  944. -----
  945. This function is not idempotent in the sense that the node labels in
  946. the returned branching may be uniquely generated each time the
  947. function is invoked. In fact, the node labels may not be integers;
  948. in order to relabel the nodes to be more readable, you can use the
  949. :func:`networkx.convert_node_labels_to_integers` function.
  950. The current implementation of this function uses
  951. :func:`networkx.prefix_tree`, so it is subject to the limitations of
  952. that function.
  953. """
  954. if has_cycle(G):
  955. msg = "dag_to_branching is only defined for acyclic graphs"
  956. raise nx.HasACycle(msg)
  957. paths = root_to_leaf_paths(G)
  958. B = nx.prefix_tree(paths)
  959. # Remove the synthetic `root`(0) and `NIL`(-1) nodes from the tree
  960. B.remove_node(0)
  961. B.remove_node(-1)
  962. return B
  963. @not_implemented_for("undirected")
  964. def compute_v_structures(G):
  965. """Iterate through the graph to compute all v-structures.
  966. V-structures are triples in the directed graph where
  967. two parent nodes point to the same child and the two parent nodes
  968. are not adjacent.
  969. Parameters
  970. ----------
  971. G : graph
  972. A networkx DiGraph.
  973. Returns
  974. -------
  975. vstructs : iterator of tuples
  976. The v structures within the graph. Each v structure is a 3-tuple with the
  977. parent, collider, and other parent.
  978. Examples
  979. --------
  980. >>> G = nx.DiGraph()
  981. >>> G.add_edges_from([(1, 2), (0, 5), (3, 1), (2, 4), (3, 1), (4, 5), (1, 5)])
  982. >>> list(nx.compute_v_structures(G))
  983. [(0, 5, 4), (0, 5, 1), (1, 5, 4)]
  984. Notes
  985. -----
  986. https://en.wikipedia.org/wiki/Collider_(statistics)
  987. """
  988. for collider, preds in G.pred.items():
  989. for common_parents in combinations(preds, r=2):
  990. # ensure that the colliders are the same
  991. common_parents = sorted(common_parents)
  992. yield (common_parents[0], collider, common_parents[1])