-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.py
821 lines (669 loc) · 24.2 KB
/
dag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# -*- coding: utf-8 -*-
# Copyright (C) 2006-2018 by
# Aric Hagberg <[email protected]>
# Dan Schult <[email protected]>
# Pieter Swart <[email protected]>
# All rights reserved.
# BSD license.
#
# Authors:
# Aric Hagberg <[email protected]>
# Dan Schult <[email protected]>
# Ben Edwards <[email protected]>
# Neil Girdhar <[email protected]>
#
"""Algorithms for directed acyclic graphs (DAGs).
Note that most of these functions are only guaranteed to work for DAGs.
In general, these functions do not check for acyclic-ness, so it is up
to the user to check for that.
"""
from collections import defaultdict, deque
from fractions import gcd
from functools import partial
from itertools import chain
from itertools import product
from itertools import starmap
import heapq
import networkx as nx
from networkx.generators.trees import NIL
from networkx.utils import arbitrary_element
from networkx.utils import consume
from networkx.utils import pairwise
from networkx.utils import generate_unique_node
from networkx.utils import not_implemented_for
__all__ = ['descendants',
'ancestors',
'topological_sort',
'lexicographical_topological_sort',
'all_topological_sorts',
'is_directed_acyclic_graph',
'is_aperiodic',
'transitive_closure',
'transitive_reduction',
'antichains',
'dag_longest_path',
'dag_longest_path_length',
'dag_to_branching']
chaini = chain.from_iterable
def descendants(G, source):
"""Return all nodes reachable from `source` in `G`.
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
source : node in `G`
Returns
-------
set()
The descendants of `source` in `G`
"""
if not G.has_node(source):
raise nx.NetworkXError("The node %s is not in the graph." % source)
des = set(n for n, d in nx.shortest_path_length(G, source=source).items())
return des - {source}
def ancestors(G, source):
"""Return all nodes having a path to `source` in `G`.
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
source : node in `G`
Returns
-------
set()
The ancestors of source in G
"""
if not G.has_node(source):
raise nx.NetworkXError("The node %s is not in the graph." % source)
anc = set(n for n, d in nx.shortest_path_length(G, target=source).items())
return anc - {source}
def has_cycle(G):
"""Decides whether the directed graph has a cycle."""
try:
consume(topological_sort(G))
except nx.NetworkXUnfeasible:
return True
else:
return False
def is_directed_acyclic_graph(G):
"""Return True if the graph `G` is a directed acyclic graph (DAG) or
False if not.
Parameters
----------
G : NetworkX graph
Returns
-------
bool
True if `G` is a DAG, False otherwise
"""
return G.is_directed() and not has_cycle(G)
def topological_sort(G):
"""Return a generator of nodes in topologically sorted order.
A topological sort is a nonunique permutation of the nodes such that an
edge from u to v implies that u appears before v in the topological sort
order.
Parameters
----------
G : NetworkX digraph
A directed acyclic graph (DAG)
Returns
-------
iterable
An iterable of node names in topological sorted order.
Raises
------
NetworkXError
Topological sort is defined for directed graphs only. If the graph `G`
is undirected, a :exc:`NetworkXError` is raised.
NetworkXUnfeasible
If `G` is not a directed acyclic graph (DAG) no topological sort exists
and a :exc:`NetworkXUnfeasible` exception is raised. This can also be
raised if `G` is changed while the returned iterator is being processed
RuntimeError
If `G` is changed while the returned iterator is being processed.
Examples
--------
To get the reverse order of the topological sort:
>>> DG = nx.DiGraph([(1, 2), (2, 3)])
>>> list(reversed(list(nx.topological_sort(DG))))
[3, 2, 1]
If your DiGraph naturally has the edges representing tasks/inputs
and nodes representing people/processes that initiate tasks, then
topological_sort is not quite what you need. You will have to change
the tasks to nodes with dependence reflected by edges. The result is
a kind of topological sort of the edges. This can be done
with :func:`networkx.line_graph` as follows:
>>> list(nx.topological_sort(nx.line_graph(DG)))
[(1, 2), (2, 3)]
Notes
-----
This algorithm is based on a description and proof in
"Introduction to Algorithms: A Creative Approach" [1]_ .
See also
--------
is_directed_acyclic_graph, lexicographical_topological_sort
References
----------
.. [1] Manber, U. (1989).
*Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
"""
if not G.is_directed():
raise nx.NetworkXError(
"Topological sort not defined on undirected graphs.")
indegree_map = {v: d for v, d in G.in_degree() if d > 0}
# These nodes have zero indegree and ready to be returned.
zero_indegree = [v for v, d in G.in_degree() if d == 0]
while zero_indegree:
node = zero_indegree.pop()
if node not in G:
raise RuntimeError("Graph changed during iteration")
for _, child in G.edges(node):
try:
indegree_map[child] -= 1
except KeyError:
raise RuntimeError("Graph changed during iteration")
if indegree_map[child] == 0:
zero_indegree.append(child)
del indegree_map[child]
yield node
if indegree_map:
raise nx.NetworkXUnfeasible("Graph contains a cycle or graph changed "
"during iteration")
def lexicographical_topological_sort(G, key=None):
"""Return a generator of nodes in lexicographically topologically sorted
order.
A topological sort is a nonunique permutation of the nodes such that an
edge from u to v implies that u appears before v in the topological sort
order.
Parameters
----------
G : NetworkX digraph
A directed acyclic graph (DAG)
key : function, optional
This function maps nodes to keys with which to resolve ambiguities in
the sort order. Defaults to the identity function.
Returns
-------
iterable
An iterable of node names in lexicographical topological sort order.
Raises
------
NetworkXError
Topological sort is defined for directed graphs only. If the graph `G`
is undirected, a :exc:`NetworkXError` is raised.
NetworkXUnfeasible
If `G` is not a directed acyclic graph (DAG) no topological sort exists
and a :exc:`NetworkXUnfeasible` exception is raised. This can also be
raised if `G` is changed while the returned iterator is being processed
RuntimeError
If `G` is changed while the returned iterator is being processed.
Notes
-----
This algorithm is based on a description and proof in
"Introduction to Algorithms: A Creative Approach" [1]_ .
See also
--------
topological_sort
References
----------
.. [1] Manber, U. (1989).
*Introduction to Algorithms - A Creative Approach.* Addison-Wesley.
"""
if not G.is_directed():
raise nx.NetworkXError(
"Topological sort not defined on undirected graphs.")
if key is None:
def key(x): return x
def create_tuple(node):
return key(node), node
indegree_map = {v: d for v, d in G.in_degree() if d > 0}
# These nodes have zero indegree and ready to be returned.
zero_indegree = [create_tuple(v) for v, d in G.in_degree() if d == 0]
heapq.heapify(zero_indegree)
while zero_indegree:
_, node = heapq.heappop(zero_indegree)
if node not in G:
raise RuntimeError("Graph changed during iteration")
for _, child in G.edges(node):
try:
indegree_map[child] -= 1
except KeyError:
raise RuntimeError("Graph changed during iteration")
if indegree_map[child] == 0:
heapq.heappush(zero_indegree, create_tuple(child))
del indegree_map[child]
yield node
if indegree_map:
raise nx.NetworkXUnfeasible("Graph contains a cycle or graph changed "
"during iteration")
@not_implemented_for('undirected')
def all_topological_sorts(G):
"""Returns a generator of _all_ topological sorts of the directed graph G.
A topological sort is a nonunique permutation of the nodes such that an
edge from u to v implies that u appears before v in the topological sort
order.
Parameters
----------
G : NetworkX DiGraph
A directed graph
Returns
-------
generator
All topological sorts of the digraph G
Raises
------
NetworkXNotImplemented
If `G` is not directed
NetworkXUnfeasible
If `G` is not acyclic
Examples
--------
To enumerate all topological sorts of directed graph:
>>> DG = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
>>> list(nx.all_topological_sorts(DG))
[[1, 2, 4, 3], [1, 2, 3, 4]]
Notes
-----
Implements an iterative version of the algorithm given in [1].
References
----------
.. [1] Knuth, Donald E., Szwarcfiter, Jayme L. (1974).
"A Structured Program to Generate All Topological Sorting Arrangements"
Information Processing Letters, Volume 2, Issue 6, 1974, Pages 153-157,
ISSN 0020-0190,
https://doi.org/10.1016/0020-0190(74)90001-5.
Elsevier (North-Holland), Amsterdam
"""
if not G.is_directed():
raise nx.NetworkXError(
"Topological sort not defined on undirected graphs.")
# the names of count and D are chosen to match the global variables in [1]
# number of edges originating in a vertex v
count = dict(G.in_degree())
# vertices with indegree 0
D = deque([v for v, d in G.in_degree() if d == 0])
# stack of first value chosen at a position k in the topological sort
bases = []
current_sort = []
# do-while construct
while True:
assert all([count[v] == 0 for v in D])
if len(current_sort) == len(G):
yield list(current_sort)
# clean-up stack
while len(current_sort) > 0:
assert len(bases) == len(current_sort)
q = current_sort.pop()
# "restores" all edges (q, x)
# NOTE: it is important to iterate over edges instead
# of successors, so count is updated correctly in multigraphs
for _, j in G.out_edges(q):
count[j] += 1
assert count[j] >= 0
# remove entries from D
while len(D) > 0 and count[D[-1]] > 0:
D.pop()
# corresponds to a circular shift of the values in D
# if the first value chosen (the base) is in the first
# position of D again, we are done and need to consider the
# previous condition
D.appendleft(q)
if D[-1] == bases[-1]:
# all possible values have been chosen at current position
# remove corresponding marker
bases.pop()
else:
# there are still elements that have not been fixed
# at the current position in the topological sort
# stop removing elements, escape inner loop
break
else:
if len(D) == 0:
raise nx.NetworkXUnfeasible("Graph contains a cycle.")
# choose next node
q = D.pop()
# "erase" all edges (q, x)
# NOTE: it is important to iterate over edges instead
# of successors, so count is updated correctly in multigraphs
for _, j in G.out_edges(q):
count[j] -= 1
assert count[j] >= 0
if count[j] == 0:
D.append(j)
current_sort.append(q)
# base for current position might _not_ be fixed yet
if len(bases) < len(current_sort):
bases.append(q)
if len(bases) == 0:
break
def is_aperiodic(G):
"""Return True if `G` is aperiodic.
A directed graph is aperiodic if there is no integer k > 1 that
divides the length of every cycle in the graph.
Parameters
----------
G : NetworkX DiGraph
A directed graph
Returns
-------
bool
True if the graph is aperiodic False otherwise
Raises
------
NetworkXError
If `G` is not directed
Notes
-----
This uses the method outlined in [1]_, which runs in $O(m)$ time
given $m$ edges in `G`. Note that a graph is not aperiodic if it is
acyclic as every integer trivial divides length 0 cycles.
References
----------
.. [1] Jarvis, J. P.; Shier, D. R. (1996),
"Graph-theoretic analysis of finite Markov chains,"
in Shier, D. R.; Wallenius, K. T., Applied Mathematical Modeling:
A Multidisciplinary Approach, CRC Press.
"""
if not G.is_directed():
raise nx.NetworkXError(
"is_aperiodic not defined for undirected graphs")
s = arbitrary_element(G)
levels = {s: 0}
this_level = [s]
g = 0
l = 1
while this_level:
next_level = []
for u in this_level:
for v in G[u]:
if v in levels: # Non-Tree Edge
g = gcd(g, levels[u] - levels[v] + 1)
else: # Tree Edge
next_level.append(v)
levels[v] = l
this_level = next_level
l += 1
if len(levels) == len(G): # All nodes in tree
return g == 1
else:
return g == 1 and nx.is_aperiodic(G.subgraph(set(G) - set(levels)))
@not_implemented_for('undirected')
def transitive_closure(G):
""" Returns transitive closure of a directed graph
The transitive closure of G = (V,E) is a graph G+ = (V,E+) such that
for all v,w in V there is an edge (v,w) in E+ if and only if there
is a non-null path from v to w in G.
Parameters
----------
G : NetworkX DiGraph
A directed graph
Returns
-------
NetworkX DiGraph
The transitive closure of `G`
Raises
------
NetworkXNotImplemented
If `G` is not directed
References
----------
.. [1] http://www.ics.uci.edu/~eppstein/PADS/PartialOrder.py
"""
TC = G.copy()
for v in G:
TC.add_edges_from((v, u) for u in nx.dfs_preorder_nodes(G, source=v)
if v != u)
return TC
@not_implemented_for('undirected')
def transitive_reduction(G):
""" Returns transitive reduction of a directed graph
The transitive reduction of G = (V,E) is a graph G- = (V,E-) such that
for all v,w in V there is an edge (v,w) in E- if and only if (v,w) is
in E and there is no path from v to w in G with length greater than 1.
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
Returns
-------
NetworkX DiGraph
The transitive reduction of `G`
Raises
------
NetworkXError
If `G` is not a directed acyclic graph (DAG) transitive reduction is
not uniquely defined and a :exc:`NetworkXError` exception is raised.
References
----------
https://en.wikipedia.org/wiki/Transitive_reduction
"""
if not is_directed_acyclic_graph(G):
msg = "Directed Acyclic Graph required for transitive_reduction"
raise nx.NetworkXError(msg)
TR = nx.DiGraph()
TR.add_nodes_from(G.nodes())
descendants = {}
# count before removing set stored in descendants
check_count = dict(G.in_degree)
for u in G:
u_nbrs = set(G[u])
for v in G[u]:
if v in u_nbrs:
if v not in descendants:
descendants[v] = {y for x, y in nx.dfs_edges(G, v)}
u_nbrs -= descendants[v]
check_count[v] -= 1
if check_count[v] == 0:
del descendants[v]
TR.add_edges_from((u, v) for v in u_nbrs)
return TR
@not_implemented_for('undirected')
def antichains(G):
"""Generates antichains from a directed acyclic graph (DAG).
An antichain is a subset of a partially ordered set such that any
two elements in the subset are incomparable.
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
Returns
-------
generator object
Raises
------
NetworkXNotImplemented
If `G` is not directed
NetworkXUnfeasible
If `G` contains a cycle
Notes
-----
This function was originally developed by Peter Jipsen and Franco Saliola
for the SAGE project. It's included in NetworkX with permission from the
authors. Original SAGE code at:
https://github.com/sagemath/sage/blob/master/src/sage/combinat/posets/hasse_diagram.py
References
----------
.. [1] Free Lattices, by R. Freese, J. Jezek and J. B. Nation,
AMS, Vol 42, 1995, p. 226.
"""
TC = nx.transitive_closure(G)
antichains_stacks = [([], list(reversed(list(nx.topological_sort(G)))))]
while antichains_stacks:
(antichain, stack) = antichains_stacks.pop()
# Invariant:
# - the elements of antichain are independent
# - the elements of stack are independent from those of antichain
yield antichain
while stack:
x = stack.pop()
new_antichain = antichain + [x]
new_stack = [
t for t in stack if not ((t in TC[x]) or (x in TC[t]))]
antichains_stacks.append((new_antichain, new_stack))
@not_implemented_for('undirected')
def dag_longest_path(G, weight='weight', default_weight=1):
"""Returns the longest path in a directed acyclic graph (DAG).
If `G` has edges with `weight` attribute the edge data are used as
weight values.
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
weight : str, optional
Edge data key to use for weight
default_weight : int, optional
The weight of edges that do not have a weight attribute
Returns
-------
list
Longest path
Raises
------
NetworkXNotImplemented
If `G` is not directed
See also
--------
dag_longest_path_length
"""
if not G:
return []
dist = {} # stores {v : (length, u)}
for v in nx.topological_sort(G):
us = [(dist[u][0] + data.get(weight, default_weight), u)
for u, data in G.pred[v].items()]
# Use the best predecessor if there is one and its distance is
# non-negative, otherwise terminate.
maxu = max(us, key=lambda x: x[0]) if us else (0, v)
dist[v] = maxu if maxu[0] >= 0 else (0, v)
u = None
v = max(dist, key=lambda x: dist[x][0])
path = []
while u != v:
path.append(v)
u = v
v = dist[v][1]
path.reverse()
return path
@not_implemented_for('undirected')
def dag_longest_path_length(G, weight='weight', default_weight=1):
"""Returns the longest path length in a DAG
Parameters
----------
G : NetworkX DiGraph
A directed acyclic graph (DAG)
weight : string, optional
Edge data key to use for weight
default_weight : int, optional
The weight of edges that do not have a weight attribute
Returns
-------
int
Longest path length
Raises
------
NetworkXNotImplemented
If `G` is not directed
See also
--------
dag_longest_path
"""
path = nx.dag_longest_path(G, weight, default_weight)
path_length = 0
for (u, v) in pairwise(path):
path_length += G[u][v].get(weight, default_weight)
return path_length
def root_to_leaf_paths(G):
"""Yields root-to-leaf paths in a directed acyclic graph.
`G` must be a directed acyclic graph. If not, the behavior of this
function is undefined. A "root" in this graph is a node of in-degree
zero and a "leaf" a node of out-degree zero.
When invoked, this function iterates over each path from any root to
any leaf. A path is a list of nodes.
"""
roots = (v for v, d in G.in_degree() if d == 0)
leaves = (v for v, d in G.out_degree() if d == 0)
all_paths = partial(nx.all_simple_paths, G)
# TODO In Python 3, this would be better as `yield from ...`.
return chaini(starmap(all_paths, product(roots, leaves)))
@not_implemented_for('multigraph')
@not_implemented_for('undirected')
def dag_to_branching(G):
"""Returns a branching representing all (overlapping) paths from
root nodes to leaf nodes in the given directed acyclic graph.
As described in :mod:`networkx.algorithms.tree.recognition`, a
*branching* is a directed forest in which each node has at most one
parent. In other words, a branching is a disjoint union of
*arborescences*. For this function, each node of in-degree zero in
`G` becomes a root of one of the arborescences, and there will be
one leaf node for each distinct path from that root to a leaf node
in `G`.
Each node `v` in `G` with *k* parents becomes *k* distinct nodes in
the returned branching, one for each parent, and the sub-DAG rooted
at `v` is duplicated for each copy. The algorithm then recurses on
the children of each copy of `v`.
Parameters
----------
G : NetworkX graph
A directed acyclic graph.
Returns
-------
DiGraph
The branching in which there is a bijection between root-to-leaf
paths in `G` (in which multiple paths may share the same leaf)
and root-to-leaf paths in the branching (in which there is a
unique path from a root to a leaf).
Each node has an attribute 'source' whose value is the original
node to which this node corresponds. No other graph, node, or
edge attributes are copied into this new graph.
Raises
------
NetworkXNotImplemented
If `G` is not directed, or if `G` is a multigraph.
HasACycle
If `G` is not acyclic.
Examples
--------
To examine which nodes in the returned branching were produced by
which original node in the directed acyclic graph, we can collect
the mapping from source node to new nodes into a dictionary. For
example, consider the directed diamond graph::
>>> from collections import defaultdict
>>> from operator import itemgetter
>>>
>>> G = nx.DiGraph(nx.utils.pairwise('abd'))
>>> G.add_edges_from(nx.utils.pairwise('acd'))
>>> B = nx.dag_to_branching(G)
>>>
>>> sources = defaultdict(set)
>>> for v, source in B.nodes(data='source'):
... sources[source].add(v)
>>> len(sources['a'])
1
>>> len(sources['d'])
2
To copy node attributes from the original graph to the new graph,
you can use a dictionary like the one constructed in the above
example::
>>> for source, nodes in sources.items():
... for v in nodes:
... B.node[v].update(G.node[source])
Notes
-----
This function is not idempotent in the sense that the node labels in
the returned branching may be uniquely generated each time the
function is invoked. In fact, the node labels may not be integers;
in order to relabel the nodes to be more readable, you can use the
:func:`networkx.convert_node_labels_to_integers` function.
The current implementation of this function uses
:func:`networkx.prefix_tree`, so it is subject to the limitations of
that function.
"""
if has_cycle(G):
msg = 'dag_to_branching is only defined for acyclic graphs'
raise nx.HasACycle(msg)
paths = root_to_leaf_paths(G)
B, root = nx.prefix_tree(paths)
# Remove the synthetic `root` and `NIL` nodes in the prefix tree.
B.remove_node(root)
B.remove_node(NIL)
return B