ZTWHHH commited on
Commit
ec8aae9
·
verified ·
1 Parent(s): 265dac1

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_asteroidal.cpython-310.pyc +0 -0
  2. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_boundary.cpython-310.pyc +0 -0
  3. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_bridges.cpython-310.pyc +0 -0
  4. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_broadcasting.cpython-310.pyc +0 -0
  5. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_clique.cpython-310.pyc +0 -0
  6. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-310.pyc +0 -0
  7. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc +0 -0
  8. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-310.pyc +0 -0
  9. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_measures.cpython-310.pyc +0 -0
  10. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_regular.cpython-310.pyc +0 -0
  11. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_efficiency.cpython-310.pyc +0 -0
  12. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_euler.cpython-310.pyc +0 -0
  13. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graph_hashing.cpython-310.pyc +0 -0
  14. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graphical.cpython-310.pyc +0 -0
  15. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_link_prediction.cpython-310.pyc +0 -0
  16. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_max_weight_clique.cpython-310.pyc +0 -0
  17. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_mis.cpython-310.pyc +0 -0
  18. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_node_classification.cpython-310.pyc +0 -0
  19. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-310.pyc +0 -0
  20. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc +0 -0
  21. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planarity.cpython-310.pyc +0 -0
  22. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_reciprocity.cpython-310.pyc +0 -0
  23. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_richclub.cpython-310.pyc +0 -0
  24. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_similarity.cpython-310.pyc +0 -0
  25. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_simple_paths.cpython-310.pyc +0 -0
  26. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-310.pyc +0 -0
  27. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_structuralholes.cpython-310.pyc +0 -0
  28. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_summarization.cpython-310.pyc +0 -0
  29. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_time_dependent.cpython-310.pyc +0 -0
  30. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_triads.cpython-310.pyc +0 -0
  31. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_vitality.cpython-310.pyc +0 -0
  32. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_walks.cpython-310.pyc +0 -0
  33. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_wiener.cpython-310.pyc +0 -0
  34. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_boundary.py +154 -0
  35. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_broadcasting.py +82 -0
  36. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_communicability.py +80 -0
  37. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_d_separation.py +348 -0
  38. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_measures.py +774 -0
  39. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominance.py +286 -0
  40. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominating.py +46 -0
  41. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_efficiency.py +58 -0
  42. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_graph_hashing.py +686 -0
  43. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_graphical.py +163 -0
  44. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_lowest_common_ancestors.py +427 -0
  45. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_matching.py +605 -0
  46. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_mis.py +62 -0
  47. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_non_randomness.py +42 -0
  48. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_reciprocity.py +37 -0
  49. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_regular.py +92 -0
  50. mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_structuralholes.py +137 -0
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_asteroidal.cpython-310.pyc ADDED
Binary file (648 Bytes). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_boundary.cpython-310.pyc ADDED
Binary file (7.14 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_bridges.cpython-310.pyc ADDED
Binary file (4.88 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_broadcasting.cpython-310.pyc ADDED
Binary file (2.17 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_clique.cpython-310.pyc ADDED
Binary file (8.73 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-310.pyc ADDED
Binary file (2.17 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc ADDED
Binary file (9.59 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-310.pyc ADDED
Binary file (9.22 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_measures.cpython-310.pyc ADDED
Binary file (29.5 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_regular.cpython-310.pyc ADDED
Binary file (3.81 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_efficiency.cpython-310.pyc ADDED
Binary file (2.85 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_euler.cpython-310.pyc ADDED
Binary file (13.3 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graph_hashing.cpython-310.pyc ADDED
Binary file (21.2 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graphical.cpython-310.pyc ADDED
Binary file (5.8 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_link_prediction.cpython-310.pyc ADDED
Binary file (21.1 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_max_weight_clique.cpython-310.pyc ADDED
Binary file (6.9 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_mis.cpython-310.pyc ADDED
Binary file (2.62 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_node_classification.cpython-310.pyc ADDED
Binary file (4.66 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-310.pyc ADDED
Binary file (1.56 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc ADDED
Binary file (7.91 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planarity.cpython-310.pyc ADDED
Binary file (14.9 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_reciprocity.cpython-310.pyc ADDED
Binary file (1.83 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_richclub.cpython-310.pyc ADDED
Binary file (3.8 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_similarity.cpython-310.pyc ADDED
Binary file (24.9 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_simple_paths.cpython-310.pyc ADDED
Binary file (31 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-310.pyc ADDED
Binary file (2.55 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_structuralholes.cpython-310.pyc ADDED
Binary file (5.32 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_summarization.cpython-310.pyc ADDED
Binary file (15.8 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_time_dependent.cpython-310.pyc ADDED
Binary file (9.29 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_triads.cpython-310.pyc ADDED
Binary file (11.9 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_vitality.cpython-310.pyc ADDED
Binary file (2.03 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_walks.cpython-310.pyc ADDED
Binary file (1.86 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_wiener.cpython-310.pyc ADDED
Binary file (3.16 kB). View file
 
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_boundary.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.boundary` module."""
2
+
3
+ from itertools import combinations
4
+
5
+ import pytest
6
+
7
+ import networkx as nx
8
+ from networkx import convert_node_labels_to_integers as cnlti
9
+ from networkx.utils import edges_equal
10
+
11
+
12
+ class TestNodeBoundary:
13
+ """Unit tests for the :func:`~networkx.node_boundary` function."""
14
+
15
+ def test_null_graph(self):
16
+ """Tests that the null graph has empty node boundaries."""
17
+ null = nx.null_graph()
18
+ assert nx.node_boundary(null, []) == set()
19
+ assert nx.node_boundary(null, [], []) == set()
20
+ assert nx.node_boundary(null, [1, 2, 3]) == set()
21
+ assert nx.node_boundary(null, [1, 2, 3], [4, 5, 6]) == set()
22
+ assert nx.node_boundary(null, [1, 2, 3], [3, 4, 5]) == set()
23
+
24
+ def test_path_graph(self):
25
+ P10 = cnlti(nx.path_graph(10), first_label=1)
26
+ assert nx.node_boundary(P10, []) == set()
27
+ assert nx.node_boundary(P10, [], []) == set()
28
+ assert nx.node_boundary(P10, [1, 2, 3]) == {4}
29
+ assert nx.node_boundary(P10, [4, 5, 6]) == {3, 7}
30
+ assert nx.node_boundary(P10, [3, 4, 5, 6, 7]) == {2, 8}
31
+ assert nx.node_boundary(P10, [8, 9, 10]) == {7}
32
+ assert nx.node_boundary(P10, [4, 5, 6], [9, 10]) == set()
33
+
34
+ def test_complete_graph(self):
35
+ K10 = cnlti(nx.complete_graph(10), first_label=1)
36
+ assert nx.node_boundary(K10, []) == set()
37
+ assert nx.node_boundary(K10, [], []) == set()
38
+ assert nx.node_boundary(K10, [1, 2, 3]) == {4, 5, 6, 7, 8, 9, 10}
39
+ assert nx.node_boundary(K10, [4, 5, 6]) == {1, 2, 3, 7, 8, 9, 10}
40
+ assert nx.node_boundary(K10, [3, 4, 5, 6, 7]) == {1, 2, 8, 9, 10}
41
+ assert nx.node_boundary(K10, [4, 5, 6], []) == set()
42
+ assert nx.node_boundary(K10, K10) == set()
43
+ assert nx.node_boundary(K10, [1, 2, 3], [3, 4, 5]) == {4, 5}
44
+
45
+ def test_petersen(self):
46
+ """Check boundaries in the petersen graph
47
+
48
+ cheeger(G,k)=min(|bdy(S)|/|S| for |S|=k, 0<k<=|V(G)|/2)
49
+
50
+ """
51
+
52
+ def cheeger(G, k):
53
+ return min(len(nx.node_boundary(G, nn)) / k for nn in combinations(G, k))
54
+
55
+ P = nx.petersen_graph()
56
+ assert cheeger(P, 1) == pytest.approx(3.00, abs=1e-2)
57
+ assert cheeger(P, 2) == pytest.approx(2.00, abs=1e-2)
58
+ assert cheeger(P, 3) == pytest.approx(1.67, abs=1e-2)
59
+ assert cheeger(P, 4) == pytest.approx(1.00, abs=1e-2)
60
+ assert cheeger(P, 5) == pytest.approx(0.80, abs=1e-2)
61
+
62
+ def test_directed(self):
63
+ """Tests the node boundary of a directed graph."""
64
+ G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)])
65
+ S = {0, 1}
66
+ boundary = nx.node_boundary(G, S)
67
+ expected = {2}
68
+ assert boundary == expected
69
+
70
+ def test_multigraph(self):
71
+ """Tests the node boundary of a multigraph."""
72
+ G = nx.MultiGraph(list(nx.cycle_graph(5).edges()) * 2)
73
+ S = {0, 1}
74
+ boundary = nx.node_boundary(G, S)
75
+ expected = {2, 4}
76
+ assert boundary == expected
77
+
78
+ def test_multidigraph(self):
79
+ """Tests the edge boundary of a multidigraph."""
80
+ edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
81
+ G = nx.MultiDiGraph(edges * 2)
82
+ S = {0, 1}
83
+ boundary = nx.node_boundary(G, S)
84
+ expected = {2}
85
+ assert boundary == expected
86
+
87
+
88
+ class TestEdgeBoundary:
89
+ """Unit tests for the :func:`~networkx.edge_boundary` function."""
90
+
91
+ def test_null_graph(self):
92
+ null = nx.null_graph()
93
+ assert list(nx.edge_boundary(null, [])) == []
94
+ assert list(nx.edge_boundary(null, [], [])) == []
95
+ assert list(nx.edge_boundary(null, [1, 2, 3])) == []
96
+ assert list(nx.edge_boundary(null, [1, 2, 3], [4, 5, 6])) == []
97
+ assert list(nx.edge_boundary(null, [1, 2, 3], [3, 4, 5])) == []
98
+
99
+ def test_path_graph(self):
100
+ P10 = cnlti(nx.path_graph(10), first_label=1)
101
+ assert list(nx.edge_boundary(P10, [])) == []
102
+ assert list(nx.edge_boundary(P10, [], [])) == []
103
+ assert list(nx.edge_boundary(P10, [1, 2, 3])) == [(3, 4)]
104
+ assert sorted(nx.edge_boundary(P10, [4, 5, 6])) == [(4, 3), (6, 7)]
105
+ assert sorted(nx.edge_boundary(P10, [3, 4, 5, 6, 7])) == [(3, 2), (7, 8)]
106
+ assert list(nx.edge_boundary(P10, [8, 9, 10])) == [(8, 7)]
107
+ assert sorted(nx.edge_boundary(P10, [4, 5, 6], [9, 10])) == []
108
+ assert list(nx.edge_boundary(P10, [1, 2, 3], [3, 4, 5])) == [(2, 3), (3, 4)]
109
+
110
+ def test_complete_graph(self):
111
+ K10 = cnlti(nx.complete_graph(10), first_label=1)
112
+
113
+ def ilen(iterable):
114
+ return sum(1 for i in iterable)
115
+
116
+ assert list(nx.edge_boundary(K10, [])) == []
117
+ assert list(nx.edge_boundary(K10, [], [])) == []
118
+ assert ilen(nx.edge_boundary(K10, [1, 2, 3])) == 21
119
+ assert ilen(nx.edge_boundary(K10, [4, 5, 6, 7])) == 24
120
+ assert ilen(nx.edge_boundary(K10, [3, 4, 5, 6, 7])) == 25
121
+ assert ilen(nx.edge_boundary(K10, [8, 9, 10])) == 21
122
+ assert edges_equal(
123
+ nx.edge_boundary(K10, [4, 5, 6], [9, 10]),
124
+ [(4, 9), (4, 10), (5, 9), (5, 10), (6, 9), (6, 10)],
125
+ )
126
+ assert edges_equal(
127
+ nx.edge_boundary(K10, [1, 2, 3], [3, 4, 5]),
128
+ [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5)],
129
+ )
130
+
131
+ def test_directed(self):
132
+ """Tests the edge boundary of a directed graph."""
133
+ G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)])
134
+ S = {0, 1}
135
+ boundary = list(nx.edge_boundary(G, S))
136
+ expected = [(1, 2)]
137
+ assert boundary == expected
138
+
139
+ def test_multigraph(self):
140
+ """Tests the edge boundary of a multigraph."""
141
+ G = nx.MultiGraph(list(nx.cycle_graph(5).edges()) * 2)
142
+ S = {0, 1}
143
+ boundary = list(nx.edge_boundary(G, S))
144
+ expected = [(0, 4), (0, 4), (1, 2), (1, 2)]
145
+ assert boundary == expected
146
+
147
+ def test_multidigraph(self):
148
+ """Tests the edge boundary of a multidigraph."""
149
+ edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
150
+ G = nx.MultiDiGraph(edges * 2)
151
+ S = {0, 1}
152
+ boundary = list(nx.edge_boundary(G, S))
153
+ expected = [(1, 2), (1, 2)]
154
+ assert boundary == expected
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_broadcasting.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the broadcasting module."""
2
+
3
+ import math
4
+
5
+ import networkx as nx
6
+
7
+
8
+ def test_example_tree_broadcast():
9
+ """
10
+ Test the BROADCAST algorithm on the example in the paper titled: "Information Dissemination in Trees"
11
+ """
12
+ edge_list = [
13
+ (0, 1),
14
+ (1, 2),
15
+ (2, 7),
16
+ (3, 4),
17
+ (5, 4),
18
+ (4, 7),
19
+ (6, 7),
20
+ (7, 9),
21
+ (8, 9),
22
+ (9, 13),
23
+ (13, 14),
24
+ (14, 15),
25
+ (14, 16),
26
+ (14, 17),
27
+ (13, 11),
28
+ (11, 10),
29
+ (11, 12),
30
+ (13, 18),
31
+ (18, 19),
32
+ (18, 20),
33
+ ]
34
+ G = nx.Graph(edge_list)
35
+ b_T, b_C = nx.tree_broadcast_center(G)
36
+ assert b_T == 6
37
+ assert b_C == {13, 9}
38
+ # test broadcast time from specific vertex
39
+ assert nx.tree_broadcast_time(G, 17) == 8
40
+ assert nx.tree_broadcast_time(G, 3) == 9
41
+ # test broadcast time of entire tree
42
+ assert nx.tree_broadcast_time(G) == 10
43
+
44
+
45
+ def test_path_broadcast():
46
+ for i in range(2, 12):
47
+ G = nx.path_graph(i)
48
+ b_T, b_C = nx.tree_broadcast_center(G)
49
+ assert b_T == math.ceil(i / 2)
50
+ assert b_C == {
51
+ math.ceil(i / 2),
52
+ math.floor(i / 2),
53
+ math.ceil(i / 2 - 1),
54
+ math.floor(i / 2 - 1),
55
+ }
56
+ assert nx.tree_broadcast_time(G) == i - 1
57
+
58
+
59
+ def test_empty_graph_broadcast():
60
+ H = nx.empty_graph(1)
61
+ b_T, b_C = nx.tree_broadcast_center(H)
62
+ assert b_T == 0
63
+ assert b_C == {0}
64
+ assert nx.tree_broadcast_time(H) == 0
65
+
66
+
67
+ def test_star_broadcast():
68
+ for i in range(4, 12):
69
+ G = nx.star_graph(i)
70
+ b_T, b_C = nx.tree_broadcast_center(G)
71
+ assert b_T == i
72
+ assert b_C == set(G.nodes())
73
+ assert nx.tree_broadcast_time(G) == b_T
74
+
75
+
76
+ def test_binomial_tree_broadcast():
77
+ for i in range(2, 8):
78
+ G = nx.binomial_tree(i)
79
+ b_T, b_C = nx.tree_broadcast_center(G)
80
+ assert b_T == i
81
+ assert b_C == {0, 2 ** (i - 1)}
82
+ assert nx.tree_broadcast_time(G) == 2 * i - 1
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_communicability.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+
3
+ import pytest
4
+
5
+ pytest.importorskip("numpy")
6
+ pytest.importorskip("scipy")
7
+
8
+ import networkx as nx
9
+ from networkx.algorithms.communicability_alg import communicability, communicability_exp
10
+
11
+
12
+ class TestCommunicability:
13
+ def test_communicability(self):
14
+ answer = {
15
+ 0: {0: 1.5430806348152435, 1: 1.1752011936438012},
16
+ 1: {0: 1.1752011936438012, 1: 1.5430806348152435},
17
+ }
18
+ # answer={(0, 0): 1.5430806348152435,
19
+ # (0, 1): 1.1752011936438012,
20
+ # (1, 0): 1.1752011936438012,
21
+ # (1, 1): 1.5430806348152435}
22
+
23
+ result = communicability(nx.path_graph(2))
24
+ for k1, val in result.items():
25
+ for k2 in val:
26
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
27
+
28
+ def test_communicability2(self):
29
+ answer_orig = {
30
+ ("1", "1"): 1.6445956054135658,
31
+ ("1", "Albert"): 0.7430186221096251,
32
+ ("1", "Aric"): 0.7430186221096251,
33
+ ("1", "Dan"): 1.6208126320442937,
34
+ ("1", "Franck"): 0.42639707170035257,
35
+ ("Albert", "1"): 0.7430186221096251,
36
+ ("Albert", "Albert"): 2.4368257358712189,
37
+ ("Albert", "Aric"): 1.4368257358712191,
38
+ ("Albert", "Dan"): 2.0472097037446453,
39
+ ("Albert", "Franck"): 1.8340111678944691,
40
+ ("Aric", "1"): 0.7430186221096251,
41
+ ("Aric", "Albert"): 1.4368257358712191,
42
+ ("Aric", "Aric"): 2.4368257358712193,
43
+ ("Aric", "Dan"): 2.0472097037446457,
44
+ ("Aric", "Franck"): 1.8340111678944691,
45
+ ("Dan", "1"): 1.6208126320442937,
46
+ ("Dan", "Albert"): 2.0472097037446453,
47
+ ("Dan", "Aric"): 2.0472097037446457,
48
+ ("Dan", "Dan"): 3.1306328496328168,
49
+ ("Dan", "Franck"): 1.4860372442192515,
50
+ ("Franck", "1"): 0.42639707170035257,
51
+ ("Franck", "Albert"): 1.8340111678944691,
52
+ ("Franck", "Aric"): 1.8340111678944691,
53
+ ("Franck", "Dan"): 1.4860372442192515,
54
+ ("Franck", "Franck"): 2.3876142275231915,
55
+ }
56
+
57
+ answer = defaultdict(dict)
58
+ for (k1, k2), v in answer_orig.items():
59
+ answer[k1][k2] = v
60
+
61
+ G1 = nx.Graph(
62
+ [
63
+ ("Franck", "Aric"),
64
+ ("Aric", "Dan"),
65
+ ("Dan", "Albert"),
66
+ ("Albert", "Franck"),
67
+ ("Dan", "1"),
68
+ ("Franck", "Albert"),
69
+ ]
70
+ )
71
+
72
+ result = communicability(G1)
73
+ for k1, val in result.items():
74
+ for k2 in val:
75
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
76
+
77
+ result = communicability_exp(G1)
78
+ for k1, val in result.items():
79
+ for k2 in val:
80
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_d_separation.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import combinations
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+
8
+ def path_graph():
9
+ """Return a path graph of length three."""
10
+ G = nx.path_graph(3, create_using=nx.DiGraph)
11
+ G.graph["name"] = "path"
12
+ nx.freeze(G)
13
+ return G
14
+
15
+
16
+ def fork_graph():
17
+ """Return a three node fork graph."""
18
+ G = nx.DiGraph(name="fork")
19
+ G.add_edges_from([(0, 1), (0, 2)])
20
+ nx.freeze(G)
21
+ return G
22
+
23
+
24
+ def collider_graph():
25
+ """Return a collider/v-structure graph with three nodes."""
26
+ G = nx.DiGraph(name="collider")
27
+ G.add_edges_from([(0, 2), (1, 2)])
28
+ nx.freeze(G)
29
+ return G
30
+
31
+
32
+ def naive_bayes_graph():
33
+ """Return a simply Naive Bayes PGM graph."""
34
+ G = nx.DiGraph(name="naive_bayes")
35
+ G.add_edges_from([(0, 1), (0, 2), (0, 3), (0, 4)])
36
+ nx.freeze(G)
37
+ return G
38
+
39
+
40
+ def asia_graph():
41
+ """Return the 'Asia' PGM graph."""
42
+ G = nx.DiGraph(name="asia")
43
+ G.add_edges_from(
44
+ [
45
+ ("asia", "tuberculosis"),
46
+ ("smoking", "cancer"),
47
+ ("smoking", "bronchitis"),
48
+ ("tuberculosis", "either"),
49
+ ("cancer", "either"),
50
+ ("either", "xray"),
51
+ ("either", "dyspnea"),
52
+ ("bronchitis", "dyspnea"),
53
+ ]
54
+ )
55
+ nx.freeze(G)
56
+ return G
57
+
58
+
59
+ @pytest.fixture(name="path_graph")
60
+ def path_graph_fixture():
61
+ return path_graph()
62
+
63
+
64
+ @pytest.fixture(name="fork_graph")
65
+ def fork_graph_fixture():
66
+ return fork_graph()
67
+
68
+
69
+ @pytest.fixture(name="collider_graph")
70
+ def collider_graph_fixture():
71
+ return collider_graph()
72
+
73
+
74
+ @pytest.fixture(name="naive_bayes_graph")
75
+ def naive_bayes_graph_fixture():
76
+ return naive_bayes_graph()
77
+
78
+
79
+ @pytest.fixture(name="asia_graph")
80
+ def asia_graph_fixture():
81
+ return asia_graph()
82
+
83
+
84
+ @pytest.fixture()
85
+ def large_collider_graph():
86
+ edge_list = [("A", "B"), ("C", "B"), ("B", "D"), ("D", "E"), ("B", "F"), ("G", "E")]
87
+ G = nx.DiGraph(edge_list)
88
+ return G
89
+
90
+
91
+ @pytest.fixture()
92
+ def chain_and_fork_graph():
93
+ edge_list = [("A", "B"), ("B", "C"), ("B", "D"), ("D", "C")]
94
+ G = nx.DiGraph(edge_list)
95
+ return G
96
+
97
+
98
+ @pytest.fixture()
99
+ def no_separating_set_graph():
100
+ edge_list = [("A", "B")]
101
+ G = nx.DiGraph(edge_list)
102
+ return G
103
+
104
+
105
+ @pytest.fixture()
106
+ def large_no_separating_set_graph():
107
+ edge_list = [("A", "B"), ("C", "A"), ("C", "B")]
108
+ G = nx.DiGraph(edge_list)
109
+ return G
110
+
111
+
112
+ @pytest.fixture()
113
+ def collider_trek_graph():
114
+ edge_list = [("A", "B"), ("C", "B"), ("C", "D")]
115
+ G = nx.DiGraph(edge_list)
116
+ return G
117
+
118
+
119
+ @pytest.mark.parametrize(
120
+ "graph",
121
+ [path_graph(), fork_graph(), collider_graph(), naive_bayes_graph(), asia_graph()],
122
+ )
123
+ def test_markov_condition(graph):
124
+ """Test that the Markov condition holds for each PGM graph."""
125
+ for node in graph.nodes:
126
+ parents = set(graph.predecessors(node))
127
+ non_descendants = graph.nodes - nx.descendants(graph, node) - {node} - parents
128
+ assert nx.is_d_separator(graph, {node}, non_descendants, parents)
129
+
130
+
131
+ def test_path_graph_dsep(path_graph):
132
+ """Example-based test of d-separation for path_graph."""
133
+ assert nx.is_d_separator(path_graph, {0}, {2}, {1})
134
+ assert not nx.is_d_separator(path_graph, {0}, {2}, set())
135
+
136
+
137
+ def test_fork_graph_dsep(fork_graph):
138
+ """Example-based test of d-separation for fork_graph."""
139
+ assert nx.is_d_separator(fork_graph, {1}, {2}, {0})
140
+ assert not nx.is_d_separator(fork_graph, {1}, {2}, set())
141
+
142
+
143
+ def test_collider_graph_dsep(collider_graph):
144
+ """Example-based test of d-separation for collider_graph."""
145
+ assert nx.is_d_separator(collider_graph, {0}, {1}, set())
146
+ assert not nx.is_d_separator(collider_graph, {0}, {1}, {2})
147
+
148
+
149
+ def test_naive_bayes_dsep(naive_bayes_graph):
150
+ """Example-based test of d-separation for naive_bayes_graph."""
151
+ for u, v in combinations(range(1, 5), 2):
152
+ assert nx.is_d_separator(naive_bayes_graph, {u}, {v}, {0})
153
+ assert not nx.is_d_separator(naive_bayes_graph, {u}, {v}, set())
154
+
155
+
156
+ def test_asia_graph_dsep(asia_graph):
157
+ """Example-based test of d-separation for asia_graph."""
158
+ assert nx.is_d_separator(
159
+ asia_graph, {"asia", "smoking"}, {"dyspnea", "xray"}, {"bronchitis", "either"}
160
+ )
161
+ assert nx.is_d_separator(
162
+ asia_graph, {"tuberculosis", "cancer"}, {"bronchitis"}, {"smoking", "xray"}
163
+ )
164
+
165
+
166
+ def test_undirected_graphs_are_not_supported():
167
+ """
168
+ Test that undirected graphs are not supported.
169
+
170
+ d-separation and its related algorithms do not apply in
171
+ the case of undirected graphs.
172
+ """
173
+ g = nx.path_graph(3, nx.Graph)
174
+ with pytest.raises(nx.NetworkXNotImplemented):
175
+ nx.is_d_separator(g, {0}, {1}, {2})
176
+ with pytest.raises(nx.NetworkXNotImplemented):
177
+ nx.is_minimal_d_separator(g, {0}, {1}, {2})
178
+ with pytest.raises(nx.NetworkXNotImplemented):
179
+ nx.find_minimal_d_separator(g, {0}, {1})
180
+
181
+
182
+ def test_cyclic_graphs_raise_error():
183
+ """
184
+ Test that cycle graphs should cause erroring.
185
+
186
+ This is because PGMs assume a directed acyclic graph.
187
+ """
188
+ g = nx.cycle_graph(3, nx.DiGraph)
189
+ with pytest.raises(nx.NetworkXError):
190
+ nx.is_d_separator(g, {0}, {1}, {2})
191
+ with pytest.raises(nx.NetworkXError):
192
+ nx.find_minimal_d_separator(g, {0}, {1})
193
+ with pytest.raises(nx.NetworkXError):
194
+ nx.is_minimal_d_separator(g, {0}, {1}, {2})
195
+
196
+
197
+ def test_invalid_nodes_raise_error(asia_graph):
198
+ """
199
+ Test that graphs that have invalid nodes passed in raise errors.
200
+ """
201
+ # Check both set and node arguments
202
+ with pytest.raises(nx.NodeNotFound):
203
+ nx.is_d_separator(asia_graph, {0}, {1}, {2})
204
+ with pytest.raises(nx.NodeNotFound):
205
+ nx.is_d_separator(asia_graph, 0, 1, 2)
206
+ with pytest.raises(nx.NodeNotFound):
207
+ nx.is_minimal_d_separator(asia_graph, {0}, {1}, {2})
208
+ with pytest.raises(nx.NodeNotFound):
209
+ nx.is_minimal_d_separator(asia_graph, 0, 1, 2)
210
+ with pytest.raises(nx.NodeNotFound):
211
+ nx.find_minimal_d_separator(asia_graph, {0}, {1})
212
+ with pytest.raises(nx.NodeNotFound):
213
+ nx.find_minimal_d_separator(asia_graph, 0, 1)
214
+
215
+
216
+ def test_nondisjoint_node_sets_raise_error(collider_graph):
217
+ """
218
+ Test that error is raised when node sets aren't disjoint.
219
+ """
220
+ with pytest.raises(nx.NetworkXError):
221
+ nx.is_d_separator(collider_graph, 0, 1, 0)
222
+ with pytest.raises(nx.NetworkXError):
223
+ nx.is_d_separator(collider_graph, 0, 2, 0)
224
+ with pytest.raises(nx.NetworkXError):
225
+ nx.is_d_separator(collider_graph, 0, 0, 1)
226
+ with pytest.raises(nx.NetworkXError):
227
+ nx.is_d_separator(collider_graph, 1, 0, 0)
228
+ with pytest.raises(nx.NetworkXError):
229
+ nx.find_minimal_d_separator(collider_graph, 0, 0)
230
+ with pytest.raises(nx.NetworkXError):
231
+ nx.find_minimal_d_separator(collider_graph, 0, 1, included=0)
232
+ with pytest.raises(nx.NetworkXError):
233
+ nx.find_minimal_d_separator(collider_graph, 1, 0, included=0)
234
+ with pytest.raises(nx.NetworkXError):
235
+ nx.is_minimal_d_separator(collider_graph, 0, 0, set())
236
+ with pytest.raises(nx.NetworkXError):
237
+ nx.is_minimal_d_separator(collider_graph, 0, 1, set(), included=0)
238
+ with pytest.raises(nx.NetworkXError):
239
+ nx.is_minimal_d_separator(collider_graph, 1, 0, set(), included=0)
240
+
241
+
242
+ def test_is_minimal_d_separator(
243
+ large_collider_graph,
244
+ chain_and_fork_graph,
245
+ no_separating_set_graph,
246
+ large_no_separating_set_graph,
247
+ collider_trek_graph,
248
+ ):
249
+ # Case 1:
250
+ # create a graph A -> B <- C
251
+ # B -> D -> E;
252
+ # B -> F;
253
+ # G -> E;
254
+ assert not nx.is_d_separator(large_collider_graph, {"B"}, {"E"}, set())
255
+
256
+ # minimal set of the corresponding graph
257
+ # for B and E should be (D,)
258
+ Zmin = nx.find_minimal_d_separator(large_collider_graph, "B", "E")
259
+ # check that the minimal d-separator is a d-separating set
260
+ assert nx.is_d_separator(large_collider_graph, "B", "E", Zmin)
261
+ # the minimal separating set should also pass the test for minimality
262
+ assert nx.is_minimal_d_separator(large_collider_graph, "B", "E", Zmin)
263
+ # function should also work with set arguments
264
+ assert nx.is_minimal_d_separator(large_collider_graph, {"A", "B"}, {"G", "E"}, Zmin)
265
+ assert Zmin == {"D"}
266
+
267
+ # Case 2:
268
+ # create a graph A -> B -> C
269
+ # B -> D -> C;
270
+ assert not nx.is_d_separator(chain_and_fork_graph, {"A"}, {"C"}, set())
271
+ Zmin = nx.find_minimal_d_separator(chain_and_fork_graph, "A", "C")
272
+
273
+ # the minimal separating set should pass the test for minimality
274
+ assert nx.is_minimal_d_separator(chain_and_fork_graph, "A", "C", Zmin)
275
+ assert Zmin == {"B"}
276
+ Znotmin = Zmin.union({"D"})
277
+ assert not nx.is_minimal_d_separator(chain_and_fork_graph, "A", "C", Znotmin)
278
+
279
+ # Case 3:
280
+ # create a graph A -> B
281
+
282
+ # there is no m-separating set between A and B at all, so
283
+ # no minimal m-separating set can exist
284
+ assert not nx.is_d_separator(no_separating_set_graph, {"A"}, {"B"}, set())
285
+ assert nx.find_minimal_d_separator(no_separating_set_graph, "A", "B") is None
286
+
287
+ # Case 4:
288
+ # create a graph A -> B with A <- C -> B
289
+
290
+ # there is no m-separating set between A and B at all, so
291
+ # no minimal m-separating set can exist
292
+ # however, the algorithm will initially propose C as a
293
+ # minimal (but invalid) separating set
294
+ assert not nx.is_d_separator(large_no_separating_set_graph, {"A"}, {"B"}, {"C"})
295
+ assert nx.find_minimal_d_separator(large_no_separating_set_graph, "A", "B") is None
296
+
297
+ # Test `included` and `excluded` args
298
+ # create graph A -> B <- C -> D
299
+ assert nx.find_minimal_d_separator(collider_trek_graph, "A", "D", included="B") == {
300
+ "B",
301
+ "C",
302
+ }
303
+ assert (
304
+ nx.find_minimal_d_separator(
305
+ collider_trek_graph, "A", "D", included="B", restricted="B"
306
+ )
307
+ is None
308
+ )
309
+
310
+
311
+ def test_is_minimal_d_separator_checks_dsep():
312
+ """Test that is_minimal_d_separator checks for d-separation as well."""
313
+ g = nx.DiGraph()
314
+ g.add_edges_from(
315
+ [
316
+ ("A", "B"),
317
+ ("A", "E"),
318
+ ("B", "C"),
319
+ ("B", "D"),
320
+ ("D", "C"),
321
+ ("D", "F"),
322
+ ("E", "D"),
323
+ ("E", "F"),
324
+ ]
325
+ )
326
+
327
+ assert not nx.is_d_separator(g, {"C"}, {"F"}, {"D"})
328
+
329
+ # since {'D'} and {} are not d-separators, we return false
330
+ assert not nx.is_minimal_d_separator(g, "C", "F", {"D"})
331
+ assert not nx.is_minimal_d_separator(g, "C", "F", set())
332
+
333
+
334
+ def test__reachable(large_collider_graph):
335
+ reachable = nx.algorithms.d_separation._reachable
336
+ g = large_collider_graph
337
+ x = {"F", "D"}
338
+ ancestors = {"A", "B", "C", "D", "F"}
339
+ assert reachable(g, x, ancestors, {"B"}) == {"B", "F", "D"}
340
+ assert reachable(g, x, ancestors, set()) == ancestors
341
+
342
+
343
+ def test_deprecations():
344
+ G = nx.DiGraph([(0, 1), (1, 2)])
345
+ with pytest.deprecated_call():
346
+ nx.d_separated(G, 0, 2, {1})
347
+ with pytest.deprecated_call():
348
+ z = nx.minimal_d_separator(G, 0, 2)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_measures.py ADDED
@@ -0,0 +1,774 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from random import Random
3
+
4
+ import pytest
5
+
6
+ import networkx as nx
7
+ from networkx import convert_node_labels_to_integers as cnlti
8
+ from networkx.algorithms.distance_measures import _extrema_bounding
9
+
10
+
11
+ def test__extrema_bounding_invalid_compute_kwarg():
12
+ G = nx.path_graph(3)
13
+ with pytest.raises(ValueError, match="compute must be one of"):
14
+ _extrema_bounding(G, compute="spam")
15
+
16
+
17
+ class TestDistance:
18
+ def setup_method(self):
19
+ G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
20
+ self.G = G
21
+
22
+ def test_eccentricity(self):
23
+ assert nx.eccentricity(self.G, 1) == 6
24
+ e = nx.eccentricity(self.G)
25
+ assert e[1] == 6
26
+
27
+ sp = dict(nx.shortest_path_length(self.G))
28
+ e = nx.eccentricity(self.G, sp=sp)
29
+ assert e[1] == 6
30
+
31
+ e = nx.eccentricity(self.G, v=1)
32
+ assert e == 6
33
+
34
+ # This behavior changed in version 1.8 (ticket #739)
35
+ e = nx.eccentricity(self.G, v=[1, 1])
36
+ assert e[1] == 6
37
+ e = nx.eccentricity(self.G, v=[1, 2])
38
+ assert e[1] == 6
39
+
40
+ # test against graph with one node
41
+ G = nx.path_graph(1)
42
+ e = nx.eccentricity(G)
43
+ assert e[0] == 0
44
+ e = nx.eccentricity(G, v=0)
45
+ assert e == 0
46
+ pytest.raises(nx.NetworkXError, nx.eccentricity, G, 1)
47
+
48
+ # test against empty graph
49
+ G = nx.empty_graph()
50
+ e = nx.eccentricity(G)
51
+ assert e == {}
52
+
53
+ def test_diameter(self):
54
+ assert nx.diameter(self.G) == 6
55
+
56
+ def test_harmonic_diameter(self):
57
+ assert abs(nx.harmonic_diameter(self.G) - 2.0477815699658715) < 1e-12
58
+
59
+ def test_harmonic_diameter_empty(self):
60
+ assert math.isnan(nx.harmonic_diameter(nx.empty_graph()))
61
+
62
+ def test_harmonic_diameter_single_node(self):
63
+ assert math.isnan(nx.harmonic_diameter(nx.empty_graph(1)))
64
+
65
+ def test_harmonic_diameter_discrete(self):
66
+ assert math.isinf(nx.harmonic_diameter(nx.empty_graph(3)))
67
+
68
+ def test_harmonic_diameter_not_strongly_connected(self):
69
+ DG = nx.DiGraph()
70
+ DG.add_edge(0, 1)
71
+ assert nx.harmonic_diameter(DG) == 2
72
+
73
+ def test_radius(self):
74
+ assert nx.radius(self.G) == 4
75
+
76
+ def test_periphery(self):
77
+ assert set(nx.periphery(self.G)) == {1, 4, 13, 16}
78
+
79
+ def test_center(self):
80
+ assert set(nx.center(self.G)) == {6, 7, 10, 11}
81
+
82
+ def test_bound_diameter(self):
83
+ assert nx.diameter(self.G, usebounds=True) == 6
84
+
85
+ def test_bound_radius(self):
86
+ assert nx.radius(self.G, usebounds=True) == 4
87
+
88
+ def test_bound_periphery(self):
89
+ result = {1, 4, 13, 16}
90
+ assert set(nx.periphery(self.G, usebounds=True)) == result
91
+
92
+ def test_bound_center(self):
93
+ result = {6, 7, 10, 11}
94
+ assert set(nx.center(self.G, usebounds=True)) == result
95
+
96
+ def test_radius_exception(self):
97
+ G = nx.Graph()
98
+ G.add_edge(1, 2)
99
+ G.add_edge(3, 4)
100
+ pytest.raises(nx.NetworkXError, nx.diameter, G)
101
+
102
+ def test_eccentricity_infinite(self):
103
+ with pytest.raises(nx.NetworkXError):
104
+ G = nx.Graph([(1, 2), (3, 4)])
105
+ e = nx.eccentricity(G)
106
+
107
+ def test_eccentricity_undirected_not_connected(self):
108
+ with pytest.raises(nx.NetworkXError):
109
+ G = nx.Graph([(1, 2), (3, 4)])
110
+ e = nx.eccentricity(G, sp=1)
111
+
112
+ def test_eccentricity_directed_weakly_connected(self):
113
+ with pytest.raises(nx.NetworkXError):
114
+ DG = nx.DiGraph([(1, 2), (1, 3)])
115
+ nx.eccentricity(DG)
116
+
117
+
118
+ class TestWeightedDistance:
119
+ def setup_method(self):
120
+ G = nx.Graph()
121
+ G.add_edge(0, 1, weight=0.6, cost=0.6, high_cost=6)
122
+ G.add_edge(0, 2, weight=0.2, cost=0.2, high_cost=2)
123
+ G.add_edge(2, 3, weight=0.1, cost=0.1, high_cost=1)
124
+ G.add_edge(2, 4, weight=0.7, cost=0.7, high_cost=7)
125
+ G.add_edge(2, 5, weight=0.9, cost=0.9, high_cost=9)
126
+ G.add_edge(1, 5, weight=0.3, cost=0.3, high_cost=3)
127
+ self.G = G
128
+ self.weight_fn = lambda v, u, e: 2
129
+
130
+ def test_eccentricity_weight_None(self):
131
+ assert nx.eccentricity(self.G, 1, weight=None) == 3
132
+ e = nx.eccentricity(self.G, weight=None)
133
+ assert e[1] == 3
134
+
135
+ e = nx.eccentricity(self.G, v=1, weight=None)
136
+ assert e == 3
137
+
138
+ # This behavior changed in version 1.8 (ticket #739)
139
+ e = nx.eccentricity(self.G, v=[1, 1], weight=None)
140
+ assert e[1] == 3
141
+ e = nx.eccentricity(self.G, v=[1, 2], weight=None)
142
+ assert e[1] == 3
143
+
144
+ def test_eccentricity_weight_attr(self):
145
+ assert nx.eccentricity(self.G, 1, weight="weight") == 1.5
146
+ e = nx.eccentricity(self.G, weight="weight")
147
+ assert (
148
+ e
149
+ == nx.eccentricity(self.G, weight="cost")
150
+ != nx.eccentricity(self.G, weight="high_cost")
151
+ )
152
+ assert e[1] == 1.5
153
+
154
+ e = nx.eccentricity(self.G, v=1, weight="weight")
155
+ assert e == 1.5
156
+
157
+ # This behavior changed in version 1.8 (ticket #739)
158
+ e = nx.eccentricity(self.G, v=[1, 1], weight="weight")
159
+ assert e[1] == 1.5
160
+ e = nx.eccentricity(self.G, v=[1, 2], weight="weight")
161
+ assert e[1] == 1.5
162
+
163
+ def test_eccentricity_weight_fn(self):
164
+ assert nx.eccentricity(self.G, 1, weight=self.weight_fn) == 6
165
+ e = nx.eccentricity(self.G, weight=self.weight_fn)
166
+ assert e[1] == 6
167
+
168
+ e = nx.eccentricity(self.G, v=1, weight=self.weight_fn)
169
+ assert e == 6
170
+
171
+ # This behavior changed in version 1.8 (ticket #739)
172
+ e = nx.eccentricity(self.G, v=[1, 1], weight=self.weight_fn)
173
+ assert e[1] == 6
174
+ e = nx.eccentricity(self.G, v=[1, 2], weight=self.weight_fn)
175
+ assert e[1] == 6
176
+
177
+ def test_diameter_weight_None(self):
178
+ assert nx.diameter(self.G, weight=None) == 3
179
+
180
+ def test_diameter_weight_attr(self):
181
+ assert (
182
+ nx.diameter(self.G, weight="weight")
183
+ == nx.diameter(self.G, weight="cost")
184
+ == 1.6
185
+ != nx.diameter(self.G, weight="high_cost")
186
+ )
187
+
188
+ def test_diameter_weight_fn(self):
189
+ assert nx.diameter(self.G, weight=self.weight_fn) == 6
190
+
191
+ def test_radius_weight_None(self):
192
+ assert pytest.approx(nx.radius(self.G, weight=None)) == 2
193
+
194
+ def test_radius_weight_attr(self):
195
+ assert (
196
+ pytest.approx(nx.radius(self.G, weight="weight"))
197
+ == pytest.approx(nx.radius(self.G, weight="cost"))
198
+ == 0.9
199
+ != nx.radius(self.G, weight="high_cost")
200
+ )
201
+
202
+ def test_radius_weight_fn(self):
203
+ assert nx.radius(self.G, weight=self.weight_fn) == 4
204
+
205
+ def test_periphery_weight_None(self):
206
+ for v in set(nx.periphery(self.G, weight=None)):
207
+ assert nx.eccentricity(self.G, v, weight=None) == nx.diameter(
208
+ self.G, weight=None
209
+ )
210
+
211
+ def test_periphery_weight_attr(self):
212
+ periphery = set(nx.periphery(self.G, weight="weight"))
213
+ assert (
214
+ periphery
215
+ == set(nx.periphery(self.G, weight="cost"))
216
+ == set(nx.periphery(self.G, weight="high_cost"))
217
+ )
218
+ for v in periphery:
219
+ assert (
220
+ nx.eccentricity(self.G, v, weight="high_cost")
221
+ != nx.eccentricity(self.G, v, weight="weight")
222
+ == nx.eccentricity(self.G, v, weight="cost")
223
+ == nx.diameter(self.G, weight="weight")
224
+ == nx.diameter(self.G, weight="cost")
225
+ != nx.diameter(self.G, weight="high_cost")
226
+ )
227
+ assert nx.eccentricity(self.G, v, weight="high_cost") == nx.diameter(
228
+ self.G, weight="high_cost"
229
+ )
230
+
231
+ def test_periphery_weight_fn(self):
232
+ for v in set(nx.periphery(self.G, weight=self.weight_fn)):
233
+ assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.diameter(
234
+ self.G, weight=self.weight_fn
235
+ )
236
+
237
+ def test_center_weight_None(self):
238
+ for v in set(nx.center(self.G, weight=None)):
239
+ assert pytest.approx(nx.eccentricity(self.G, v, weight=None)) == nx.radius(
240
+ self.G, weight=None
241
+ )
242
+
243
+ def test_center_weight_attr(self):
244
+ center = set(nx.center(self.G, weight="weight"))
245
+ assert (
246
+ center
247
+ == set(nx.center(self.G, weight="cost"))
248
+ != set(nx.center(self.G, weight="high_cost"))
249
+ )
250
+ for v in center:
251
+ assert (
252
+ nx.eccentricity(self.G, v, weight="high_cost")
253
+ != pytest.approx(nx.eccentricity(self.G, v, weight="weight"))
254
+ == pytest.approx(nx.eccentricity(self.G, v, weight="cost"))
255
+ == nx.radius(self.G, weight="weight")
256
+ == nx.radius(self.G, weight="cost")
257
+ != nx.radius(self.G, weight="high_cost")
258
+ )
259
+ assert nx.eccentricity(self.G, v, weight="high_cost") == nx.radius(
260
+ self.G, weight="high_cost"
261
+ )
262
+
263
+ def test_center_weight_fn(self):
264
+ for v in set(nx.center(self.G, weight=self.weight_fn)):
265
+ assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.radius(
266
+ self.G, weight=self.weight_fn
267
+ )
268
+
269
+ def test_bound_diameter_weight_None(self):
270
+ assert nx.diameter(self.G, usebounds=True, weight=None) == 3
271
+
272
+ def test_bound_diameter_weight_attr(self):
273
+ assert (
274
+ nx.diameter(self.G, usebounds=True, weight="high_cost")
275
+ != nx.diameter(self.G, usebounds=True, weight="weight")
276
+ == nx.diameter(self.G, usebounds=True, weight="cost")
277
+ == 1.6
278
+ != nx.diameter(self.G, usebounds=True, weight="high_cost")
279
+ )
280
+ assert nx.diameter(self.G, usebounds=True, weight="high_cost") == nx.diameter(
281
+ self.G, usebounds=True, weight="high_cost"
282
+ )
283
+
284
+ def test_bound_diameter_weight_fn(self):
285
+ assert nx.diameter(self.G, usebounds=True, weight=self.weight_fn) == 6
286
+
287
+ def test_bound_radius_weight_None(self):
288
+ assert pytest.approx(nx.radius(self.G, usebounds=True, weight=None)) == 2
289
+
290
+ def test_bound_radius_weight_attr(self):
291
+ assert (
292
+ nx.radius(self.G, usebounds=True, weight="high_cost")
293
+ != pytest.approx(nx.radius(self.G, usebounds=True, weight="weight"))
294
+ == pytest.approx(nx.radius(self.G, usebounds=True, weight="cost"))
295
+ == 0.9
296
+ != nx.radius(self.G, usebounds=True, weight="high_cost")
297
+ )
298
+ assert nx.radius(self.G, usebounds=True, weight="high_cost") == nx.radius(
299
+ self.G, usebounds=True, weight="high_cost"
300
+ )
301
+
302
+ def test_bound_radius_weight_fn(self):
303
+ assert nx.radius(self.G, usebounds=True, weight=self.weight_fn) == 4
304
+
305
+ def test_bound_periphery_weight_None(self):
306
+ result = {1, 3, 4}
307
+ assert set(nx.periphery(self.G, usebounds=True, weight=None)) == result
308
+
309
+ def test_bound_periphery_weight_attr(self):
310
+ result = {4, 5}
311
+ assert (
312
+ set(nx.periphery(self.G, usebounds=True, weight="weight"))
313
+ == set(nx.periphery(self.G, usebounds=True, weight="cost"))
314
+ == result
315
+ )
316
+
317
+ def test_bound_periphery_weight_fn(self):
318
+ result = {1, 3, 4}
319
+ assert (
320
+ set(nx.periphery(self.G, usebounds=True, weight=self.weight_fn)) == result
321
+ )
322
+
323
+ def test_bound_center_weight_None(self):
324
+ result = {0, 2, 5}
325
+ assert set(nx.center(self.G, usebounds=True, weight=None)) == result
326
+
327
+ def test_bound_center_weight_attr(self):
328
+ result = {0}
329
+ assert (
330
+ set(nx.center(self.G, usebounds=True, weight="weight"))
331
+ == set(nx.center(self.G, usebounds=True, weight="cost"))
332
+ == result
333
+ )
334
+
335
+ def test_bound_center_weight_fn(self):
336
+ result = {0, 2, 5}
337
+ assert set(nx.center(self.G, usebounds=True, weight=self.weight_fn)) == result
338
+
339
+
340
+ class TestResistanceDistance:
341
+ @classmethod
342
+ def setup_class(cls):
343
+ global np
344
+ np = pytest.importorskip("numpy")
345
+ sp = pytest.importorskip("scipy")
346
+
347
+ def setup_method(self):
348
+ G = nx.Graph()
349
+ G.add_edge(1, 2, weight=2)
350
+ G.add_edge(2, 3, weight=4)
351
+ G.add_edge(3, 4, weight=1)
352
+ G.add_edge(1, 4, weight=3)
353
+ self.G = G
354
+
355
+ def test_resistance_distance_directed_graph(self):
356
+ G = nx.DiGraph()
357
+ with pytest.raises(nx.NetworkXNotImplemented):
358
+ nx.resistance_distance(G)
359
+
360
+ def test_resistance_distance_empty(self):
361
+ G = nx.Graph()
362
+ with pytest.raises(nx.NetworkXError):
363
+ nx.resistance_distance(G)
364
+
365
+ def test_resistance_distance_not_connected(self):
366
+ with pytest.raises(nx.NetworkXError):
367
+ self.G.add_node(5)
368
+ nx.resistance_distance(self.G, 1, 5)
369
+
370
+ def test_resistance_distance_nodeA_not_in_graph(self):
371
+ with pytest.raises(nx.NetworkXError):
372
+ nx.resistance_distance(self.G, 9, 1)
373
+
374
+ def test_resistance_distance_nodeB_not_in_graph(self):
375
+ with pytest.raises(nx.NetworkXError):
376
+ nx.resistance_distance(self.G, 1, 9)
377
+
378
+ def test_resistance_distance(self):
379
+ rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
380
+ test_data = 1 / (1 / (2 + 4) + 1 / (1 + 3))
381
+ assert round(rd, 5) == round(test_data, 5)
382
+
383
+ def test_resistance_distance_noinv(self):
384
+ rd = nx.resistance_distance(self.G, 1, 3, "weight", False)
385
+ test_data = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1 + 1 / 3))
386
+ assert round(rd, 5) == round(test_data, 5)
387
+
388
+ def test_resistance_distance_no_weight(self):
389
+ rd = nx.resistance_distance(self.G, 1, 3)
390
+ assert round(rd, 5) == 1
391
+
392
+ def test_resistance_distance_neg_weight(self):
393
+ self.G[2][3]["weight"] = -4
394
+ rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
395
+ test_data = 1 / (1 / (2 + -4) + 1 / (1 + 3))
396
+ assert round(rd, 5) == round(test_data, 5)
397
+
398
+ def test_multigraph(self):
399
+ G = nx.MultiGraph()
400
+ G.add_edge(1, 2, weight=2)
401
+ G.add_edge(2, 3, weight=4)
402
+ G.add_edge(3, 4, weight=1)
403
+ G.add_edge(1, 4, weight=3)
404
+ rd = nx.resistance_distance(G, 1, 3, "weight", True)
405
+ assert np.isclose(rd, 1 / (1 / (2 + 4) + 1 / (1 + 3)))
406
+
407
+ def test_resistance_distance_div0(self):
408
+ with pytest.raises(ZeroDivisionError):
409
+ self.G[1][2]["weight"] = 0
410
+ nx.resistance_distance(self.G, 1, 3, "weight")
411
+
412
+ def test_resistance_distance_same_node(self):
413
+ assert nx.resistance_distance(self.G, 1, 1) == 0
414
+
415
+ def test_resistance_distance_only_nodeA(self):
416
+ rd = nx.resistance_distance(self.G, nodeA=1)
417
+ test_data = {}
418
+ test_data[1] = 0
419
+ test_data[2] = 0.75
420
+ test_data[3] = 1
421
+ test_data[4] = 0.75
422
+ assert type(rd) == dict
423
+ assert sorted(rd.keys()) == sorted(test_data.keys())
424
+ for key in rd:
425
+ assert np.isclose(rd[key], test_data[key])
426
+
427
+ def test_resistance_distance_only_nodeB(self):
428
+ rd = nx.resistance_distance(self.G, nodeB=1)
429
+ test_data = {}
430
+ test_data[1] = 0
431
+ test_data[2] = 0.75
432
+ test_data[3] = 1
433
+ test_data[4] = 0.75
434
+ assert type(rd) == dict
435
+ assert sorted(rd.keys()) == sorted(test_data.keys())
436
+ for key in rd:
437
+ assert np.isclose(rd[key], test_data[key])
438
+
439
+ def test_resistance_distance_all(self):
440
+ rd = nx.resistance_distance(self.G)
441
+ assert type(rd) == dict
442
+ assert round(rd[1][3], 5) == 1
443
+
444
+
445
+ class TestEffectiveGraphResistance:
446
+ @classmethod
447
+ def setup_class(cls):
448
+ global np
449
+ np = pytest.importorskip("numpy")
450
+ sp = pytest.importorskip("scipy")
451
+
452
+ def setup_method(self):
453
+ G = nx.Graph()
454
+ G.add_edge(1, 2, weight=2)
455
+ G.add_edge(1, 3, weight=1)
456
+ G.add_edge(2, 3, weight=4)
457
+ self.G = G
458
+
459
+ def test_effective_graph_resistance_directed_graph(self):
460
+ G = nx.DiGraph()
461
+ with pytest.raises(nx.NetworkXNotImplemented):
462
+ nx.effective_graph_resistance(G)
463
+
464
+ def test_effective_graph_resistance_empty(self):
465
+ G = nx.Graph()
466
+ with pytest.raises(nx.NetworkXError):
467
+ nx.effective_graph_resistance(G)
468
+
469
+ def test_effective_graph_resistance_not_connected(self):
470
+ G = nx.Graph([(1, 2), (3, 4)])
471
+ RG = nx.effective_graph_resistance(G)
472
+ assert np.isinf(RG)
473
+
474
+ def test_effective_graph_resistance(self):
475
+ RG = nx.effective_graph_resistance(self.G, "weight", True)
476
+ rd12 = 1 / (1 / (1 + 4) + 1 / 2)
477
+ rd13 = 1 / (1 / (1 + 2) + 1 / 4)
478
+ rd23 = 1 / (1 / (2 + 4) + 1 / 1)
479
+ assert np.isclose(RG, rd12 + rd13 + rd23)
480
+
481
+ def test_effective_graph_resistance_noinv(self):
482
+ RG = nx.effective_graph_resistance(self.G, "weight", False)
483
+ rd12 = 1 / (1 / (1 / 1 + 1 / 4) + 1 / (1 / 2))
484
+ rd13 = 1 / (1 / (1 / 1 + 1 / 2) + 1 / (1 / 4))
485
+ rd23 = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1))
486
+ assert np.isclose(RG, rd12 + rd13 + rd23)
487
+
488
+ def test_effective_graph_resistance_no_weight(self):
489
+ RG = nx.effective_graph_resistance(self.G)
490
+ assert np.isclose(RG, 2)
491
+
492
+ def test_effective_graph_resistance_neg_weight(self):
493
+ self.G[2][3]["weight"] = -4
494
+ RG = nx.effective_graph_resistance(self.G, "weight", True)
495
+ rd12 = 1 / (1 / (1 + -4) + 1 / 2)
496
+ rd13 = 1 / (1 / (1 + 2) + 1 / (-4))
497
+ rd23 = 1 / (1 / (2 + -4) + 1 / 1)
498
+ assert np.isclose(RG, rd12 + rd13 + rd23)
499
+
500
+ def test_effective_graph_resistance_multigraph(self):
501
+ G = nx.MultiGraph()
502
+ G.add_edge(1, 2, weight=2)
503
+ G.add_edge(1, 3, weight=1)
504
+ G.add_edge(2, 3, weight=1)
505
+ G.add_edge(2, 3, weight=3)
506
+ RG = nx.effective_graph_resistance(G, "weight", True)
507
+ edge23 = 1 / (1 / 1 + 1 / 3)
508
+ rd12 = 1 / (1 / (1 + edge23) + 1 / 2)
509
+ rd13 = 1 / (1 / (1 + 2) + 1 / edge23)
510
+ rd23 = 1 / (1 / (2 + edge23) + 1 / 1)
511
+ assert np.isclose(RG, rd12 + rd13 + rd23)
512
+
513
+ def test_effective_graph_resistance_div0(self):
514
+ with pytest.raises(ZeroDivisionError):
515
+ self.G[1][2]["weight"] = 0
516
+ nx.effective_graph_resistance(self.G, "weight")
517
+
518
+ def test_effective_graph_resistance_complete_graph(self):
519
+ N = 10
520
+ G = nx.complete_graph(N)
521
+ RG = nx.effective_graph_resistance(G)
522
+ assert np.isclose(RG, N - 1)
523
+
524
+ def test_effective_graph_resistance_path_graph(self):
525
+ N = 10
526
+ G = nx.path_graph(N)
527
+ RG = nx.effective_graph_resistance(G)
528
+ assert np.isclose(RG, (N - 1) * N * (N + 1) // 6)
529
+
530
+
531
+ class TestBarycenter:
532
+ """Test :func:`networkx.algorithms.distance_measures.barycenter`."""
533
+
534
+ def barycenter_as_subgraph(self, g, **kwargs):
535
+ """Return the subgraph induced on the barycenter of g"""
536
+ b = nx.barycenter(g, **kwargs)
537
+ assert isinstance(b, list)
538
+ assert set(b) <= set(g)
539
+ return g.subgraph(b)
540
+
541
+ def test_must_be_connected(self):
542
+ pytest.raises(nx.NetworkXNoPath, nx.barycenter, nx.empty_graph(5))
543
+
544
+ def test_sp_kwarg(self):
545
+ # Complete graph K_5. Normally it works...
546
+ K_5 = nx.complete_graph(5)
547
+ sp = dict(nx.shortest_path_length(K_5))
548
+ assert nx.barycenter(K_5, sp=sp) == list(K_5)
549
+
550
+ # ...but not with the weight argument
551
+ for u, v, data in K_5.edges.data():
552
+ data["weight"] = 1
553
+ pytest.raises(ValueError, nx.barycenter, K_5, sp=sp, weight="weight")
554
+
555
+ # ...and a corrupted sp can make it seem like K_5 is disconnected
556
+ del sp[0][1]
557
+ pytest.raises(nx.NetworkXNoPath, nx.barycenter, K_5, sp=sp)
558
+
559
+ def test_trees(self):
560
+ """The barycenter of a tree is a single vertex or an edge.
561
+
562
+ See [West01]_, p. 78.
563
+ """
564
+ prng = Random(0xDEADBEEF)
565
+ for i in range(50):
566
+ RT = nx.random_labeled_tree(prng.randint(1, 75), seed=prng)
567
+ b = self.barycenter_as_subgraph(RT)
568
+ if len(b) == 2:
569
+ assert b.size() == 1
570
+ else:
571
+ assert len(b) == 1
572
+ assert b.size() == 0
573
+
574
+ def test_this_one_specific_tree(self):
575
+ """Test the tree pictured at the bottom of [West01]_, p. 78."""
576
+ g = nx.Graph(
577
+ {
578
+ "a": ["b"],
579
+ "b": ["a", "x"],
580
+ "x": ["b", "y"],
581
+ "y": ["x", "z"],
582
+ "z": ["y", 0, 1, 2, 3, 4],
583
+ 0: ["z"],
584
+ 1: ["z"],
585
+ 2: ["z"],
586
+ 3: ["z"],
587
+ 4: ["z"],
588
+ }
589
+ )
590
+ b = self.barycenter_as_subgraph(g, attr="barycentricity")
591
+ assert list(b) == ["z"]
592
+ assert not b.edges
593
+ expected_barycentricity = {
594
+ 0: 23,
595
+ 1: 23,
596
+ 2: 23,
597
+ 3: 23,
598
+ 4: 23,
599
+ "a": 35,
600
+ "b": 27,
601
+ "x": 21,
602
+ "y": 17,
603
+ "z": 15,
604
+ }
605
+ for node, barycentricity in expected_barycentricity.items():
606
+ assert g.nodes[node]["barycentricity"] == barycentricity
607
+
608
+ # Doubling weights should do nothing but double the barycentricities
609
+ for edge in g.edges:
610
+ g.edges[edge]["weight"] = 2
611
+ b = self.barycenter_as_subgraph(g, weight="weight", attr="barycentricity2")
612
+ assert list(b) == ["z"]
613
+ assert not b.edges
614
+ for node, barycentricity in expected_barycentricity.items():
615
+ assert g.nodes[node]["barycentricity2"] == barycentricity * 2
616
+
617
+
618
+ class TestKemenyConstant:
619
+ @classmethod
620
+ def setup_class(cls):
621
+ global np
622
+ np = pytest.importorskip("numpy")
623
+ sp = pytest.importorskip("scipy")
624
+
625
+ def setup_method(self):
626
+ G = nx.Graph()
627
+ w12 = 2
628
+ w13 = 3
629
+ w23 = 4
630
+ G.add_edge(1, 2, weight=w12)
631
+ G.add_edge(1, 3, weight=w13)
632
+ G.add_edge(2, 3, weight=w23)
633
+ self.G = G
634
+
635
+ def test_kemeny_constant_directed(self):
636
+ G = nx.DiGraph()
637
+ G.add_edge(1, 2)
638
+ G.add_edge(1, 3)
639
+ G.add_edge(2, 3)
640
+ with pytest.raises(nx.NetworkXNotImplemented):
641
+ nx.kemeny_constant(G)
642
+
643
+ def test_kemeny_constant_not_connected(self):
644
+ self.G.add_node(5)
645
+ with pytest.raises(nx.NetworkXError):
646
+ nx.kemeny_constant(self.G)
647
+
648
+ def test_kemeny_constant_no_nodes(self):
649
+ G = nx.Graph()
650
+ with pytest.raises(nx.NetworkXError):
651
+ nx.kemeny_constant(G)
652
+
653
+ def test_kemeny_constant_negative_weight(self):
654
+ G = nx.Graph()
655
+ w12 = 2
656
+ w13 = 3
657
+ w23 = -10
658
+ G.add_edge(1, 2, weight=w12)
659
+ G.add_edge(1, 3, weight=w13)
660
+ G.add_edge(2, 3, weight=w23)
661
+ with pytest.raises(nx.NetworkXError):
662
+ nx.kemeny_constant(G, weight="weight")
663
+
664
+ def test_kemeny_constant(self):
665
+ K = nx.kemeny_constant(self.G, weight="weight")
666
+ w12 = 2
667
+ w13 = 3
668
+ w23 = 4
669
+ test_data = (
670
+ 3
671
+ / 2
672
+ * (w12 + w13)
673
+ * (w12 + w23)
674
+ * (w13 + w23)
675
+ / (
676
+ w12**2 * (w13 + w23)
677
+ + w13**2 * (w12 + w23)
678
+ + w23**2 * (w12 + w13)
679
+ + 3 * w12 * w13 * w23
680
+ )
681
+ )
682
+ assert np.isclose(K, test_data)
683
+
684
+ def test_kemeny_constant_no_weight(self):
685
+ K = nx.kemeny_constant(self.G)
686
+ assert np.isclose(K, 4 / 3)
687
+
688
+ def test_kemeny_constant_multigraph(self):
689
+ G = nx.MultiGraph()
690
+ w12_1 = 2
691
+ w12_2 = 1
692
+ w13 = 3
693
+ w23 = 4
694
+ G.add_edge(1, 2, weight=w12_1)
695
+ G.add_edge(1, 2, weight=w12_2)
696
+ G.add_edge(1, 3, weight=w13)
697
+ G.add_edge(2, 3, weight=w23)
698
+ K = nx.kemeny_constant(G, weight="weight")
699
+ w12 = w12_1 + w12_2
700
+ test_data = (
701
+ 3
702
+ / 2
703
+ * (w12 + w13)
704
+ * (w12 + w23)
705
+ * (w13 + w23)
706
+ / (
707
+ w12**2 * (w13 + w23)
708
+ + w13**2 * (w12 + w23)
709
+ + w23**2 * (w12 + w13)
710
+ + 3 * w12 * w13 * w23
711
+ )
712
+ )
713
+ assert np.isclose(K, test_data)
714
+
715
+ def test_kemeny_constant_weight0(self):
716
+ G = nx.Graph()
717
+ w12 = 0
718
+ w13 = 3
719
+ w23 = 4
720
+ G.add_edge(1, 2, weight=w12)
721
+ G.add_edge(1, 3, weight=w13)
722
+ G.add_edge(2, 3, weight=w23)
723
+ K = nx.kemeny_constant(G, weight="weight")
724
+ test_data = (
725
+ 3
726
+ / 2
727
+ * (w12 + w13)
728
+ * (w12 + w23)
729
+ * (w13 + w23)
730
+ / (
731
+ w12**2 * (w13 + w23)
732
+ + w13**2 * (w12 + w23)
733
+ + w23**2 * (w12 + w13)
734
+ + 3 * w12 * w13 * w23
735
+ )
736
+ )
737
+ assert np.isclose(K, test_data)
738
+
739
+ def test_kemeny_constant_selfloop(self):
740
+ G = nx.Graph()
741
+ w11 = 1
742
+ w12 = 2
743
+ w13 = 3
744
+ w23 = 4
745
+ G.add_edge(1, 1, weight=w11)
746
+ G.add_edge(1, 2, weight=w12)
747
+ G.add_edge(1, 3, weight=w13)
748
+ G.add_edge(2, 3, weight=w23)
749
+ K = nx.kemeny_constant(G, weight="weight")
750
+ test_data = (
751
+ (2 * w11 + 3 * w12 + 3 * w13)
752
+ * (w12 + w23)
753
+ * (w13 + w23)
754
+ / (
755
+ (w12 * w13 + w12 * w23 + w13 * w23)
756
+ * (w11 + 2 * w12 + 2 * w13 + 2 * w23)
757
+ )
758
+ )
759
+ assert np.isclose(K, test_data)
760
+
761
+ def test_kemeny_constant_complete_bipartite_graph(self):
762
+ # Theorem 1 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
763
+ n1 = 5
764
+ n2 = 4
765
+ G = nx.complete_bipartite_graph(n1, n2)
766
+ K = nx.kemeny_constant(G)
767
+ assert np.isclose(K, n1 + n2 - 3 / 2)
768
+
769
+ def test_kemeny_constant_path_graph(self):
770
+ # Theorem 2 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
771
+ n = 10
772
+ G = nx.path_graph(n)
773
+ K = nx.kemeny_constant(G)
774
+ assert np.isclose(K, n**2 / 3 - 2 * n / 3 + 1 / 2)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominance.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ class TestImmediateDominators:
7
+ def test_exceptions(self):
8
+ G = nx.Graph()
9
+ G.add_node(0)
10
+ pytest.raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
11
+ G = nx.MultiGraph(G)
12
+ pytest.raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
13
+ G = nx.DiGraph([[0, 0]])
14
+ pytest.raises(nx.NetworkXError, nx.immediate_dominators, G, 1)
15
+
16
+ def test_singleton(self):
17
+ G = nx.DiGraph()
18
+ G.add_node(0)
19
+ assert nx.immediate_dominators(G, 0) == {0: 0}
20
+ G.add_edge(0, 0)
21
+ assert nx.immediate_dominators(G, 0) == {0: 0}
22
+
23
+ def test_path(self):
24
+ n = 5
25
+ G = nx.path_graph(n, create_using=nx.DiGraph())
26
+ assert nx.immediate_dominators(G, 0) == {i: max(i - 1, 0) for i in range(n)}
27
+
28
+ def test_cycle(self):
29
+ n = 5
30
+ G = nx.cycle_graph(n, create_using=nx.DiGraph())
31
+ assert nx.immediate_dominators(G, 0) == {i: max(i - 1, 0) for i in range(n)}
32
+
33
+ def test_unreachable(self):
34
+ n = 5
35
+ assert n > 1
36
+ G = nx.path_graph(n, create_using=nx.DiGraph())
37
+ assert nx.immediate_dominators(G, n // 2) == {
38
+ i: max(i - 1, n // 2) for i in range(n // 2, n)
39
+ }
40
+
41
+ def test_irreducible1(self):
42
+ """
43
+ Graph taken from figure 2 of "A simple, fast dominance algorithm." (2006).
44
+ https://hdl.handle.net/1911/96345
45
+ """
46
+ edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
47
+ G = nx.DiGraph(edges)
48
+ assert nx.immediate_dominators(G, 5) == {i: 5 for i in range(1, 6)}
49
+
50
+ def test_irreducible2(self):
51
+ """
52
+ Graph taken from figure 4 of "A simple, fast dominance algorithm." (2006).
53
+ https://hdl.handle.net/1911/96345
54
+ """
55
+
56
+ edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1), (6, 4), (6, 5)]
57
+ G = nx.DiGraph(edges)
58
+ result = nx.immediate_dominators(G, 6)
59
+ assert result == {i: 6 for i in range(1, 7)}
60
+
61
+ def test_domrel_png(self):
62
+ # Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
63
+ edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
64
+ G = nx.DiGraph(edges)
65
+ result = nx.immediate_dominators(G, 1)
66
+ assert result == {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2}
67
+ # Test postdominance.
68
+ result = nx.immediate_dominators(G.reverse(copy=False), 6)
69
+ assert result == {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}
70
+
71
+ def test_boost_example(self):
72
+ # Graph taken from Figure 1 of
73
+ # http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
74
+ edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6), (5, 7), (6, 4)]
75
+ G = nx.DiGraph(edges)
76
+ result = nx.immediate_dominators(G, 0)
77
+ assert result == {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1}
78
+ # Test postdominance.
79
+ result = nx.immediate_dominators(G.reverse(copy=False), 7)
80
+ assert result == {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}
81
+
82
+
83
+ class TestDominanceFrontiers:
84
+ def test_exceptions(self):
85
+ G = nx.Graph()
86
+ G.add_node(0)
87
+ pytest.raises(nx.NetworkXNotImplemented, nx.dominance_frontiers, G, 0)
88
+ G = nx.MultiGraph(G)
89
+ pytest.raises(nx.NetworkXNotImplemented, nx.dominance_frontiers, G, 0)
90
+ G = nx.DiGraph([[0, 0]])
91
+ pytest.raises(nx.NetworkXError, nx.dominance_frontiers, G, 1)
92
+
93
+ def test_singleton(self):
94
+ G = nx.DiGraph()
95
+ G.add_node(0)
96
+ assert nx.dominance_frontiers(G, 0) == {0: set()}
97
+ G.add_edge(0, 0)
98
+ assert nx.dominance_frontiers(G, 0) == {0: set()}
99
+
100
+ def test_path(self):
101
+ n = 5
102
+ G = nx.path_graph(n, create_using=nx.DiGraph())
103
+ assert nx.dominance_frontiers(G, 0) == {i: set() for i in range(n)}
104
+
105
+ def test_cycle(self):
106
+ n = 5
107
+ G = nx.cycle_graph(n, create_using=nx.DiGraph())
108
+ assert nx.dominance_frontiers(G, 0) == {i: set() for i in range(n)}
109
+
110
+ def test_unreachable(self):
111
+ n = 5
112
+ assert n > 1
113
+ G = nx.path_graph(n, create_using=nx.DiGraph())
114
+ assert nx.dominance_frontiers(G, n // 2) == {i: set() for i in range(n // 2, n)}
115
+
116
+ def test_irreducible1(self):
117
+ """
118
+ Graph taken from figure 2 of "A simple, fast dominance algorithm." (2006).
119
+ https://hdl.handle.net/1911/96345
120
+ """
121
+ edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
122
+ G = nx.DiGraph(edges)
123
+ assert dict(nx.dominance_frontiers(G, 5).items()) == {
124
+ 1: {2},
125
+ 2: {1},
126
+ 3: {2},
127
+ 4: {1},
128
+ 5: set(),
129
+ }
130
+
131
+ def test_irreducible2(self):
132
+ """
133
+ Graph taken from figure 4 of "A simple, fast dominance algorithm." (2006).
134
+ https://hdl.handle.net/1911/96345
135
+ """
136
+ edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1), (6, 4), (6, 5)]
137
+ G = nx.DiGraph(edges)
138
+ assert nx.dominance_frontiers(G, 6) == {
139
+ 1: {2},
140
+ 2: {1, 3},
141
+ 3: {2},
142
+ 4: {2, 3},
143
+ 5: {1},
144
+ 6: set(),
145
+ }
146
+
147
+ def test_domrel_png(self):
148
+ # Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
149
+ edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
150
+ G = nx.DiGraph(edges)
151
+ assert nx.dominance_frontiers(G, 1) == {
152
+ 1: set(),
153
+ 2: {2},
154
+ 3: {5},
155
+ 4: {5},
156
+ 5: {2},
157
+ 6: set(),
158
+ }
159
+ # Test postdominance.
160
+ result = nx.dominance_frontiers(G.reverse(copy=False), 6)
161
+ assert result == {1: set(), 2: {2}, 3: {2}, 4: {2}, 5: {2}, 6: set()}
162
+
163
+ def test_boost_example(self):
164
+ # Graph taken from Figure 1 of
165
+ # http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
166
+ edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6), (5, 7), (6, 4)]
167
+ G = nx.DiGraph(edges)
168
+ assert nx.dominance_frontiers(G, 0) == {
169
+ 0: set(),
170
+ 1: set(),
171
+ 2: {7},
172
+ 3: {7},
173
+ 4: {4, 7},
174
+ 5: {7},
175
+ 6: {4},
176
+ 7: set(),
177
+ }
178
+ # Test postdominance.
179
+ result = nx.dominance_frontiers(G.reverse(copy=False), 7)
180
+ expected = {
181
+ 0: set(),
182
+ 1: set(),
183
+ 2: {1},
184
+ 3: {1},
185
+ 4: {1, 4},
186
+ 5: {1},
187
+ 6: {4},
188
+ 7: set(),
189
+ }
190
+ assert result == expected
191
+
192
+ def test_discard_issue(self):
193
+ # https://github.com/networkx/networkx/issues/2071
194
+ g = nx.DiGraph()
195
+ g.add_edges_from(
196
+ [
197
+ ("b0", "b1"),
198
+ ("b1", "b2"),
199
+ ("b2", "b3"),
200
+ ("b3", "b1"),
201
+ ("b1", "b5"),
202
+ ("b5", "b6"),
203
+ ("b5", "b8"),
204
+ ("b6", "b7"),
205
+ ("b8", "b7"),
206
+ ("b7", "b3"),
207
+ ("b3", "b4"),
208
+ ]
209
+ )
210
+ df = nx.dominance_frontiers(g, "b0")
211
+ assert df == {
212
+ "b4": set(),
213
+ "b5": {"b3"},
214
+ "b6": {"b7"},
215
+ "b7": {"b3"},
216
+ "b0": set(),
217
+ "b1": {"b1"},
218
+ "b2": {"b3"},
219
+ "b3": {"b1"},
220
+ "b8": {"b7"},
221
+ }
222
+
223
+ def test_loop(self):
224
+ g = nx.DiGraph()
225
+ g.add_edges_from([("a", "b"), ("b", "c"), ("b", "a")])
226
+ df = nx.dominance_frontiers(g, "a")
227
+ assert df == {"a": set(), "b": set(), "c": set()}
228
+
229
+ def test_missing_immediate_doms(self):
230
+ # see https://github.com/networkx/networkx/issues/2070
231
+ g = nx.DiGraph()
232
+ edges = [
233
+ ("entry_1", "b1"),
234
+ ("b1", "b2"),
235
+ ("b2", "b3"),
236
+ ("b3", "exit"),
237
+ ("entry_2", "b3"),
238
+ ]
239
+
240
+ # entry_1
241
+ # |
242
+ # b1
243
+ # |
244
+ # b2 entry_2
245
+ # | /
246
+ # b3
247
+ # |
248
+ # exit
249
+
250
+ g.add_edges_from(edges)
251
+ # formerly raised KeyError on entry_2 when parsing b3
252
+ # because entry_2 does not have immediate doms (no path)
253
+ nx.dominance_frontiers(g, "entry_1")
254
+
255
+ def test_loops_larger(self):
256
+ # from
257
+ # http://ecee.colorado.edu/~waite/Darmstadt/motion.html
258
+ g = nx.DiGraph()
259
+ edges = [
260
+ ("entry", "exit"),
261
+ ("entry", "1"),
262
+ ("1", "2"),
263
+ ("2", "3"),
264
+ ("3", "4"),
265
+ ("4", "5"),
266
+ ("5", "6"),
267
+ ("6", "exit"),
268
+ ("6", "2"),
269
+ ("5", "3"),
270
+ ("4", "4"),
271
+ ]
272
+
273
+ g.add_edges_from(edges)
274
+ df = nx.dominance_frontiers(g, "entry")
275
+ answer = {
276
+ "entry": set(),
277
+ "1": {"exit"},
278
+ "2": {"exit", "2"},
279
+ "3": {"exit", "3", "2"},
280
+ "4": {"exit", "4", "3", "2"},
281
+ "5": {"exit", "3", "2"},
282
+ "6": {"exit", "2"},
283
+ "exit": set(),
284
+ }
285
+ for n in df:
286
+ assert set(df[n]) == set(answer[n])
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominating.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_dominating_set():
7
+ G = nx.gnp_random_graph(100, 0.1)
8
+ D = nx.dominating_set(G)
9
+ assert nx.is_dominating_set(G, D)
10
+ D = nx.dominating_set(G, start_with=0)
11
+ assert nx.is_dominating_set(G, D)
12
+
13
+
14
+ def test_complete():
15
+ """In complete graphs each node is a dominating set.
16
+ Thus the dominating set has to be of cardinality 1.
17
+ """
18
+ K4 = nx.complete_graph(4)
19
+ assert len(nx.dominating_set(K4)) == 1
20
+ K5 = nx.complete_graph(5)
21
+ assert len(nx.dominating_set(K5)) == 1
22
+
23
+
24
+ def test_raise_dominating_set():
25
+ with pytest.raises(nx.NetworkXError):
26
+ G = nx.path_graph(4)
27
+ D = nx.dominating_set(G, start_with=10)
28
+
29
+
30
+ def test_is_dominating_set():
31
+ G = nx.path_graph(4)
32
+ d = {1, 3}
33
+ assert nx.is_dominating_set(G, d)
34
+ d = {0, 2}
35
+ assert nx.is_dominating_set(G, d)
36
+ d = {1}
37
+ assert not nx.is_dominating_set(G, d)
38
+
39
+
40
+ def test_wikipedia_is_dominating_set():
41
+ """Example from https://en.wikipedia.org/wiki/Dominating_set"""
42
+ G = nx.cycle_graph(4)
43
+ G.add_edges_from([(0, 4), (1, 4), (2, 5)])
44
+ assert nx.is_dominating_set(G, {4, 3, 5})
45
+ assert nx.is_dominating_set(G, {0, 2})
46
+ assert nx.is_dominating_set(G, {1, 2})
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_efficiency.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.efficiency` module."""
2
+
3
+ import networkx as nx
4
+
5
+
6
+ class TestEfficiency:
7
+ def setup_method(self):
8
+ # G1 is a disconnected graph
9
+ self.G1 = nx.Graph()
10
+ self.G1.add_nodes_from([1, 2, 3])
11
+ # G2 is a cycle graph
12
+ self.G2 = nx.cycle_graph(4)
13
+ # G3 is the triangle graph with one additional edge
14
+ self.G3 = nx.lollipop_graph(3, 1)
15
+
16
+ def test_efficiency_disconnected_nodes(self):
17
+ """
18
+ When nodes are disconnected, efficiency is 0
19
+ """
20
+ assert nx.efficiency(self.G1, 1, 2) == 0
21
+
22
+ def test_local_efficiency_disconnected_graph(self):
23
+ """
24
+ In a disconnected graph the efficiency is 0
25
+ """
26
+ assert nx.local_efficiency(self.G1) == 0
27
+
28
+ def test_efficiency(self):
29
+ assert nx.efficiency(self.G2, 0, 1) == 1
30
+ assert nx.efficiency(self.G2, 0, 2) == 1 / 2
31
+
32
+ def test_global_efficiency(self):
33
+ assert nx.global_efficiency(self.G2) == 5 / 6
34
+
35
+ def test_global_efficiency_complete_graph(self):
36
+ """
37
+ Tests that the average global efficiency of the complete graph is one.
38
+ """
39
+ for n in range(2, 10):
40
+ G = nx.complete_graph(n)
41
+ assert nx.global_efficiency(G) == 1
42
+
43
+ def test_local_efficiency_complete_graph(self):
44
+ """
45
+ Test that the local efficiency for a complete graph with at least 3
46
+ nodes should be one. For a graph with only 2 nodes, the induced
47
+ subgraph has no edges.
48
+ """
49
+ for n in range(3, 10):
50
+ G = nx.complete_graph(n)
51
+ assert nx.local_efficiency(G) == 1
52
+
53
+ def test_using_ego_graph(self):
54
+ """
55
+ Test that the ego graph is used when computing local efficiency.
56
+ For more information, see GitHub issue #2710.
57
+ """
58
+ assert nx.local_efficiency(self.G3) == 7 / 12
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_graph_hashing.py ADDED
@@ -0,0 +1,686 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.generators import directed
5
+
6
+ # Unit tests for the :func:`~networkx.weisfeiler_lehman_graph_hash` function
7
+
8
+
9
+ def test_empty_graph_hash():
10
+ """
11
+ empty graphs should give hashes regardless of other params
12
+ """
13
+ G1 = nx.empty_graph()
14
+ G2 = nx.empty_graph()
15
+
16
+ h1 = nx.weisfeiler_lehman_graph_hash(G1)
17
+ h2 = nx.weisfeiler_lehman_graph_hash(G2)
18
+ h3 = nx.weisfeiler_lehman_graph_hash(G2, edge_attr="edge_attr1")
19
+ h4 = nx.weisfeiler_lehman_graph_hash(G2, node_attr="node_attr1")
20
+ h5 = nx.weisfeiler_lehman_graph_hash(
21
+ G2, edge_attr="edge_attr1", node_attr="node_attr1"
22
+ )
23
+ h6 = nx.weisfeiler_lehman_graph_hash(G2, iterations=10)
24
+
25
+ assert h1 == h2
26
+ assert h1 == h3
27
+ assert h1 == h4
28
+ assert h1 == h5
29
+ assert h1 == h6
30
+
31
+
32
+ def test_directed():
33
+ """
34
+ A directed graph with no bi-directional edges should yield different a graph hash
35
+ to the same graph taken as undirected if there are no hash collisions.
36
+ """
37
+ r = 10
38
+ for i in range(r):
39
+ G_directed = nx.gn_graph(10 + r, seed=100 + i)
40
+ G_undirected = nx.to_undirected(G_directed)
41
+
42
+ h_directed = nx.weisfeiler_lehman_graph_hash(G_directed)
43
+ h_undirected = nx.weisfeiler_lehman_graph_hash(G_undirected)
44
+
45
+ assert h_directed != h_undirected
46
+
47
+
48
+ def test_reversed():
49
+ """
50
+ A directed graph with no bi-directional edges should yield different a graph hash
51
+ to the same graph taken with edge directions reversed if there are no hash collisions.
52
+ Here we test a cycle graph which is the minimal counterexample
53
+ """
54
+ G = nx.cycle_graph(5, create_using=nx.DiGraph)
55
+ nx.set_node_attributes(G, {n: str(n) for n in G.nodes()}, name="label")
56
+
57
+ G_reversed = G.reverse()
58
+
59
+ h = nx.weisfeiler_lehman_graph_hash(G, node_attr="label")
60
+ h_reversed = nx.weisfeiler_lehman_graph_hash(G_reversed, node_attr="label")
61
+
62
+ assert h != h_reversed
63
+
64
+
65
+ def test_isomorphic():
66
+ """
67
+ graph hashes should be invariant to node-relabeling (when the output is reindexed
68
+ by the same mapping)
69
+ """
70
+ n, r = 100, 10
71
+ p = 1.0 / r
72
+ for i in range(1, r + 1):
73
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=200 + i)
74
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
75
+
76
+ g1_hash = nx.weisfeiler_lehman_graph_hash(G1)
77
+ g2_hash = nx.weisfeiler_lehman_graph_hash(G2)
78
+
79
+ assert g1_hash == g2_hash
80
+
81
+
82
+ def test_isomorphic_edge_attr():
83
+ """
84
+ Isomorphic graphs with differing edge attributes should yield different graph
85
+ hashes if the 'edge_attr' argument is supplied and populated in the graph,
86
+ and there are no hash collisions.
87
+ The output should still be invariant to node-relabeling
88
+ """
89
+ n, r = 100, 10
90
+ p = 1.0 / r
91
+ for i in range(1, r + 1):
92
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=300 + i)
93
+
94
+ for a, b in G1.edges:
95
+ G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
96
+ G1[a][b]["edge_attr2"] = f"{a}-{b}-2"
97
+
98
+ g1_hash_with_edge_attr1 = nx.weisfeiler_lehman_graph_hash(
99
+ G1, edge_attr="edge_attr1"
100
+ )
101
+ g1_hash_with_edge_attr2 = nx.weisfeiler_lehman_graph_hash(
102
+ G1, edge_attr="edge_attr2"
103
+ )
104
+ g1_hash_no_edge_attr = nx.weisfeiler_lehman_graph_hash(G1, edge_attr=None)
105
+
106
+ assert g1_hash_with_edge_attr1 != g1_hash_no_edge_attr
107
+ assert g1_hash_with_edge_attr2 != g1_hash_no_edge_attr
108
+ assert g1_hash_with_edge_attr1 != g1_hash_with_edge_attr2
109
+
110
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
111
+
112
+ g2_hash_with_edge_attr1 = nx.weisfeiler_lehman_graph_hash(
113
+ G2, edge_attr="edge_attr1"
114
+ )
115
+ g2_hash_with_edge_attr2 = nx.weisfeiler_lehman_graph_hash(
116
+ G2, edge_attr="edge_attr2"
117
+ )
118
+
119
+ assert g1_hash_with_edge_attr1 == g2_hash_with_edge_attr1
120
+ assert g1_hash_with_edge_attr2 == g2_hash_with_edge_attr2
121
+
122
+
123
+ def test_missing_edge_attr():
124
+ """
125
+ If the 'edge_attr' argument is supplied but is missing from an edge in the graph,
126
+ we should raise a KeyError
127
+ """
128
+ G = nx.Graph()
129
+ G.add_edges_from([(1, 2, {"edge_attr1": "a"}), (1, 3, {})])
130
+ pytest.raises(KeyError, nx.weisfeiler_lehman_graph_hash, G, edge_attr="edge_attr1")
131
+
132
+
133
+ def test_isomorphic_node_attr():
134
+ """
135
+ Isomorphic graphs with differing node attributes should yield different graph
136
+ hashes if the 'node_attr' argument is supplied and populated in the graph, and
137
+ there are no hash collisions.
138
+ The output should still be invariant to node-relabeling
139
+ """
140
+ n, r = 100, 10
141
+ p = 1.0 / r
142
+ for i in range(1, r + 1):
143
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=400 + i)
144
+
145
+ for u in G1.nodes():
146
+ G1.nodes[u]["node_attr1"] = f"{u}-1"
147
+ G1.nodes[u]["node_attr2"] = f"{u}-2"
148
+
149
+ g1_hash_with_node_attr1 = nx.weisfeiler_lehman_graph_hash(
150
+ G1, node_attr="node_attr1"
151
+ )
152
+ g1_hash_with_node_attr2 = nx.weisfeiler_lehman_graph_hash(
153
+ G1, node_attr="node_attr2"
154
+ )
155
+ g1_hash_no_node_attr = nx.weisfeiler_lehman_graph_hash(G1, node_attr=None)
156
+
157
+ assert g1_hash_with_node_attr1 != g1_hash_no_node_attr
158
+ assert g1_hash_with_node_attr2 != g1_hash_no_node_attr
159
+ assert g1_hash_with_node_attr1 != g1_hash_with_node_attr2
160
+
161
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
162
+
163
+ g2_hash_with_node_attr1 = nx.weisfeiler_lehman_graph_hash(
164
+ G2, node_attr="node_attr1"
165
+ )
166
+ g2_hash_with_node_attr2 = nx.weisfeiler_lehman_graph_hash(
167
+ G2, node_attr="node_attr2"
168
+ )
169
+
170
+ assert g1_hash_with_node_attr1 == g2_hash_with_node_attr1
171
+ assert g1_hash_with_node_attr2 == g2_hash_with_node_attr2
172
+
173
+
174
+ def test_missing_node_attr():
175
+ """
176
+ If the 'node_attr' argument is supplied but is missing from a node in the graph,
177
+ we should raise a KeyError
178
+ """
179
+ G = nx.Graph()
180
+ G.add_nodes_from([(1, {"node_attr1": "a"}), (2, {})])
181
+ G.add_edges_from([(1, 2), (2, 3), (3, 1), (1, 4)])
182
+ pytest.raises(KeyError, nx.weisfeiler_lehman_graph_hash, G, node_attr="node_attr1")
183
+
184
+
185
+ def test_isomorphic_edge_attr_and_node_attr():
186
+ """
187
+ Isomorphic graphs with differing node attributes should yield different graph
188
+ hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
189
+ the graph, and there are no hash collisions.
190
+ The output should still be invariant to node-relabeling
191
+ """
192
+ n, r = 100, 10
193
+ p = 1.0 / r
194
+ for i in range(1, r + 1):
195
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=500 + i)
196
+
197
+ for u in G1.nodes():
198
+ G1.nodes[u]["node_attr1"] = f"{u}-1"
199
+ G1.nodes[u]["node_attr2"] = f"{u}-2"
200
+
201
+ for a, b in G1.edges:
202
+ G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
203
+ G1[a][b]["edge_attr2"] = f"{a}-{b}-2"
204
+
205
+ g1_hash_edge1_node1 = nx.weisfeiler_lehman_graph_hash(
206
+ G1, edge_attr="edge_attr1", node_attr="node_attr1"
207
+ )
208
+ g1_hash_edge2_node2 = nx.weisfeiler_lehman_graph_hash(
209
+ G1, edge_attr="edge_attr2", node_attr="node_attr2"
210
+ )
211
+ g1_hash_edge1_node2 = nx.weisfeiler_lehman_graph_hash(
212
+ G1, edge_attr="edge_attr1", node_attr="node_attr2"
213
+ )
214
+ g1_hash_no_attr = nx.weisfeiler_lehman_graph_hash(G1)
215
+
216
+ assert g1_hash_edge1_node1 != g1_hash_no_attr
217
+ assert g1_hash_edge2_node2 != g1_hash_no_attr
218
+ assert g1_hash_edge1_node1 != g1_hash_edge2_node2
219
+ assert g1_hash_edge1_node2 != g1_hash_edge2_node2
220
+ assert g1_hash_edge1_node2 != g1_hash_edge1_node1
221
+
222
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
223
+
224
+ g2_hash_edge1_node1 = nx.weisfeiler_lehman_graph_hash(
225
+ G2, edge_attr="edge_attr1", node_attr="node_attr1"
226
+ )
227
+ g2_hash_edge2_node2 = nx.weisfeiler_lehman_graph_hash(
228
+ G2, edge_attr="edge_attr2", node_attr="node_attr2"
229
+ )
230
+
231
+ assert g1_hash_edge1_node1 == g2_hash_edge1_node1
232
+ assert g1_hash_edge2_node2 == g2_hash_edge2_node2
233
+
234
+
235
+ def test_digest_size():
236
+ """
237
+ The hash string lengths should be as expected for a variety of graphs and
238
+ digest sizes
239
+ """
240
+ n, r = 100, 10
241
+ p = 1.0 / r
242
+ for i in range(1, r + 1):
243
+ G = nx.erdos_renyi_graph(n, p * i, seed=1000 + i)
244
+
245
+ h16 = nx.weisfeiler_lehman_graph_hash(G)
246
+ h32 = nx.weisfeiler_lehman_graph_hash(G, digest_size=32)
247
+
248
+ assert h16 != h32
249
+ assert len(h16) == 16 * 2
250
+ assert len(h32) == 32 * 2
251
+
252
+
253
+ # Unit tests for the :func:`~networkx.weisfeiler_lehman_hash_subgraphs` function
254
+
255
+
256
+ def is_subiteration(a, b):
257
+ """
258
+ returns True if that each hash sequence in 'a' is a prefix for
259
+ the corresponding sequence indexed by the same node in 'b'.
260
+ """
261
+ return all(b[node][: len(hashes)] == hashes for node, hashes in a.items())
262
+
263
+
264
+ def hexdigest_sizes_correct(a, digest_size):
265
+ """
266
+ returns True if all hex digest sizes are the expected length in a node:subgraph-hashes
267
+ dictionary. Hex digest string length == 2 * bytes digest length since each pair of hex
268
+ digits encodes 1 byte (https://docs.python.org/3/library/hashlib.html)
269
+ """
270
+ hexdigest_size = digest_size * 2
271
+ list_digest_sizes_correct = lambda l: all(len(x) == hexdigest_size for x in l)
272
+ return all(list_digest_sizes_correct(hashes) for hashes in a.values())
273
+
274
+
275
+ def test_empty_graph_subgraph_hash():
276
+ """ "
277
+ empty graphs should give empty dict subgraph hashes regardless of other params
278
+ """
279
+ G = nx.empty_graph()
280
+
281
+ subgraph_hashes1 = nx.weisfeiler_lehman_subgraph_hashes(G)
282
+ subgraph_hashes2 = nx.weisfeiler_lehman_subgraph_hashes(G, edge_attr="edge_attr")
283
+ subgraph_hashes3 = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="edge_attr")
284
+ subgraph_hashes4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=2)
285
+ subgraph_hashes5 = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=64)
286
+
287
+ assert subgraph_hashes1 == {}
288
+ assert subgraph_hashes2 == {}
289
+ assert subgraph_hashes3 == {}
290
+ assert subgraph_hashes4 == {}
291
+ assert subgraph_hashes5 == {}
292
+
293
+
294
+ def test_directed_subgraph_hash():
295
+ """
296
+ A directed graph with no bi-directional edges should yield different subgraph hashes
297
+ to the same graph taken as undirected, if all hashes don't collide.
298
+ """
299
+ r = 10
300
+ for i in range(r):
301
+ G_directed = nx.gn_graph(10 + r, seed=100 + i)
302
+ G_undirected = nx.to_undirected(G_directed)
303
+
304
+ directed_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_directed)
305
+ undirected_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_undirected)
306
+
307
+ assert directed_subgraph_hashes != undirected_subgraph_hashes
308
+
309
+
310
+ def test_reversed_subgraph_hash():
311
+ """
312
+ A directed graph with no bi-directional edges should yield different subgraph hashes
313
+ to the same graph taken with edge directions reversed if there are no hash collisions.
314
+ Here we test a cycle graph which is the minimal counterexample
315
+ """
316
+ G = nx.cycle_graph(5, create_using=nx.DiGraph)
317
+ nx.set_node_attributes(G, {n: str(n) for n in G.nodes()}, name="label")
318
+
319
+ G_reversed = G.reverse()
320
+
321
+ h = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="label")
322
+ h_reversed = nx.weisfeiler_lehman_subgraph_hashes(G_reversed, node_attr="label")
323
+
324
+ assert h != h_reversed
325
+
326
+
327
+ def test_isomorphic_subgraph_hash():
328
+ """
329
+ the subgraph hashes should be invariant to node-relabeling when the output is reindexed
330
+ by the same mapping and all hashes don't collide.
331
+ """
332
+ n, r = 100, 10
333
+ p = 1.0 / r
334
+ for i in range(1, r + 1):
335
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=200 + i)
336
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
337
+
338
+ g1_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G1)
339
+ g2_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G2)
340
+
341
+ assert g1_subgraph_hashes == {-1 * k: v for k, v in g2_subgraph_hashes.items()}
342
+
343
+
344
+ def test_isomorphic_edge_attr_subgraph_hash():
345
+ """
346
+ Isomorphic graphs with differing edge attributes should yield different subgraph
347
+ hashes if the 'edge_attr' argument is supplied and populated in the graph, and
348
+ all hashes don't collide.
349
+ The output should still be invariant to node-relabeling
350
+ """
351
+ n, r = 100, 10
352
+ p = 1.0 / r
353
+ for i in range(1, r + 1):
354
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=300 + i)
355
+
356
+ for a, b in G1.edges:
357
+ G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
358
+ G1[a][b]["edge_attr2"] = f"{a}-{b}-2"
359
+
360
+ g1_hash_with_edge_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
361
+ G1, edge_attr="edge_attr1"
362
+ )
363
+ g1_hash_with_edge_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
364
+ G1, edge_attr="edge_attr2"
365
+ )
366
+ g1_hash_no_edge_attr = nx.weisfeiler_lehman_subgraph_hashes(G1, edge_attr=None)
367
+
368
+ assert g1_hash_with_edge_attr1 != g1_hash_no_edge_attr
369
+ assert g1_hash_with_edge_attr2 != g1_hash_no_edge_attr
370
+ assert g1_hash_with_edge_attr1 != g1_hash_with_edge_attr2
371
+
372
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
373
+
374
+ g2_hash_with_edge_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
375
+ G2, edge_attr="edge_attr1"
376
+ )
377
+ g2_hash_with_edge_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
378
+ G2, edge_attr="edge_attr2"
379
+ )
380
+
381
+ assert g1_hash_with_edge_attr1 == {
382
+ -1 * k: v for k, v in g2_hash_with_edge_attr1.items()
383
+ }
384
+ assert g1_hash_with_edge_attr2 == {
385
+ -1 * k: v for k, v in g2_hash_with_edge_attr2.items()
386
+ }
387
+
388
+
389
+ def test_missing_edge_attr_subgraph_hash():
390
+ """
391
+ If the 'edge_attr' argument is supplied but is missing from an edge in the graph,
392
+ we should raise a KeyError
393
+ """
394
+ G = nx.Graph()
395
+ G.add_edges_from([(1, 2, {"edge_attr1": "a"}), (1, 3, {})])
396
+ pytest.raises(
397
+ KeyError, nx.weisfeiler_lehman_subgraph_hashes, G, edge_attr="edge_attr1"
398
+ )
399
+
400
+
401
+ def test_isomorphic_node_attr_subgraph_hash():
402
+ """
403
+ Isomorphic graphs with differing node attributes should yield different subgraph
404
+ hashes if the 'node_attr' argument is supplied and populated in the graph, and
405
+ all hashes don't collide.
406
+ The output should still be invariant to node-relabeling
407
+ """
408
+ n, r = 100, 10
409
+ p = 1.0 / r
410
+ for i in range(1, r + 1):
411
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=400 + i)
412
+
413
+ for u in G1.nodes():
414
+ G1.nodes[u]["node_attr1"] = f"{u}-1"
415
+ G1.nodes[u]["node_attr2"] = f"{u}-2"
416
+
417
+ g1_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
418
+ G1, node_attr="node_attr1"
419
+ )
420
+ g1_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
421
+ G1, node_attr="node_attr2"
422
+ )
423
+ g1_hash_no_node_attr = nx.weisfeiler_lehman_subgraph_hashes(G1, node_attr=None)
424
+
425
+ assert g1_hash_with_node_attr1 != g1_hash_no_node_attr
426
+ assert g1_hash_with_node_attr2 != g1_hash_no_node_attr
427
+ assert g1_hash_with_node_attr1 != g1_hash_with_node_attr2
428
+
429
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
430
+
431
+ g2_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
432
+ G2, node_attr="node_attr1"
433
+ )
434
+ g2_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
435
+ G2, node_attr="node_attr2"
436
+ )
437
+
438
+ assert g1_hash_with_node_attr1 == {
439
+ -1 * k: v for k, v in g2_hash_with_node_attr1.items()
440
+ }
441
+ assert g1_hash_with_node_attr2 == {
442
+ -1 * k: v for k, v in g2_hash_with_node_attr2.items()
443
+ }
444
+
445
+
446
+ def test_missing_node_attr_subgraph_hash():
447
+ """
448
+ If the 'node_attr' argument is supplied but is missing from a node in the graph,
449
+ we should raise a KeyError
450
+ """
451
+ G = nx.Graph()
452
+ G.add_nodes_from([(1, {"node_attr1": "a"}), (2, {})])
453
+ G.add_edges_from([(1, 2), (2, 3), (3, 1), (1, 4)])
454
+ pytest.raises(
455
+ KeyError, nx.weisfeiler_lehman_subgraph_hashes, G, node_attr="node_attr1"
456
+ )
457
+
458
+
459
+ def test_isomorphic_edge_attr_and_node_attr_subgraph_hash():
460
+ """
461
+ Isomorphic graphs with differing node attributes should yield different subgraph
462
+ hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
463
+ the graph, and all hashes don't collide
464
+ The output should still be invariant to node-relabeling
465
+ """
466
+ n, r = 100, 10
467
+ p = 1.0 / r
468
+ for i in range(1, r + 1):
469
+ G1 = nx.erdos_renyi_graph(n, p * i, seed=500 + i)
470
+
471
+ for u in G1.nodes():
472
+ G1.nodes[u]["node_attr1"] = f"{u}-1"
473
+ G1.nodes[u]["node_attr2"] = f"{u}-2"
474
+
475
+ for a, b in G1.edges:
476
+ G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
477
+ G1[a][b]["edge_attr2"] = f"{a}-{b}-2"
478
+
479
+ g1_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
480
+ G1, edge_attr="edge_attr1", node_attr="node_attr1"
481
+ )
482
+ g1_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
483
+ G1, edge_attr="edge_attr2", node_attr="node_attr2"
484
+ )
485
+ g1_hash_edge1_node2 = nx.weisfeiler_lehman_subgraph_hashes(
486
+ G1, edge_attr="edge_attr1", node_attr="node_attr2"
487
+ )
488
+ g1_hash_no_attr = nx.weisfeiler_lehman_subgraph_hashes(G1)
489
+
490
+ assert g1_hash_edge1_node1 != g1_hash_no_attr
491
+ assert g1_hash_edge2_node2 != g1_hash_no_attr
492
+ assert g1_hash_edge1_node1 != g1_hash_edge2_node2
493
+ assert g1_hash_edge1_node2 != g1_hash_edge2_node2
494
+ assert g1_hash_edge1_node2 != g1_hash_edge1_node1
495
+
496
+ G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})
497
+
498
+ g2_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
499
+ G2, edge_attr="edge_attr1", node_attr="node_attr1"
500
+ )
501
+ g2_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
502
+ G2, edge_attr="edge_attr2", node_attr="node_attr2"
503
+ )
504
+
505
+ assert g1_hash_edge1_node1 == {
506
+ -1 * k: v for k, v in g2_hash_edge1_node1.items()
507
+ }
508
+ assert g1_hash_edge2_node2 == {
509
+ -1 * k: v for k, v in g2_hash_edge2_node2.items()
510
+ }
511
+
512
+
513
+ def test_iteration_depth():
514
+ """
515
+ All nodes should have the correct number of subgraph hashes in the output when
516
+ using degree as initial node labels
517
+ Subsequent iteration depths for the same graph should be additive for each node
518
+ """
519
+ n, r = 100, 10
520
+ p = 1.0 / r
521
+ for i in range(1, r + 1):
522
+ G = nx.erdos_renyi_graph(n, p * i, seed=600 + i)
523
+
524
+ depth3 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=3)
525
+ depth4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=4)
526
+ depth5 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=5)
527
+
528
+ assert all(len(hashes) == 3 for hashes in depth3.values())
529
+ assert all(len(hashes) == 4 for hashes in depth4.values())
530
+ assert all(len(hashes) == 5 for hashes in depth5.values())
531
+
532
+ assert is_subiteration(depth3, depth4)
533
+ assert is_subiteration(depth4, depth5)
534
+ assert is_subiteration(depth3, depth5)
535
+
536
+
537
+ def test_iteration_depth_edge_attr():
538
+ """
539
+ All nodes should have the correct number of subgraph hashes in the output when
540
+ setting initial node labels empty and using an edge attribute when aggregating
541
+ neighborhoods.
542
+ Subsequent iteration depths for the same graph should be additive for each node
543
+ """
544
+ n, r = 100, 10
545
+ p = 1.0 / r
546
+ for i in range(1, r + 1):
547
+ G = nx.erdos_renyi_graph(n, p * i, seed=700 + i)
548
+
549
+ for a, b in G.edges:
550
+ G[a][b]["edge_attr1"] = f"{a}-{b}-1"
551
+
552
+ depth3 = nx.weisfeiler_lehman_subgraph_hashes(
553
+ G, edge_attr="edge_attr1", iterations=3
554
+ )
555
+ depth4 = nx.weisfeiler_lehman_subgraph_hashes(
556
+ G, edge_attr="edge_attr1", iterations=4
557
+ )
558
+ depth5 = nx.weisfeiler_lehman_subgraph_hashes(
559
+ G, edge_attr="edge_attr1", iterations=5
560
+ )
561
+
562
+ assert all(len(hashes) == 3 for hashes in depth3.values())
563
+ assert all(len(hashes) == 4 for hashes in depth4.values())
564
+ assert all(len(hashes) == 5 for hashes in depth5.values())
565
+
566
+ assert is_subiteration(depth3, depth4)
567
+ assert is_subiteration(depth4, depth5)
568
+ assert is_subiteration(depth3, depth5)
569
+
570
+
571
+ def test_iteration_depth_node_attr():
572
+ """
573
+ All nodes should have the correct number of subgraph hashes in the output when
574
+ setting initial node labels to an attribute.
575
+ Subsequent iteration depths for the same graph should be additive for each node
576
+ """
577
+ n, r = 100, 10
578
+ p = 1.0 / r
579
+ for i in range(1, r + 1):
580
+ G = nx.erdos_renyi_graph(n, p * i, seed=800 + i)
581
+
582
+ for u in G.nodes():
583
+ G.nodes[u]["node_attr1"] = f"{u}-1"
584
+
585
+ depth3 = nx.weisfeiler_lehman_subgraph_hashes(
586
+ G, node_attr="node_attr1", iterations=3
587
+ )
588
+ depth4 = nx.weisfeiler_lehman_subgraph_hashes(
589
+ G, node_attr="node_attr1", iterations=4
590
+ )
591
+ depth5 = nx.weisfeiler_lehman_subgraph_hashes(
592
+ G, node_attr="node_attr1", iterations=5
593
+ )
594
+
595
+ assert all(len(hashes) == 3 for hashes in depth3.values())
596
+ assert all(len(hashes) == 4 for hashes in depth4.values())
597
+ assert all(len(hashes) == 5 for hashes in depth5.values())
598
+
599
+ assert is_subiteration(depth3, depth4)
600
+ assert is_subiteration(depth4, depth5)
601
+ assert is_subiteration(depth3, depth5)
602
+
603
+
604
+ def test_iteration_depth_node_edge_attr():
605
+ """
606
+ All nodes should have the correct number of subgraph hashes in the output when
607
+ setting initial node labels to an attribute and also using an edge attribute when
608
+ aggregating neighborhoods.
609
+ Subsequent iteration depths for the same graph should be additive for each node
610
+ """
611
+ n, r = 100, 10
612
+ p = 1.0 / r
613
+ for i in range(1, r + 1):
614
+ G = nx.erdos_renyi_graph(n, p * i, seed=900 + i)
615
+
616
+ for u in G.nodes():
617
+ G.nodes[u]["node_attr1"] = f"{u}-1"
618
+
619
+ for a, b in G.edges:
620
+ G[a][b]["edge_attr1"] = f"{a}-{b}-1"
621
+
622
+ depth3 = nx.weisfeiler_lehman_subgraph_hashes(
623
+ G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=3
624
+ )
625
+ depth4 = nx.weisfeiler_lehman_subgraph_hashes(
626
+ G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=4
627
+ )
628
+ depth5 = nx.weisfeiler_lehman_subgraph_hashes(
629
+ G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=5
630
+ )
631
+
632
+ assert all(len(hashes) == 3 for hashes in depth3.values())
633
+ assert all(len(hashes) == 4 for hashes in depth4.values())
634
+ assert all(len(hashes) == 5 for hashes in depth5.values())
635
+
636
+ assert is_subiteration(depth3, depth4)
637
+ assert is_subiteration(depth4, depth5)
638
+ assert is_subiteration(depth3, depth5)
639
+
640
+
641
+ def test_digest_size_subgraph_hash():
642
+ """
643
+ The hash string lengths should be as expected for a variety of graphs and
644
+ digest sizes
645
+ """
646
+ n, r = 100, 10
647
+ p = 1.0 / r
648
+ for i in range(1, r + 1):
649
+ G = nx.erdos_renyi_graph(n, p * i, seed=1000 + i)
650
+
651
+ digest_size16_hashes = nx.weisfeiler_lehman_subgraph_hashes(G)
652
+ digest_size32_hashes = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=32)
653
+
654
+ assert digest_size16_hashes != digest_size32_hashes
655
+
656
+ assert hexdigest_sizes_correct(digest_size16_hashes, 16)
657
+ assert hexdigest_sizes_correct(digest_size32_hashes, 32)
658
+
659
+
660
+ def test_initial_node_labels_subgraph_hash():
661
+ """
662
+ Including the hashed initial label prepends an extra hash to the lists
663
+ """
664
+ G = nx.path_graph(5)
665
+ nx.set_node_attributes(G, {i: int(0 < i < 4) for i in G}, "label")
666
+ # initial node labels:
667
+ # 0--1--1--1--0
668
+
669
+ without_initial_label = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="label")
670
+ assert all(len(v) == 3 for v in without_initial_label.values())
671
+ # 3 different 1 hop nhds
672
+ assert len({v[0] for v in without_initial_label.values()}) == 3
673
+
674
+ with_initial_label = nx.weisfeiler_lehman_subgraph_hashes(
675
+ G, node_attr="label", include_initial_labels=True
676
+ )
677
+ assert all(len(v) == 4 for v in with_initial_label.values())
678
+ # 2 different initial labels
679
+ assert len({v[0] for v in with_initial_label.values()}) == 2
680
+
681
+ # check hashes match otherwise
682
+ for u in G:
683
+ for a, b in zip(
684
+ with_initial_label[u][1:], without_initial_label[u], strict=True
685
+ ):
686
+ assert a == b
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_graphical.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_valid_degree_sequence1():
7
+ n = 100
8
+ p = 0.3
9
+ for i in range(10):
10
+ G = nx.erdos_renyi_graph(n, p)
11
+ deg = (d for n, d in G.degree())
12
+ assert nx.is_graphical(deg, method="eg")
13
+ assert nx.is_graphical(deg, method="hh")
14
+
15
+
16
+ def test_valid_degree_sequence2():
17
+ n = 100
18
+ for i in range(10):
19
+ G = nx.barabasi_albert_graph(n, 1)
20
+ deg = (d for n, d in G.degree())
21
+ assert nx.is_graphical(deg, method="eg")
22
+ assert nx.is_graphical(deg, method="hh")
23
+
24
+
25
+ def test_string_input():
26
+ pytest.raises(nx.NetworkXException, nx.is_graphical, [], "foo")
27
+ pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "hh")
28
+ pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "eg")
29
+
30
+
31
+ def test_non_integer_input():
32
+ pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "eg")
33
+ pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "hh")
34
+
35
+
36
+ def test_negative_input():
37
+ assert not nx.is_graphical([-1], "hh")
38
+ assert not nx.is_graphical([-1], "eg")
39
+
40
+
41
+ class TestAtlas:
42
+ @classmethod
43
+ def setup_class(cls):
44
+ global atlas
45
+ from networkx.generators import atlas
46
+
47
+ cls.GAG = atlas.graph_atlas_g()
48
+
49
+ def test_atlas(self):
50
+ for graph in self.GAG:
51
+ deg = (d for n, d in graph.degree())
52
+ assert nx.is_graphical(deg, method="eg")
53
+ assert nx.is_graphical(deg, method="hh")
54
+
55
+
56
+ def test_small_graph_true():
57
+ z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
58
+ assert nx.is_graphical(z, method="hh")
59
+ assert nx.is_graphical(z, method="eg")
60
+ z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
61
+ assert nx.is_graphical(z, method="hh")
62
+ assert nx.is_graphical(z, method="eg")
63
+ z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
64
+ assert nx.is_graphical(z, method="hh")
65
+ assert nx.is_graphical(z, method="eg")
66
+
67
+
68
+ def test_small_graph_false():
69
+ z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
70
+ assert not nx.is_graphical(z, method="hh")
71
+ assert not nx.is_graphical(z, method="eg")
72
+ z = [6, 5, 4, 4, 2, 1, 1, 1]
73
+ assert not nx.is_graphical(z, method="hh")
74
+ assert not nx.is_graphical(z, method="eg")
75
+ z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
76
+ assert not nx.is_graphical(z, method="hh")
77
+ assert not nx.is_graphical(z, method="eg")
78
+
79
+
80
+ def test_directed_degree_sequence():
81
+ # Test a range of valid directed degree sequences
82
+ n, r = 100, 10
83
+ p = 1.0 / r
84
+ for i in range(r):
85
+ G = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
86
+ din = (d for n, d in G.in_degree())
87
+ dout = (d for n, d in G.out_degree())
88
+ assert nx.is_digraphical(din, dout)
89
+
90
+
91
+ def test_small_directed_sequences():
92
+ dout = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
93
+ din = [3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1]
94
+ assert nx.is_digraphical(din, dout)
95
+ # Test nongraphical directed sequence
96
+ dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
97
+ din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
98
+ assert not nx.is_digraphical(din, dout)
99
+ # Test digraphical small sequence
100
+ dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
101
+ din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1]
102
+ assert nx.is_digraphical(din, dout)
103
+ # Test nonmatching sum
104
+ din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
105
+ assert not nx.is_digraphical(din, dout)
106
+ # Test for negative integer in sequence
107
+ din = [2, 2, 2, -2, 2, 2, 2, 2, 1, 1, 4]
108
+ assert not nx.is_digraphical(din, dout)
109
+ # Test for noninteger
110
+ din = dout = [1, 1, 1.1, 1]
111
+ assert not nx.is_digraphical(din, dout)
112
+ din = dout = [1, 1, "rer", 1]
113
+ assert not nx.is_digraphical(din, dout)
114
+
115
+
116
+ def test_multi_sequence():
117
+ # Test nongraphical multi sequence
118
+ seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
119
+ assert not nx.is_multigraphical(seq)
120
+ # Test small graphical multi sequence
121
+ seq = [6, 5, 4, 4, 2, 1, 1, 1]
122
+ assert nx.is_multigraphical(seq)
123
+ # Test for negative integer in sequence
124
+ seq = [6, 5, 4, -4, 2, 1, 1, 1]
125
+ assert not nx.is_multigraphical(seq)
126
+ # Test for sequence with odd sum
127
+ seq = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
128
+ assert not nx.is_multigraphical(seq)
129
+ # Test for noninteger
130
+ seq = [1, 1, 1.1, 1]
131
+ assert not nx.is_multigraphical(seq)
132
+ seq = [1, 1, "rer", 1]
133
+ assert not nx.is_multigraphical(seq)
134
+
135
+
136
+ def test_pseudo_sequence():
137
+ # Test small valid pseudo sequence
138
+ seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
139
+ assert nx.is_pseudographical(seq)
140
+ # Test for sequence with odd sum
141
+ seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
142
+ assert not nx.is_pseudographical(seq)
143
+ # Test for negative integer in sequence
144
+ seq = [1000, 3, 3, 3, 3, 2, 2, -2, 1, 1]
145
+ assert not nx.is_pseudographical(seq)
146
+ # Test for noninteger
147
+ seq = [1, 1, 1.1, 1]
148
+ assert not nx.is_pseudographical(seq)
149
+ seq = [1, 1, "rer", 1]
150
+ assert not nx.is_pseudographical(seq)
151
+
152
+
153
+ def test_numpy_degree_sequence():
154
+ np = pytest.importorskip("numpy")
155
+ ds = np.array([1, 2, 2, 2, 1], dtype=np.int64)
156
+ assert nx.is_graphical(ds, "eg")
157
+ assert nx.is_graphical(ds, "hh")
158
+ ds = np.array([1, 2, 2, 2, 1], dtype=np.float64)
159
+ assert nx.is_graphical(ds, "eg")
160
+ assert nx.is_graphical(ds, "hh")
161
+ ds = np.array([1.1, 2, 2, 2, 1], dtype=np.float64)
162
+ pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "eg")
163
+ pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "hh")
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_lowest_common_ancestors.py ADDED
@@ -0,0 +1,427 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import chain, combinations, product
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+ tree_all_pairs_lca = nx.tree_all_pairs_lowest_common_ancestor
8
+ all_pairs_lca = nx.all_pairs_lowest_common_ancestor
9
+
10
+
11
+ def get_pair(dictionary, n1, n2):
12
+ if (n1, n2) in dictionary:
13
+ return dictionary[n1, n2]
14
+ else:
15
+ return dictionary[n2, n1]
16
+
17
+
18
+ class TestTreeLCA:
19
+ @classmethod
20
+ def setup_class(cls):
21
+ cls.DG = nx.DiGraph()
22
+ edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]
23
+ cls.DG.add_edges_from(edges)
24
+ cls.ans = dict(tree_all_pairs_lca(cls.DG, 0))
25
+ gold = {(n, n): n for n in cls.DG}
26
+ gold.update({(0, i): 0 for i in range(1, 7)})
27
+ gold.update(
28
+ {
29
+ (1, 2): 0,
30
+ (1, 3): 1,
31
+ (1, 4): 1,
32
+ (1, 5): 0,
33
+ (1, 6): 0,
34
+ (2, 3): 0,
35
+ (2, 4): 0,
36
+ (2, 5): 2,
37
+ (2, 6): 2,
38
+ (3, 4): 1,
39
+ (3, 5): 0,
40
+ (3, 6): 0,
41
+ (4, 5): 0,
42
+ (4, 6): 0,
43
+ (5, 6): 2,
44
+ }
45
+ )
46
+
47
+ cls.gold = gold
48
+
49
+ @staticmethod
50
+ def assert_has_same_pairs(d1, d2):
51
+ for a, b in ((min(pair), max(pair)) for pair in chain(d1, d2)):
52
+ assert get_pair(d1, a, b) == get_pair(d2, a, b)
53
+
54
+ def test_tree_all_pairs_lca_default_root(self):
55
+ assert dict(tree_all_pairs_lca(self.DG)) == self.ans
56
+
57
+ def test_tree_all_pairs_lca_return_subset(self):
58
+ test_pairs = [(0, 1), (0, 1), (1, 0)]
59
+ ans = dict(tree_all_pairs_lca(self.DG, 0, test_pairs))
60
+ assert (0, 1) in ans and (1, 0) in ans
61
+ assert len(ans) == 2
62
+
63
+ def test_tree_all_pairs_lca(self):
64
+ all_pairs = chain(combinations(self.DG, 2), ((node, node) for node in self.DG))
65
+
66
+ ans = dict(tree_all_pairs_lca(self.DG, 0, all_pairs))
67
+ self.assert_has_same_pairs(ans, self.ans)
68
+
69
+ def test_tree_all_pairs_gold_example(self):
70
+ ans = dict(tree_all_pairs_lca(self.DG))
71
+ self.assert_has_same_pairs(self.gold, ans)
72
+
73
+ def test_tree_all_pairs_lca_invalid_input(self):
74
+ empty_digraph = tree_all_pairs_lca(nx.DiGraph())
75
+ pytest.raises(nx.NetworkXPointlessConcept, list, empty_digraph)
76
+
77
+ bad_pairs_digraph = tree_all_pairs_lca(self.DG, pairs=[(-1, -2)])
78
+ pytest.raises(nx.NodeNotFound, list, bad_pairs_digraph)
79
+
80
+ def test_tree_all_pairs_lca_subtrees(self):
81
+ ans = dict(tree_all_pairs_lca(self.DG, 1))
82
+ gold = {
83
+ pair: lca
84
+ for (pair, lca) in self.gold.items()
85
+ if all(n in (1, 3, 4) for n in pair)
86
+ }
87
+ self.assert_has_same_pairs(gold, ans)
88
+
89
+ def test_tree_all_pairs_lca_disconnected_nodes(self):
90
+ G = nx.DiGraph()
91
+ G.add_node(1)
92
+ assert {(1, 1): 1} == dict(tree_all_pairs_lca(G))
93
+
94
+ G.add_node(0)
95
+ assert {(1, 1): 1} == dict(tree_all_pairs_lca(G, 1))
96
+ assert {(0, 0): 0} == dict(tree_all_pairs_lca(G, 0))
97
+
98
+ pytest.raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
99
+
100
+ def test_tree_all_pairs_lca_error_if_input_not_tree(self):
101
+ # Cycle
102
+ G = nx.DiGraph([(1, 2), (2, 1)])
103
+ pytest.raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
104
+ # DAG
105
+ G = nx.DiGraph([(0, 2), (1, 2)])
106
+ pytest.raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
107
+
108
+ def test_tree_all_pairs_lca_generator(self):
109
+ pairs = iter([(0, 1), (0, 1), (1, 0)])
110
+ some_pairs = dict(tree_all_pairs_lca(self.DG, 0, pairs))
111
+ assert (0, 1) in some_pairs and (1, 0) in some_pairs
112
+ assert len(some_pairs) == 2
113
+
114
+ def test_tree_all_pairs_lca_nonexisting_pairs_exception(self):
115
+ lca = tree_all_pairs_lca(self.DG, 0, [(-1, -1)])
116
+ pytest.raises(nx.NodeNotFound, list, lca)
117
+ # check if node is None
118
+ lca = tree_all_pairs_lca(self.DG, None, [(-1, -1)])
119
+ pytest.raises(nx.NodeNotFound, list, lca)
120
+
121
+ def test_tree_all_pairs_lca_routine_bails_on_DAGs(self):
122
+ G = nx.DiGraph([(3, 4), (5, 4)])
123
+ pytest.raises(nx.NetworkXError, list, tree_all_pairs_lca(G))
124
+
125
+ def test_tree_all_pairs_lca_not_implemented(self):
126
+ NNI = nx.NetworkXNotImplemented
127
+ G = nx.Graph([(0, 1)])
128
+ with pytest.raises(NNI):
129
+ next(tree_all_pairs_lca(G))
130
+ with pytest.raises(NNI):
131
+ next(all_pairs_lca(G))
132
+ pytest.raises(NNI, nx.lowest_common_ancestor, G, 0, 1)
133
+ G = nx.MultiGraph([(0, 1)])
134
+ with pytest.raises(NNI):
135
+ next(tree_all_pairs_lca(G))
136
+ with pytest.raises(NNI):
137
+ next(all_pairs_lca(G))
138
+ pytest.raises(NNI, nx.lowest_common_ancestor, G, 0, 1)
139
+
140
+ def test_tree_all_pairs_lca_trees_without_LCAs(self):
141
+ G = nx.DiGraph()
142
+ G.add_node(3)
143
+ ans = list(tree_all_pairs_lca(G))
144
+ assert ans == [((3, 3), 3)]
145
+
146
+
147
+ class TestMultiTreeLCA(TestTreeLCA):
148
+ @classmethod
149
+ def setup_class(cls):
150
+ cls.DG = nx.MultiDiGraph()
151
+ edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]
152
+ cls.DG.add_edges_from(edges)
153
+ cls.ans = dict(tree_all_pairs_lca(cls.DG, 0))
154
+ # add multiedges
155
+ cls.DG.add_edges_from(edges)
156
+
157
+ gold = {(n, n): n for n in cls.DG}
158
+ gold.update({(0, i): 0 for i in range(1, 7)})
159
+ gold.update(
160
+ {
161
+ (1, 2): 0,
162
+ (1, 3): 1,
163
+ (1, 4): 1,
164
+ (1, 5): 0,
165
+ (1, 6): 0,
166
+ (2, 3): 0,
167
+ (2, 4): 0,
168
+ (2, 5): 2,
169
+ (2, 6): 2,
170
+ (3, 4): 1,
171
+ (3, 5): 0,
172
+ (3, 6): 0,
173
+ (4, 5): 0,
174
+ (4, 6): 0,
175
+ (5, 6): 2,
176
+ }
177
+ )
178
+
179
+ cls.gold = gold
180
+
181
+
182
+ class TestDAGLCA:
183
+ @classmethod
184
+ def setup_class(cls):
185
+ cls.DG = nx.DiGraph()
186
+ nx.add_path(cls.DG, (0, 1, 2, 3))
187
+ nx.add_path(cls.DG, (0, 4, 3))
188
+ nx.add_path(cls.DG, (0, 5, 6, 8, 3))
189
+ nx.add_path(cls.DG, (5, 7, 8))
190
+ cls.DG.add_edge(6, 2)
191
+ cls.DG.add_edge(7, 2)
192
+
193
+ cls.root_distance = nx.shortest_path_length(cls.DG, source=0)
194
+
195
+ cls.gold = {
196
+ (1, 1): 1,
197
+ (1, 2): 1,
198
+ (1, 3): 1,
199
+ (1, 4): 0,
200
+ (1, 5): 0,
201
+ (1, 6): 0,
202
+ (1, 7): 0,
203
+ (1, 8): 0,
204
+ (2, 2): 2,
205
+ (2, 3): 2,
206
+ (2, 4): 0,
207
+ (2, 5): 5,
208
+ (2, 6): 6,
209
+ (2, 7): 7,
210
+ (2, 8): 7,
211
+ (3, 3): 3,
212
+ (3, 4): 4,
213
+ (3, 5): 5,
214
+ (3, 6): 6,
215
+ (3, 7): 7,
216
+ (3, 8): 8,
217
+ (4, 4): 4,
218
+ (4, 5): 0,
219
+ (4, 6): 0,
220
+ (4, 7): 0,
221
+ (4, 8): 0,
222
+ (5, 5): 5,
223
+ (5, 6): 5,
224
+ (5, 7): 5,
225
+ (5, 8): 5,
226
+ (6, 6): 6,
227
+ (6, 7): 5,
228
+ (6, 8): 6,
229
+ (7, 7): 7,
230
+ (7, 8): 7,
231
+ (8, 8): 8,
232
+ }
233
+ cls.gold.update(((0, n), 0) for n in cls.DG)
234
+
235
+ def assert_lca_dicts_same(self, d1, d2, G=None):
236
+ """Checks if d1 and d2 contain the same pairs and
237
+ have a node at the same distance from root for each.
238
+ If G is None use self.DG."""
239
+ if G is None:
240
+ G = self.DG
241
+ root_distance = self.root_distance
242
+ else:
243
+ roots = [n for n, deg in G.in_degree if deg == 0]
244
+ assert len(roots) == 1
245
+ root_distance = nx.shortest_path_length(G, source=roots[0])
246
+
247
+ for a, b in ((min(pair), max(pair)) for pair in chain(d1, d2)):
248
+ assert (
249
+ root_distance[get_pair(d1, a, b)] == root_distance[get_pair(d2, a, b)]
250
+ )
251
+
252
+ def test_all_pairs_lca_gold_example(self):
253
+ self.assert_lca_dicts_same(dict(all_pairs_lca(self.DG)), self.gold)
254
+
255
+ def test_all_pairs_lca_all_pairs_given(self):
256
+ all_pairs = list(product(self.DG.nodes(), self.DG.nodes()))
257
+ ans = all_pairs_lca(self.DG, pairs=all_pairs)
258
+ self.assert_lca_dicts_same(dict(ans), self.gold)
259
+
260
+ def test_all_pairs_lca_generator(self):
261
+ all_pairs = product(self.DG.nodes(), self.DG.nodes())
262
+ ans = all_pairs_lca(self.DG, pairs=all_pairs)
263
+ self.assert_lca_dicts_same(dict(ans), self.gold)
264
+
265
+ def test_all_pairs_lca_input_graph_with_two_roots(self):
266
+ G = self.DG.copy()
267
+ G.add_edge(9, 10)
268
+ G.add_edge(9, 4)
269
+ gold = self.gold.copy()
270
+ gold[9, 9] = 9
271
+ gold[9, 10] = 9
272
+ gold[9, 4] = 9
273
+ gold[9, 3] = 9
274
+ gold[10, 4] = 9
275
+ gold[10, 3] = 9
276
+ gold[10, 10] = 10
277
+
278
+ testing = dict(all_pairs_lca(G))
279
+
280
+ G.add_edge(-1, 9)
281
+ G.add_edge(-1, 0)
282
+ self.assert_lca_dicts_same(testing, gold, G)
283
+
284
+ def test_all_pairs_lca_nonexisting_pairs_exception(self):
285
+ pytest.raises(nx.NodeNotFound, all_pairs_lca, self.DG, [(-1, -1)])
286
+
287
+ def test_all_pairs_lca_pairs_without_lca(self):
288
+ G = self.DG.copy()
289
+ G.add_node(-1)
290
+ gen = all_pairs_lca(G, [(-1, -1), (-1, 0)])
291
+ assert dict(gen) == {(-1, -1): -1}
292
+
293
+ def test_all_pairs_lca_null_graph(self):
294
+ pytest.raises(nx.NetworkXPointlessConcept, all_pairs_lca, nx.DiGraph())
295
+
296
+ def test_all_pairs_lca_non_dags(self):
297
+ pytest.raises(nx.NetworkXError, all_pairs_lca, nx.DiGraph([(3, 4), (4, 3)]))
298
+
299
+ def test_all_pairs_lca_nonempty_graph_without_lca(self):
300
+ G = nx.DiGraph()
301
+ G.add_node(3)
302
+ ans = list(all_pairs_lca(G))
303
+ assert ans == [((3, 3), 3)]
304
+
305
+ def test_all_pairs_lca_bug_gh4942(self):
306
+ G = nx.DiGraph([(0, 2), (1, 2), (2, 3)])
307
+ ans = list(all_pairs_lca(G))
308
+ assert len(ans) == 9
309
+
310
+ def test_all_pairs_lca_default_kwarg(self):
311
+ G = nx.DiGraph([(0, 1), (2, 1)])
312
+ sentinel = object()
313
+ assert nx.lowest_common_ancestor(G, 0, 2, default=sentinel) is sentinel
314
+
315
+ def test_all_pairs_lca_identity(self):
316
+ G = nx.DiGraph()
317
+ G.add_node(3)
318
+ assert nx.lowest_common_ancestor(G, 3, 3) == 3
319
+
320
+ def test_all_pairs_lca_issue_4574(self):
321
+ G = nx.DiGraph()
322
+ G.add_nodes_from(range(17))
323
+ G.add_edges_from(
324
+ [
325
+ (2, 0),
326
+ (1, 2),
327
+ (3, 2),
328
+ (5, 2),
329
+ (8, 2),
330
+ (11, 2),
331
+ (4, 5),
332
+ (6, 5),
333
+ (7, 8),
334
+ (10, 8),
335
+ (13, 11),
336
+ (14, 11),
337
+ (15, 11),
338
+ (9, 10),
339
+ (12, 13),
340
+ (16, 15),
341
+ ]
342
+ )
343
+
344
+ assert nx.lowest_common_ancestor(G, 7, 9) == None
345
+
346
+ def test_all_pairs_lca_one_pair_gh4942(self):
347
+ G = nx.DiGraph()
348
+ # Note: order edge addition is critical to the test
349
+ G.add_edge(0, 1)
350
+ G.add_edge(2, 0)
351
+ G.add_edge(2, 3)
352
+ G.add_edge(4, 0)
353
+ G.add_edge(5, 2)
354
+
355
+ assert nx.lowest_common_ancestor(G, 1, 3) == 2
356
+
357
+
358
+ class TestMultiDiGraph_DAGLCA(TestDAGLCA):
359
+ @classmethod
360
+ def setup_class(cls):
361
+ cls.DG = nx.MultiDiGraph()
362
+ nx.add_path(cls.DG, (0, 1, 2, 3))
363
+ # add multiedges
364
+ nx.add_path(cls.DG, (0, 1, 2, 3))
365
+ nx.add_path(cls.DG, (0, 4, 3))
366
+ nx.add_path(cls.DG, (0, 5, 6, 8, 3))
367
+ nx.add_path(cls.DG, (5, 7, 8))
368
+ cls.DG.add_edge(6, 2)
369
+ cls.DG.add_edge(7, 2)
370
+
371
+ cls.root_distance = nx.shortest_path_length(cls.DG, source=0)
372
+
373
+ cls.gold = {
374
+ (1, 1): 1,
375
+ (1, 2): 1,
376
+ (1, 3): 1,
377
+ (1, 4): 0,
378
+ (1, 5): 0,
379
+ (1, 6): 0,
380
+ (1, 7): 0,
381
+ (1, 8): 0,
382
+ (2, 2): 2,
383
+ (2, 3): 2,
384
+ (2, 4): 0,
385
+ (2, 5): 5,
386
+ (2, 6): 6,
387
+ (2, 7): 7,
388
+ (2, 8): 7,
389
+ (3, 3): 3,
390
+ (3, 4): 4,
391
+ (3, 5): 5,
392
+ (3, 6): 6,
393
+ (3, 7): 7,
394
+ (3, 8): 8,
395
+ (4, 4): 4,
396
+ (4, 5): 0,
397
+ (4, 6): 0,
398
+ (4, 7): 0,
399
+ (4, 8): 0,
400
+ (5, 5): 5,
401
+ (5, 6): 5,
402
+ (5, 7): 5,
403
+ (5, 8): 5,
404
+ (6, 6): 6,
405
+ (6, 7): 5,
406
+ (6, 8): 6,
407
+ (7, 7): 7,
408
+ (7, 8): 7,
409
+ (8, 8): 8,
410
+ }
411
+ cls.gold.update(((0, n), 0) for n in cls.DG)
412
+
413
+
414
+ def test_all_pairs_lca_self_ancestors():
415
+ """Self-ancestors should always be the node itself, i.e. lca of (0, 0) is 0.
416
+ See gh-4458."""
417
+ # DAG for test - note order of node/edge addition is relevant
418
+ G = nx.DiGraph()
419
+ G.add_nodes_from(range(5))
420
+ G.add_edges_from([(1, 0), (2, 0), (3, 2), (4, 1), (4, 3)])
421
+
422
+ ap_lca = nx.all_pairs_lowest_common_ancestor
423
+ assert all(u == v == a for (u, v), a in ap_lca(G) if u == v)
424
+ MG = nx.MultiDiGraph(G)
425
+ assert all(u == v == a for (u, v), a in ap_lca(MG) if u == v)
426
+ MG.add_edges_from([(1, 0), (2, 0)])
427
+ assert all(u == v == a for (u, v), a in ap_lca(MG) if u == v)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_matching.py ADDED
@@ -0,0 +1,605 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from itertools import permutations
3
+
4
+ from pytest import raises
5
+
6
+ import networkx as nx
7
+ from networkx.algorithms.matching import matching_dict_to_set
8
+ from networkx.utils import edges_equal
9
+
10
+
11
+ class TestMaxWeightMatching:
12
+ """Unit tests for the
13
+ :func:`~networkx.algorithms.matching.max_weight_matching` function.
14
+
15
+ """
16
+
17
+ def test_trivial1(self):
18
+ """Empty graph"""
19
+ G = nx.Graph()
20
+ assert nx.max_weight_matching(G) == set()
21
+ assert nx.min_weight_matching(G) == set()
22
+
23
+ def test_selfloop(self):
24
+ G = nx.Graph()
25
+ G.add_edge(0, 0, weight=100)
26
+ assert nx.max_weight_matching(G) == set()
27
+ assert nx.min_weight_matching(G) == set()
28
+
29
+ def test_single_edge(self):
30
+ G = nx.Graph()
31
+ G.add_edge(0, 1)
32
+ assert edges_equal(
33
+ nx.max_weight_matching(G), matching_dict_to_set({0: 1, 1: 0})
34
+ )
35
+ assert edges_equal(
36
+ nx.min_weight_matching(G), matching_dict_to_set({0: 1, 1: 0})
37
+ )
38
+
39
+ def test_two_path(self):
40
+ G = nx.Graph()
41
+ G.add_edge("one", "two", weight=10)
42
+ G.add_edge("two", "three", weight=11)
43
+ assert edges_equal(
44
+ nx.max_weight_matching(G),
45
+ matching_dict_to_set({"three": "two", "two": "three"}),
46
+ )
47
+ assert edges_equal(
48
+ nx.min_weight_matching(G),
49
+ matching_dict_to_set({"one": "two", "two": "one"}),
50
+ )
51
+
52
+ def test_path(self):
53
+ G = nx.Graph()
54
+ G.add_edge(1, 2, weight=5)
55
+ G.add_edge(2, 3, weight=11)
56
+ G.add_edge(3, 4, weight=5)
57
+ assert edges_equal(
58
+ nx.max_weight_matching(G), matching_dict_to_set({2: 3, 3: 2})
59
+ )
60
+ assert edges_equal(
61
+ nx.max_weight_matching(G, 1), matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3})
62
+ )
63
+ assert edges_equal(
64
+ nx.min_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
65
+ )
66
+ assert edges_equal(
67
+ nx.min_weight_matching(G, 1), matching_dict_to_set({1: 2, 3: 4})
68
+ )
69
+
70
+ def test_square(self):
71
+ G = nx.Graph()
72
+ G.add_edge(1, 4, weight=2)
73
+ G.add_edge(2, 3, weight=2)
74
+ G.add_edge(1, 2, weight=1)
75
+ G.add_edge(3, 4, weight=4)
76
+ assert edges_equal(
77
+ nx.max_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
78
+ )
79
+ assert edges_equal(
80
+ nx.min_weight_matching(G), matching_dict_to_set({1: 4, 2: 3})
81
+ )
82
+
83
+ def test_edge_attribute_name(self):
84
+ G = nx.Graph()
85
+ G.add_edge("one", "two", weight=10, abcd=11)
86
+ G.add_edge("two", "three", weight=11, abcd=10)
87
+ assert edges_equal(
88
+ nx.max_weight_matching(G, weight="abcd"),
89
+ matching_dict_to_set({"one": "two", "two": "one"}),
90
+ )
91
+ assert edges_equal(
92
+ nx.min_weight_matching(G, weight="abcd"),
93
+ matching_dict_to_set({"three": "two"}),
94
+ )
95
+
96
+ def test_floating_point_weights(self):
97
+ G = nx.Graph()
98
+ G.add_edge(1, 2, weight=math.pi)
99
+ G.add_edge(2, 3, weight=math.exp(1))
100
+ G.add_edge(1, 3, weight=3.0)
101
+ G.add_edge(1, 4, weight=math.sqrt(2.0))
102
+ assert edges_equal(
103
+ nx.max_weight_matching(G), matching_dict_to_set({1: 4, 2: 3, 3: 2, 4: 1})
104
+ )
105
+ assert edges_equal(
106
+ nx.min_weight_matching(G), matching_dict_to_set({1: 4, 2: 3, 3: 2, 4: 1})
107
+ )
108
+
109
+ def test_negative_weights(self):
110
+ G = nx.Graph()
111
+ G.add_edge(1, 2, weight=2)
112
+ G.add_edge(1, 3, weight=-2)
113
+ G.add_edge(2, 3, weight=1)
114
+ G.add_edge(2, 4, weight=-1)
115
+ G.add_edge(3, 4, weight=-6)
116
+ assert edges_equal(
117
+ nx.max_weight_matching(G), matching_dict_to_set({1: 2, 2: 1})
118
+ )
119
+ assert edges_equal(
120
+ nx.max_weight_matching(G, maxcardinality=True),
121
+ matching_dict_to_set({1: 3, 2: 4, 3: 1, 4: 2}),
122
+ )
123
+ assert edges_equal(
124
+ nx.min_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
125
+ )
126
+
127
+ def test_s_blossom(self):
128
+ """Create S-blossom and use it for augmentation:"""
129
+ G = nx.Graph()
130
+ G.add_weighted_edges_from([(1, 2, 8), (1, 3, 9), (2, 3, 10), (3, 4, 7)])
131
+ answer = matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3})
132
+ assert edges_equal(nx.max_weight_matching(G), answer)
133
+ assert edges_equal(nx.min_weight_matching(G), answer)
134
+
135
+ G.add_weighted_edges_from([(1, 6, 5), (4, 5, 6)])
136
+ answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
137
+ assert edges_equal(nx.max_weight_matching(G), answer)
138
+ assert edges_equal(nx.min_weight_matching(G), answer)
139
+
140
+ def test_s_t_blossom(self):
141
+ """Create S-blossom, relabel as T-blossom, use for augmentation:"""
142
+ G = nx.Graph()
143
+ G.add_weighted_edges_from(
144
+ [(1, 2, 9), (1, 3, 8), (2, 3, 10), (1, 4, 5), (4, 5, 4), (1, 6, 3)]
145
+ )
146
+ answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
147
+ assert edges_equal(nx.max_weight_matching(G), answer)
148
+ assert edges_equal(nx.min_weight_matching(G), answer)
149
+
150
+ G.add_edge(4, 5, weight=3)
151
+ G.add_edge(1, 6, weight=4)
152
+ assert edges_equal(nx.max_weight_matching(G), answer)
153
+ assert edges_equal(nx.min_weight_matching(G), answer)
154
+
155
+ G.remove_edge(1, 6)
156
+ G.add_edge(3, 6, weight=4)
157
+ answer = matching_dict_to_set({1: 2, 2: 1, 3: 6, 4: 5, 5: 4, 6: 3})
158
+ assert edges_equal(nx.max_weight_matching(G), answer)
159
+ assert edges_equal(nx.min_weight_matching(G), answer)
160
+
161
+ def test_nested_s_blossom(self):
162
+ """Create nested S-blossom, use for augmentation:"""
163
+
164
+ G = nx.Graph()
165
+ G.add_weighted_edges_from(
166
+ [
167
+ (1, 2, 9),
168
+ (1, 3, 9),
169
+ (2, 3, 10),
170
+ (2, 4, 8),
171
+ (3, 5, 8),
172
+ (4, 5, 10),
173
+ (5, 6, 6),
174
+ ]
175
+ )
176
+ dict_format = {1: 3, 2: 4, 3: 1, 4: 2, 5: 6, 6: 5}
177
+ expected = {frozenset(e) for e in matching_dict_to_set(dict_format)}
178
+ answer = {frozenset(e) for e in nx.max_weight_matching(G)}
179
+ assert answer == expected
180
+ answer = {frozenset(e) for e in nx.min_weight_matching(G)}
181
+ assert answer == expected
182
+
183
+ def test_nested_s_blossom_relabel(self):
184
+ """Create S-blossom, relabel as S, include in nested S-blossom:"""
185
+ G = nx.Graph()
186
+ G.add_weighted_edges_from(
187
+ [
188
+ (1, 2, 10),
189
+ (1, 7, 10),
190
+ (2, 3, 12),
191
+ (3, 4, 20),
192
+ (3, 5, 20),
193
+ (4, 5, 25),
194
+ (5, 6, 10),
195
+ (6, 7, 10),
196
+ (7, 8, 8),
197
+ ]
198
+ )
199
+ answer = matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3, 5: 6, 6: 5, 7: 8, 8: 7})
200
+ assert edges_equal(nx.max_weight_matching(G), answer)
201
+ assert edges_equal(nx.min_weight_matching(G), answer)
202
+
203
+ def test_nested_s_blossom_expand(self):
204
+ """Create nested S-blossom, augment, expand recursively:"""
205
+ G = nx.Graph()
206
+ G.add_weighted_edges_from(
207
+ [
208
+ (1, 2, 8),
209
+ (1, 3, 8),
210
+ (2, 3, 10),
211
+ (2, 4, 12),
212
+ (3, 5, 12),
213
+ (4, 5, 14),
214
+ (4, 6, 12),
215
+ (5, 7, 12),
216
+ (6, 7, 14),
217
+ (7, 8, 12),
218
+ ]
219
+ )
220
+ answer = matching_dict_to_set({1: 2, 2: 1, 3: 5, 4: 6, 5: 3, 6: 4, 7: 8, 8: 7})
221
+ assert edges_equal(nx.max_weight_matching(G), answer)
222
+ assert edges_equal(nx.min_weight_matching(G), answer)
223
+
224
+ def test_s_blossom_relabel_expand(self):
225
+ """Create S-blossom, relabel as T, expand:"""
226
+ G = nx.Graph()
227
+ G.add_weighted_edges_from(
228
+ [
229
+ (1, 2, 23),
230
+ (1, 5, 22),
231
+ (1, 6, 15),
232
+ (2, 3, 25),
233
+ (3, 4, 22),
234
+ (4, 5, 25),
235
+ (4, 8, 14),
236
+ (5, 7, 13),
237
+ ]
238
+ )
239
+ answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4})
240
+ assert edges_equal(nx.max_weight_matching(G), answer)
241
+ assert edges_equal(nx.min_weight_matching(G), answer)
242
+
243
+ def test_nested_s_blossom_relabel_expand(self):
244
+ """Create nested S-blossom, relabel as T, expand:"""
245
+ G = nx.Graph()
246
+ G.add_weighted_edges_from(
247
+ [
248
+ (1, 2, 19),
249
+ (1, 3, 20),
250
+ (1, 8, 8),
251
+ (2, 3, 25),
252
+ (2, 4, 18),
253
+ (3, 5, 18),
254
+ (4, 5, 13),
255
+ (4, 7, 7),
256
+ (5, 6, 7),
257
+ ]
258
+ )
259
+ answer = matching_dict_to_set({1: 8, 2: 3, 3: 2, 4: 7, 5: 6, 6: 5, 7: 4, 8: 1})
260
+ assert edges_equal(nx.max_weight_matching(G), answer)
261
+ assert edges_equal(nx.min_weight_matching(G), answer)
262
+
263
+ def test_nasty_blossom1(self):
264
+ """Create blossom, relabel as T in more than one way, expand,
265
+ augment:
266
+ """
267
+ G = nx.Graph()
268
+ G.add_weighted_edges_from(
269
+ [
270
+ (1, 2, 45),
271
+ (1, 5, 45),
272
+ (2, 3, 50),
273
+ (3, 4, 45),
274
+ (4, 5, 50),
275
+ (1, 6, 30),
276
+ (3, 9, 35),
277
+ (4, 8, 35),
278
+ (5, 7, 26),
279
+ (9, 10, 5),
280
+ ]
281
+ )
282
+ ansdict = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
283
+ answer = matching_dict_to_set(ansdict)
284
+ assert edges_equal(nx.max_weight_matching(G), answer)
285
+ assert edges_equal(nx.min_weight_matching(G), answer)
286
+
287
+ def test_nasty_blossom2(self):
288
+ """Again but slightly different:"""
289
+ G = nx.Graph()
290
+ G.add_weighted_edges_from(
291
+ [
292
+ (1, 2, 45),
293
+ (1, 5, 45),
294
+ (2, 3, 50),
295
+ (3, 4, 45),
296
+ (4, 5, 50),
297
+ (1, 6, 30),
298
+ (3, 9, 35),
299
+ (4, 8, 26),
300
+ (5, 7, 40),
301
+ (9, 10, 5),
302
+ ]
303
+ )
304
+ ans = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
305
+ answer = matching_dict_to_set(ans)
306
+ assert edges_equal(nx.max_weight_matching(G), answer)
307
+ assert edges_equal(nx.min_weight_matching(G), answer)
308
+
309
+ def test_nasty_blossom_least_slack(self):
310
+ """Create blossom, relabel as T, expand such that a new
311
+ least-slack S-to-free dge is produced, augment:
312
+ """
313
+ G = nx.Graph()
314
+ G.add_weighted_edges_from(
315
+ [
316
+ (1, 2, 45),
317
+ (1, 5, 45),
318
+ (2, 3, 50),
319
+ (3, 4, 45),
320
+ (4, 5, 50),
321
+ (1, 6, 30),
322
+ (3, 9, 35),
323
+ (4, 8, 28),
324
+ (5, 7, 26),
325
+ (9, 10, 5),
326
+ ]
327
+ )
328
+ ans = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
329
+ answer = matching_dict_to_set(ans)
330
+ assert edges_equal(nx.max_weight_matching(G), answer)
331
+ assert edges_equal(nx.min_weight_matching(G), answer)
332
+
333
+ def test_nasty_blossom_augmenting(self):
334
+ """Create nested blossom, relabel as T in more than one way"""
335
+ # expand outer blossom such that inner blossom ends up on an
336
+ # augmenting path:
337
+ G = nx.Graph()
338
+ G.add_weighted_edges_from(
339
+ [
340
+ (1, 2, 45),
341
+ (1, 7, 45),
342
+ (2, 3, 50),
343
+ (3, 4, 45),
344
+ (4, 5, 95),
345
+ (4, 6, 94),
346
+ (5, 6, 94),
347
+ (6, 7, 50),
348
+ (1, 8, 30),
349
+ (3, 11, 35),
350
+ (5, 9, 36),
351
+ (7, 10, 26),
352
+ (11, 12, 5),
353
+ ]
354
+ )
355
+ ans = {
356
+ 1: 8,
357
+ 2: 3,
358
+ 3: 2,
359
+ 4: 6,
360
+ 5: 9,
361
+ 6: 4,
362
+ 7: 10,
363
+ 8: 1,
364
+ 9: 5,
365
+ 10: 7,
366
+ 11: 12,
367
+ 12: 11,
368
+ }
369
+ answer = matching_dict_to_set(ans)
370
+ assert edges_equal(nx.max_weight_matching(G), answer)
371
+ assert edges_equal(nx.min_weight_matching(G), answer)
372
+
373
+ def test_nasty_blossom_expand_recursively(self):
374
+ """Create nested S-blossom, relabel as S, expand recursively:"""
375
+ G = nx.Graph()
376
+ G.add_weighted_edges_from(
377
+ [
378
+ (1, 2, 40),
379
+ (1, 3, 40),
380
+ (2, 3, 60),
381
+ (2, 4, 55),
382
+ (3, 5, 55),
383
+ (4, 5, 50),
384
+ (1, 8, 15),
385
+ (5, 7, 30),
386
+ (7, 6, 10),
387
+ (8, 10, 10),
388
+ (4, 9, 30),
389
+ ]
390
+ )
391
+ ans = {1: 2, 2: 1, 3: 5, 4: 9, 5: 3, 6: 7, 7: 6, 8: 10, 9: 4, 10: 8}
392
+ answer = matching_dict_to_set(ans)
393
+ assert edges_equal(nx.max_weight_matching(G), answer)
394
+ assert edges_equal(nx.min_weight_matching(G), answer)
395
+
396
+ def test_wrong_graph_type(self):
397
+ error = nx.NetworkXNotImplemented
398
+ raises(error, nx.max_weight_matching, nx.MultiGraph())
399
+ raises(error, nx.max_weight_matching, nx.MultiDiGraph())
400
+ raises(error, nx.max_weight_matching, nx.DiGraph())
401
+ raises(error, nx.min_weight_matching, nx.DiGraph())
402
+
403
+
404
+ class TestIsMatching:
405
+ """Unit tests for the
406
+ :func:`~networkx.algorithms.matching.is_matching` function.
407
+
408
+ """
409
+
410
+ def test_dict(self):
411
+ G = nx.path_graph(4)
412
+ assert nx.is_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
413
+
414
+ def test_empty_matching(self):
415
+ G = nx.path_graph(4)
416
+ assert nx.is_matching(G, set())
417
+
418
+ def test_single_edge(self):
419
+ G = nx.path_graph(4)
420
+ assert nx.is_matching(G, {(1, 2)})
421
+
422
+ def test_edge_order(self):
423
+ G = nx.path_graph(4)
424
+ assert nx.is_matching(G, {(0, 1), (2, 3)})
425
+ assert nx.is_matching(G, {(1, 0), (2, 3)})
426
+ assert nx.is_matching(G, {(0, 1), (3, 2)})
427
+ assert nx.is_matching(G, {(1, 0), (3, 2)})
428
+
429
+ def test_valid_matching(self):
430
+ G = nx.path_graph(4)
431
+ assert nx.is_matching(G, {(0, 1), (2, 3)})
432
+
433
+ def test_invalid_input(self):
434
+ error = nx.NetworkXError
435
+ G = nx.path_graph(4)
436
+ # edge to node not in G
437
+ raises(error, nx.is_matching, G, {(0, 5), (2, 3)})
438
+ # edge not a 2-tuple
439
+ raises(error, nx.is_matching, G, {(0, 1, 2), (2, 3)})
440
+ raises(error, nx.is_matching, G, {(0,), (2, 3)})
441
+
442
+ def test_selfloops(self):
443
+ error = nx.NetworkXError
444
+ G = nx.path_graph(4)
445
+ # selfloop for node not in G
446
+ raises(error, nx.is_matching, G, {(5, 5), (2, 3)})
447
+ # selfloop edge not in G
448
+ assert not nx.is_matching(G, {(0, 0), (1, 2), (2, 3)})
449
+ # selfloop edge in G
450
+ G.add_edge(0, 0)
451
+ assert not nx.is_matching(G, {(0, 0), (1, 2)})
452
+
453
+ def test_invalid_matching(self):
454
+ G = nx.path_graph(4)
455
+ assert not nx.is_matching(G, {(0, 1), (1, 2), (2, 3)})
456
+
457
+ def test_invalid_edge(self):
458
+ G = nx.path_graph(4)
459
+ assert not nx.is_matching(G, {(0, 3), (1, 2)})
460
+ raises(nx.NetworkXError, nx.is_matching, G, {(0, 55)})
461
+
462
+ G = nx.DiGraph(G.edges)
463
+ assert nx.is_matching(G, {(0, 1)})
464
+ assert not nx.is_matching(G, {(1, 0)})
465
+
466
+
467
+ class TestIsMaximalMatching:
468
+ """Unit tests for the
469
+ :func:`~networkx.algorithms.matching.is_maximal_matching` function.
470
+
471
+ """
472
+
473
+ def test_dict(self):
474
+ G = nx.path_graph(4)
475
+ assert nx.is_maximal_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
476
+
477
+ def test_invalid_input(self):
478
+ error = nx.NetworkXError
479
+ G = nx.path_graph(4)
480
+ # edge to node not in G
481
+ raises(error, nx.is_maximal_matching, G, {(0, 5)})
482
+ raises(error, nx.is_maximal_matching, G, {(5, 0)})
483
+ # edge not a 2-tuple
484
+ raises(error, nx.is_maximal_matching, G, {(0, 1, 2), (2, 3)})
485
+ raises(error, nx.is_maximal_matching, G, {(0,), (2, 3)})
486
+
487
+ def test_valid(self):
488
+ G = nx.path_graph(4)
489
+ assert nx.is_maximal_matching(G, {(0, 1), (2, 3)})
490
+
491
+ def test_not_matching(self):
492
+ G = nx.path_graph(4)
493
+ assert not nx.is_maximal_matching(G, {(0, 1), (1, 2), (2, 3)})
494
+ assert not nx.is_maximal_matching(G, {(0, 3)})
495
+ G.add_edge(0, 0)
496
+ assert not nx.is_maximal_matching(G, {(0, 0)})
497
+
498
+ def test_not_maximal(self):
499
+ G = nx.path_graph(4)
500
+ assert not nx.is_maximal_matching(G, {(0, 1)})
501
+
502
+
503
+ class TestIsPerfectMatching:
504
+ """Unit tests for the
505
+ :func:`~networkx.algorithms.matching.is_perfect_matching` function.
506
+
507
+ """
508
+
509
+ def test_dict(self):
510
+ G = nx.path_graph(4)
511
+ assert nx.is_perfect_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
512
+
513
+ def test_valid(self):
514
+ G = nx.path_graph(4)
515
+ assert nx.is_perfect_matching(G, {(0, 1), (2, 3)})
516
+
517
+ def test_valid_not_path(self):
518
+ G = nx.cycle_graph(4)
519
+ G.add_edge(0, 4)
520
+ G.add_edge(1, 4)
521
+ G.add_edge(5, 2)
522
+
523
+ assert nx.is_perfect_matching(G, {(1, 4), (0, 3), (5, 2)})
524
+
525
+ def test_invalid_input(self):
526
+ error = nx.NetworkXError
527
+ G = nx.path_graph(4)
528
+ # edge to node not in G
529
+ raises(error, nx.is_perfect_matching, G, {(0, 5)})
530
+ raises(error, nx.is_perfect_matching, G, {(5, 0)})
531
+ # edge not a 2-tuple
532
+ raises(error, nx.is_perfect_matching, G, {(0, 1, 2), (2, 3)})
533
+ raises(error, nx.is_perfect_matching, G, {(0,), (2, 3)})
534
+
535
+ def test_selfloops(self):
536
+ error = nx.NetworkXError
537
+ G = nx.path_graph(4)
538
+ # selfloop for node not in G
539
+ raises(error, nx.is_perfect_matching, G, {(5, 5), (2, 3)})
540
+ # selfloop edge not in G
541
+ assert not nx.is_perfect_matching(G, {(0, 0), (1, 2), (2, 3)})
542
+ # selfloop edge in G
543
+ G.add_edge(0, 0)
544
+ assert not nx.is_perfect_matching(G, {(0, 0), (1, 2)})
545
+
546
+ def test_not_matching(self):
547
+ G = nx.path_graph(4)
548
+ assert not nx.is_perfect_matching(G, {(0, 3)})
549
+ assert not nx.is_perfect_matching(G, {(0, 1), (1, 2), (2, 3)})
550
+
551
+ def test_maximal_but_not_perfect(self):
552
+ G = nx.cycle_graph(4)
553
+ G.add_edge(0, 4)
554
+ G.add_edge(1, 4)
555
+
556
+ assert not nx.is_perfect_matching(G, {(1, 4), (0, 3)})
557
+
558
+
559
+ class TestMaximalMatching:
560
+ """Unit tests for the
561
+ :func:`~networkx.algorithms.matching.maximal_matching`.
562
+
563
+ """
564
+
565
+ def test_valid_matching(self):
566
+ edges = [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 6), (5, 6)]
567
+ G = nx.Graph(edges)
568
+ matching = nx.maximal_matching(G)
569
+ assert nx.is_maximal_matching(G, matching)
570
+
571
+ def test_single_edge_matching(self):
572
+ # In the star graph, any maximal matching has just one edge.
573
+ G = nx.star_graph(5)
574
+ matching = nx.maximal_matching(G)
575
+ assert 1 == len(matching)
576
+ assert nx.is_maximal_matching(G, matching)
577
+
578
+ def test_self_loops(self):
579
+ # Create the path graph with two self-loops.
580
+ G = nx.path_graph(3)
581
+ G.add_edges_from([(0, 0), (1, 1)])
582
+ matching = nx.maximal_matching(G)
583
+ assert len(matching) == 1
584
+ # The matching should never include self-loops.
585
+ assert not any(u == v for u, v in matching)
586
+ assert nx.is_maximal_matching(G, matching)
587
+
588
+ def test_ordering(self):
589
+ """Tests that a maximal matching is computed correctly
590
+ regardless of the order in which nodes are added to the graph.
591
+
592
+ """
593
+ for nodes in permutations(range(3)):
594
+ G = nx.Graph()
595
+ G.add_nodes_from(nodes)
596
+ G.add_edges_from([(0, 1), (0, 2)])
597
+ matching = nx.maximal_matching(G)
598
+ assert len(matching) == 1
599
+ assert nx.is_maximal_matching(G, matching)
600
+
601
+ def test_wrong_graph_type(self):
602
+ error = nx.NetworkXNotImplemented
603
+ raises(error, nx.maximal_matching, nx.MultiGraph())
604
+ raises(error, nx.maximal_matching, nx.MultiDiGraph())
605
+ raises(error, nx.maximal_matching, nx.DiGraph())
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_mis.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for maximal (not maximum) independent sets.
3
+
4
+ """
5
+
6
+ import random
7
+
8
+ import pytest
9
+
10
+ import networkx as nx
11
+
12
+
13
+ def test_random_seed():
14
+ G = nx.empty_graph(5)
15
+ assert nx.maximal_independent_set(G, seed=1) == [1, 0, 3, 2, 4]
16
+
17
+
18
+ @pytest.mark.parametrize("graph", [nx.complete_graph(5), nx.complete_graph(55)])
19
+ def test_K5(graph):
20
+ """Maximal independent set for complete graphs"""
21
+ assert all(nx.maximal_independent_set(graph, [n]) == [n] for n in graph)
22
+
23
+
24
+ def test_exceptions():
25
+ """Bad input should raise exception."""
26
+ G = nx.florentine_families_graph()
27
+ pytest.raises(nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Smith"])
28
+ pytest.raises(
29
+ nx.NetworkXUnfeasible, nx.maximal_independent_set, G, ["Salviati", "Pazzi"]
30
+ )
31
+ # MaximalIndependentSet is not implemented for directed graphs
32
+ pytest.raises(nx.NetworkXNotImplemented, nx.maximal_independent_set, nx.DiGraph(G))
33
+
34
+
35
+ def test_florentine_family():
36
+ G = nx.florentine_families_graph()
37
+ indep = nx.maximal_independent_set(G, ["Medici", "Bischeri"])
38
+ assert set(indep) == {
39
+ "Medici",
40
+ "Bischeri",
41
+ "Castellani",
42
+ "Pazzi",
43
+ "Ginori",
44
+ "Lamberteschi",
45
+ }
46
+
47
+
48
+ def test_bipartite():
49
+ G = nx.complete_bipartite_graph(12, 34)
50
+ indep = nx.maximal_independent_set(G, [4, 5, 9, 10])
51
+ assert sorted(indep) == list(range(12))
52
+
53
+
54
+ def test_random_graphs():
55
+ """Generate 5 random graphs of different types and sizes and
56
+ make sure that all sets are independent and maximal."""
57
+ for i in range(0, 50, 10):
58
+ G = nx.erdos_renyi_graph(i * 10 + 1, random.random())
59
+ IS = nx.maximal_independent_set(G)
60
+ assert G.subgraph(IS).number_of_edges() == 0
61
+ nbrs_of_MIS = set.union(*(set(G.neighbors(v)) for v in IS))
62
+ assert all(v in nbrs_of_MIS for v in set(G.nodes()).difference(IS))
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_non_randomness.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+ np = pytest.importorskip("numpy")
6
+
7
+
8
+ @pytest.mark.parametrize(
9
+ "k, weight, expected",
10
+ [
11
+ (None, None, 7.21), # infers 3 communities
12
+ (2, None, 11.7),
13
+ (None, "weight", 25.45),
14
+ (2, "weight", 38.8),
15
+ ],
16
+ )
17
+ def test_non_randomness(k, weight, expected):
18
+ G = nx.karate_club_graph()
19
+ np.testing.assert_almost_equal(
20
+ nx.non_randomness(G, k, weight)[0], expected, decimal=2
21
+ )
22
+
23
+
24
+ def test_non_connected():
25
+ G = nx.Graph([(1, 2)])
26
+ G.add_node(3)
27
+ with pytest.raises(nx.NetworkXException, match="Non connected"):
28
+ nx.non_randomness(G)
29
+
30
+
31
+ def test_self_loops():
32
+ G = nx.Graph()
33
+ G.add_edge(1, 2)
34
+ G.add_edge(1, 1)
35
+ with pytest.raises(nx.NetworkXError, match="Graph must not contain self-loops"):
36
+ nx.non_randomness(G)
37
+
38
+
39
+ def test_empty_graph():
40
+ G = nx.empty_graph(1)
41
+ with pytest.raises(nx.NetworkXError, match=".*not applicable to empty graphs"):
42
+ nx.non_randomness(G)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_reciprocity.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ class TestReciprocity:
7
+ # test overall reciprocity by passing whole graph
8
+ def test_reciprocity_digraph(self):
9
+ DG = nx.DiGraph([(1, 2), (2, 1)])
10
+ reciprocity = nx.reciprocity(DG)
11
+ assert reciprocity == 1.0
12
+
13
+ # test empty graph's overall reciprocity which will throw an error
14
+ def test_overall_reciprocity_empty_graph(self):
15
+ with pytest.raises(nx.NetworkXError):
16
+ DG = nx.DiGraph()
17
+ nx.overall_reciprocity(DG)
18
+
19
+ # test for reciprocity for a list of nodes
20
+ def test_reciprocity_graph_nodes(self):
21
+ DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
22
+ reciprocity = nx.reciprocity(DG, [1, 2])
23
+ expected_reciprocity = {1: 0.0, 2: 0.6666666666666666}
24
+ assert reciprocity == expected_reciprocity
25
+
26
+ # test for reciprocity for a single node
27
+ def test_reciprocity_graph_node(self):
28
+ DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
29
+ reciprocity = nx.reciprocity(DG, 2)
30
+ assert reciprocity == 0.6666666666666666
31
+
32
+ # test for reciprocity for an isolated node
33
+ def test_reciprocity_graph_isolated_nodes(self):
34
+ with pytest.raises(nx.NetworkXError):
35
+ DG = nx.DiGraph([(1, 2)])
36
+ DG.add_node(4)
37
+ nx.reciprocity(DG, 4)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_regular.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx
4
+ import networkx as nx
5
+ import networkx.algorithms.regular as reg
6
+ import networkx.generators as gen
7
+
8
+
9
+ class TestKFactor:
10
+ def test_k_factor_trivial(self):
11
+ g = gen.cycle_graph(4)
12
+ f = reg.k_factor(g, 2)
13
+ assert g.edges == f.edges
14
+
15
+ def test_k_factor1(self):
16
+ g = gen.grid_2d_graph(4, 4)
17
+ g_kf = reg.k_factor(g, 2)
18
+ for edge in g_kf.edges():
19
+ assert g.has_edge(edge[0], edge[1])
20
+ for _, degree in g_kf.degree():
21
+ assert degree == 2
22
+
23
+ def test_k_factor2(self):
24
+ g = gen.complete_graph(6)
25
+ g_kf = reg.k_factor(g, 3)
26
+ for edge in g_kf.edges():
27
+ assert g.has_edge(edge[0], edge[1])
28
+ for _, degree in g_kf.degree():
29
+ assert degree == 3
30
+
31
+ def test_k_factor3(self):
32
+ g = gen.grid_2d_graph(4, 4)
33
+ with pytest.raises(nx.NetworkXUnfeasible):
34
+ reg.k_factor(g, 3)
35
+
36
+ def test_k_factor4(self):
37
+ g = gen.lattice.hexagonal_lattice_graph(4, 4)
38
+ # Perfect matching doesn't exist for 4,4 hexagonal lattice graph
39
+ with pytest.raises(nx.NetworkXUnfeasible):
40
+ reg.k_factor(g, 2)
41
+
42
+ def test_k_factor5(self):
43
+ g = gen.complete_graph(6)
44
+ # small k to exercise SmallKGadget
45
+ g_kf = reg.k_factor(g, 2)
46
+ for edge in g_kf.edges():
47
+ assert g.has_edge(edge[0], edge[1])
48
+ for _, degree in g_kf.degree():
49
+ assert degree == 2
50
+
51
+
52
+ class TestIsRegular:
53
+ def test_is_regular1(self):
54
+ g = gen.cycle_graph(4)
55
+ assert reg.is_regular(g)
56
+
57
+ def test_is_regular2(self):
58
+ g = gen.complete_graph(5)
59
+ assert reg.is_regular(g)
60
+
61
+ def test_is_regular3(self):
62
+ g = gen.lollipop_graph(5, 5)
63
+ assert not reg.is_regular(g)
64
+
65
+ def test_is_regular4(self):
66
+ g = nx.DiGraph()
67
+ g.add_edges_from([(0, 1), (1, 2), (2, 0)])
68
+ assert reg.is_regular(g)
69
+
70
+
71
+ def test_is_regular_empty_graph_raises():
72
+ G = nx.Graph()
73
+ with pytest.raises(nx.NetworkXPointlessConcept, match="Graph has no nodes"):
74
+ nx.is_regular(G)
75
+
76
+
77
+ class TestIsKRegular:
78
+ def test_is_k_regular1(self):
79
+ g = gen.cycle_graph(4)
80
+ assert reg.is_k_regular(g, 2)
81
+ assert not reg.is_k_regular(g, 3)
82
+
83
+ def test_is_k_regular2(self):
84
+ g = gen.complete_graph(5)
85
+ assert reg.is_k_regular(g, 4)
86
+ assert not reg.is_k_regular(g, 3)
87
+ assert not reg.is_k_regular(g, 6)
88
+
89
+ def test_is_k_regular3(self):
90
+ g = gen.lollipop_graph(5, 5)
91
+ assert not reg.is_k_regular(g, 5)
92
+ assert not reg.is_k_regular(g, 6)
mplug_owl2/lib/python3.10/site-packages/networkx/algorithms/tests/test_structuralholes.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.structuralholes` module."""
2
+
3
+ import math
4
+
5
+ import pytest
6
+
7
+ import networkx as nx
8
+ from networkx.classes.tests import dispatch_interface
9
+
10
+
11
+ class TestStructuralHoles:
12
+ """Unit tests for computing measures of structural holes.
13
+
14
+ The expected values for these functions were originally computed using the
15
+ proprietary software `UCINET`_ and the free software `IGraph`_ , and then
16
+ computed by hand to make sure that the results are correct.
17
+
18
+ .. _UCINET: https://sites.google.com/site/ucinetsoftware/home
19
+ .. _IGraph: http://igraph.org/
20
+
21
+ """
22
+
23
+ def setup_method(self):
24
+ self.D = nx.DiGraph()
25
+ self.D.add_edges_from([(0, 1), (0, 2), (1, 0), (2, 1)])
26
+ self.D_weights = {(0, 1): 2, (0, 2): 2, (1, 0): 1, (2, 1): 1}
27
+ # Example from http://www.analytictech.com/connections/v20(1)/holes.htm
28
+ self.G = nx.Graph()
29
+ self.G.add_edges_from(
30
+ [
31
+ ("A", "B"),
32
+ ("A", "F"),
33
+ ("A", "G"),
34
+ ("A", "E"),
35
+ ("E", "G"),
36
+ ("F", "G"),
37
+ ("B", "G"),
38
+ ("B", "D"),
39
+ ("D", "G"),
40
+ ("G", "C"),
41
+ ]
42
+ )
43
+ self.G_weights = {
44
+ ("A", "B"): 2,
45
+ ("A", "F"): 3,
46
+ ("A", "G"): 5,
47
+ ("A", "E"): 2,
48
+ ("E", "G"): 8,
49
+ ("F", "G"): 3,
50
+ ("B", "G"): 4,
51
+ ("B", "D"): 1,
52
+ ("D", "G"): 3,
53
+ ("G", "C"): 10,
54
+ }
55
+
56
+ def test_constraint_directed(self):
57
+ constraint = nx.constraint(self.D)
58
+ assert constraint[0] == pytest.approx(1.003, abs=1e-3)
59
+ assert constraint[1] == pytest.approx(1.003, abs=1e-3)
60
+ assert constraint[2] == pytest.approx(1.389, abs=1e-3)
61
+
62
+ def test_effective_size_directed(self):
63
+ effective_size = nx.effective_size(self.D)
64
+ assert effective_size[0] == pytest.approx(1.167, abs=1e-3)
65
+ assert effective_size[1] == pytest.approx(1.167, abs=1e-3)
66
+ assert effective_size[2] == pytest.approx(1, abs=1e-3)
67
+
68
+ def test_constraint_weighted_directed(self):
69
+ D = self.D.copy()
70
+ nx.set_edge_attributes(D, self.D_weights, "weight")
71
+ constraint = nx.constraint(D, weight="weight")
72
+ assert constraint[0] == pytest.approx(0.840, abs=1e-3)
73
+ assert constraint[1] == pytest.approx(1.143, abs=1e-3)
74
+ assert constraint[2] == pytest.approx(1.378, abs=1e-3)
75
+
76
+ def test_effective_size_weighted_directed(self):
77
+ D = self.D.copy()
78
+ nx.set_edge_attributes(D, self.D_weights, "weight")
79
+ effective_size = nx.effective_size(D, weight="weight")
80
+ assert effective_size[0] == pytest.approx(1.567, abs=1e-3)
81
+ assert effective_size[1] == pytest.approx(1.083, abs=1e-3)
82
+ assert effective_size[2] == pytest.approx(1, abs=1e-3)
83
+
84
+ def test_constraint_undirected(self):
85
+ constraint = nx.constraint(self.G)
86
+ assert constraint["G"] == pytest.approx(0.400, abs=1e-3)
87
+ assert constraint["A"] == pytest.approx(0.595, abs=1e-3)
88
+ assert constraint["C"] == pytest.approx(1, abs=1e-3)
89
+
90
+ def test_effective_size_undirected_borgatti(self):
91
+ effective_size = nx.effective_size(self.G)
92
+ assert effective_size["G"] == pytest.approx(4.67, abs=1e-2)
93
+ assert effective_size["A"] == pytest.approx(2.50, abs=1e-2)
94
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
95
+
96
+ def test_effective_size_undirected(self):
97
+ G = self.G.copy()
98
+ nx.set_edge_attributes(G, 1, "weight")
99
+ effective_size = nx.effective_size(G, weight="weight")
100
+ assert effective_size["G"] == pytest.approx(4.67, abs=1e-2)
101
+ assert effective_size["A"] == pytest.approx(2.50, abs=1e-2)
102
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
103
+
104
+ def test_constraint_weighted_undirected(self):
105
+ G = self.G.copy()
106
+ nx.set_edge_attributes(G, self.G_weights, "weight")
107
+ constraint = nx.constraint(G, weight="weight")
108
+ assert constraint["G"] == pytest.approx(0.299, abs=1e-3)
109
+ assert constraint["A"] == pytest.approx(0.795, abs=1e-3)
110
+ assert constraint["C"] == pytest.approx(1, abs=1e-3)
111
+
112
+ def test_effective_size_weighted_undirected(self):
113
+ G = self.G.copy()
114
+ nx.set_edge_attributes(G, self.G_weights, "weight")
115
+ effective_size = nx.effective_size(G, weight="weight")
116
+ assert effective_size["G"] == pytest.approx(5.47, abs=1e-2)
117
+ assert effective_size["A"] == pytest.approx(2.47, abs=1e-2)
118
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
119
+
120
+ def test_constraint_isolated(self):
121
+ G = self.G.copy()
122
+ G.add_node(1)
123
+ constraint = nx.constraint(G)
124
+ assert math.isnan(constraint[1])
125
+
126
+ def test_effective_size_isolated(self):
127
+ G = self.G.copy()
128
+ G.add_node(1)
129
+ nx.set_edge_attributes(G, self.G_weights, "weight")
130
+ effective_size = nx.effective_size(G, weight="weight")
131
+ assert math.isnan(effective_size[1])
132
+
133
+ def test_effective_size_borgatti_isolated(self):
134
+ G = self.G.copy()
135
+ G.add_node(1)
136
+ effective_size = nx.effective_size(G)
137
+ assert math.isnan(effective_size[1])