comparison env/lib/python3.9/site-packages/networkx/algorithms/operators/all.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Operations on many graphs.
2 """
3 from itertools import zip_longest
4 import networkx as nx
5
6 __all__ = ["union_all", "compose_all", "disjoint_union_all", "intersection_all"]
7
8
9 def union_all(graphs, rename=(None,)):
10 """Returns the union of all graphs.
11
12 The graphs must be disjoint, otherwise an exception is raised.
13
14 Parameters
15 ----------
16 graphs : list of graphs
17 List of NetworkX graphs
18
19 rename : bool , default=(None, None)
20 Node names of G and H can be changed by specifying the tuple
21 rename=('G-','H-') (for example). Node "u" in G is then renamed
22 "G-u" and "v" in H is renamed "H-v".
23
24 Returns
25 -------
26 U : a graph with the same type as the first graph in list
27
28 Raises
29 ------
30 ValueError
31 If `graphs` is an empty list.
32
33 Notes
34 -----
35 To force a disjoint union with node relabeling, use
36 disjoint_union_all(G,H) or convert_node_labels_to integers().
37
38 Graph, edge, and node attributes are propagated to the union graph.
39 If a graph attribute is present in multiple graphs, then the value
40 from the last graph in the list with that attribute is used.
41
42 See Also
43 --------
44 union
45 disjoint_union_all
46 """
47 if not graphs:
48 raise ValueError("cannot apply union_all to an empty list")
49 graphs_names = zip_longest(graphs, rename)
50 U, gname = next(graphs_names)
51 for H, hname in graphs_names:
52 U = nx.union(U, H, (gname, hname))
53 gname = None
54 return U
55
56
57 def disjoint_union_all(graphs):
58 """Returns the disjoint union of all graphs.
59
60 This operation forces distinct integer node labels starting with 0
61 for the first graph in the list and numbering consecutively.
62
63 Parameters
64 ----------
65 graphs : list
66 List of NetworkX graphs
67
68 Returns
69 -------
70 U : A graph with the same type as the first graph in list
71
72 Raises
73 ------
74 ValueError
75 If `graphs` is an empty list.
76
77 Notes
78 -----
79 It is recommended that the graphs be either all directed or all undirected.
80
81 Graph, edge, and node attributes are propagated to the union graph.
82 If a graph attribute is present in multiple graphs, then the value
83 from the last graph in the list with that attribute is used.
84 """
85 if not graphs:
86 raise ValueError("cannot apply disjoint_union_all to an empty list")
87 graphs = iter(graphs)
88 U = next(graphs)
89 for H in graphs:
90 U = nx.disjoint_union(U, H)
91 return U
92
93
94 def compose_all(graphs):
95 """Returns the composition of all graphs.
96
97 Composition is the simple union of the node sets and edge sets.
98 The node sets of the supplied graphs need not be disjoint.
99
100 Parameters
101 ----------
102 graphs : list
103 List of NetworkX graphs
104
105 Returns
106 -------
107 C : A graph with the same type as the first graph in list
108
109 Raises
110 ------
111 ValueError
112 If `graphs` is an empty list.
113
114 Notes
115 -----
116 It is recommended that the supplied graphs be either all directed or all
117 undirected.
118
119 Graph, edge, and node attributes are propagated to the union graph.
120 If a graph attribute is present in multiple graphs, then the value
121 from the last graph in the list with that attribute is used.
122 """
123 if not graphs:
124 raise ValueError("cannot apply compose_all to an empty list")
125 graphs = iter(graphs)
126 C = next(graphs)
127 for H in graphs:
128 C = nx.compose(C, H)
129 return C
130
131
132 def intersection_all(graphs):
133 """Returns a new graph that contains only the edges that exist in
134 all graphs.
135
136 All supplied graphs must have the same node set.
137
138 Parameters
139 ----------
140 graphs : list
141 List of NetworkX graphs
142
143 Returns
144 -------
145 R : A new graph with the same type as the first graph in list
146
147 Raises
148 ------
149 ValueError
150 If `graphs` is an empty list.
151
152 Notes
153 -----
154 Attributes from the graph, nodes, and edges are not copied to the new
155 graph.
156 """
157 if not graphs:
158 raise ValueError("cannot apply intersection_all to an empty list")
159 graphs = iter(graphs)
160 R = next(graphs)
161 for H in graphs:
162 R = nx.intersection(R, H)
163 return R