Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_asteroidal.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_boundary.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_bridges.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_broadcasting.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_chains.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_chordal.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cluster.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_covering.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cuts.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cycles.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_regular.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_dominating.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_efficiency.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graphical.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_hierarchy.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_hybrid.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_isolate.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_link_prediction.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_lowest_common_ancestors.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_matching.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_max_weight_clique.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_mis.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_moral.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_node_classification.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planarity.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_reciprocity.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_richclub.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_similarity.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_summarization.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_swap.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_threshold.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_time_dependent.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_tournament.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_triads.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_walks.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_wiener.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_chains.py +140 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_clique.py +291 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_core.py +266 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_d_separation.py +348 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_dag.py +777 -0
- llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_measures.py +756 -0
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (198 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_asteroidal.cpython-310.pyc
ADDED
Binary file (665 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_boundary.cpython-310.pyc
ADDED
Binary file (7.16 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_bridges.cpython-310.pyc
ADDED
Binary file (4.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_broadcasting.cpython-310.pyc
ADDED
Binary file (2.19 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_chains.cpython-310.pyc
ADDED
Binary file (4.5 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_chordal.cpython-310.pyc
ADDED
Binary file (4.42 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cluster.cpython-310.pyc
ADDED
Binary file (15.4 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-310.pyc
ADDED
Binary file (2.18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc
ADDED
Binary file (9.61 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_covering.cpython-310.pyc
ADDED
Binary file (3.74 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cuts.cpython-310.pyc
ADDED
Binary file (6.29 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_cycles.cpython-310.pyc
ADDED
Binary file (35.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-310.pyc
ADDED
Binary file (9.24 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_distance_regular.cpython-310.pyc
ADDED
Binary file (3.83 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_dominating.cpython-310.pyc
ADDED
Binary file (1.77 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_efficiency.cpython-310.pyc
ADDED
Binary file (2.87 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_graphical.cpython-310.pyc
ADDED
Binary file (5.82 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_hierarchy.cpython-310.pyc
ADDED
Binary file (1.45 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_hybrid.cpython-310.pyc
ADDED
Binary file (848 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_isolate.cpython-310.pyc
ADDED
Binary file (1.01 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_link_prediction.cpython-310.pyc
ADDED
Binary file (21.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_lowest_common_ancestors.cpython-310.pyc
ADDED
Binary file (16 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_matching.cpython-310.pyc
ADDED
Binary file (18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_max_weight_clique.cpython-310.pyc
ADDED
Binary file (6.92 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_mis.cpython-310.pyc
ADDED
Binary file (2.64 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_moral.cpython-310.pyc
ADDED
Binary file (773 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_node_classification.cpython-310.pyc
ADDED
Binary file (4.68 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-310.pyc
ADDED
Binary file (1.25 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc
ADDED
Binary file (7.93 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planarity.cpython-310.pyc
ADDED
Binary file (14.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_reciprocity.cpython-310.pyc
ADDED
Binary file (1.84 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_richclub.cpython-310.pyc
ADDED
Binary file (3.82 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_similarity.cpython-310.pyc
ADDED
Binary file (24.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-310.pyc
ADDED
Binary file (2.57 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_summarization.cpython-310.pyc
ADDED
Binary file (15.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_swap.cpython-310.pyc
ADDED
Binary file (8.48 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_threshold.cpython-310.pyc
ADDED
Binary file (11.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_time_dependent.cpython-310.pyc
ADDED
Binary file (9.31 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_tournament.cpython-310.pyc
ADDED
Binary file (6.16 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_triads.cpython-310.pyc
ADDED
Binary file (12 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_walks.cpython-310.pyc
ADDED
Binary file (1.88 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_wiener.cpython-310.pyc
ADDED
Binary file (3.18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_chains.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the chain decomposition functions."""
|
2 |
+
from itertools import cycle, islice
|
3 |
+
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import networkx as nx
|
7 |
+
|
8 |
+
|
9 |
+
def cycles(seq):
|
10 |
+
"""Yields cyclic permutations of the given sequence.
|
11 |
+
|
12 |
+
For example::
|
13 |
+
|
14 |
+
>>> list(cycles("abc"))
|
15 |
+
[('a', 'b', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b')]
|
16 |
+
|
17 |
+
"""
|
18 |
+
n = len(seq)
|
19 |
+
cycled_seq = cycle(seq)
|
20 |
+
for x in seq:
|
21 |
+
yield tuple(islice(cycled_seq, n))
|
22 |
+
next(cycled_seq)
|
23 |
+
|
24 |
+
|
25 |
+
def cyclic_equals(seq1, seq2):
|
26 |
+
"""Decide whether two sequences are equal up to cyclic permutations.
|
27 |
+
|
28 |
+
For example::
|
29 |
+
|
30 |
+
>>> cyclic_equals("xyz", "zxy")
|
31 |
+
True
|
32 |
+
>>> cyclic_equals("xyz", "zyx")
|
33 |
+
False
|
34 |
+
|
35 |
+
"""
|
36 |
+
# Cast seq2 to a tuple since `cycles()` yields tuples.
|
37 |
+
seq2 = tuple(seq2)
|
38 |
+
return any(x == tuple(seq2) for x in cycles(seq1))
|
39 |
+
|
40 |
+
|
41 |
+
class TestChainDecomposition:
|
42 |
+
"""Unit tests for the chain decomposition function."""
|
43 |
+
|
44 |
+
def assertContainsChain(self, chain, expected):
|
45 |
+
# A cycle could be expressed in two different orientations, one
|
46 |
+
# forward and one backward, so we need to check for cyclic
|
47 |
+
# equality in both orientations.
|
48 |
+
reversed_chain = list(reversed([tuple(reversed(e)) for e in chain]))
|
49 |
+
for candidate in expected:
|
50 |
+
if cyclic_equals(chain, candidate):
|
51 |
+
break
|
52 |
+
if cyclic_equals(reversed_chain, candidate):
|
53 |
+
break
|
54 |
+
else:
|
55 |
+
self.fail("chain not found")
|
56 |
+
|
57 |
+
def test_decomposition(self):
|
58 |
+
edges = [
|
59 |
+
# DFS tree edges.
|
60 |
+
(1, 2),
|
61 |
+
(2, 3),
|
62 |
+
(3, 4),
|
63 |
+
(3, 5),
|
64 |
+
(5, 6),
|
65 |
+
(6, 7),
|
66 |
+
(7, 8),
|
67 |
+
(5, 9),
|
68 |
+
(9, 10),
|
69 |
+
# Nontree edges.
|
70 |
+
(1, 3),
|
71 |
+
(1, 4),
|
72 |
+
(2, 5),
|
73 |
+
(5, 10),
|
74 |
+
(6, 8),
|
75 |
+
]
|
76 |
+
G = nx.Graph(edges)
|
77 |
+
expected = [
|
78 |
+
[(1, 3), (3, 2), (2, 1)],
|
79 |
+
[(1, 4), (4, 3)],
|
80 |
+
[(2, 5), (5, 3)],
|
81 |
+
[(5, 10), (10, 9), (9, 5)],
|
82 |
+
[(6, 8), (8, 7), (7, 6)],
|
83 |
+
]
|
84 |
+
chains = list(nx.chain_decomposition(G, root=1))
|
85 |
+
assert len(chains) == len(expected)
|
86 |
+
|
87 |
+
# This chain decomposition isn't unique
|
88 |
+
# for chain in chains:
|
89 |
+
# print(chain)
|
90 |
+
# self.assertContainsChain(chain, expected)
|
91 |
+
|
92 |
+
def test_barbell_graph(self):
|
93 |
+
# The (3, 0) barbell graph has two triangles joined by a single edge.
|
94 |
+
G = nx.barbell_graph(3, 0)
|
95 |
+
chains = list(nx.chain_decomposition(G, root=0))
|
96 |
+
expected = [[(0, 1), (1, 2), (2, 0)], [(3, 4), (4, 5), (5, 3)]]
|
97 |
+
assert len(chains) == len(expected)
|
98 |
+
for chain in chains:
|
99 |
+
self.assertContainsChain(chain, expected)
|
100 |
+
|
101 |
+
def test_disconnected_graph(self):
|
102 |
+
"""Test for a graph with multiple connected components."""
|
103 |
+
G = nx.barbell_graph(3, 0)
|
104 |
+
H = nx.barbell_graph(3, 0)
|
105 |
+
mapping = dict(zip(range(6), "abcdef"))
|
106 |
+
nx.relabel_nodes(H, mapping, copy=False)
|
107 |
+
G = nx.union(G, H)
|
108 |
+
chains = list(nx.chain_decomposition(G))
|
109 |
+
expected = [
|
110 |
+
[(0, 1), (1, 2), (2, 0)],
|
111 |
+
[(3, 4), (4, 5), (5, 3)],
|
112 |
+
[("a", "b"), ("b", "c"), ("c", "a")],
|
113 |
+
[("d", "e"), ("e", "f"), ("f", "d")],
|
114 |
+
]
|
115 |
+
assert len(chains) == len(expected)
|
116 |
+
for chain in chains:
|
117 |
+
self.assertContainsChain(chain, expected)
|
118 |
+
|
119 |
+
def test_disconnected_graph_root_node(self):
|
120 |
+
"""Test for a single component of a disconnected graph."""
|
121 |
+
G = nx.barbell_graph(3, 0)
|
122 |
+
H = nx.barbell_graph(3, 0)
|
123 |
+
mapping = dict(zip(range(6), "abcdef"))
|
124 |
+
nx.relabel_nodes(H, mapping, copy=False)
|
125 |
+
G = nx.union(G, H)
|
126 |
+
chains = list(nx.chain_decomposition(G, root="a"))
|
127 |
+
expected = [
|
128 |
+
[("a", "b"), ("b", "c"), ("c", "a")],
|
129 |
+
[("d", "e"), ("e", "f"), ("f", "d")],
|
130 |
+
]
|
131 |
+
assert len(chains) == len(expected)
|
132 |
+
for chain in chains:
|
133 |
+
self.assertContainsChain(chain, expected)
|
134 |
+
|
135 |
+
def test_chain_decomposition_root_not_in_G(self):
|
136 |
+
"""Test chain decomposition when root is not in graph"""
|
137 |
+
G = nx.Graph()
|
138 |
+
G.add_nodes_from([1, 2, 3])
|
139 |
+
with pytest.raises(nx.NodeNotFound):
|
140 |
+
nx.has_bridges(G, root=6)
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_clique.py
ADDED
@@ -0,0 +1,291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import networkx as nx
|
4 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
5 |
+
|
6 |
+
|
7 |
+
class TestCliques:
|
8 |
+
def setup_method(self):
|
9 |
+
z = [3, 4, 3, 4, 2, 4, 2, 1, 1, 1, 1]
|
10 |
+
self.G = cnlti(nx.generators.havel_hakimi_graph(z), first_label=1)
|
11 |
+
self.cl = list(nx.find_cliques(self.G))
|
12 |
+
H = nx.complete_graph(6)
|
13 |
+
H = nx.relabel_nodes(H, {i: i + 1 for i in range(6)})
|
14 |
+
H.remove_edges_from([(2, 6), (2, 5), (2, 4), (1, 3), (5, 3)])
|
15 |
+
self.H = H
|
16 |
+
|
17 |
+
def test_find_cliques1(self):
|
18 |
+
cl = list(nx.find_cliques(self.G))
|
19 |
+
rcl = nx.find_cliques_recursive(self.G)
|
20 |
+
expected = [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
|
21 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, rcl))
|
22 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
23 |
+
|
24 |
+
def test_selfloops(self):
|
25 |
+
self.G.add_edge(1, 1)
|
26 |
+
cl = list(nx.find_cliques(self.G))
|
27 |
+
rcl = list(nx.find_cliques_recursive(self.G))
|
28 |
+
assert set(map(frozenset, cl)) == set(map(frozenset, rcl))
|
29 |
+
answer = [{2, 6, 1, 3}, {2, 6, 4}, {5, 4, 7}, {8, 9}, {10, 11}]
|
30 |
+
assert len(answer) == len(cl)
|
31 |
+
assert all(set(c) in answer for c in cl)
|
32 |
+
|
33 |
+
def test_find_cliques2(self):
|
34 |
+
hcl = list(nx.find_cliques(self.H))
|
35 |
+
assert sorted(map(sorted, hcl)) == [[1, 2], [1, 4, 5, 6], [2, 3], [3, 4, 6]]
|
36 |
+
|
37 |
+
def test_find_cliques3(self):
|
38 |
+
# all cliques are [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
|
39 |
+
|
40 |
+
cl = list(nx.find_cliques(self.G, [2]))
|
41 |
+
rcl = nx.find_cliques_recursive(self.G, [2])
|
42 |
+
expected = [[2, 6, 1, 3], [2, 6, 4]]
|
43 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
44 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
45 |
+
|
46 |
+
cl = list(nx.find_cliques(self.G, [2, 3]))
|
47 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 3])
|
48 |
+
expected = [[2, 6, 1, 3]]
|
49 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
50 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
51 |
+
|
52 |
+
cl = list(nx.find_cliques(self.G, [2, 6, 4]))
|
53 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 6, 4])
|
54 |
+
expected = [[2, 6, 4]]
|
55 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
56 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
57 |
+
|
58 |
+
cl = list(nx.find_cliques(self.G, [2, 6, 4]))
|
59 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 6, 4])
|
60 |
+
expected = [[2, 6, 4]]
|
61 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
62 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
63 |
+
|
64 |
+
with pytest.raises(ValueError):
|
65 |
+
list(nx.find_cliques(self.G, [2, 6, 4, 1]))
|
66 |
+
|
67 |
+
with pytest.raises(ValueError):
|
68 |
+
list(nx.find_cliques_recursive(self.G, [2, 6, 4, 1]))
|
69 |
+
|
70 |
+
def test_number_of_cliques(self):
|
71 |
+
G = self.G
|
72 |
+
assert nx.number_of_cliques(G, 1) == 1
|
73 |
+
assert list(nx.number_of_cliques(G, [1]).values()) == [1]
|
74 |
+
assert list(nx.number_of_cliques(G, [1, 2]).values()) == [1, 2]
|
75 |
+
assert nx.number_of_cliques(G, [1, 2]) == {1: 1, 2: 2}
|
76 |
+
assert nx.number_of_cliques(G, 2) == 2
|
77 |
+
assert nx.number_of_cliques(G) == {
|
78 |
+
1: 1,
|
79 |
+
2: 2,
|
80 |
+
3: 1,
|
81 |
+
4: 2,
|
82 |
+
5: 1,
|
83 |
+
6: 2,
|
84 |
+
7: 1,
|
85 |
+
8: 1,
|
86 |
+
9: 1,
|
87 |
+
10: 1,
|
88 |
+
11: 1,
|
89 |
+
}
|
90 |
+
assert nx.number_of_cliques(G, nodes=list(G)) == {
|
91 |
+
1: 1,
|
92 |
+
2: 2,
|
93 |
+
3: 1,
|
94 |
+
4: 2,
|
95 |
+
5: 1,
|
96 |
+
6: 2,
|
97 |
+
7: 1,
|
98 |
+
8: 1,
|
99 |
+
9: 1,
|
100 |
+
10: 1,
|
101 |
+
11: 1,
|
102 |
+
}
|
103 |
+
assert nx.number_of_cliques(G, nodes=[2, 3, 4]) == {2: 2, 3: 1, 4: 2}
|
104 |
+
assert nx.number_of_cliques(G, cliques=self.cl) == {
|
105 |
+
1: 1,
|
106 |
+
2: 2,
|
107 |
+
3: 1,
|
108 |
+
4: 2,
|
109 |
+
5: 1,
|
110 |
+
6: 2,
|
111 |
+
7: 1,
|
112 |
+
8: 1,
|
113 |
+
9: 1,
|
114 |
+
10: 1,
|
115 |
+
11: 1,
|
116 |
+
}
|
117 |
+
assert nx.number_of_cliques(G, list(G), cliques=self.cl) == {
|
118 |
+
1: 1,
|
119 |
+
2: 2,
|
120 |
+
3: 1,
|
121 |
+
4: 2,
|
122 |
+
5: 1,
|
123 |
+
6: 2,
|
124 |
+
7: 1,
|
125 |
+
8: 1,
|
126 |
+
9: 1,
|
127 |
+
10: 1,
|
128 |
+
11: 1,
|
129 |
+
}
|
130 |
+
|
131 |
+
def test_node_clique_number(self):
|
132 |
+
G = self.G
|
133 |
+
assert nx.node_clique_number(G, 1) == 4
|
134 |
+
assert list(nx.node_clique_number(G, [1]).values()) == [4]
|
135 |
+
assert list(nx.node_clique_number(G, [1, 2]).values()) == [4, 4]
|
136 |
+
assert nx.node_clique_number(G, [1, 2]) == {1: 4, 2: 4}
|
137 |
+
assert nx.node_clique_number(G, 1) == 4
|
138 |
+
assert nx.node_clique_number(G) == {
|
139 |
+
1: 4,
|
140 |
+
2: 4,
|
141 |
+
3: 4,
|
142 |
+
4: 3,
|
143 |
+
5: 3,
|
144 |
+
6: 4,
|
145 |
+
7: 3,
|
146 |
+
8: 2,
|
147 |
+
9: 2,
|
148 |
+
10: 2,
|
149 |
+
11: 2,
|
150 |
+
}
|
151 |
+
assert nx.node_clique_number(G, cliques=self.cl) == {
|
152 |
+
1: 4,
|
153 |
+
2: 4,
|
154 |
+
3: 4,
|
155 |
+
4: 3,
|
156 |
+
5: 3,
|
157 |
+
6: 4,
|
158 |
+
7: 3,
|
159 |
+
8: 2,
|
160 |
+
9: 2,
|
161 |
+
10: 2,
|
162 |
+
11: 2,
|
163 |
+
}
|
164 |
+
assert nx.node_clique_number(G, [1, 2], cliques=self.cl) == {1: 4, 2: 4}
|
165 |
+
assert nx.node_clique_number(G, 1, cliques=self.cl) == 4
|
166 |
+
|
167 |
+
def test_make_clique_bipartite(self):
|
168 |
+
G = self.G
|
169 |
+
B = nx.make_clique_bipartite(G)
|
170 |
+
assert sorted(B) == [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
171 |
+
# Project onto the nodes of the original graph.
|
172 |
+
H = nx.projected_graph(B, range(1, 12))
|
173 |
+
assert H.adj == G.adj
|
174 |
+
# Project onto the nodes representing the cliques.
|
175 |
+
H1 = nx.projected_graph(B, range(-5, 0))
|
176 |
+
# Relabel the negative numbers as positive ones.
|
177 |
+
H1 = nx.relabel_nodes(H1, {-v: v for v in range(1, 6)})
|
178 |
+
assert sorted(H1) == [1, 2, 3, 4, 5]
|
179 |
+
|
180 |
+
def test_make_max_clique_graph(self):
|
181 |
+
"""Tests that the maximal clique graph is the same as the bipartite
|
182 |
+
clique graph after being projected onto the nodes representing the
|
183 |
+
cliques.
|
184 |
+
|
185 |
+
"""
|
186 |
+
G = self.G
|
187 |
+
B = nx.make_clique_bipartite(G)
|
188 |
+
# Project onto the nodes representing the cliques.
|
189 |
+
H1 = nx.projected_graph(B, range(-5, 0))
|
190 |
+
# Relabel the negative numbers as nonnegative ones, starting at
|
191 |
+
# 0.
|
192 |
+
H1 = nx.relabel_nodes(H1, {-v: v - 1 for v in range(1, 6)})
|
193 |
+
H2 = nx.make_max_clique_graph(G)
|
194 |
+
assert H1.adj == H2.adj
|
195 |
+
|
196 |
+
def test_directed(self):
|
197 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
198 |
+
next(nx.find_cliques(nx.DiGraph()))
|
199 |
+
|
200 |
+
def test_find_cliques_trivial(self):
|
201 |
+
G = nx.Graph()
|
202 |
+
assert sorted(nx.find_cliques(G)) == []
|
203 |
+
assert sorted(nx.find_cliques_recursive(G)) == []
|
204 |
+
|
205 |
+
def test_make_max_clique_graph_create_using(self):
|
206 |
+
G = nx.Graph([(1, 2), (3, 1), (4, 1), (5, 6)])
|
207 |
+
E = nx.Graph([(0, 1), (0, 2), (1, 2)])
|
208 |
+
E.add_node(3)
|
209 |
+
assert nx.is_isomorphic(nx.make_max_clique_graph(G, create_using=nx.Graph), E)
|
210 |
+
|
211 |
+
|
212 |
+
class TestEnumerateAllCliques:
|
213 |
+
def test_paper_figure_4(self):
|
214 |
+
# Same graph as given in Fig. 4 of paper enumerate_all_cliques is
|
215 |
+
# based on.
|
216 |
+
# http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1559964&isnumber=33129
|
217 |
+
G = nx.Graph()
|
218 |
+
edges_fig_4 = [
|
219 |
+
("a", "b"),
|
220 |
+
("a", "c"),
|
221 |
+
("a", "d"),
|
222 |
+
("a", "e"),
|
223 |
+
("b", "c"),
|
224 |
+
("b", "d"),
|
225 |
+
("b", "e"),
|
226 |
+
("c", "d"),
|
227 |
+
("c", "e"),
|
228 |
+
("d", "e"),
|
229 |
+
("f", "b"),
|
230 |
+
("f", "c"),
|
231 |
+
("f", "g"),
|
232 |
+
("g", "f"),
|
233 |
+
("g", "c"),
|
234 |
+
("g", "d"),
|
235 |
+
("g", "e"),
|
236 |
+
]
|
237 |
+
G.add_edges_from(edges_fig_4)
|
238 |
+
|
239 |
+
cliques = list(nx.enumerate_all_cliques(G))
|
240 |
+
clique_sizes = list(map(len, cliques))
|
241 |
+
assert sorted(clique_sizes) == clique_sizes
|
242 |
+
|
243 |
+
expected_cliques = [
|
244 |
+
["a"],
|
245 |
+
["b"],
|
246 |
+
["c"],
|
247 |
+
["d"],
|
248 |
+
["e"],
|
249 |
+
["f"],
|
250 |
+
["g"],
|
251 |
+
["a", "b"],
|
252 |
+
["a", "b", "d"],
|
253 |
+
["a", "b", "d", "e"],
|
254 |
+
["a", "b", "e"],
|
255 |
+
["a", "c"],
|
256 |
+
["a", "c", "d"],
|
257 |
+
["a", "c", "d", "e"],
|
258 |
+
["a", "c", "e"],
|
259 |
+
["a", "d"],
|
260 |
+
["a", "d", "e"],
|
261 |
+
["a", "e"],
|
262 |
+
["b", "c"],
|
263 |
+
["b", "c", "d"],
|
264 |
+
["b", "c", "d", "e"],
|
265 |
+
["b", "c", "e"],
|
266 |
+
["b", "c", "f"],
|
267 |
+
["b", "d"],
|
268 |
+
["b", "d", "e"],
|
269 |
+
["b", "e"],
|
270 |
+
["b", "f"],
|
271 |
+
["c", "d"],
|
272 |
+
["c", "d", "e"],
|
273 |
+
["c", "d", "e", "g"],
|
274 |
+
["c", "d", "g"],
|
275 |
+
["c", "e"],
|
276 |
+
["c", "e", "g"],
|
277 |
+
["c", "f"],
|
278 |
+
["c", "f", "g"],
|
279 |
+
["c", "g"],
|
280 |
+
["d", "e"],
|
281 |
+
["d", "e", "g"],
|
282 |
+
["d", "g"],
|
283 |
+
["e", "g"],
|
284 |
+
["f", "g"],
|
285 |
+
["a", "b", "c"],
|
286 |
+
["a", "b", "c", "d"],
|
287 |
+
["a", "b", "c", "d", "e"],
|
288 |
+
["a", "b", "c", "e"],
|
289 |
+
]
|
290 |
+
|
291 |
+
assert sorted(map(sorted, cliques)) == sorted(map(sorted, expected_cliques))
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_core.py
ADDED
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import networkx as nx
|
4 |
+
from networkx.utils import nodes_equal
|
5 |
+
|
6 |
+
|
7 |
+
class TestCore:
|
8 |
+
@classmethod
|
9 |
+
def setup_class(cls):
|
10 |
+
# G is the example graph in Figure 1 from Batagelj and
|
11 |
+
# Zaversnik's paper titled An O(m) Algorithm for Cores
|
12 |
+
# Decomposition of Networks, 2003,
|
13 |
+
# http://arXiv.org/abs/cs/0310049. With nodes labeled as
|
14 |
+
# shown, the 3-core is given by nodes 1-8, the 2-core by nodes
|
15 |
+
# 9-16, the 1-core by nodes 17-20 and node 21 is in the
|
16 |
+
# 0-core.
|
17 |
+
t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
|
18 |
+
t2 = nx.convert_node_labels_to_integers(t1, 5)
|
19 |
+
G = nx.union(t1, t2)
|
20 |
+
G.add_edges_from(
|
21 |
+
[
|
22 |
+
(3, 7),
|
23 |
+
(2, 11),
|
24 |
+
(11, 5),
|
25 |
+
(11, 12),
|
26 |
+
(5, 12),
|
27 |
+
(12, 19),
|
28 |
+
(12, 18),
|
29 |
+
(3, 9),
|
30 |
+
(7, 9),
|
31 |
+
(7, 10),
|
32 |
+
(9, 10),
|
33 |
+
(9, 20),
|
34 |
+
(17, 13),
|
35 |
+
(13, 14),
|
36 |
+
(14, 15),
|
37 |
+
(15, 16),
|
38 |
+
(16, 13),
|
39 |
+
]
|
40 |
+
)
|
41 |
+
G.add_node(21)
|
42 |
+
cls.G = G
|
43 |
+
|
44 |
+
# Create the graph H resulting from the degree sequence
|
45 |
+
# [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.
|
46 |
+
|
47 |
+
degseq = [0, 1, 2, 2, 2, 2, 3]
|
48 |
+
H = nx.havel_hakimi_graph(degseq)
|
49 |
+
mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
|
50 |
+
cls.H = nx.relabel_nodes(H, mapping)
|
51 |
+
|
52 |
+
def test_trivial(self):
|
53 |
+
"""Empty graph"""
|
54 |
+
G = nx.Graph()
|
55 |
+
assert nx.core_number(G) == {}
|
56 |
+
|
57 |
+
def test_core_number(self):
|
58 |
+
core = nx.core_number(self.G)
|
59 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)]
|
60 |
+
assert nodes_equal(nodes_by_core[0], [21])
|
61 |
+
assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20])
|
62 |
+
assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16])
|
63 |
+
assert nodes_equal(nodes_by_core[3], [1, 2, 3, 4, 5, 6, 7, 8])
|
64 |
+
|
65 |
+
def test_core_number2(self):
|
66 |
+
core = nx.core_number(self.H)
|
67 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(3)]
|
68 |
+
assert nodes_equal(nodes_by_core[0], [0])
|
69 |
+
assert nodes_equal(nodes_by_core[1], [1, 3])
|
70 |
+
assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6])
|
71 |
+
|
72 |
+
def test_core_number_multigraph(self):
|
73 |
+
G = nx.complete_graph(3)
|
74 |
+
G = nx.MultiGraph(G)
|
75 |
+
G.add_edge(1, 2)
|
76 |
+
with pytest.raises(
|
77 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
78 |
+
):
|
79 |
+
nx.core_number(G)
|
80 |
+
|
81 |
+
def test_core_number_self_loop(self):
|
82 |
+
G = nx.cycle_graph(3)
|
83 |
+
G.add_edge(0, 0)
|
84 |
+
with pytest.raises(
|
85 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
86 |
+
):
|
87 |
+
nx.core_number(G)
|
88 |
+
|
89 |
+
def test_directed_core_number(self):
|
90 |
+
"""core number had a bug for directed graphs found in issue #1959"""
|
91 |
+
# small example where too timid edge removal can make cn[2] = 3
|
92 |
+
G = nx.DiGraph()
|
93 |
+
edges = [(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)]
|
94 |
+
G.add_edges_from(edges)
|
95 |
+
assert nx.core_number(G) == {1: 2, 2: 2, 3: 2, 4: 2}
|
96 |
+
# small example where too aggressive edge removal can make cn[2] = 2
|
97 |
+
more_edges = [(1, 5), (3, 5), (4, 5), (3, 6), (4, 6), (5, 6)]
|
98 |
+
G.add_edges_from(more_edges)
|
99 |
+
assert nx.core_number(G) == {1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3}
|
100 |
+
|
101 |
+
def test_main_core(self):
|
102 |
+
main_core_subgraph = nx.k_core(self.H)
|
103 |
+
assert sorted(main_core_subgraph.nodes()) == [2, 4, 5, 6]
|
104 |
+
|
105 |
+
def test_k_core(self):
|
106 |
+
# k=0
|
107 |
+
k_core_subgraph = nx.k_core(self.H, k=0)
|
108 |
+
assert sorted(k_core_subgraph.nodes()) == sorted(self.H.nodes())
|
109 |
+
# k=1
|
110 |
+
k_core_subgraph = nx.k_core(self.H, k=1)
|
111 |
+
assert sorted(k_core_subgraph.nodes()) == [1, 2, 3, 4, 5, 6]
|
112 |
+
# k = 2
|
113 |
+
k_core_subgraph = nx.k_core(self.H, k=2)
|
114 |
+
assert sorted(k_core_subgraph.nodes()) == [2, 4, 5, 6]
|
115 |
+
|
116 |
+
def test_k_core_multigraph(self):
|
117 |
+
core_number = nx.core_number(self.H)
|
118 |
+
H = nx.MultiGraph(self.H)
|
119 |
+
with pytest.deprecated_call():
|
120 |
+
nx.k_core(H, k=0, core_number=core_number)
|
121 |
+
|
122 |
+
def test_main_crust(self):
|
123 |
+
main_crust_subgraph = nx.k_crust(self.H)
|
124 |
+
assert sorted(main_crust_subgraph.nodes()) == [0, 1, 3]
|
125 |
+
|
126 |
+
def test_k_crust(self):
|
127 |
+
# k = 0
|
128 |
+
k_crust_subgraph = nx.k_crust(self.H, k=2)
|
129 |
+
assert sorted(k_crust_subgraph.nodes()) == sorted(self.H.nodes())
|
130 |
+
# k=1
|
131 |
+
k_crust_subgraph = nx.k_crust(self.H, k=1)
|
132 |
+
assert sorted(k_crust_subgraph.nodes()) == [0, 1, 3]
|
133 |
+
# k=2
|
134 |
+
k_crust_subgraph = nx.k_crust(self.H, k=0)
|
135 |
+
assert sorted(k_crust_subgraph.nodes()) == [0]
|
136 |
+
|
137 |
+
def test_k_crust_multigraph(self):
|
138 |
+
core_number = nx.core_number(self.H)
|
139 |
+
H = nx.MultiGraph(self.H)
|
140 |
+
with pytest.deprecated_call():
|
141 |
+
nx.k_crust(H, k=0, core_number=core_number)
|
142 |
+
|
143 |
+
def test_main_shell(self):
|
144 |
+
main_shell_subgraph = nx.k_shell(self.H)
|
145 |
+
assert sorted(main_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
146 |
+
|
147 |
+
def test_k_shell(self):
|
148 |
+
# k=0
|
149 |
+
k_shell_subgraph = nx.k_shell(self.H, k=2)
|
150 |
+
assert sorted(k_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
151 |
+
# k=1
|
152 |
+
k_shell_subgraph = nx.k_shell(self.H, k=1)
|
153 |
+
assert sorted(k_shell_subgraph.nodes()) == [1, 3]
|
154 |
+
# k=2
|
155 |
+
k_shell_subgraph = nx.k_shell(self.H, k=0)
|
156 |
+
assert sorted(k_shell_subgraph.nodes()) == [0]
|
157 |
+
|
158 |
+
def test_k_shell_multigraph(self):
|
159 |
+
core_number = nx.core_number(self.H)
|
160 |
+
H = nx.MultiGraph(self.H)
|
161 |
+
with pytest.deprecated_call():
|
162 |
+
nx.k_shell(H, k=0, core_number=core_number)
|
163 |
+
|
164 |
+
def test_k_corona(self):
|
165 |
+
# k=0
|
166 |
+
k_corona_subgraph = nx.k_corona(self.H, k=2)
|
167 |
+
assert sorted(k_corona_subgraph.nodes()) == [2, 4, 5, 6]
|
168 |
+
# k=1
|
169 |
+
k_corona_subgraph = nx.k_corona(self.H, k=1)
|
170 |
+
assert sorted(k_corona_subgraph.nodes()) == [1]
|
171 |
+
# k=2
|
172 |
+
k_corona_subgraph = nx.k_corona(self.H, k=0)
|
173 |
+
assert sorted(k_corona_subgraph.nodes()) == [0]
|
174 |
+
|
175 |
+
def test_k_corona_multigraph(self):
|
176 |
+
core_number = nx.core_number(self.H)
|
177 |
+
H = nx.MultiGraph(self.H)
|
178 |
+
with pytest.deprecated_call():
|
179 |
+
nx.k_corona(H, k=0, core_number=core_number)
|
180 |
+
|
181 |
+
def test_k_truss(self):
|
182 |
+
# k=-1
|
183 |
+
k_truss_subgraph = nx.k_truss(self.G, -1)
|
184 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
185 |
+
# k=0
|
186 |
+
k_truss_subgraph = nx.k_truss(self.G, 0)
|
187 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
188 |
+
# k=1
|
189 |
+
k_truss_subgraph = nx.k_truss(self.G, 1)
|
190 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
191 |
+
# k=2
|
192 |
+
k_truss_subgraph = nx.k_truss(self.G, 2)
|
193 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
194 |
+
# k=3
|
195 |
+
k_truss_subgraph = nx.k_truss(self.G, 3)
|
196 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 13))
|
197 |
+
|
198 |
+
k_truss_subgraph = nx.k_truss(self.G, 4)
|
199 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 9))
|
200 |
+
|
201 |
+
k_truss_subgraph = nx.k_truss(self.G, 5)
|
202 |
+
assert sorted(k_truss_subgraph.nodes()) == []
|
203 |
+
|
204 |
+
def test_k_truss_digraph(self):
|
205 |
+
G = nx.complete_graph(3)
|
206 |
+
G = nx.DiGraph(G)
|
207 |
+
G.add_edge(2, 1)
|
208 |
+
with pytest.raises(
|
209 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
210 |
+
):
|
211 |
+
nx.k_truss(G, k=1)
|
212 |
+
|
213 |
+
def test_k_truss_multigraph(self):
|
214 |
+
G = nx.complete_graph(3)
|
215 |
+
G = nx.MultiGraph(G)
|
216 |
+
G.add_edge(1, 2)
|
217 |
+
with pytest.raises(
|
218 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
219 |
+
):
|
220 |
+
nx.k_truss(G, k=1)
|
221 |
+
|
222 |
+
def test_k_truss_self_loop(self):
|
223 |
+
G = nx.cycle_graph(3)
|
224 |
+
G.add_edge(0, 0)
|
225 |
+
with pytest.raises(
|
226 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
227 |
+
):
|
228 |
+
nx.k_truss(G, k=1)
|
229 |
+
|
230 |
+
def test_onion_layers(self):
|
231 |
+
layers = nx.onion_layers(self.G)
|
232 |
+
nodes_by_layer = [
|
233 |
+
sorted(n for n in layers if layers[n] == val) for val in range(1, 7)
|
234 |
+
]
|
235 |
+
assert nodes_equal(nodes_by_layer[0], [21])
|
236 |
+
assert nodes_equal(nodes_by_layer[1], [17, 18, 19, 20])
|
237 |
+
assert nodes_equal(nodes_by_layer[2], [10, 12, 13, 14, 15, 16])
|
238 |
+
assert nodes_equal(nodes_by_layer[3], [9, 11])
|
239 |
+
assert nodes_equal(nodes_by_layer[4], [1, 2, 4, 5, 6, 8])
|
240 |
+
assert nodes_equal(nodes_by_layer[5], [3, 7])
|
241 |
+
|
242 |
+
def test_onion_digraph(self):
|
243 |
+
G = nx.complete_graph(3)
|
244 |
+
G = nx.DiGraph(G)
|
245 |
+
G.add_edge(2, 1)
|
246 |
+
with pytest.raises(
|
247 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
248 |
+
):
|
249 |
+
nx.onion_layers(G)
|
250 |
+
|
251 |
+
def test_onion_multigraph(self):
|
252 |
+
G = nx.complete_graph(3)
|
253 |
+
G = nx.MultiGraph(G)
|
254 |
+
G.add_edge(1, 2)
|
255 |
+
with pytest.raises(
|
256 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
257 |
+
):
|
258 |
+
nx.onion_layers(G)
|
259 |
+
|
260 |
+
def test_onion_self_loop(self):
|
261 |
+
G = nx.cycle_graph(3)
|
262 |
+
G.add_edge(0, 0)
|
263 |
+
with pytest.raises(
|
264 |
+
nx.NetworkXNotImplemented, match="Input graph contains self loops"
|
265 |
+
):
|
266 |
+
nx.onion_layers(G)
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_dag.py
ADDED
@@ -0,0 +1,777 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import deque
|
2 |
+
from itertools import combinations, permutations
|
3 |
+
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import networkx as nx
|
7 |
+
from networkx.utils import edges_equal, pairwise
|
8 |
+
|
9 |
+
|
10 |
+
# Recipe from the itertools documentation.
|
11 |
+
def _consume(iterator):
|
12 |
+
"Consume the iterator entirely."
|
13 |
+
# Feed the entire iterator into a zero-length deque.
|
14 |
+
deque(iterator, maxlen=0)
|
15 |
+
|
16 |
+
|
17 |
+
class TestDagLongestPath:
|
18 |
+
"""Unit tests computing the longest path in a directed acyclic graph."""
|
19 |
+
|
20 |
+
def test_empty(self):
|
21 |
+
G = nx.DiGraph()
|
22 |
+
assert nx.dag_longest_path(G) == []
|
23 |
+
|
24 |
+
def test_unweighted1(self):
|
25 |
+
edges = [(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (3, 7)]
|
26 |
+
G = nx.DiGraph(edges)
|
27 |
+
assert nx.dag_longest_path(G) == [1, 2, 3, 5, 6]
|
28 |
+
|
29 |
+
def test_unweighted2(self):
|
30 |
+
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 3), (1, 5), (3, 5)]
|
31 |
+
G = nx.DiGraph(edges)
|
32 |
+
assert nx.dag_longest_path(G) == [1, 2, 3, 4, 5]
|
33 |
+
|
34 |
+
def test_weighted(self):
|
35 |
+
G = nx.DiGraph()
|
36 |
+
edges = [(1, 2, -5), (2, 3, 1), (3, 4, 1), (4, 5, 0), (3, 5, 4), (1, 6, 2)]
|
37 |
+
G.add_weighted_edges_from(edges)
|
38 |
+
assert nx.dag_longest_path(G) == [2, 3, 5]
|
39 |
+
|
40 |
+
def test_undirected_not_implemented(self):
|
41 |
+
G = nx.Graph()
|
42 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.dag_longest_path, G)
|
43 |
+
|
44 |
+
def test_unorderable_nodes(self):
|
45 |
+
"""Tests that computing the longest path does not depend on
|
46 |
+
nodes being orderable.
|
47 |
+
|
48 |
+
For more information, see issue #1989.
|
49 |
+
|
50 |
+
"""
|
51 |
+
# Create the directed path graph on four nodes in a diamond shape,
|
52 |
+
# with nodes represented as (unorderable) Python objects.
|
53 |
+
nodes = [object() for n in range(4)]
|
54 |
+
G = nx.DiGraph()
|
55 |
+
G.add_edge(nodes[0], nodes[1])
|
56 |
+
G.add_edge(nodes[0], nodes[2])
|
57 |
+
G.add_edge(nodes[2], nodes[3])
|
58 |
+
G.add_edge(nodes[1], nodes[3])
|
59 |
+
|
60 |
+
# this will raise NotImplementedError when nodes need to be ordered
|
61 |
+
nx.dag_longest_path(G)
|
62 |
+
|
63 |
+
def test_multigraph_unweighted(self):
|
64 |
+
edges = [(1, 2), (2, 3), (2, 3), (3, 4), (4, 5), (1, 3), (1, 5), (3, 5)]
|
65 |
+
G = nx.MultiDiGraph(edges)
|
66 |
+
assert nx.dag_longest_path(G) == [1, 2, 3, 4, 5]
|
67 |
+
|
68 |
+
def test_multigraph_weighted(self):
|
69 |
+
G = nx.MultiDiGraph()
|
70 |
+
edges = [
|
71 |
+
(1, 2, 2),
|
72 |
+
(2, 3, 2),
|
73 |
+
(1, 3, 1),
|
74 |
+
(1, 3, 5),
|
75 |
+
(1, 3, 2),
|
76 |
+
]
|
77 |
+
G.add_weighted_edges_from(edges)
|
78 |
+
assert nx.dag_longest_path(G) == [1, 3]
|
79 |
+
|
80 |
+
def test_multigraph_weighted_default_weight(self):
|
81 |
+
G = nx.MultiDiGraph([(1, 2), (2, 3)]) # Unweighted edges
|
82 |
+
G.add_weighted_edges_from([(1, 3, 1), (1, 3, 5), (1, 3, 2)])
|
83 |
+
|
84 |
+
# Default value for default weight is 1
|
85 |
+
assert nx.dag_longest_path(G) == [1, 3]
|
86 |
+
assert nx.dag_longest_path(G, default_weight=3) == [1, 2, 3]
|
87 |
+
|
88 |
+
|
89 |
+
class TestDagLongestPathLength:
|
90 |
+
"""Unit tests for computing the length of a longest path in a
|
91 |
+
directed acyclic graph.
|
92 |
+
|
93 |
+
"""
|
94 |
+
|
95 |
+
def test_unweighted(self):
|
96 |
+
edges = [(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (5, 7)]
|
97 |
+
G = nx.DiGraph(edges)
|
98 |
+
assert nx.dag_longest_path_length(G) == 4
|
99 |
+
|
100 |
+
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 3), (1, 5), (3, 5)]
|
101 |
+
G = nx.DiGraph(edges)
|
102 |
+
assert nx.dag_longest_path_length(G) == 4
|
103 |
+
|
104 |
+
# test degenerate graphs
|
105 |
+
G = nx.DiGraph()
|
106 |
+
G.add_node(1)
|
107 |
+
assert nx.dag_longest_path_length(G) == 0
|
108 |
+
|
109 |
+
def test_undirected_not_implemented(self):
|
110 |
+
G = nx.Graph()
|
111 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.dag_longest_path_length, G)
|
112 |
+
|
113 |
+
def test_weighted(self):
|
114 |
+
edges = [(1, 2, -5), (2, 3, 1), (3, 4, 1), (4, 5, 0), (3, 5, 4), (1, 6, 2)]
|
115 |
+
G = nx.DiGraph()
|
116 |
+
G.add_weighted_edges_from(edges)
|
117 |
+
assert nx.dag_longest_path_length(G) == 5
|
118 |
+
|
119 |
+
def test_multigraph_unweighted(self):
|
120 |
+
edges = [(1, 2), (2, 3), (2, 3), (3, 4), (4, 5), (1, 3), (1, 5), (3, 5)]
|
121 |
+
G = nx.MultiDiGraph(edges)
|
122 |
+
assert nx.dag_longest_path_length(G) == 4
|
123 |
+
|
124 |
+
def test_multigraph_weighted(self):
|
125 |
+
G = nx.MultiDiGraph()
|
126 |
+
edges = [
|
127 |
+
(1, 2, 2),
|
128 |
+
(2, 3, 2),
|
129 |
+
(1, 3, 1),
|
130 |
+
(1, 3, 5),
|
131 |
+
(1, 3, 2),
|
132 |
+
]
|
133 |
+
G.add_weighted_edges_from(edges)
|
134 |
+
assert nx.dag_longest_path_length(G) == 5
|
135 |
+
|
136 |
+
|
137 |
+
class TestDAG:
|
138 |
+
@classmethod
|
139 |
+
def setup_class(cls):
|
140 |
+
pass
|
141 |
+
|
142 |
+
def test_topological_sort1(self):
|
143 |
+
DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
|
144 |
+
|
145 |
+
for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]:
|
146 |
+
assert tuple(algorithm(DG)) == (1, 2, 3)
|
147 |
+
|
148 |
+
DG.add_edge(3, 2)
|
149 |
+
|
150 |
+
for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]:
|
151 |
+
pytest.raises(nx.NetworkXUnfeasible, _consume, algorithm(DG))
|
152 |
+
|
153 |
+
DG.remove_edge(2, 3)
|
154 |
+
|
155 |
+
for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]:
|
156 |
+
assert tuple(algorithm(DG)) == (1, 3, 2)
|
157 |
+
|
158 |
+
DG.remove_edge(3, 2)
|
159 |
+
|
160 |
+
assert tuple(nx.topological_sort(DG)) in {(1, 2, 3), (1, 3, 2)}
|
161 |
+
assert tuple(nx.lexicographical_topological_sort(DG)) == (1, 2, 3)
|
162 |
+
|
163 |
+
def test_is_directed_acyclic_graph(self):
|
164 |
+
G = nx.generators.complete_graph(2)
|
165 |
+
assert not nx.is_directed_acyclic_graph(G)
|
166 |
+
assert not nx.is_directed_acyclic_graph(G.to_directed())
|
167 |
+
assert not nx.is_directed_acyclic_graph(nx.Graph([(3, 4), (4, 5)]))
|
168 |
+
assert nx.is_directed_acyclic_graph(nx.DiGraph([(3, 4), (4, 5)]))
|
169 |
+
|
170 |
+
def test_topological_sort2(self):
|
171 |
+
DG = nx.DiGraph(
|
172 |
+
{
|
173 |
+
1: [2],
|
174 |
+
2: [3],
|
175 |
+
3: [4],
|
176 |
+
4: [5],
|
177 |
+
5: [1],
|
178 |
+
11: [12],
|
179 |
+
12: [13],
|
180 |
+
13: [14],
|
181 |
+
14: [15],
|
182 |
+
}
|
183 |
+
)
|
184 |
+
pytest.raises(nx.NetworkXUnfeasible, _consume, nx.topological_sort(DG))
|
185 |
+
|
186 |
+
assert not nx.is_directed_acyclic_graph(DG)
|
187 |
+
|
188 |
+
DG.remove_edge(1, 2)
|
189 |
+
_consume(nx.topological_sort(DG))
|
190 |
+
assert nx.is_directed_acyclic_graph(DG)
|
191 |
+
|
192 |
+
def test_topological_sort3(self):
|
193 |
+
DG = nx.DiGraph()
|
194 |
+
DG.add_edges_from([(1, i) for i in range(2, 5)])
|
195 |
+
DG.add_edges_from([(2, i) for i in range(5, 9)])
|
196 |
+
DG.add_edges_from([(6, i) for i in range(9, 12)])
|
197 |
+
DG.add_edges_from([(4, i) for i in range(12, 15)])
|
198 |
+
|
199 |
+
def validate(order):
|
200 |
+
assert isinstance(order, list)
|
201 |
+
assert set(order) == set(DG)
|
202 |
+
for u, v in combinations(order, 2):
|
203 |
+
assert not nx.has_path(DG, v, u)
|
204 |
+
|
205 |
+
validate(list(nx.topological_sort(DG)))
|
206 |
+
|
207 |
+
DG.add_edge(14, 1)
|
208 |
+
pytest.raises(nx.NetworkXUnfeasible, _consume, nx.topological_sort(DG))
|
209 |
+
|
210 |
+
def test_topological_sort4(self):
|
211 |
+
G = nx.Graph()
|
212 |
+
G.add_edge(1, 2)
|
213 |
+
# Only directed graphs can be topologically sorted.
|
214 |
+
pytest.raises(nx.NetworkXError, _consume, nx.topological_sort(G))
|
215 |
+
|
216 |
+
def test_topological_sort5(self):
|
217 |
+
G = nx.DiGraph()
|
218 |
+
G.add_edge(0, 1)
|
219 |
+
assert list(nx.topological_sort(G)) == [0, 1]
|
220 |
+
|
221 |
+
def test_topological_sort6(self):
|
222 |
+
for algorithm in [nx.topological_sort, nx.lexicographical_topological_sort]:
|
223 |
+
|
224 |
+
def runtime_error():
|
225 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
226 |
+
first = True
|
227 |
+
for x in algorithm(DG):
|
228 |
+
if first:
|
229 |
+
first = False
|
230 |
+
DG.add_edge(5 - x, 5)
|
231 |
+
|
232 |
+
def unfeasible_error():
|
233 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
234 |
+
first = True
|
235 |
+
for x in algorithm(DG):
|
236 |
+
if first:
|
237 |
+
first = False
|
238 |
+
DG.remove_node(4)
|
239 |
+
|
240 |
+
def runtime_error2():
|
241 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
242 |
+
first = True
|
243 |
+
for x in algorithm(DG):
|
244 |
+
if first:
|
245 |
+
first = False
|
246 |
+
DG.remove_node(2)
|
247 |
+
|
248 |
+
pytest.raises(RuntimeError, runtime_error)
|
249 |
+
pytest.raises(RuntimeError, runtime_error2)
|
250 |
+
pytest.raises(nx.NetworkXUnfeasible, unfeasible_error)
|
251 |
+
|
252 |
+
def test_all_topological_sorts_1(self):
|
253 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 5)])
|
254 |
+
assert list(nx.all_topological_sorts(DG)) == [[1, 2, 3, 4, 5]]
|
255 |
+
|
256 |
+
def test_all_topological_sorts_2(self):
|
257 |
+
DG = nx.DiGraph([(1, 3), (2, 1), (2, 4), (4, 3), (4, 5)])
|
258 |
+
assert sorted(nx.all_topological_sorts(DG)) == [
|
259 |
+
[2, 1, 4, 3, 5],
|
260 |
+
[2, 1, 4, 5, 3],
|
261 |
+
[2, 4, 1, 3, 5],
|
262 |
+
[2, 4, 1, 5, 3],
|
263 |
+
[2, 4, 5, 1, 3],
|
264 |
+
]
|
265 |
+
|
266 |
+
def test_all_topological_sorts_3(self):
|
267 |
+
def unfeasible():
|
268 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 2), (4, 5)])
|
269 |
+
# convert to list to execute generator
|
270 |
+
list(nx.all_topological_sorts(DG))
|
271 |
+
|
272 |
+
def not_implemented():
|
273 |
+
G = nx.Graph([(1, 2), (2, 3)])
|
274 |
+
# convert to list to execute generator
|
275 |
+
list(nx.all_topological_sorts(G))
|
276 |
+
|
277 |
+
def not_implemented_2():
|
278 |
+
G = nx.MultiGraph([(1, 2), (1, 2), (2, 3)])
|
279 |
+
list(nx.all_topological_sorts(G))
|
280 |
+
|
281 |
+
pytest.raises(nx.NetworkXUnfeasible, unfeasible)
|
282 |
+
pytest.raises(nx.NetworkXNotImplemented, not_implemented)
|
283 |
+
pytest.raises(nx.NetworkXNotImplemented, not_implemented_2)
|
284 |
+
|
285 |
+
def test_all_topological_sorts_4(self):
|
286 |
+
DG = nx.DiGraph()
|
287 |
+
for i in range(7):
|
288 |
+
DG.add_node(i)
|
289 |
+
assert sorted(map(list, permutations(DG.nodes))) == sorted(
|
290 |
+
nx.all_topological_sorts(DG)
|
291 |
+
)
|
292 |
+
|
293 |
+
def test_all_topological_sorts_multigraph_1(self):
|
294 |
+
DG = nx.MultiDiGraph([(1, 2), (1, 2), (2, 3), (3, 4), (3, 5), (3, 5), (3, 5)])
|
295 |
+
assert sorted(nx.all_topological_sorts(DG)) == sorted(
|
296 |
+
[[1, 2, 3, 4, 5], [1, 2, 3, 5, 4]]
|
297 |
+
)
|
298 |
+
|
299 |
+
def test_all_topological_sorts_multigraph_2(self):
|
300 |
+
N = 9
|
301 |
+
edges = []
|
302 |
+
for i in range(1, N):
|
303 |
+
edges.extend([(i, i + 1)] * i)
|
304 |
+
DG = nx.MultiDiGraph(edges)
|
305 |
+
assert list(nx.all_topological_sorts(DG)) == [list(range(1, N + 1))]
|
306 |
+
|
307 |
+
def test_ancestors(self):
|
308 |
+
G = nx.DiGraph()
|
309 |
+
ancestors = nx.algorithms.dag.ancestors
|
310 |
+
G.add_edges_from([(1, 2), (1, 3), (4, 2), (4, 3), (4, 5), (2, 6), (5, 6)])
|
311 |
+
assert ancestors(G, 6) == {1, 2, 4, 5}
|
312 |
+
assert ancestors(G, 3) == {1, 4}
|
313 |
+
assert ancestors(G, 1) == set()
|
314 |
+
pytest.raises(nx.NetworkXError, ancestors, G, 8)
|
315 |
+
|
316 |
+
def test_descendants(self):
|
317 |
+
G = nx.DiGraph()
|
318 |
+
descendants = nx.algorithms.dag.descendants
|
319 |
+
G.add_edges_from([(1, 2), (1, 3), (4, 2), (4, 3), (4, 5), (2, 6), (5, 6)])
|
320 |
+
assert descendants(G, 1) == {2, 3, 6}
|
321 |
+
assert descendants(G, 4) == {2, 3, 5, 6}
|
322 |
+
assert descendants(G, 3) == set()
|
323 |
+
pytest.raises(nx.NetworkXError, descendants, G, 8)
|
324 |
+
|
325 |
+
def test_transitive_closure(self):
|
326 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
327 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
328 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
329 |
+
G = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
|
330 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)]
|
331 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
332 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
|
333 |
+
solution = [(1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)]
|
334 |
+
soln = sorted(solution + [(n, n) for n in G])
|
335 |
+
assert edges_equal(sorted(nx.transitive_closure(G).edges()), soln)
|
336 |
+
|
337 |
+
G = nx.Graph([(1, 2), (2, 3), (3, 4)])
|
338 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
339 |
+
assert edges_equal(sorted(nx.transitive_closure(G).edges()), solution)
|
340 |
+
|
341 |
+
G = nx.MultiGraph([(1, 2), (2, 3), (3, 4)])
|
342 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
343 |
+
assert edges_equal(sorted(nx.transitive_closure(G).edges()), solution)
|
344 |
+
|
345 |
+
G = nx.MultiDiGraph([(1, 2), (2, 3), (3, 4)])
|
346 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
347 |
+
assert edges_equal(sorted(nx.transitive_closure(G).edges()), solution)
|
348 |
+
|
349 |
+
# test if edge data is copied
|
350 |
+
G = nx.DiGraph([(1, 2, {"a": 3}), (2, 3, {"b": 0}), (3, 4)])
|
351 |
+
H = nx.transitive_closure(G)
|
352 |
+
for u, v in G.edges():
|
353 |
+
assert G.get_edge_data(u, v) == H.get_edge_data(u, v)
|
354 |
+
|
355 |
+
k = 10
|
356 |
+
G = nx.DiGraph((i, i + 1, {"f": "b", "weight": i}) for i in range(k))
|
357 |
+
H = nx.transitive_closure(G)
|
358 |
+
for u, v in G.edges():
|
359 |
+
assert G.get_edge_data(u, v) == H.get_edge_data(u, v)
|
360 |
+
|
361 |
+
G = nx.Graph()
|
362 |
+
with pytest.raises(nx.NetworkXError):
|
363 |
+
nx.transitive_closure(G, reflexive="wrong input")
|
364 |
+
|
365 |
+
def test_reflexive_transitive_closure(self):
|
366 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
367 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
368 |
+
soln = sorted(solution + [(n, n) for n in G])
|
369 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
370 |
+
assert edges_equal(nx.transitive_closure(G, False).edges(), solution)
|
371 |
+
assert edges_equal(nx.transitive_closure(G, True).edges(), soln)
|
372 |
+
assert edges_equal(nx.transitive_closure(G, None).edges(), solution)
|
373 |
+
|
374 |
+
G = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
|
375 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)]
|
376 |
+
soln = sorted(solution + [(n, n) for n in G])
|
377 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
378 |
+
assert edges_equal(nx.transitive_closure(G, False).edges(), solution)
|
379 |
+
assert edges_equal(nx.transitive_closure(G, True).edges(), soln)
|
380 |
+
assert edges_equal(nx.transitive_closure(G, None).edges(), solution)
|
381 |
+
|
382 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
|
383 |
+
solution = sorted([(1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)])
|
384 |
+
soln = sorted(solution + [(n, n) for n in G])
|
385 |
+
assert edges_equal(sorted(nx.transitive_closure(G).edges()), soln)
|
386 |
+
assert edges_equal(sorted(nx.transitive_closure(G, False).edges()), soln)
|
387 |
+
assert edges_equal(sorted(nx.transitive_closure(G, None).edges()), solution)
|
388 |
+
assert edges_equal(sorted(nx.transitive_closure(G, True).edges()), soln)
|
389 |
+
|
390 |
+
G = nx.Graph([(1, 2), (2, 3), (3, 4)])
|
391 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
392 |
+
soln = sorted(solution + [(n, n) for n in G])
|
393 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
394 |
+
assert edges_equal(nx.transitive_closure(G, False).edges(), solution)
|
395 |
+
assert edges_equal(nx.transitive_closure(G, True).edges(), soln)
|
396 |
+
assert edges_equal(nx.transitive_closure(G, None).edges(), solution)
|
397 |
+
|
398 |
+
G = nx.MultiGraph([(1, 2), (2, 3), (3, 4)])
|
399 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
400 |
+
soln = sorted(solution + [(n, n) for n in G])
|
401 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
402 |
+
assert edges_equal(nx.transitive_closure(G, False).edges(), solution)
|
403 |
+
assert edges_equal(nx.transitive_closure(G, True).edges(), soln)
|
404 |
+
assert edges_equal(nx.transitive_closure(G, None).edges(), solution)
|
405 |
+
|
406 |
+
G = nx.MultiDiGraph([(1, 2), (2, 3), (3, 4)])
|
407 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
408 |
+
soln = sorted(solution + [(n, n) for n in G])
|
409 |
+
assert edges_equal(nx.transitive_closure(G).edges(), solution)
|
410 |
+
assert edges_equal(nx.transitive_closure(G, False).edges(), solution)
|
411 |
+
assert edges_equal(nx.transitive_closure(G, True).edges(), soln)
|
412 |
+
assert edges_equal(nx.transitive_closure(G, None).edges(), solution)
|
413 |
+
|
414 |
+
def test_transitive_closure_dag(self):
|
415 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
416 |
+
transitive_closure = nx.algorithms.dag.transitive_closure_dag
|
417 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
|
418 |
+
assert edges_equal(transitive_closure(G).edges(), solution)
|
419 |
+
G = nx.DiGraph([(1, 2), (2, 3), (2, 4)])
|
420 |
+
solution = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)]
|
421 |
+
assert edges_equal(transitive_closure(G).edges(), solution)
|
422 |
+
G = nx.Graph([(1, 2), (2, 3), (3, 4)])
|
423 |
+
pytest.raises(nx.NetworkXNotImplemented, transitive_closure, G)
|
424 |
+
|
425 |
+
# test if edge data is copied
|
426 |
+
G = nx.DiGraph([(1, 2, {"a": 3}), (2, 3, {"b": 0}), (3, 4)])
|
427 |
+
H = transitive_closure(G)
|
428 |
+
for u, v in G.edges():
|
429 |
+
assert G.get_edge_data(u, v) == H.get_edge_data(u, v)
|
430 |
+
|
431 |
+
k = 10
|
432 |
+
G = nx.DiGraph((i, i + 1, {"foo": "bar", "weight": i}) for i in range(k))
|
433 |
+
H = transitive_closure(G)
|
434 |
+
for u, v in G.edges():
|
435 |
+
assert G.get_edge_data(u, v) == H.get_edge_data(u, v)
|
436 |
+
|
437 |
+
def test_transitive_reduction(self):
|
438 |
+
G = nx.DiGraph([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
|
439 |
+
transitive_reduction = nx.algorithms.dag.transitive_reduction
|
440 |
+
solution = [(1, 2), (2, 3), (3, 4)]
|
441 |
+
assert edges_equal(transitive_reduction(G).edges(), solution)
|
442 |
+
G = nx.DiGraph([(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)])
|
443 |
+
transitive_reduction = nx.algorithms.dag.transitive_reduction
|
444 |
+
solution = [(1, 2), (2, 3), (2, 4)]
|
445 |
+
assert edges_equal(transitive_reduction(G).edges(), solution)
|
446 |
+
G = nx.Graph([(1, 2), (2, 3), (3, 4)])
|
447 |
+
pytest.raises(nx.NetworkXNotImplemented, transitive_reduction, G)
|
448 |
+
|
449 |
+
def _check_antichains(self, solution, result):
|
450 |
+
sol = [frozenset(a) for a in solution]
|
451 |
+
res = [frozenset(a) for a in result]
|
452 |
+
assert set(sol) == set(res)
|
453 |
+
|
454 |
+
def test_antichains(self):
|
455 |
+
antichains = nx.algorithms.dag.antichains
|
456 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
457 |
+
solution = [[], [4], [3], [2], [1]]
|
458 |
+
self._check_antichains(list(antichains(G)), solution)
|
459 |
+
G = nx.DiGraph([(1, 2), (2, 3), (2, 4), (3, 5), (5, 6), (5, 7)])
|
460 |
+
solution = [
|
461 |
+
[],
|
462 |
+
[4],
|
463 |
+
[7],
|
464 |
+
[7, 4],
|
465 |
+
[6],
|
466 |
+
[6, 4],
|
467 |
+
[6, 7],
|
468 |
+
[6, 7, 4],
|
469 |
+
[5],
|
470 |
+
[5, 4],
|
471 |
+
[3],
|
472 |
+
[3, 4],
|
473 |
+
[2],
|
474 |
+
[1],
|
475 |
+
]
|
476 |
+
self._check_antichains(list(antichains(G)), solution)
|
477 |
+
G = nx.DiGraph([(1, 2), (1, 3), (3, 4), (3, 5), (5, 6)])
|
478 |
+
solution = [
|
479 |
+
[],
|
480 |
+
[6],
|
481 |
+
[5],
|
482 |
+
[4],
|
483 |
+
[4, 6],
|
484 |
+
[4, 5],
|
485 |
+
[3],
|
486 |
+
[2],
|
487 |
+
[2, 6],
|
488 |
+
[2, 5],
|
489 |
+
[2, 4],
|
490 |
+
[2, 4, 6],
|
491 |
+
[2, 4, 5],
|
492 |
+
[2, 3],
|
493 |
+
[1],
|
494 |
+
]
|
495 |
+
self._check_antichains(list(antichains(G)), solution)
|
496 |
+
G = nx.DiGraph({0: [1, 2], 1: [4], 2: [3], 3: [4]})
|
497 |
+
solution = [[], [4], [3], [2], [1], [1, 3], [1, 2], [0]]
|
498 |
+
self._check_antichains(list(antichains(G)), solution)
|
499 |
+
G = nx.DiGraph()
|
500 |
+
self._check_antichains(list(antichains(G)), [[]])
|
501 |
+
G = nx.DiGraph()
|
502 |
+
G.add_nodes_from([0, 1, 2])
|
503 |
+
solution = [[], [0], [1], [1, 0], [2], [2, 0], [2, 1], [2, 1, 0]]
|
504 |
+
self._check_antichains(list(antichains(G)), solution)
|
505 |
+
|
506 |
+
def f(x):
|
507 |
+
return list(antichains(x))
|
508 |
+
|
509 |
+
G = nx.Graph([(1, 2), (2, 3), (3, 4)])
|
510 |
+
pytest.raises(nx.NetworkXNotImplemented, f, G)
|
511 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 1)])
|
512 |
+
pytest.raises(nx.NetworkXUnfeasible, f, G)
|
513 |
+
|
514 |
+
def test_lexicographical_topological_sort(self):
|
515 |
+
G = nx.DiGraph([(1, 2), (2, 3), (1, 4), (1, 5), (2, 6)])
|
516 |
+
assert list(nx.lexicographical_topological_sort(G)) == [1, 2, 3, 4, 5, 6]
|
517 |
+
assert list(nx.lexicographical_topological_sort(G, key=lambda x: x)) == [
|
518 |
+
1,
|
519 |
+
2,
|
520 |
+
3,
|
521 |
+
4,
|
522 |
+
5,
|
523 |
+
6,
|
524 |
+
]
|
525 |
+
assert list(nx.lexicographical_topological_sort(G, key=lambda x: -x)) == [
|
526 |
+
1,
|
527 |
+
5,
|
528 |
+
4,
|
529 |
+
2,
|
530 |
+
6,
|
531 |
+
3,
|
532 |
+
]
|
533 |
+
|
534 |
+
def test_lexicographical_topological_sort2(self):
|
535 |
+
"""
|
536 |
+
Check the case of two or more nodes with same key value.
|
537 |
+
Want to avoid exception raised due to comparing nodes directly.
|
538 |
+
See Issue #3493
|
539 |
+
"""
|
540 |
+
|
541 |
+
class Test_Node:
|
542 |
+
def __init__(self, n):
|
543 |
+
self.label = n
|
544 |
+
self.priority = 1
|
545 |
+
|
546 |
+
def __repr__(self):
|
547 |
+
return f"Node({self.label})"
|
548 |
+
|
549 |
+
def sorting_key(node):
|
550 |
+
return node.priority
|
551 |
+
|
552 |
+
test_nodes = [Test_Node(n) for n in range(4)]
|
553 |
+
G = nx.DiGraph()
|
554 |
+
edges = [(0, 1), (0, 2), (0, 3), (2, 3)]
|
555 |
+
G.add_edges_from((test_nodes[a], test_nodes[b]) for a, b in edges)
|
556 |
+
|
557 |
+
sorting = list(nx.lexicographical_topological_sort(G, key=sorting_key))
|
558 |
+
assert sorting == test_nodes
|
559 |
+
|
560 |
+
|
561 |
+
def test_topological_generations():
|
562 |
+
G = nx.DiGraph(
|
563 |
+
{1: [2, 3], 2: [4, 5], 3: [7], 4: [], 5: [6, 7], 6: [], 7: []}
|
564 |
+
).reverse()
|
565 |
+
# order within each generation is inconsequential
|
566 |
+
generations = [sorted(gen) for gen in nx.topological_generations(G)]
|
567 |
+
expected = [[4, 6, 7], [3, 5], [2], [1]]
|
568 |
+
assert generations == expected
|
569 |
+
|
570 |
+
MG = nx.MultiDiGraph(G.edges)
|
571 |
+
MG.add_edge(2, 1)
|
572 |
+
generations = [sorted(gen) for gen in nx.topological_generations(MG)]
|
573 |
+
assert generations == expected
|
574 |
+
|
575 |
+
|
576 |
+
def test_topological_generations_empty():
|
577 |
+
G = nx.DiGraph()
|
578 |
+
assert list(nx.topological_generations(G)) == []
|
579 |
+
|
580 |
+
|
581 |
+
def test_topological_generations_cycle():
|
582 |
+
G = nx.DiGraph([[2, 1], [3, 1], [1, 2]])
|
583 |
+
with pytest.raises(nx.NetworkXUnfeasible):
|
584 |
+
list(nx.topological_generations(G))
|
585 |
+
|
586 |
+
|
587 |
+
def test_is_aperiodic_cycle():
|
588 |
+
G = nx.DiGraph()
|
589 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
590 |
+
assert not nx.is_aperiodic(G)
|
591 |
+
|
592 |
+
|
593 |
+
def test_is_aperiodic_cycle2():
|
594 |
+
G = nx.DiGraph()
|
595 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
596 |
+
nx.add_cycle(G, [3, 4, 5, 6, 7])
|
597 |
+
assert nx.is_aperiodic(G)
|
598 |
+
|
599 |
+
|
600 |
+
def test_is_aperiodic_cycle3():
|
601 |
+
G = nx.DiGraph()
|
602 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
603 |
+
nx.add_cycle(G, [3, 4, 5, 6])
|
604 |
+
assert not nx.is_aperiodic(G)
|
605 |
+
|
606 |
+
|
607 |
+
def test_is_aperiodic_cycle4():
|
608 |
+
G = nx.DiGraph()
|
609 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
610 |
+
G.add_edge(1, 3)
|
611 |
+
assert nx.is_aperiodic(G)
|
612 |
+
|
613 |
+
|
614 |
+
def test_is_aperiodic_selfloop():
|
615 |
+
G = nx.DiGraph()
|
616 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
617 |
+
G.add_edge(1, 1)
|
618 |
+
assert nx.is_aperiodic(G)
|
619 |
+
|
620 |
+
|
621 |
+
def test_is_aperiodic_undirected_raises():
|
622 |
+
G = nx.Graph()
|
623 |
+
pytest.raises(nx.NetworkXError, nx.is_aperiodic, G)
|
624 |
+
|
625 |
+
|
626 |
+
def test_is_aperiodic_empty_graph():
|
627 |
+
G = nx.empty_graph(create_using=nx.DiGraph)
|
628 |
+
with pytest.raises(nx.NetworkXPointlessConcept, match="Graph has no nodes."):
|
629 |
+
nx.is_aperiodic(G)
|
630 |
+
|
631 |
+
|
632 |
+
def test_is_aperiodic_bipartite():
|
633 |
+
# Bipartite graph
|
634 |
+
G = nx.DiGraph(nx.davis_southern_women_graph())
|
635 |
+
assert not nx.is_aperiodic(G)
|
636 |
+
|
637 |
+
|
638 |
+
def test_is_aperiodic_rary_tree():
|
639 |
+
G = nx.full_rary_tree(3, 27, create_using=nx.DiGraph())
|
640 |
+
assert not nx.is_aperiodic(G)
|
641 |
+
|
642 |
+
|
643 |
+
def test_is_aperiodic_disconnected():
|
644 |
+
# disconnected graph
|
645 |
+
G = nx.DiGraph()
|
646 |
+
nx.add_cycle(G, [1, 2, 3, 4])
|
647 |
+
nx.add_cycle(G, [5, 6, 7, 8])
|
648 |
+
assert not nx.is_aperiodic(G)
|
649 |
+
G.add_edge(1, 3)
|
650 |
+
G.add_edge(5, 7)
|
651 |
+
assert nx.is_aperiodic(G)
|
652 |
+
|
653 |
+
|
654 |
+
def test_is_aperiodic_disconnected2():
|
655 |
+
G = nx.DiGraph()
|
656 |
+
nx.add_cycle(G, [0, 1, 2])
|
657 |
+
G.add_edge(3, 3)
|
658 |
+
assert not nx.is_aperiodic(G)
|
659 |
+
|
660 |
+
|
661 |
+
class TestDagToBranching:
|
662 |
+
"""Unit tests for the :func:`networkx.dag_to_branching` function."""
|
663 |
+
|
664 |
+
def test_single_root(self):
|
665 |
+
"""Tests that a directed acyclic graph with a single degree
|
666 |
+
zero node produces an arborescence.
|
667 |
+
|
668 |
+
"""
|
669 |
+
G = nx.DiGraph([(0, 1), (0, 2), (1, 3), (2, 3)])
|
670 |
+
B = nx.dag_to_branching(G)
|
671 |
+
expected = nx.DiGraph([(0, 1), (1, 3), (0, 2), (2, 4)])
|
672 |
+
assert nx.is_arborescence(B)
|
673 |
+
assert nx.is_isomorphic(B, expected)
|
674 |
+
|
675 |
+
def test_multiple_roots(self):
|
676 |
+
"""Tests that a directed acyclic graph with multiple degree zero
|
677 |
+
nodes creates an arborescence with multiple (weakly) connected
|
678 |
+
components.
|
679 |
+
|
680 |
+
"""
|
681 |
+
G = nx.DiGraph([(0, 1), (0, 2), (1, 3), (2, 3), (5, 2)])
|
682 |
+
B = nx.dag_to_branching(G)
|
683 |
+
expected = nx.DiGraph([(0, 1), (1, 3), (0, 2), (2, 4), (5, 6), (6, 7)])
|
684 |
+
assert nx.is_branching(B)
|
685 |
+
assert not nx.is_arborescence(B)
|
686 |
+
assert nx.is_isomorphic(B, expected)
|
687 |
+
|
688 |
+
# # Attributes are not copied by this function. If they were, this would
|
689 |
+
# # be a good test to uncomment.
|
690 |
+
# def test_copy_attributes(self):
|
691 |
+
# """Tests that node attributes are copied in the branching."""
|
692 |
+
# G = nx.DiGraph([(0, 1), (0, 2), (1, 3), (2, 3)])
|
693 |
+
# for v in G:
|
694 |
+
# G.node[v]['label'] = str(v)
|
695 |
+
# B = nx.dag_to_branching(G)
|
696 |
+
# # Determine the root node of the branching.
|
697 |
+
# root = next(v for v, d in B.in_degree() if d == 0)
|
698 |
+
# assert_equal(B.node[root]['label'], '0')
|
699 |
+
# children = B[root]
|
700 |
+
# # Get the left and right children, nodes 1 and 2, respectively.
|
701 |
+
# left, right = sorted(children, key=lambda v: B.node[v]['label'])
|
702 |
+
# assert_equal(B.node[left]['label'], '1')
|
703 |
+
# assert_equal(B.node[right]['label'], '2')
|
704 |
+
# # Get the left grandchild.
|
705 |
+
# children = B[left]
|
706 |
+
# assert_equal(len(children), 1)
|
707 |
+
# left_grandchild = arbitrary_element(children)
|
708 |
+
# assert_equal(B.node[left_grandchild]['label'], '3')
|
709 |
+
# # Get the right grandchild.
|
710 |
+
# children = B[right]
|
711 |
+
# assert_equal(len(children), 1)
|
712 |
+
# right_grandchild = arbitrary_element(children)
|
713 |
+
# assert_equal(B.node[right_grandchild]['label'], '3')
|
714 |
+
|
715 |
+
def test_already_arborescence(self):
|
716 |
+
"""Tests that a directed acyclic graph that is already an
|
717 |
+
arborescence produces an isomorphic arborescence as output.
|
718 |
+
|
719 |
+
"""
|
720 |
+
A = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
|
721 |
+
B = nx.dag_to_branching(A)
|
722 |
+
assert nx.is_isomorphic(A, B)
|
723 |
+
|
724 |
+
def test_already_branching(self):
|
725 |
+
"""Tests that a directed acyclic graph that is already a
|
726 |
+
branching produces an isomorphic branching as output.
|
727 |
+
|
728 |
+
"""
|
729 |
+
T1 = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
|
730 |
+
T2 = nx.balanced_tree(2, 2, create_using=nx.DiGraph())
|
731 |
+
G = nx.disjoint_union(T1, T2)
|
732 |
+
B = nx.dag_to_branching(G)
|
733 |
+
assert nx.is_isomorphic(G, B)
|
734 |
+
|
735 |
+
def test_not_acyclic(self):
|
736 |
+
"""Tests that a non-acyclic graph causes an exception."""
|
737 |
+
with pytest.raises(nx.HasACycle):
|
738 |
+
G = nx.DiGraph(pairwise("abc", cyclic=True))
|
739 |
+
nx.dag_to_branching(G)
|
740 |
+
|
741 |
+
def test_undirected(self):
|
742 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
743 |
+
nx.dag_to_branching(nx.Graph())
|
744 |
+
|
745 |
+
def test_multigraph(self):
|
746 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
747 |
+
nx.dag_to_branching(nx.MultiGraph())
|
748 |
+
|
749 |
+
def test_multidigraph(self):
|
750 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
751 |
+
nx.dag_to_branching(nx.MultiDiGraph())
|
752 |
+
|
753 |
+
|
754 |
+
def test_ancestors_descendants_undirected():
|
755 |
+
"""Regression test to ensure ancestors and descendants work as expected on
|
756 |
+
undirected graphs."""
|
757 |
+
G = nx.path_graph(5)
|
758 |
+
nx.ancestors(G, 2) == nx.descendants(G, 2) == {0, 1, 3, 4}
|
759 |
+
|
760 |
+
|
761 |
+
def test_compute_v_structures_raise():
|
762 |
+
G = nx.Graph()
|
763 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.compute_v_structures, G)
|
764 |
+
|
765 |
+
|
766 |
+
def test_compute_v_structures():
|
767 |
+
edges = [(0, 1), (0, 2), (3, 2)]
|
768 |
+
G = nx.DiGraph(edges)
|
769 |
+
|
770 |
+
v_structs = set(nx.compute_v_structures(G))
|
771 |
+
assert len(v_structs) == 1
|
772 |
+
assert (0, 2, 3) in v_structs
|
773 |
+
|
774 |
+
edges = [("A", "B"), ("C", "B"), ("B", "D"), ("D", "E"), ("G", "E")]
|
775 |
+
G = nx.DiGraph(edges)
|
776 |
+
v_structs = set(nx.compute_v_structures(G))
|
777 |
+
assert len(v_structs) == 2
|
llmeval-env/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_measures.py
ADDED
@@ -0,0 +1,756 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from random import Random
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
import networkx as nx
|
6 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
7 |
+
from networkx.algorithms.distance_measures import _extrema_bounding
|
8 |
+
|
9 |
+
|
10 |
+
def test__extrema_bounding_invalid_compute_kwarg():
|
11 |
+
G = nx.path_graph(3)
|
12 |
+
with pytest.raises(ValueError, match="compute must be one of"):
|
13 |
+
_extrema_bounding(G, compute="spam")
|
14 |
+
|
15 |
+
|
16 |
+
class TestDistance:
|
17 |
+
def setup_method(self):
|
18 |
+
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
|
19 |
+
self.G = G
|
20 |
+
|
21 |
+
def test_eccentricity(self):
|
22 |
+
assert nx.eccentricity(self.G, 1) == 6
|
23 |
+
e = nx.eccentricity(self.G)
|
24 |
+
assert e[1] == 6
|
25 |
+
|
26 |
+
sp = dict(nx.shortest_path_length(self.G))
|
27 |
+
e = nx.eccentricity(self.G, sp=sp)
|
28 |
+
assert e[1] == 6
|
29 |
+
|
30 |
+
e = nx.eccentricity(self.G, v=1)
|
31 |
+
assert e == 6
|
32 |
+
|
33 |
+
# This behavior changed in version 1.8 (ticket #739)
|
34 |
+
e = nx.eccentricity(self.G, v=[1, 1])
|
35 |
+
assert e[1] == 6
|
36 |
+
e = nx.eccentricity(self.G, v=[1, 2])
|
37 |
+
assert e[1] == 6
|
38 |
+
|
39 |
+
# test against graph with one node
|
40 |
+
G = nx.path_graph(1)
|
41 |
+
e = nx.eccentricity(G)
|
42 |
+
assert e[0] == 0
|
43 |
+
e = nx.eccentricity(G, v=0)
|
44 |
+
assert e == 0
|
45 |
+
pytest.raises(nx.NetworkXError, nx.eccentricity, G, 1)
|
46 |
+
|
47 |
+
# test against empty graph
|
48 |
+
G = nx.empty_graph()
|
49 |
+
e = nx.eccentricity(G)
|
50 |
+
assert e == {}
|
51 |
+
|
52 |
+
def test_diameter(self):
|
53 |
+
assert nx.diameter(self.G) == 6
|
54 |
+
|
55 |
+
def test_radius(self):
|
56 |
+
assert nx.radius(self.G) == 4
|
57 |
+
|
58 |
+
def test_periphery(self):
|
59 |
+
assert set(nx.periphery(self.G)) == {1, 4, 13, 16}
|
60 |
+
|
61 |
+
def test_center(self):
|
62 |
+
assert set(nx.center(self.G)) == {6, 7, 10, 11}
|
63 |
+
|
64 |
+
def test_bound_diameter(self):
|
65 |
+
assert nx.diameter(self.G, usebounds=True) == 6
|
66 |
+
|
67 |
+
def test_bound_radius(self):
|
68 |
+
assert nx.radius(self.G, usebounds=True) == 4
|
69 |
+
|
70 |
+
def test_bound_periphery(self):
|
71 |
+
result = {1, 4, 13, 16}
|
72 |
+
assert set(nx.periphery(self.G, usebounds=True)) == result
|
73 |
+
|
74 |
+
def test_bound_center(self):
|
75 |
+
result = {6, 7, 10, 11}
|
76 |
+
assert set(nx.center(self.G, usebounds=True)) == result
|
77 |
+
|
78 |
+
def test_radius_exception(self):
|
79 |
+
G = nx.Graph()
|
80 |
+
G.add_edge(1, 2)
|
81 |
+
G.add_edge(3, 4)
|
82 |
+
pytest.raises(nx.NetworkXError, nx.diameter, G)
|
83 |
+
|
84 |
+
def test_eccentricity_infinite(self):
|
85 |
+
with pytest.raises(nx.NetworkXError):
|
86 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
87 |
+
e = nx.eccentricity(G)
|
88 |
+
|
89 |
+
def test_eccentricity_undirected_not_connected(self):
|
90 |
+
with pytest.raises(nx.NetworkXError):
|
91 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
92 |
+
e = nx.eccentricity(G, sp=1)
|
93 |
+
|
94 |
+
def test_eccentricity_directed_weakly_connected(self):
|
95 |
+
with pytest.raises(nx.NetworkXError):
|
96 |
+
DG = nx.DiGraph([(1, 2), (1, 3)])
|
97 |
+
nx.eccentricity(DG)
|
98 |
+
|
99 |
+
|
100 |
+
class TestWeightedDistance:
|
101 |
+
def setup_method(self):
|
102 |
+
G = nx.Graph()
|
103 |
+
G.add_edge(0, 1, weight=0.6, cost=0.6, high_cost=6)
|
104 |
+
G.add_edge(0, 2, weight=0.2, cost=0.2, high_cost=2)
|
105 |
+
G.add_edge(2, 3, weight=0.1, cost=0.1, high_cost=1)
|
106 |
+
G.add_edge(2, 4, weight=0.7, cost=0.7, high_cost=7)
|
107 |
+
G.add_edge(2, 5, weight=0.9, cost=0.9, high_cost=9)
|
108 |
+
G.add_edge(1, 5, weight=0.3, cost=0.3, high_cost=3)
|
109 |
+
self.G = G
|
110 |
+
self.weight_fn = lambda v, u, e: 2
|
111 |
+
|
112 |
+
def test_eccentricity_weight_None(self):
|
113 |
+
assert nx.eccentricity(self.G, 1, weight=None) == 3
|
114 |
+
e = nx.eccentricity(self.G, weight=None)
|
115 |
+
assert e[1] == 3
|
116 |
+
|
117 |
+
e = nx.eccentricity(self.G, v=1, weight=None)
|
118 |
+
assert e == 3
|
119 |
+
|
120 |
+
# This behavior changed in version 1.8 (ticket #739)
|
121 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight=None)
|
122 |
+
assert e[1] == 3
|
123 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight=None)
|
124 |
+
assert e[1] == 3
|
125 |
+
|
126 |
+
def test_eccentricity_weight_attr(self):
|
127 |
+
assert nx.eccentricity(self.G, 1, weight="weight") == 1.5
|
128 |
+
e = nx.eccentricity(self.G, weight="weight")
|
129 |
+
assert (
|
130 |
+
e
|
131 |
+
== nx.eccentricity(self.G, weight="cost")
|
132 |
+
!= nx.eccentricity(self.G, weight="high_cost")
|
133 |
+
)
|
134 |
+
assert e[1] == 1.5
|
135 |
+
|
136 |
+
e = nx.eccentricity(self.G, v=1, weight="weight")
|
137 |
+
assert e == 1.5
|
138 |
+
|
139 |
+
# This behavior changed in version 1.8 (ticket #739)
|
140 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight="weight")
|
141 |
+
assert e[1] == 1.5
|
142 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight="weight")
|
143 |
+
assert e[1] == 1.5
|
144 |
+
|
145 |
+
def test_eccentricity_weight_fn(self):
|
146 |
+
assert nx.eccentricity(self.G, 1, weight=self.weight_fn) == 6
|
147 |
+
e = nx.eccentricity(self.G, weight=self.weight_fn)
|
148 |
+
assert e[1] == 6
|
149 |
+
|
150 |
+
e = nx.eccentricity(self.G, v=1, weight=self.weight_fn)
|
151 |
+
assert e == 6
|
152 |
+
|
153 |
+
# This behavior changed in version 1.8 (ticket #739)
|
154 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight=self.weight_fn)
|
155 |
+
assert e[1] == 6
|
156 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight=self.weight_fn)
|
157 |
+
assert e[1] == 6
|
158 |
+
|
159 |
+
def test_diameter_weight_None(self):
|
160 |
+
assert nx.diameter(self.G, weight=None) == 3
|
161 |
+
|
162 |
+
def test_diameter_weight_attr(self):
|
163 |
+
assert (
|
164 |
+
nx.diameter(self.G, weight="weight")
|
165 |
+
== nx.diameter(self.G, weight="cost")
|
166 |
+
== 1.6
|
167 |
+
!= nx.diameter(self.G, weight="high_cost")
|
168 |
+
)
|
169 |
+
|
170 |
+
def test_diameter_weight_fn(self):
|
171 |
+
assert nx.diameter(self.G, weight=self.weight_fn) == 6
|
172 |
+
|
173 |
+
def test_radius_weight_None(self):
|
174 |
+
assert pytest.approx(nx.radius(self.G, weight=None)) == 2
|
175 |
+
|
176 |
+
def test_radius_weight_attr(self):
|
177 |
+
assert (
|
178 |
+
pytest.approx(nx.radius(self.G, weight="weight"))
|
179 |
+
== pytest.approx(nx.radius(self.G, weight="cost"))
|
180 |
+
== 0.9
|
181 |
+
!= nx.radius(self.G, weight="high_cost")
|
182 |
+
)
|
183 |
+
|
184 |
+
def test_radius_weight_fn(self):
|
185 |
+
assert nx.radius(self.G, weight=self.weight_fn) == 4
|
186 |
+
|
187 |
+
def test_periphery_weight_None(self):
|
188 |
+
for v in set(nx.periphery(self.G, weight=None)):
|
189 |
+
assert nx.eccentricity(self.G, v, weight=None) == nx.diameter(
|
190 |
+
self.G, weight=None
|
191 |
+
)
|
192 |
+
|
193 |
+
def test_periphery_weight_attr(self):
|
194 |
+
periphery = set(nx.periphery(self.G, weight="weight"))
|
195 |
+
assert (
|
196 |
+
periphery
|
197 |
+
== set(nx.periphery(self.G, weight="cost"))
|
198 |
+
== set(nx.periphery(self.G, weight="high_cost"))
|
199 |
+
)
|
200 |
+
for v in periphery:
|
201 |
+
assert (
|
202 |
+
nx.eccentricity(self.G, v, weight="high_cost")
|
203 |
+
!= nx.eccentricity(self.G, v, weight="weight")
|
204 |
+
== nx.eccentricity(self.G, v, weight="cost")
|
205 |
+
== nx.diameter(self.G, weight="weight")
|
206 |
+
== nx.diameter(self.G, weight="cost")
|
207 |
+
!= nx.diameter(self.G, weight="high_cost")
|
208 |
+
)
|
209 |
+
assert nx.eccentricity(self.G, v, weight="high_cost") == nx.diameter(
|
210 |
+
self.G, weight="high_cost"
|
211 |
+
)
|
212 |
+
|
213 |
+
def test_periphery_weight_fn(self):
|
214 |
+
for v in set(nx.periphery(self.G, weight=self.weight_fn)):
|
215 |
+
assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.diameter(
|
216 |
+
self.G, weight=self.weight_fn
|
217 |
+
)
|
218 |
+
|
219 |
+
def test_center_weight_None(self):
|
220 |
+
for v in set(nx.center(self.G, weight=None)):
|
221 |
+
assert pytest.approx(nx.eccentricity(self.G, v, weight=None)) == nx.radius(
|
222 |
+
self.G, weight=None
|
223 |
+
)
|
224 |
+
|
225 |
+
def test_center_weight_attr(self):
|
226 |
+
center = set(nx.center(self.G, weight="weight"))
|
227 |
+
assert (
|
228 |
+
center
|
229 |
+
== set(nx.center(self.G, weight="cost"))
|
230 |
+
!= set(nx.center(self.G, weight="high_cost"))
|
231 |
+
)
|
232 |
+
for v in center:
|
233 |
+
assert (
|
234 |
+
nx.eccentricity(self.G, v, weight="high_cost")
|
235 |
+
!= pytest.approx(nx.eccentricity(self.G, v, weight="weight"))
|
236 |
+
== pytest.approx(nx.eccentricity(self.G, v, weight="cost"))
|
237 |
+
== nx.radius(self.G, weight="weight")
|
238 |
+
== nx.radius(self.G, weight="cost")
|
239 |
+
!= nx.radius(self.G, weight="high_cost")
|
240 |
+
)
|
241 |
+
assert nx.eccentricity(self.G, v, weight="high_cost") == nx.radius(
|
242 |
+
self.G, weight="high_cost"
|
243 |
+
)
|
244 |
+
|
245 |
+
def test_center_weight_fn(self):
|
246 |
+
for v in set(nx.center(self.G, weight=self.weight_fn)):
|
247 |
+
assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.radius(
|
248 |
+
self.G, weight=self.weight_fn
|
249 |
+
)
|
250 |
+
|
251 |
+
def test_bound_diameter_weight_None(self):
|
252 |
+
assert nx.diameter(self.G, usebounds=True, weight=None) == 3
|
253 |
+
|
254 |
+
def test_bound_diameter_weight_attr(self):
|
255 |
+
assert (
|
256 |
+
nx.diameter(self.G, usebounds=True, weight="high_cost")
|
257 |
+
!= nx.diameter(self.G, usebounds=True, weight="weight")
|
258 |
+
== nx.diameter(self.G, usebounds=True, weight="cost")
|
259 |
+
== 1.6
|
260 |
+
!= nx.diameter(self.G, usebounds=True, weight="high_cost")
|
261 |
+
)
|
262 |
+
assert nx.diameter(self.G, usebounds=True, weight="high_cost") == nx.diameter(
|
263 |
+
self.G, usebounds=True, weight="high_cost"
|
264 |
+
)
|
265 |
+
|
266 |
+
def test_bound_diameter_weight_fn(self):
|
267 |
+
assert nx.diameter(self.G, usebounds=True, weight=self.weight_fn) == 6
|
268 |
+
|
269 |
+
def test_bound_radius_weight_None(self):
|
270 |
+
assert pytest.approx(nx.radius(self.G, usebounds=True, weight=None)) == 2
|
271 |
+
|
272 |
+
def test_bound_radius_weight_attr(self):
|
273 |
+
assert (
|
274 |
+
nx.radius(self.G, usebounds=True, weight="high_cost")
|
275 |
+
!= pytest.approx(nx.radius(self.G, usebounds=True, weight="weight"))
|
276 |
+
== pytest.approx(nx.radius(self.G, usebounds=True, weight="cost"))
|
277 |
+
== 0.9
|
278 |
+
!= nx.radius(self.G, usebounds=True, weight="high_cost")
|
279 |
+
)
|
280 |
+
assert nx.radius(self.G, usebounds=True, weight="high_cost") == nx.radius(
|
281 |
+
self.G, usebounds=True, weight="high_cost"
|
282 |
+
)
|
283 |
+
|
284 |
+
def test_bound_radius_weight_fn(self):
|
285 |
+
assert nx.radius(self.G, usebounds=True, weight=self.weight_fn) == 4
|
286 |
+
|
287 |
+
def test_bound_periphery_weight_None(self):
|
288 |
+
result = {1, 3, 4}
|
289 |
+
assert set(nx.periphery(self.G, usebounds=True, weight=None)) == result
|
290 |
+
|
291 |
+
def test_bound_periphery_weight_attr(self):
|
292 |
+
result = {4, 5}
|
293 |
+
assert (
|
294 |
+
set(nx.periphery(self.G, usebounds=True, weight="weight"))
|
295 |
+
== set(nx.periphery(self.G, usebounds=True, weight="cost"))
|
296 |
+
== result
|
297 |
+
)
|
298 |
+
|
299 |
+
def test_bound_periphery_weight_fn(self):
|
300 |
+
result = {1, 3, 4}
|
301 |
+
assert (
|
302 |
+
set(nx.periphery(self.G, usebounds=True, weight=self.weight_fn)) == result
|
303 |
+
)
|
304 |
+
|
305 |
+
def test_bound_center_weight_None(self):
|
306 |
+
result = {0, 2, 5}
|
307 |
+
assert set(nx.center(self.G, usebounds=True, weight=None)) == result
|
308 |
+
|
309 |
+
def test_bound_center_weight_attr(self):
|
310 |
+
result = {0}
|
311 |
+
assert (
|
312 |
+
set(nx.center(self.G, usebounds=True, weight="weight"))
|
313 |
+
== set(nx.center(self.G, usebounds=True, weight="cost"))
|
314 |
+
== result
|
315 |
+
)
|
316 |
+
|
317 |
+
def test_bound_center_weight_fn(self):
|
318 |
+
result = {0, 2, 5}
|
319 |
+
assert set(nx.center(self.G, usebounds=True, weight=self.weight_fn)) == result
|
320 |
+
|
321 |
+
|
322 |
+
class TestResistanceDistance:
|
323 |
+
@classmethod
|
324 |
+
def setup_class(cls):
|
325 |
+
global np
|
326 |
+
np = pytest.importorskip("numpy")
|
327 |
+
sp = pytest.importorskip("scipy")
|
328 |
+
|
329 |
+
def setup_method(self):
|
330 |
+
G = nx.Graph()
|
331 |
+
G.add_edge(1, 2, weight=2)
|
332 |
+
G.add_edge(2, 3, weight=4)
|
333 |
+
G.add_edge(3, 4, weight=1)
|
334 |
+
G.add_edge(1, 4, weight=3)
|
335 |
+
self.G = G
|
336 |
+
|
337 |
+
def test_resistance_distance_directed_graph(self):
|
338 |
+
G = nx.DiGraph()
|
339 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
340 |
+
nx.resistance_distance(G)
|
341 |
+
|
342 |
+
def test_resistance_distance_empty(self):
|
343 |
+
G = nx.Graph()
|
344 |
+
with pytest.raises(nx.NetworkXError):
|
345 |
+
nx.resistance_distance(G)
|
346 |
+
|
347 |
+
def test_resistance_distance_not_connected(self):
|
348 |
+
with pytest.raises(nx.NetworkXError):
|
349 |
+
self.G.add_node(5)
|
350 |
+
nx.resistance_distance(self.G, 1, 5)
|
351 |
+
|
352 |
+
def test_resistance_distance_nodeA_not_in_graph(self):
|
353 |
+
with pytest.raises(nx.NetworkXError):
|
354 |
+
nx.resistance_distance(self.G, 9, 1)
|
355 |
+
|
356 |
+
def test_resistance_distance_nodeB_not_in_graph(self):
|
357 |
+
with pytest.raises(nx.NetworkXError):
|
358 |
+
nx.resistance_distance(self.G, 1, 9)
|
359 |
+
|
360 |
+
def test_resistance_distance(self):
|
361 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
|
362 |
+
test_data = 1 / (1 / (2 + 4) + 1 / (1 + 3))
|
363 |
+
assert round(rd, 5) == round(test_data, 5)
|
364 |
+
|
365 |
+
def test_resistance_distance_noinv(self):
|
366 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", False)
|
367 |
+
test_data = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1 + 1 / 3))
|
368 |
+
assert round(rd, 5) == round(test_data, 5)
|
369 |
+
|
370 |
+
def test_resistance_distance_no_weight(self):
|
371 |
+
rd = nx.resistance_distance(self.G, 1, 3)
|
372 |
+
assert round(rd, 5) == 1
|
373 |
+
|
374 |
+
def test_resistance_distance_neg_weight(self):
|
375 |
+
self.G[2][3]["weight"] = -4
|
376 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
|
377 |
+
test_data = 1 / (1 / (2 + -4) + 1 / (1 + 3))
|
378 |
+
assert round(rd, 5) == round(test_data, 5)
|
379 |
+
|
380 |
+
def test_multigraph(self):
|
381 |
+
G = nx.MultiGraph()
|
382 |
+
G.add_edge(1, 2, weight=2)
|
383 |
+
G.add_edge(2, 3, weight=4)
|
384 |
+
G.add_edge(3, 4, weight=1)
|
385 |
+
G.add_edge(1, 4, weight=3)
|
386 |
+
rd = nx.resistance_distance(G, 1, 3, "weight", True)
|
387 |
+
assert np.isclose(rd, 1 / (1 / (2 + 4) + 1 / (1 + 3)))
|
388 |
+
|
389 |
+
def test_resistance_distance_div0(self):
|
390 |
+
with pytest.raises(ZeroDivisionError):
|
391 |
+
self.G[1][2]["weight"] = 0
|
392 |
+
nx.resistance_distance(self.G, 1, 3, "weight")
|
393 |
+
|
394 |
+
def test_resistance_distance_same_node(self):
|
395 |
+
assert nx.resistance_distance(self.G, 1, 1) == 0
|
396 |
+
|
397 |
+
def test_resistance_distance_only_nodeA(self):
|
398 |
+
rd = nx.resistance_distance(self.G, nodeA=1)
|
399 |
+
test_data = {}
|
400 |
+
test_data[1] = 0
|
401 |
+
test_data[2] = 0.75
|
402 |
+
test_data[3] = 1
|
403 |
+
test_data[4] = 0.75
|
404 |
+
assert type(rd) == dict
|
405 |
+
assert sorted(rd.keys()) == sorted(test_data.keys())
|
406 |
+
for key in rd:
|
407 |
+
assert np.isclose(rd[key], test_data[key])
|
408 |
+
|
409 |
+
def test_resistance_distance_only_nodeB(self):
|
410 |
+
rd = nx.resistance_distance(self.G, nodeB=1)
|
411 |
+
test_data = {}
|
412 |
+
test_data[1] = 0
|
413 |
+
test_data[2] = 0.75
|
414 |
+
test_data[3] = 1
|
415 |
+
test_data[4] = 0.75
|
416 |
+
assert type(rd) == dict
|
417 |
+
assert sorted(rd.keys()) == sorted(test_data.keys())
|
418 |
+
for key in rd:
|
419 |
+
assert np.isclose(rd[key], test_data[key])
|
420 |
+
|
421 |
+
def test_resistance_distance_all(self):
|
422 |
+
rd = nx.resistance_distance(self.G)
|
423 |
+
assert type(rd) == dict
|
424 |
+
assert round(rd[1][3], 5) == 1
|
425 |
+
|
426 |
+
|
427 |
+
class TestEffectiveGraphResistance:
|
428 |
+
@classmethod
|
429 |
+
def setup_class(cls):
|
430 |
+
global np
|
431 |
+
np = pytest.importorskip("numpy")
|
432 |
+
sp = pytest.importorskip("scipy")
|
433 |
+
|
434 |
+
def setup_method(self):
|
435 |
+
G = nx.Graph()
|
436 |
+
G.add_edge(1, 2, weight=2)
|
437 |
+
G.add_edge(1, 3, weight=1)
|
438 |
+
G.add_edge(2, 3, weight=4)
|
439 |
+
self.G = G
|
440 |
+
|
441 |
+
def test_effective_graph_resistance_directed_graph(self):
|
442 |
+
G = nx.DiGraph()
|
443 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
444 |
+
nx.effective_graph_resistance(G)
|
445 |
+
|
446 |
+
def test_effective_graph_resistance_empty(self):
|
447 |
+
G = nx.Graph()
|
448 |
+
with pytest.raises(nx.NetworkXError):
|
449 |
+
nx.effective_graph_resistance(G)
|
450 |
+
|
451 |
+
def test_effective_graph_resistance_not_connected(self):
|
452 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
453 |
+
RG = nx.effective_graph_resistance(G)
|
454 |
+
assert np.isinf(RG)
|
455 |
+
|
456 |
+
def test_effective_graph_resistance(self):
|
457 |
+
RG = nx.effective_graph_resistance(self.G, "weight", True)
|
458 |
+
rd12 = 1 / (1 / (1 + 4) + 1 / 2)
|
459 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / 4)
|
460 |
+
rd23 = 1 / (1 / (2 + 4) + 1 / 1)
|
461 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
462 |
+
|
463 |
+
def test_effective_graph_resistance_noinv(self):
|
464 |
+
RG = nx.effective_graph_resistance(self.G, "weight", False)
|
465 |
+
rd12 = 1 / (1 / (1 / 1 + 1 / 4) + 1 / (1 / 2))
|
466 |
+
rd13 = 1 / (1 / (1 / 1 + 1 / 2) + 1 / (1 / 4))
|
467 |
+
rd23 = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1))
|
468 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
469 |
+
|
470 |
+
def test_effective_graph_resistance_no_weight(self):
|
471 |
+
RG = nx.effective_graph_resistance(self.G)
|
472 |
+
assert np.isclose(RG, 2)
|
473 |
+
|
474 |
+
def test_effective_graph_resistance_neg_weight(self):
|
475 |
+
self.G[2][3]["weight"] = -4
|
476 |
+
RG = nx.effective_graph_resistance(self.G, "weight", True)
|
477 |
+
rd12 = 1 / (1 / (1 + -4) + 1 / 2)
|
478 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / (-4))
|
479 |
+
rd23 = 1 / (1 / (2 + -4) + 1 / 1)
|
480 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
481 |
+
|
482 |
+
def test_effective_graph_resistance_multigraph(self):
|
483 |
+
G = nx.MultiGraph()
|
484 |
+
G.add_edge(1, 2, weight=2)
|
485 |
+
G.add_edge(1, 3, weight=1)
|
486 |
+
G.add_edge(2, 3, weight=1)
|
487 |
+
G.add_edge(2, 3, weight=3)
|
488 |
+
RG = nx.effective_graph_resistance(G, "weight", True)
|
489 |
+
edge23 = 1 / (1 / 1 + 1 / 3)
|
490 |
+
rd12 = 1 / (1 / (1 + edge23) + 1 / 2)
|
491 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / edge23)
|
492 |
+
rd23 = 1 / (1 / (2 + edge23) + 1 / 1)
|
493 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
494 |
+
|
495 |
+
def test_effective_graph_resistance_div0(self):
|
496 |
+
with pytest.raises(ZeroDivisionError):
|
497 |
+
self.G[1][2]["weight"] = 0
|
498 |
+
nx.effective_graph_resistance(self.G, "weight")
|
499 |
+
|
500 |
+
def test_effective_graph_resistance_complete_graph(self):
|
501 |
+
N = 10
|
502 |
+
G = nx.complete_graph(N)
|
503 |
+
RG = nx.effective_graph_resistance(G)
|
504 |
+
assert np.isclose(RG, N - 1)
|
505 |
+
|
506 |
+
def test_effective_graph_resistance_path_graph(self):
|
507 |
+
N = 10
|
508 |
+
G = nx.path_graph(N)
|
509 |
+
RG = nx.effective_graph_resistance(G)
|
510 |
+
assert np.isclose(RG, (N - 1) * N * (N + 1) // 6)
|
511 |
+
|
512 |
+
|
513 |
+
class TestBarycenter:
|
514 |
+
"""Test :func:`networkx.algorithms.distance_measures.barycenter`."""
|
515 |
+
|
516 |
+
def barycenter_as_subgraph(self, g, **kwargs):
|
517 |
+
"""Return the subgraph induced on the barycenter of g"""
|
518 |
+
b = nx.barycenter(g, **kwargs)
|
519 |
+
assert isinstance(b, list)
|
520 |
+
assert set(b) <= set(g)
|
521 |
+
return g.subgraph(b)
|
522 |
+
|
523 |
+
def test_must_be_connected(self):
|
524 |
+
pytest.raises(nx.NetworkXNoPath, nx.barycenter, nx.empty_graph(5))
|
525 |
+
|
526 |
+
def test_sp_kwarg(self):
|
527 |
+
# Complete graph K_5. Normally it works...
|
528 |
+
K_5 = nx.complete_graph(5)
|
529 |
+
sp = dict(nx.shortest_path_length(K_5))
|
530 |
+
assert nx.barycenter(K_5, sp=sp) == list(K_5)
|
531 |
+
|
532 |
+
# ...but not with the weight argument
|
533 |
+
for u, v, data in K_5.edges.data():
|
534 |
+
data["weight"] = 1
|
535 |
+
pytest.raises(ValueError, nx.barycenter, K_5, sp=sp, weight="weight")
|
536 |
+
|
537 |
+
# ...and a corrupted sp can make it seem like K_5 is disconnected
|
538 |
+
del sp[0][1]
|
539 |
+
pytest.raises(nx.NetworkXNoPath, nx.barycenter, K_5, sp=sp)
|
540 |
+
|
541 |
+
def test_trees(self):
|
542 |
+
"""The barycenter of a tree is a single vertex or an edge.
|
543 |
+
|
544 |
+
See [West01]_, p. 78.
|
545 |
+
"""
|
546 |
+
prng = Random(0xDEADBEEF)
|
547 |
+
for i in range(50):
|
548 |
+
RT = nx.random_labeled_tree(prng.randint(1, 75), seed=prng)
|
549 |
+
b = self.barycenter_as_subgraph(RT)
|
550 |
+
if len(b) == 2:
|
551 |
+
assert b.size() == 1
|
552 |
+
else:
|
553 |
+
assert len(b) == 1
|
554 |
+
assert b.size() == 0
|
555 |
+
|
556 |
+
def test_this_one_specific_tree(self):
|
557 |
+
"""Test the tree pictured at the bottom of [West01]_, p. 78."""
|
558 |
+
g = nx.Graph(
|
559 |
+
{
|
560 |
+
"a": ["b"],
|
561 |
+
"b": ["a", "x"],
|
562 |
+
"x": ["b", "y"],
|
563 |
+
"y": ["x", "z"],
|
564 |
+
"z": ["y", 0, 1, 2, 3, 4],
|
565 |
+
0: ["z"],
|
566 |
+
1: ["z"],
|
567 |
+
2: ["z"],
|
568 |
+
3: ["z"],
|
569 |
+
4: ["z"],
|
570 |
+
}
|
571 |
+
)
|
572 |
+
b = self.barycenter_as_subgraph(g, attr="barycentricity")
|
573 |
+
assert list(b) == ["z"]
|
574 |
+
assert not b.edges
|
575 |
+
expected_barycentricity = {
|
576 |
+
0: 23,
|
577 |
+
1: 23,
|
578 |
+
2: 23,
|
579 |
+
3: 23,
|
580 |
+
4: 23,
|
581 |
+
"a": 35,
|
582 |
+
"b": 27,
|
583 |
+
"x": 21,
|
584 |
+
"y": 17,
|
585 |
+
"z": 15,
|
586 |
+
}
|
587 |
+
for node, barycentricity in expected_barycentricity.items():
|
588 |
+
assert g.nodes[node]["barycentricity"] == barycentricity
|
589 |
+
|
590 |
+
# Doubling weights should do nothing but double the barycentricities
|
591 |
+
for edge in g.edges:
|
592 |
+
g.edges[edge]["weight"] = 2
|
593 |
+
b = self.barycenter_as_subgraph(g, weight="weight", attr="barycentricity2")
|
594 |
+
assert list(b) == ["z"]
|
595 |
+
assert not b.edges
|
596 |
+
for node, barycentricity in expected_barycentricity.items():
|
597 |
+
assert g.nodes[node]["barycentricity2"] == barycentricity * 2
|
598 |
+
|
599 |
+
|
600 |
+
class TestKemenyConstant:
|
601 |
+
@classmethod
|
602 |
+
def setup_class(cls):
|
603 |
+
global np
|
604 |
+
np = pytest.importorskip("numpy")
|
605 |
+
sp = pytest.importorskip("scipy")
|
606 |
+
|
607 |
+
def setup_method(self):
|
608 |
+
G = nx.Graph()
|
609 |
+
w12 = 2
|
610 |
+
w13 = 3
|
611 |
+
w23 = 4
|
612 |
+
G.add_edge(1, 2, weight=w12)
|
613 |
+
G.add_edge(1, 3, weight=w13)
|
614 |
+
G.add_edge(2, 3, weight=w23)
|
615 |
+
self.G = G
|
616 |
+
|
617 |
+
def test_kemeny_constant_directed(self):
|
618 |
+
G = nx.DiGraph()
|
619 |
+
G.add_edge(1, 2)
|
620 |
+
G.add_edge(1, 3)
|
621 |
+
G.add_edge(2, 3)
|
622 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
623 |
+
nx.kemeny_constant(G)
|
624 |
+
|
625 |
+
def test_kemeny_constant_not_connected(self):
|
626 |
+
self.G.add_node(5)
|
627 |
+
with pytest.raises(nx.NetworkXError):
|
628 |
+
nx.kemeny_constant(self.G)
|
629 |
+
|
630 |
+
def test_kemeny_constant_no_nodes(self):
|
631 |
+
G = nx.Graph()
|
632 |
+
with pytest.raises(nx.NetworkXError):
|
633 |
+
nx.kemeny_constant(G)
|
634 |
+
|
635 |
+
def test_kemeny_constant_negative_weight(self):
|
636 |
+
G = nx.Graph()
|
637 |
+
w12 = 2
|
638 |
+
w13 = 3
|
639 |
+
w23 = -10
|
640 |
+
G.add_edge(1, 2, weight=w12)
|
641 |
+
G.add_edge(1, 3, weight=w13)
|
642 |
+
G.add_edge(2, 3, weight=w23)
|
643 |
+
with pytest.raises(nx.NetworkXError):
|
644 |
+
nx.kemeny_constant(G, weight="weight")
|
645 |
+
|
646 |
+
def test_kemeny_constant(self):
|
647 |
+
K = nx.kemeny_constant(self.G, weight="weight")
|
648 |
+
w12 = 2
|
649 |
+
w13 = 3
|
650 |
+
w23 = 4
|
651 |
+
test_data = (
|
652 |
+
3
|
653 |
+
/ 2
|
654 |
+
* (w12 + w13)
|
655 |
+
* (w12 + w23)
|
656 |
+
* (w13 + w23)
|
657 |
+
/ (
|
658 |
+
w12**2 * (w13 + w23)
|
659 |
+
+ w13**2 * (w12 + w23)
|
660 |
+
+ w23**2 * (w12 + w13)
|
661 |
+
+ 3 * w12 * w13 * w23
|
662 |
+
)
|
663 |
+
)
|
664 |
+
assert np.isclose(K, test_data)
|
665 |
+
|
666 |
+
def test_kemeny_constant_no_weight(self):
|
667 |
+
K = nx.kemeny_constant(self.G)
|
668 |
+
assert np.isclose(K, 4 / 3)
|
669 |
+
|
670 |
+
def test_kemeny_constant_multigraph(self):
|
671 |
+
G = nx.MultiGraph()
|
672 |
+
w12_1 = 2
|
673 |
+
w12_2 = 1
|
674 |
+
w13 = 3
|
675 |
+
w23 = 4
|
676 |
+
G.add_edge(1, 2, weight=w12_1)
|
677 |
+
G.add_edge(1, 2, weight=w12_2)
|
678 |
+
G.add_edge(1, 3, weight=w13)
|
679 |
+
G.add_edge(2, 3, weight=w23)
|
680 |
+
K = nx.kemeny_constant(G, weight="weight")
|
681 |
+
w12 = w12_1 + w12_2
|
682 |
+
test_data = (
|
683 |
+
3
|
684 |
+
/ 2
|
685 |
+
* (w12 + w13)
|
686 |
+
* (w12 + w23)
|
687 |
+
* (w13 + w23)
|
688 |
+
/ (
|
689 |
+
w12**2 * (w13 + w23)
|
690 |
+
+ w13**2 * (w12 + w23)
|
691 |
+
+ w23**2 * (w12 + w13)
|
692 |
+
+ 3 * w12 * w13 * w23
|
693 |
+
)
|
694 |
+
)
|
695 |
+
assert np.isclose(K, test_data)
|
696 |
+
|
697 |
+
def test_kemeny_constant_weight0(self):
|
698 |
+
G = nx.Graph()
|
699 |
+
w12 = 0
|
700 |
+
w13 = 3
|
701 |
+
w23 = 4
|
702 |
+
G.add_edge(1, 2, weight=w12)
|
703 |
+
G.add_edge(1, 3, weight=w13)
|
704 |
+
G.add_edge(2, 3, weight=w23)
|
705 |
+
K = nx.kemeny_constant(G, weight="weight")
|
706 |
+
test_data = (
|
707 |
+
3
|
708 |
+
/ 2
|
709 |
+
* (w12 + w13)
|
710 |
+
* (w12 + w23)
|
711 |
+
* (w13 + w23)
|
712 |
+
/ (
|
713 |
+
w12**2 * (w13 + w23)
|
714 |
+
+ w13**2 * (w12 + w23)
|
715 |
+
+ w23**2 * (w12 + w13)
|
716 |
+
+ 3 * w12 * w13 * w23
|
717 |
+
)
|
718 |
+
)
|
719 |
+
assert np.isclose(K, test_data)
|
720 |
+
|
721 |
+
def test_kemeny_constant_selfloop(self):
|
722 |
+
G = nx.Graph()
|
723 |
+
w11 = 1
|
724 |
+
w12 = 2
|
725 |
+
w13 = 3
|
726 |
+
w23 = 4
|
727 |
+
G.add_edge(1, 1, weight=w11)
|
728 |
+
G.add_edge(1, 2, weight=w12)
|
729 |
+
G.add_edge(1, 3, weight=w13)
|
730 |
+
G.add_edge(2, 3, weight=w23)
|
731 |
+
K = nx.kemeny_constant(G, weight="weight")
|
732 |
+
test_data = (
|
733 |
+
(2 * w11 + 3 * w12 + 3 * w13)
|
734 |
+
* (w12 + w23)
|
735 |
+
* (w13 + w23)
|
736 |
+
/ (
|
737 |
+
(w12 * w13 + w12 * w23 + w13 * w23)
|
738 |
+
* (w11 + 2 * w12 + 2 * w13 + 2 * w23)
|
739 |
+
)
|
740 |
+
)
|
741 |
+
assert np.isclose(K, test_data)
|
742 |
+
|
743 |
+
def test_kemeny_constant_complete_bipartite_graph(self):
|
744 |
+
# Theorem 1 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
|
745 |
+
n1 = 5
|
746 |
+
n2 = 4
|
747 |
+
G = nx.complete_bipartite_graph(n1, n2)
|
748 |
+
K = nx.kemeny_constant(G)
|
749 |
+
assert np.isclose(K, n1 + n2 - 3 / 2)
|
750 |
+
|
751 |
+
def test_kemeny_constant_path_graph(self):
|
752 |
+
# Theorem 2 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
|
753 |
+
n = 10
|
754 |
+
G = nx.path_graph(n)
|
755 |
+
K = nx.kemeny_constant(G)
|
756 |
+
assert np.isclose(K, n**2 / 3 - 2 * n / 3 + 1 / 2)
|