Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

161249ba-7bda-4541-bb94-51b6a74586f3 14 KB
Raw

You have to be logged in to leave a comment. Sign In
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
  1. """Unit tests for the :mod:`networkx.algorithms.minors.contraction` module."""
  2. import pytest
  3. import networkx as nx
  4. from networkx.utils import arbitrary_element, edges_equal, nodes_equal
  5. def test_quotient_graph_complete_multipartite():
  6. """Tests that the quotient graph of the complete *n*-partite graph
  7. under the "same neighbors" node relation is the complete graph on *n*
  8. nodes.
  9. """
  10. G = nx.complete_multipartite_graph(2, 3, 4)
  11. # Two nodes are equivalent if they are not adjacent but have the same
  12. # neighbor set.
  13. def same_neighbors(u, v):
  14. return u not in G[v] and v not in G[u] and G[u] == G[v]
  15. expected = nx.complete_graph(3)
  16. actual = nx.quotient_graph(G, same_neighbors)
  17. # It won't take too long to run a graph isomorphism algorithm on such
  18. # small graphs.
  19. assert nx.is_isomorphic(expected, actual)
  20. def test_quotient_graph_complete_bipartite():
  21. """Tests that the quotient graph of the complete bipartite graph under
  22. the "same neighbors" node relation is `K_2`.
  23. """
  24. G = nx.complete_bipartite_graph(2, 3)
  25. # Two nodes are equivalent if they are not adjacent but have the same
  26. # neighbor set.
  27. def same_neighbors(u, v):
  28. return u not in G[v] and v not in G[u] and G[u] == G[v]
  29. expected = nx.complete_graph(2)
  30. actual = nx.quotient_graph(G, same_neighbors)
  31. # It won't take too long to run a graph isomorphism algorithm on such
  32. # small graphs.
  33. assert nx.is_isomorphic(expected, actual)
  34. def test_quotient_graph_edge_relation():
  35. """Tests for specifying an alternate edge relation for the quotient
  36. graph.
  37. """
  38. G = nx.path_graph(5)
  39. def identity(u, v):
  40. return u == v
  41. def same_parity(b, c):
  42. return arbitrary_element(b) % 2 == arbitrary_element(c) % 2
  43. actual = nx.quotient_graph(G, identity, same_parity)
  44. expected = nx.Graph()
  45. expected.add_edges_from([(0, 2), (0, 4), (2, 4)])
  46. expected.add_edge(1, 3)
  47. assert nx.is_isomorphic(actual, expected)
  48. def test_condensation_as_quotient():
  49. """This tests that the condensation of a graph can be viewed as the
  50. quotient graph under the "in the same connected component" equivalence
  51. relation.
  52. """
  53. # This example graph comes from the file `test_strongly_connected.py`.
  54. G = nx.DiGraph()
  55. G.add_edges_from(
  56. [
  57. (1, 2),
  58. (2, 3),
  59. (2, 11),
  60. (2, 12),
  61. (3, 4),
  62. (4, 3),
  63. (4, 5),
  64. (5, 6),
  65. (6, 5),
  66. (6, 7),
  67. (7, 8),
  68. (7, 9),
  69. (7, 10),
  70. (8, 9),
  71. (9, 7),
  72. (10, 6),
  73. (11, 2),
  74. (11, 4),
  75. (11, 6),
  76. (12, 6),
  77. (12, 11),
  78. ]
  79. )
  80. scc = list(nx.strongly_connected_components(G))
  81. C = nx.condensation(G, scc)
  82. component_of = C.graph["mapping"]
  83. # Two nodes are equivalent if they are in the same connected component.
  84. def same_component(u, v):
  85. return component_of[u] == component_of[v]
  86. Q = nx.quotient_graph(G, same_component)
  87. assert nx.is_isomorphic(C, Q)
  88. def test_path():
  89. G = nx.path_graph(6)
  90. partition = [{0, 1}, {2, 3}, {4, 5}]
  91. M = nx.quotient_graph(G, partition, relabel=True)
  92. assert nodes_equal(M, [0, 1, 2])
  93. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  94. for n in M:
  95. assert M.nodes[n]["nedges"] == 1
  96. assert M.nodes[n]["nnodes"] == 2
  97. assert M.nodes[n]["density"] == 1
  98. def test_path__partition_provided_as_dict_of_lists():
  99. G = nx.path_graph(6)
  100. partition = {0: [0, 1], 2: [2, 3], 4: [4, 5]}
  101. M = nx.quotient_graph(G, partition, relabel=True)
  102. assert nodes_equal(M, [0, 1, 2])
  103. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  104. for n in M:
  105. assert M.nodes[n]["nedges"] == 1
  106. assert M.nodes[n]["nnodes"] == 2
  107. assert M.nodes[n]["density"] == 1
  108. def test_path__partition_provided_as_dict_of_tuples():
  109. G = nx.path_graph(6)
  110. partition = {0: (0, 1), 2: (2, 3), 4: (4, 5)}
  111. M = nx.quotient_graph(G, partition, relabel=True)
  112. assert nodes_equal(M, [0, 1, 2])
  113. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  114. for n in M:
  115. assert M.nodes[n]["nedges"] == 1
  116. assert M.nodes[n]["nnodes"] == 2
  117. assert M.nodes[n]["density"] == 1
  118. def test_path__partition_provided_as_dict_of_sets():
  119. G = nx.path_graph(6)
  120. partition = {0: {0, 1}, 2: {2, 3}, 4: {4, 5}}
  121. M = nx.quotient_graph(G, partition, relabel=True)
  122. assert nodes_equal(M, [0, 1, 2])
  123. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  124. for n in M:
  125. assert M.nodes[n]["nedges"] == 1
  126. assert M.nodes[n]["nnodes"] == 2
  127. assert M.nodes[n]["density"] == 1
  128. def test_multigraph_path():
  129. G = nx.MultiGraph(nx.path_graph(6))
  130. partition = [{0, 1}, {2, 3}, {4, 5}]
  131. M = nx.quotient_graph(G, partition, relabel=True)
  132. assert nodes_equal(M, [0, 1, 2])
  133. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  134. for n in M:
  135. assert M.nodes[n]["nedges"] == 1
  136. assert M.nodes[n]["nnodes"] == 2
  137. assert M.nodes[n]["density"] == 1
  138. def test_directed_path():
  139. G = nx.DiGraph()
  140. nx.add_path(G, range(6))
  141. partition = [{0, 1}, {2, 3}, {4, 5}]
  142. M = nx.quotient_graph(G, partition, relabel=True)
  143. assert nodes_equal(M, [0, 1, 2])
  144. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  145. for n in M:
  146. assert M.nodes[n]["nedges"] == 1
  147. assert M.nodes[n]["nnodes"] == 2
  148. assert M.nodes[n]["density"] == 0.5
  149. def test_directed_multigraph_path():
  150. G = nx.MultiDiGraph()
  151. nx.add_path(G, range(6))
  152. partition = [{0, 1}, {2, 3}, {4, 5}]
  153. M = nx.quotient_graph(G, partition, relabel=True)
  154. assert nodes_equal(M, [0, 1, 2])
  155. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  156. for n in M:
  157. assert M.nodes[n]["nedges"] == 1
  158. assert M.nodes[n]["nnodes"] == 2
  159. assert M.nodes[n]["density"] == 0.5
  160. def test_overlapping_blocks():
  161. with pytest.raises(nx.NetworkXException):
  162. G = nx.path_graph(6)
  163. partition = [{0, 1, 2}, {2, 3}, {4, 5}]
  164. nx.quotient_graph(G, partition)
  165. def test_weighted_path():
  166. G = nx.path_graph(6)
  167. for i in range(5):
  168. G[i][i + 1]["w"] = i + 1
  169. partition = [{0, 1}, {2, 3}, {4, 5}]
  170. M = nx.quotient_graph(G, partition, weight="w", relabel=True)
  171. assert nodes_equal(M, [0, 1, 2])
  172. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  173. assert M[0][1]["weight"] == 2
  174. assert M[1][2]["weight"] == 4
  175. for n in M:
  176. assert M.nodes[n]["nedges"] == 1
  177. assert M.nodes[n]["nnodes"] == 2
  178. assert M.nodes[n]["density"] == 1
  179. def test_barbell():
  180. G = nx.barbell_graph(3, 0)
  181. partition = [{0, 1, 2}, {3, 4, 5}]
  182. M = nx.quotient_graph(G, partition, relabel=True)
  183. assert nodes_equal(M, [0, 1])
  184. assert edges_equal(M.edges(), [(0, 1)])
  185. for n in M:
  186. assert M.nodes[n]["nedges"] == 3
  187. assert M.nodes[n]["nnodes"] == 3
  188. assert M.nodes[n]["density"] == 1
  189. def test_barbell_plus():
  190. G = nx.barbell_graph(3, 0)
  191. # Add an extra edge joining the bells.
  192. G.add_edge(0, 5)
  193. partition = [{0, 1, 2}, {3, 4, 5}]
  194. M = nx.quotient_graph(G, partition, relabel=True)
  195. assert nodes_equal(M, [0, 1])
  196. assert edges_equal(M.edges(), [(0, 1)])
  197. assert M[0][1]["weight"] == 2
  198. for n in M:
  199. assert M.nodes[n]["nedges"] == 3
  200. assert M.nodes[n]["nnodes"] == 3
  201. assert M.nodes[n]["density"] == 1
  202. def test_blockmodel():
  203. G = nx.path_graph(6)
  204. partition = [[0, 1], [2, 3], [4, 5]]
  205. M = nx.quotient_graph(G, partition, relabel=True)
  206. assert nodes_equal(M.nodes(), [0, 1, 2])
  207. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  208. for n in M.nodes():
  209. assert M.nodes[n]["nedges"] == 1
  210. assert M.nodes[n]["nnodes"] == 2
  211. assert M.nodes[n]["density"] == 1.0
  212. def test_multigraph_blockmodel():
  213. G = nx.MultiGraph(nx.path_graph(6))
  214. partition = [[0, 1], [2, 3], [4, 5]]
  215. M = nx.quotient_graph(G, partition, create_using=nx.MultiGraph(), relabel=True)
  216. assert nodes_equal(M.nodes(), [0, 1, 2])
  217. assert edges_equal(M.edges(), [(0, 1), (1, 2)])
  218. for n in M.nodes():
  219. assert M.nodes[n]["nedges"] == 1
  220. assert M.nodes[n]["nnodes"] == 2
  221. assert M.nodes[n]["density"] == 1.0
  222. def test_quotient_graph_incomplete_partition():
  223. G = nx.path_graph(6)
  224. partition = []
  225. H = nx.quotient_graph(G, partition, relabel=True)
  226. assert nodes_equal(H.nodes(), [])
  227. assert edges_equal(H.edges(), [])
  228. partition = [[0, 1], [2, 3], [5]]
  229. H = nx.quotient_graph(G, partition, relabel=True)
  230. assert nodes_equal(H.nodes(), [0, 1, 2])
  231. assert edges_equal(H.edges(), [(0, 1)])
  232. def test_undirected_node_contraction():
  233. """Tests for node contraction in an undirected graph."""
  234. G = nx.cycle_graph(4)
  235. actual = nx.contracted_nodes(G, 0, 1)
  236. expected = nx.cycle_graph(3)
  237. expected.add_edge(0, 0)
  238. assert nx.is_isomorphic(actual, expected)
  239. def test_directed_node_contraction():
  240. """Tests for node contraction in a directed graph."""
  241. G = nx.DiGraph(nx.cycle_graph(4))
  242. actual = nx.contracted_nodes(G, 0, 1)
  243. expected = nx.DiGraph(nx.cycle_graph(3))
  244. expected.add_edge(0, 0)
  245. expected.add_edge(0, 0)
  246. assert nx.is_isomorphic(actual, expected)
  247. def test_undirected_node_contraction_no_copy():
  248. """Tests for node contraction in an undirected graph
  249. by making changes in place."""
  250. G = nx.cycle_graph(4)
  251. actual = nx.contracted_nodes(G, 0, 1, copy=False)
  252. expected = nx.cycle_graph(3)
  253. expected.add_edge(0, 0)
  254. assert nx.is_isomorphic(actual, G)
  255. assert nx.is_isomorphic(actual, expected)
  256. def test_directed_node_contraction_no_copy():
  257. """Tests for node contraction in a directed graph
  258. by making changes in place."""
  259. G = nx.DiGraph(nx.cycle_graph(4))
  260. actual = nx.contracted_nodes(G, 0, 1, copy=False)
  261. expected = nx.DiGraph(nx.cycle_graph(3))
  262. expected.add_edge(0, 0)
  263. expected.add_edge(0, 0)
  264. assert nx.is_isomorphic(actual, G)
  265. assert nx.is_isomorphic(actual, expected)
  266. def test_create_multigraph():
  267. """Tests that using a MultiGraph creates multiple edges."""
  268. G = nx.path_graph(3, create_using=nx.MultiGraph())
  269. G.add_edge(0, 1)
  270. G.add_edge(0, 0)
  271. G.add_edge(0, 2)
  272. actual = nx.contracted_nodes(G, 0, 2)
  273. expected = nx.MultiGraph()
  274. expected.add_edge(0, 1)
  275. expected.add_edge(0, 1)
  276. expected.add_edge(0, 1)
  277. expected.add_edge(0, 0)
  278. expected.add_edge(0, 0)
  279. assert edges_equal(actual.edges, expected.edges)
  280. def test_multigraph_keys():
  281. """Tests that multiedge keys are reset in new graph."""
  282. G = nx.path_graph(3, create_using=nx.MultiGraph())
  283. G.add_edge(0, 1, 5)
  284. G.add_edge(0, 0, 0)
  285. G.add_edge(0, 2, 5)
  286. actual = nx.contracted_nodes(G, 0, 2)
  287. expected = nx.MultiGraph()
  288. expected.add_edge(0, 1, 0)
  289. expected.add_edge(0, 1, 5)
  290. expected.add_edge(0, 1, 2) # keyed as 2 b/c 2 edges already in G
  291. expected.add_edge(0, 0, 0)
  292. expected.add_edge(0, 0, 1) # this comes from (0, 2, 5)
  293. assert edges_equal(actual.edges, expected.edges)
  294. def test_node_attributes():
  295. """Tests that node contraction preserves node attributes."""
  296. G = nx.cycle_graph(4)
  297. # Add some data to the two nodes being contracted.
  298. G.nodes[0]["foo"] = "bar"
  299. G.nodes[1]["baz"] = "xyzzy"
  300. actual = nx.contracted_nodes(G, 0, 1)
  301. # We expect that contracting the nodes 0 and 1 in C_4 yields K_3, but
  302. # with nodes labeled 0, 2, and 3, and with a -loop on 0.
  303. expected = nx.complete_graph(3)
  304. expected = nx.relabel_nodes(expected, {1: 2, 2: 3})
  305. expected.add_edge(0, 0)
  306. cdict = {1: {"baz": "xyzzy"}}
  307. expected.nodes[0].update({"foo": "bar", "contraction": cdict})
  308. assert nx.is_isomorphic(actual, expected)
  309. assert actual.nodes == expected.nodes
  310. def test_edge_attributes():
  311. """Tests that node contraction preserves edge attributes."""
  312. # Shape: src1 --> dest <-- src2
  313. G = nx.DiGraph([("src1", "dest"), ("src2", "dest")])
  314. G["src1"]["dest"]["value"] = "src1-->dest"
  315. G["src2"]["dest"]["value"] = "src2-->dest"
  316. H = nx.MultiDiGraph(G)
  317. G = nx.contracted_nodes(G, "src1", "src2") # New Shape: src1 --> dest
  318. assert G.edges[("src1", "dest")]["value"] == "src1-->dest"
  319. assert (
  320. G.edges[("src1", "dest")]["contraction"][("src2", "dest")]["value"]
  321. == "src2-->dest"
  322. )
  323. H = nx.contracted_nodes(H, "src1", "src2") # New Shape: src1 -(x2)-> dest
  324. assert len(H.edges(("src1", "dest"))) == 2
  325. def test_without_self_loops():
  326. """Tests for node contraction without preserving -loops."""
  327. G = nx.cycle_graph(4)
  328. actual = nx.contracted_nodes(G, 0, 1, self_loops=False)
  329. expected = nx.complete_graph(3)
  330. assert nx.is_isomorphic(actual, expected)
  331. def test_contract_loop_graph():
  332. """Tests for node contraction when nodes have loops."""
  333. G = nx.cycle_graph(4)
  334. G.add_edge(0, 0)
  335. actual = nx.contracted_nodes(G, 0, 1)
  336. expected = nx.complete_graph([0, 2, 3])
  337. expected.add_edge(0, 0)
  338. expected.add_edge(0, 0)
  339. assert edges_equal(actual.edges, expected.edges)
  340. actual = nx.contracted_nodes(G, 1, 0)
  341. expected = nx.complete_graph([1, 2, 3])
  342. expected.add_edge(1, 1)
  343. expected.add_edge(1, 1)
  344. assert edges_equal(actual.edges, expected.edges)
  345. def test_undirected_edge_contraction():
  346. """Tests for edge contraction in an undirected graph."""
  347. G = nx.cycle_graph(4)
  348. actual = nx.contracted_edge(G, (0, 1))
  349. expected = nx.complete_graph(3)
  350. expected.add_edge(0, 0)
  351. assert nx.is_isomorphic(actual, expected)
  352. def test_multigraph_edge_contraction():
  353. """Tests for edge contraction in a multigraph"""
  354. G = nx.cycle_graph(4)
  355. actual = nx.contracted_edge(G, (0, 1, 0))
  356. expected = nx.complete_graph(3)
  357. expected.add_edge(0, 0)
  358. assert nx.is_isomorphic(actual, expected)
  359. def test_nonexistent_edge():
  360. """Tests that attempting to contract a nonexistent edge raises an
  361. exception.
  362. """
  363. with pytest.raises(ValueError):
  364. G = nx.cycle_graph(4)
  365. nx.contracted_edge(G, (0, 2))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...