Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step80/zero/26.input_layernorm.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/26.input_layernorm.weight/fp32.pt +3 -0
- venv/lib/python3.10/site-packages/networkx/generators/__pycache__/community.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/__pycache__/geometric.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/__pycache__/random_graphs.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__init__.py +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_classic.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_cographs.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_directed.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_nonisomorphic_trees.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_spectral_graph_forge.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_stochastic.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_triads.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_atlas.py +75 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_classic.py +635 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_cographs.py +20 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_community.py +362 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_degree_seq.py +230 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_directed.py +162 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_duplication.py +84 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_ego.py +39 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_expanders.py +164 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_geometric.py +488 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_harary_graph.py +134 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_internet_as_graphs.py +176 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_intersection.py +28 -0
- venv/lib/python3.10/site-packages/networkx/generators/tests/test_interval_graph.py +145 -0
ckpts/universal/global_step80/zero/26.input_layernorm.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:20f6bb8c264a773f831be28422f9c9fdd7877e2c10a629e1f68afd160c7c90ea
|
3 |
+
size 9387
|
ckpts/universal/global_step80/zero/26.input_layernorm.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7707fff121789cea016f154201dfb45731f5a444529157570435ab17353ac45a
|
3 |
+
size 9293
|
venv/lib/python3.10/site-packages/networkx/generators/__pycache__/community.cpython-310.pyc
ADDED
Binary file (31.2 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/__pycache__/geometric.cpython-310.pyc
ADDED
Binary file (38.9 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-310.pyc
ADDED
Binary file (12.5 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/__pycache__/random_graphs.cpython-310.pyc
ADDED
Binary file (35.5 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (193 Bytes). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-310.pyc
ADDED
Binary file (3.58 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_classic.cpython-310.pyc
ADDED
Binary file (21.4 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_cographs.cpython-310.pyc
ADDED
Binary file (658 Bytes). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-310.pyc
ADDED
Binary file (10.3 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-310.pyc
ADDED
Binary file (8.65 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_directed.cpython-310.pyc
ADDED
Binary file (7.01 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-310.pyc
ADDED
Binary file (3.58 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-310.pyc
ADDED
Binary file (1.57 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-310.pyc
ADDED
Binary file (5.85 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-310.pyc
ADDED
Binary file (20.1 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-310.pyc
ADDED
Binary file (2.84 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-310.pyc
ADDED
Binary file (4.83 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-310.pyc
ADDED
Binary file (1.56 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-310.pyc
ADDED
Binary file (4.19 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-310.pyc
ADDED
Binary file (2.52 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-310.pyc
ADDED
Binary file (10.5 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-310.pyc
ADDED
Binary file (10.7 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-310.pyc
ADDED
Binary file (1.49 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_nonisomorphic_trees.cpython-310.pyc
ADDED
Binary file (3.27 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-310.pyc
ADDED
Binary file (2.19 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-310.pyc
ADDED
Binary file (11.7 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-310.pyc
ADDED
Binary file (7.42 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_spectral_graph_forge.cpython-310.pyc
ADDED
Binary file (1.14 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_stochastic.cpython-310.pyc
ADDED
Binary file (2.86 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-310.pyc
ADDED
Binary file (1.79 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-310.pyc
ADDED
Binary file (2.65 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-310.pyc
ADDED
Binary file (6.5 kB). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/__pycache__/test_triads.cpython-310.pyc
ADDED
Binary file (874 Bytes). View file
|
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_atlas.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import groupby
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
import networkx as nx
|
6 |
+
from networkx import graph_atlas, graph_atlas_g
|
7 |
+
from networkx.generators.atlas import NUM_GRAPHS
|
8 |
+
from networkx.utils import edges_equal, nodes_equal, pairwise
|
9 |
+
|
10 |
+
|
11 |
+
class TestAtlasGraph:
|
12 |
+
"""Unit tests for the :func:`~networkx.graph_atlas` function."""
|
13 |
+
|
14 |
+
def test_index_too_small(self):
|
15 |
+
with pytest.raises(ValueError):
|
16 |
+
graph_atlas(-1)
|
17 |
+
|
18 |
+
def test_index_too_large(self):
|
19 |
+
with pytest.raises(ValueError):
|
20 |
+
graph_atlas(NUM_GRAPHS)
|
21 |
+
|
22 |
+
def test_graph(self):
|
23 |
+
G = graph_atlas(6)
|
24 |
+
assert nodes_equal(G.nodes(), range(3))
|
25 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2)])
|
26 |
+
|
27 |
+
|
28 |
+
class TestAtlasGraphG:
|
29 |
+
"""Unit tests for the :func:`~networkx.graph_atlas_g` function."""
|
30 |
+
|
31 |
+
@classmethod
|
32 |
+
def setup_class(cls):
|
33 |
+
cls.GAG = graph_atlas_g()
|
34 |
+
|
35 |
+
def test_sizes(self):
|
36 |
+
G = self.GAG[0]
|
37 |
+
assert G.number_of_nodes() == 0
|
38 |
+
assert G.number_of_edges() == 0
|
39 |
+
|
40 |
+
G = self.GAG[7]
|
41 |
+
assert G.number_of_nodes() == 3
|
42 |
+
assert G.number_of_edges() == 3
|
43 |
+
|
44 |
+
def test_names(self):
|
45 |
+
for i, G in enumerate(self.GAG):
|
46 |
+
assert int(G.name[1:]) == i
|
47 |
+
|
48 |
+
def test_nondecreasing_nodes(self):
|
49 |
+
# check for nondecreasing number of nodes
|
50 |
+
for n1, n2 in pairwise(map(len, self.GAG)):
|
51 |
+
assert n2 <= n1 + 1
|
52 |
+
|
53 |
+
def test_nondecreasing_edges(self):
|
54 |
+
# check for nondecreasing number of edges (for fixed number of
|
55 |
+
# nodes)
|
56 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
57 |
+
for m1, m2 in pairwise(map(nx.number_of_edges, group)):
|
58 |
+
assert m2 <= m1 + 1
|
59 |
+
|
60 |
+
def test_nondecreasing_degree_sequence(self):
|
61 |
+
# Check for lexicographically nondecreasing degree sequences
|
62 |
+
# (for fixed number of nodes and edges).
|
63 |
+
#
|
64 |
+
# There are three exceptions to this rule in the order given in
|
65 |
+
# the "Atlas of Graphs" book, so we need to manually exclude
|
66 |
+
# those.
|
67 |
+
exceptions = [("G55", "G56"), ("G1007", "G1008"), ("G1012", "G1013")]
|
68 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
69 |
+
for m, group in groupby(group, key=nx.number_of_edges):
|
70 |
+
for G1, G2 in pairwise(group):
|
71 |
+
if (G1.name, G2.name) in exceptions:
|
72 |
+
continue
|
73 |
+
d1 = sorted(d for v, d in G1.degree())
|
74 |
+
d2 = sorted(d for v, d in G2.degree())
|
75 |
+
assert d1 <= d2
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_classic.py
ADDED
@@ -0,0 +1,635 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
====================
|
3 |
+
Generators - Classic
|
4 |
+
====================
|
5 |
+
|
6 |
+
Unit tests for various classic graph generators in generators/classic.py
|
7 |
+
"""
|
8 |
+
import itertools
|
9 |
+
import typing
|
10 |
+
|
11 |
+
import pytest
|
12 |
+
|
13 |
+
import networkx as nx
|
14 |
+
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
|
15 |
+
from networkx.utils import edges_equal, nodes_equal
|
16 |
+
|
17 |
+
is_isomorphic = graph_could_be_isomorphic
|
18 |
+
|
19 |
+
|
20 |
+
class TestGeneratorClassic:
|
21 |
+
def test_balanced_tree(self):
|
22 |
+
# balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges
|
23 |
+
for r, h in [(2, 2), (3, 3), (6, 2)]:
|
24 |
+
t = nx.balanced_tree(r, h)
|
25 |
+
order = t.order()
|
26 |
+
assert order == (r ** (h + 1) - 1) / (r - 1)
|
27 |
+
assert nx.is_connected(t)
|
28 |
+
assert t.size() == order - 1
|
29 |
+
dh = nx.degree_histogram(t)
|
30 |
+
assert dh[0] == 0 # no nodes of 0
|
31 |
+
assert dh[1] == r**h # nodes of degree 1 are leaves
|
32 |
+
assert dh[r] == 1 # root is degree r
|
33 |
+
assert dh[r + 1] == order - r**h - 1 # everyone else is degree r+1
|
34 |
+
assert len(dh) == r + 2
|
35 |
+
|
36 |
+
def test_balanced_tree_star(self):
|
37 |
+
# balanced_tree(r,1) is the r-star
|
38 |
+
t = nx.balanced_tree(r=2, h=1)
|
39 |
+
assert is_isomorphic(t, nx.star_graph(2))
|
40 |
+
t = nx.balanced_tree(r=5, h=1)
|
41 |
+
assert is_isomorphic(t, nx.star_graph(5))
|
42 |
+
t = nx.balanced_tree(r=10, h=1)
|
43 |
+
assert is_isomorphic(t, nx.star_graph(10))
|
44 |
+
|
45 |
+
def test_balanced_tree_path(self):
|
46 |
+
"""Tests that the balanced tree with branching factor one is the
|
47 |
+
path graph.
|
48 |
+
|
49 |
+
"""
|
50 |
+
# A tree of height four has five levels.
|
51 |
+
T = nx.balanced_tree(1, 4)
|
52 |
+
P = nx.path_graph(5)
|
53 |
+
assert is_isomorphic(T, P)
|
54 |
+
|
55 |
+
def test_full_rary_tree(self):
|
56 |
+
r = 2
|
57 |
+
n = 9
|
58 |
+
t = nx.full_rary_tree(r, n)
|
59 |
+
assert t.order() == n
|
60 |
+
assert nx.is_connected(t)
|
61 |
+
dh = nx.degree_histogram(t)
|
62 |
+
assert dh[0] == 0 # no nodes of 0
|
63 |
+
assert dh[1] == 5 # nodes of degree 1 are leaves
|
64 |
+
assert dh[r] == 1 # root is degree r
|
65 |
+
assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1
|
66 |
+
assert len(dh) == r + 2
|
67 |
+
|
68 |
+
def test_full_rary_tree_balanced(self):
|
69 |
+
t = nx.full_rary_tree(2, 15)
|
70 |
+
th = nx.balanced_tree(2, 3)
|
71 |
+
assert is_isomorphic(t, th)
|
72 |
+
|
73 |
+
def test_full_rary_tree_path(self):
|
74 |
+
t = nx.full_rary_tree(1, 10)
|
75 |
+
assert is_isomorphic(t, nx.path_graph(10))
|
76 |
+
|
77 |
+
def test_full_rary_tree_empty(self):
|
78 |
+
t = nx.full_rary_tree(0, 10)
|
79 |
+
assert is_isomorphic(t, nx.empty_graph(10))
|
80 |
+
t = nx.full_rary_tree(3, 0)
|
81 |
+
assert is_isomorphic(t, nx.empty_graph(0))
|
82 |
+
|
83 |
+
def test_full_rary_tree_3_20(self):
|
84 |
+
t = nx.full_rary_tree(3, 20)
|
85 |
+
assert t.order() == 20
|
86 |
+
|
87 |
+
def test_barbell_graph(self):
|
88 |
+
# number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges)
|
89 |
+
# number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1
|
90 |
+
m1 = 3
|
91 |
+
m2 = 5
|
92 |
+
b = nx.barbell_graph(m1, m2)
|
93 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
94 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
95 |
+
|
96 |
+
m1 = 4
|
97 |
+
m2 = 10
|
98 |
+
b = nx.barbell_graph(m1, m2)
|
99 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
100 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
101 |
+
|
102 |
+
m1 = 3
|
103 |
+
m2 = 20
|
104 |
+
b = nx.barbell_graph(m1, m2)
|
105 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
106 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
107 |
+
|
108 |
+
# Raise NetworkXError if m1<2
|
109 |
+
m1 = 1
|
110 |
+
m2 = 20
|
111 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
112 |
+
|
113 |
+
# Raise NetworkXError if m2<0
|
114 |
+
m1 = 5
|
115 |
+
m2 = -2
|
116 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
117 |
+
|
118 |
+
# nx.barbell_graph(2,m) = nx.path_graph(m+4)
|
119 |
+
m1 = 2
|
120 |
+
m2 = 5
|
121 |
+
b = nx.barbell_graph(m1, m2)
|
122 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
123 |
+
|
124 |
+
m1 = 2
|
125 |
+
m2 = 10
|
126 |
+
b = nx.barbell_graph(m1, m2)
|
127 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
128 |
+
|
129 |
+
m1 = 2
|
130 |
+
m2 = 20
|
131 |
+
b = nx.barbell_graph(m1, m2)
|
132 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
133 |
+
|
134 |
+
pytest.raises(
|
135 |
+
nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph()
|
136 |
+
)
|
137 |
+
|
138 |
+
mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph())
|
139 |
+
assert edges_equal(mb.edges(), b.edges())
|
140 |
+
|
141 |
+
def test_binomial_tree(self):
|
142 |
+
graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
|
143 |
+
for create_using in graphs:
|
144 |
+
for n in range(4):
|
145 |
+
b = nx.binomial_tree(n, create_using)
|
146 |
+
assert nx.number_of_nodes(b) == 2**n
|
147 |
+
assert nx.number_of_edges(b) == (2**n - 1)
|
148 |
+
|
149 |
+
def test_complete_graph(self):
|
150 |
+
# complete_graph(m) is a connected graph with
|
151 |
+
# m nodes and m*(m+1)/2 edges
|
152 |
+
for m in [0, 1, 3, 5]:
|
153 |
+
g = nx.complete_graph(m)
|
154 |
+
assert nx.number_of_nodes(g) == m
|
155 |
+
assert nx.number_of_edges(g) == m * (m - 1) // 2
|
156 |
+
|
157 |
+
mg = nx.complete_graph(m, create_using=nx.MultiGraph)
|
158 |
+
assert edges_equal(mg.edges(), g.edges())
|
159 |
+
|
160 |
+
g = nx.complete_graph("abc")
|
161 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
162 |
+
assert g.size() == 3
|
163 |
+
|
164 |
+
# creates a self-loop... should it? <backward compatible says yes>
|
165 |
+
g = nx.complete_graph("abcb")
|
166 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
167 |
+
assert g.size() == 4
|
168 |
+
|
169 |
+
g = nx.complete_graph("abcb", create_using=nx.MultiGraph)
|
170 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
171 |
+
assert g.size() == 6
|
172 |
+
|
173 |
+
def test_complete_digraph(self):
|
174 |
+
# complete_graph(m) is a connected graph with
|
175 |
+
# m nodes and m*(m+1)/2 edges
|
176 |
+
for m in [0, 1, 3, 5]:
|
177 |
+
g = nx.complete_graph(m, create_using=nx.DiGraph)
|
178 |
+
assert nx.number_of_nodes(g) == m
|
179 |
+
assert nx.number_of_edges(g) == m * (m - 1)
|
180 |
+
|
181 |
+
g = nx.complete_graph("abc", create_using=nx.DiGraph)
|
182 |
+
assert len(g) == 3
|
183 |
+
assert g.size() == 6
|
184 |
+
assert g.is_directed()
|
185 |
+
|
186 |
+
def test_circular_ladder_graph(self):
|
187 |
+
G = nx.circular_ladder_graph(5)
|
188 |
+
pytest.raises(
|
189 |
+
nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph
|
190 |
+
)
|
191 |
+
mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph)
|
192 |
+
assert edges_equal(mG.edges(), G.edges())
|
193 |
+
|
194 |
+
def test_circulant_graph(self):
|
195 |
+
# Ci_n(1) is the cycle graph for all n
|
196 |
+
Ci6_1 = nx.circulant_graph(6, [1])
|
197 |
+
C6 = nx.cycle_graph(6)
|
198 |
+
assert edges_equal(Ci6_1.edges(), C6.edges())
|
199 |
+
|
200 |
+
# Ci_n(1, 2, ..., n div 2) is the complete graph for all n
|
201 |
+
Ci7 = nx.circulant_graph(7, [1, 2, 3])
|
202 |
+
K7 = nx.complete_graph(7)
|
203 |
+
assert edges_equal(Ci7.edges(), K7.edges())
|
204 |
+
|
205 |
+
# Ci_6(1, 3) is K_3,3 i.e. the utility graph
|
206 |
+
Ci6_1_3 = nx.circulant_graph(6, [1, 3])
|
207 |
+
K3_3 = nx.complete_bipartite_graph(3, 3)
|
208 |
+
assert is_isomorphic(Ci6_1_3, K3_3)
|
209 |
+
|
210 |
+
def test_cycle_graph(self):
|
211 |
+
G = nx.cycle_graph(4)
|
212 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
213 |
+
mG = nx.cycle_graph(4, create_using=nx.MultiGraph)
|
214 |
+
assert edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
215 |
+
G = nx.cycle_graph(4, create_using=nx.DiGraph)
|
216 |
+
assert not G.has_edge(2, 1)
|
217 |
+
assert G.has_edge(1, 2)
|
218 |
+
assert G.is_directed()
|
219 |
+
|
220 |
+
G = nx.cycle_graph("abc")
|
221 |
+
assert len(G) == 3
|
222 |
+
assert G.size() == 3
|
223 |
+
G = nx.cycle_graph("abcb")
|
224 |
+
assert len(G) == 3
|
225 |
+
assert G.size() == 2
|
226 |
+
g = nx.cycle_graph("abc", nx.DiGraph)
|
227 |
+
assert len(g) == 3
|
228 |
+
assert g.size() == 3
|
229 |
+
assert g.is_directed()
|
230 |
+
g = nx.cycle_graph("abcb", nx.DiGraph)
|
231 |
+
assert len(g) == 3
|
232 |
+
assert g.size() == 4
|
233 |
+
|
234 |
+
def test_dorogovtsev_goltsev_mendes_graph(self):
|
235 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(0)
|
236 |
+
assert edges_equal(G.edges(), [(0, 1)])
|
237 |
+
assert nodes_equal(list(G), [0, 1])
|
238 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(1)
|
239 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
|
240 |
+
assert nx.average_clustering(G) == 1.0
|
241 |
+
assert sorted(nx.triangles(G).values()) == [1, 1, 1]
|
242 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(10)
|
243 |
+
assert nx.number_of_nodes(G) == 29526
|
244 |
+
assert nx.number_of_edges(G) == 59049
|
245 |
+
assert G.degree(0) == 1024
|
246 |
+
assert G.degree(1) == 1024
|
247 |
+
assert G.degree(2) == 1024
|
248 |
+
|
249 |
+
pytest.raises(
|
250 |
+
nx.NetworkXError,
|
251 |
+
nx.dorogovtsev_goltsev_mendes_graph,
|
252 |
+
7,
|
253 |
+
create_using=nx.DiGraph,
|
254 |
+
)
|
255 |
+
pytest.raises(
|
256 |
+
nx.NetworkXError,
|
257 |
+
nx.dorogovtsev_goltsev_mendes_graph,
|
258 |
+
7,
|
259 |
+
create_using=nx.MultiGraph,
|
260 |
+
)
|
261 |
+
|
262 |
+
def test_create_using(self):
|
263 |
+
G = nx.empty_graph()
|
264 |
+
assert isinstance(G, nx.Graph)
|
265 |
+
pytest.raises(TypeError, nx.empty_graph, create_using=0.0)
|
266 |
+
pytest.raises(TypeError, nx.empty_graph, create_using="Graph")
|
267 |
+
|
268 |
+
G = nx.empty_graph(create_using=nx.MultiGraph)
|
269 |
+
assert isinstance(G, nx.MultiGraph)
|
270 |
+
G = nx.empty_graph(create_using=nx.DiGraph)
|
271 |
+
assert isinstance(G, nx.DiGraph)
|
272 |
+
|
273 |
+
G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph)
|
274 |
+
assert isinstance(G, nx.DiGraph)
|
275 |
+
G = nx.empty_graph(create_using=None, default=nx.MultiGraph)
|
276 |
+
assert isinstance(G, nx.MultiGraph)
|
277 |
+
G = nx.empty_graph(default=nx.MultiGraph)
|
278 |
+
assert isinstance(G, nx.MultiGraph)
|
279 |
+
|
280 |
+
G = nx.path_graph(5)
|
281 |
+
H = nx.empty_graph(create_using=G)
|
282 |
+
assert not H.is_multigraph()
|
283 |
+
assert not H.is_directed()
|
284 |
+
assert len(H) == 0
|
285 |
+
assert G is H
|
286 |
+
|
287 |
+
H = nx.empty_graph(create_using=nx.MultiGraph())
|
288 |
+
assert H.is_multigraph()
|
289 |
+
assert not H.is_directed()
|
290 |
+
assert G is not H
|
291 |
+
|
292 |
+
# test for subclasses that also use typing.Protocol. See gh-6243
|
293 |
+
class Mixin(typing.Protocol):
|
294 |
+
pass
|
295 |
+
|
296 |
+
class MyGraph(Mixin, nx.DiGraph):
|
297 |
+
pass
|
298 |
+
|
299 |
+
G = nx.empty_graph(create_using=MyGraph)
|
300 |
+
|
301 |
+
def test_empty_graph(self):
|
302 |
+
G = nx.empty_graph()
|
303 |
+
assert nx.number_of_nodes(G) == 0
|
304 |
+
G = nx.empty_graph(42)
|
305 |
+
assert nx.number_of_nodes(G) == 42
|
306 |
+
assert nx.number_of_edges(G) == 0
|
307 |
+
|
308 |
+
G = nx.empty_graph("abc")
|
309 |
+
assert len(G) == 3
|
310 |
+
assert G.size() == 0
|
311 |
+
|
312 |
+
# create empty digraph
|
313 |
+
G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh"))
|
314 |
+
assert nx.number_of_nodes(G) == 42
|
315 |
+
assert nx.number_of_edges(G) == 0
|
316 |
+
assert isinstance(G, nx.DiGraph)
|
317 |
+
|
318 |
+
# create empty multigraph
|
319 |
+
G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh"))
|
320 |
+
assert nx.number_of_nodes(G) == 42
|
321 |
+
assert nx.number_of_edges(G) == 0
|
322 |
+
assert isinstance(G, nx.MultiGraph)
|
323 |
+
|
324 |
+
# create empty graph from another
|
325 |
+
pete = nx.petersen_graph()
|
326 |
+
G = nx.empty_graph(42, create_using=pete)
|
327 |
+
assert nx.number_of_nodes(G) == 42
|
328 |
+
assert nx.number_of_edges(G) == 0
|
329 |
+
assert isinstance(G, nx.Graph)
|
330 |
+
|
331 |
+
def test_ladder_graph(self):
|
332 |
+
for i, G in [
|
333 |
+
(0, nx.empty_graph(0)),
|
334 |
+
(1, nx.path_graph(2)),
|
335 |
+
(2, nx.hypercube_graph(2)),
|
336 |
+
(10, nx.grid_graph([2, 10])),
|
337 |
+
]:
|
338 |
+
assert is_isomorphic(nx.ladder_graph(i), G)
|
339 |
+
|
340 |
+
pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph)
|
341 |
+
|
342 |
+
g = nx.ladder_graph(2)
|
343 |
+
mg = nx.ladder_graph(2, create_using=nx.MultiGraph)
|
344 |
+
assert edges_equal(mg.edges(), g.edges())
|
345 |
+
|
346 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 5), (4, 10), (3, 20)])
|
347 |
+
def test_lollipop_graph_right_sizes(self, m, n):
|
348 |
+
G = nx.lollipop_graph(m, n)
|
349 |
+
assert nx.number_of_nodes(G) == m + n
|
350 |
+
assert nx.number_of_edges(G) == m * (m - 1) / 2 + n
|
351 |
+
|
352 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("abc", "defg")])
|
353 |
+
def test_lollipop_graph_size_node_sequence(self, m, n):
|
354 |
+
G = nx.lollipop_graph(m, n)
|
355 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
356 |
+
assert nx.number_of_edges(G) == len(m) * (len(m) - 1) / 2 + len(n)
|
357 |
+
|
358 |
+
def test_lollipop_graph_exceptions(self):
|
359 |
+
# Raise NetworkXError if m<2
|
360 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, -1, 2)
|
361 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20)
|
362 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "", 20)
|
363 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "a", 20)
|
364 |
+
|
365 |
+
# Raise NetworkXError if n<0
|
366 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2)
|
367 |
+
|
368 |
+
# raise NetworkXError if create_using is directed
|
369 |
+
with pytest.raises(nx.NetworkXError):
|
370 |
+
nx.lollipop_graph(2, 20, create_using=nx.DiGraph)
|
371 |
+
with pytest.raises(nx.NetworkXError):
|
372 |
+
nx.lollipop_graph(2, 20, create_using=nx.MultiDiGraph)
|
373 |
+
|
374 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
375 |
+
def test_lollipop_graph_same_as_path_when_m1_is_2(self, m, n):
|
376 |
+
G = nx.lollipop_graph(m, n)
|
377 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
378 |
+
|
379 |
+
def test_lollipop_graph_for_multigraph(self):
|
380 |
+
G = nx.lollipop_graph(5, 20)
|
381 |
+
MG = nx.lollipop_graph(5, 20, create_using=nx.MultiGraph)
|
382 |
+
assert edges_equal(MG.edges(), G.edges())
|
383 |
+
|
384 |
+
@pytest.mark.parametrize(
|
385 |
+
("m", "n"),
|
386 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
387 |
+
)
|
388 |
+
def test_lollipop_graph_mixing_input_types(self, m, n):
|
389 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
390 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
391 |
+
assert is_isomorphic(nx.lollipop_graph(m, n), expected)
|
392 |
+
|
393 |
+
def test_lollipop_graph_non_builtin_ints(self):
|
394 |
+
np = pytest.importorskip("numpy")
|
395 |
+
G = nx.lollipop_graph(np.int32(4), np.int64(3))
|
396 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
397 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
398 |
+
assert is_isomorphic(G, expected)
|
399 |
+
|
400 |
+
def test_null_graph(self):
|
401 |
+
assert nx.number_of_nodes(nx.null_graph()) == 0
|
402 |
+
|
403 |
+
def test_path_graph(self):
|
404 |
+
p = nx.path_graph(0)
|
405 |
+
assert is_isomorphic(p, nx.null_graph())
|
406 |
+
|
407 |
+
p = nx.path_graph(1)
|
408 |
+
assert is_isomorphic(p, nx.empty_graph(1))
|
409 |
+
|
410 |
+
p = nx.path_graph(10)
|
411 |
+
assert nx.is_connected(p)
|
412 |
+
assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
|
413 |
+
assert p.order() - 1 == p.size()
|
414 |
+
|
415 |
+
dp = nx.path_graph(3, create_using=nx.DiGraph)
|
416 |
+
assert dp.has_edge(0, 1)
|
417 |
+
assert not dp.has_edge(1, 0)
|
418 |
+
|
419 |
+
mp = nx.path_graph(10, create_using=nx.MultiGraph)
|
420 |
+
assert edges_equal(mp.edges(), p.edges())
|
421 |
+
|
422 |
+
G = nx.path_graph("abc")
|
423 |
+
assert len(G) == 3
|
424 |
+
assert G.size() == 2
|
425 |
+
G = nx.path_graph("abcb")
|
426 |
+
assert len(G) == 3
|
427 |
+
assert G.size() == 2
|
428 |
+
g = nx.path_graph("abc", nx.DiGraph)
|
429 |
+
assert len(g) == 3
|
430 |
+
assert g.size() == 2
|
431 |
+
assert g.is_directed()
|
432 |
+
g = nx.path_graph("abcb", nx.DiGraph)
|
433 |
+
assert len(g) == 3
|
434 |
+
assert g.size() == 3
|
435 |
+
|
436 |
+
G = nx.path_graph((1, 2, 3, 2, 4))
|
437 |
+
assert G.has_edge(2, 4)
|
438 |
+
|
439 |
+
def test_star_graph(self):
|
440 |
+
assert is_isomorphic(nx.star_graph(""), nx.empty_graph(0))
|
441 |
+
assert is_isomorphic(nx.star_graph([]), nx.empty_graph(0))
|
442 |
+
assert is_isomorphic(nx.star_graph(0), nx.empty_graph(1))
|
443 |
+
assert is_isomorphic(nx.star_graph(1), nx.path_graph(2))
|
444 |
+
assert is_isomorphic(nx.star_graph(2), nx.path_graph(3))
|
445 |
+
assert is_isomorphic(nx.star_graph(5), nx.complete_bipartite_graph(1, 5))
|
446 |
+
|
447 |
+
s = nx.star_graph(10)
|
448 |
+
assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10]
|
449 |
+
|
450 |
+
pytest.raises(nx.NetworkXError, nx.star_graph, 10, create_using=nx.DiGraph)
|
451 |
+
|
452 |
+
ms = nx.star_graph(10, create_using=nx.MultiGraph)
|
453 |
+
assert edges_equal(ms.edges(), s.edges())
|
454 |
+
|
455 |
+
G = nx.star_graph("abc")
|
456 |
+
assert len(G) == 3
|
457 |
+
assert G.size() == 2
|
458 |
+
|
459 |
+
G = nx.star_graph("abcb")
|
460 |
+
assert len(G) == 3
|
461 |
+
assert G.size() == 2
|
462 |
+
G = nx.star_graph("abcb", create_using=nx.MultiGraph)
|
463 |
+
assert len(G) == 3
|
464 |
+
assert G.size() == 3
|
465 |
+
|
466 |
+
G = nx.star_graph("abcdefg")
|
467 |
+
assert len(G) == 7
|
468 |
+
assert G.size() == 6
|
469 |
+
|
470 |
+
def test_non_int_integers_for_star_graph(self):
|
471 |
+
np = pytest.importorskip("numpy")
|
472 |
+
G = nx.star_graph(np.int32(3))
|
473 |
+
assert len(G) == 4
|
474 |
+
assert G.size() == 3
|
475 |
+
|
476 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 0), (3, 5), (4, 10), (3, 20)])
|
477 |
+
def test_tadpole_graph_right_sizes(self, m, n):
|
478 |
+
G = nx.tadpole_graph(m, n)
|
479 |
+
assert nx.number_of_nodes(G) == m + n
|
480 |
+
assert nx.number_of_edges(G) == m + n - (m == 2)
|
481 |
+
|
482 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("ab", "c"), ("abc", "defg")])
|
483 |
+
def test_tadpole_graph_size_node_sequences(self, m, n):
|
484 |
+
G = nx.tadpole_graph(m, n)
|
485 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
486 |
+
assert nx.number_of_edges(G) == len(m) + len(n) - (len(m) == 2)
|
487 |
+
|
488 |
+
def test_tadpole_graph_exceptions(self):
|
489 |
+
# Raise NetworkXError if m<2
|
490 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, -1, 3)
|
491 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 0, 3)
|
492 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 1, 3)
|
493 |
+
|
494 |
+
# Raise NetworkXError if n<0
|
495 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 5, -2)
|
496 |
+
|
497 |
+
# Raise NetworkXError for digraphs
|
498 |
+
with pytest.raises(nx.NetworkXError):
|
499 |
+
nx.tadpole_graph(2, 20, create_using=nx.DiGraph)
|
500 |
+
with pytest.raises(nx.NetworkXError):
|
501 |
+
nx.tadpole_graph(2, 20, create_using=nx.MultiDiGraph)
|
502 |
+
|
503 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
504 |
+
def test_tadpole_graph_same_as_path_when_m_is_2(self, m, n):
|
505 |
+
G = nx.tadpole_graph(m, n)
|
506 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
507 |
+
|
508 |
+
@pytest.mark.parametrize("m", [4, 7])
|
509 |
+
def test_tadpole_graph_same_as_cycle_when_m2_is_0(self, m):
|
510 |
+
G = nx.tadpole_graph(m, 0)
|
511 |
+
assert is_isomorphic(G, nx.cycle_graph(m))
|
512 |
+
|
513 |
+
def test_tadpole_graph_for_multigraph(self):
|
514 |
+
G = nx.tadpole_graph(5, 20)
|
515 |
+
MG = nx.tadpole_graph(5, 20, create_using=nx.MultiGraph)
|
516 |
+
assert edges_equal(MG.edges(), G.edges())
|
517 |
+
|
518 |
+
@pytest.mark.parametrize(
|
519 |
+
("m", "n"),
|
520 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
521 |
+
)
|
522 |
+
def test_tadpole_graph_mixing_input_types(self, m, n):
|
523 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
524 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
525 |
+
assert is_isomorphic(nx.tadpole_graph(m, n), expected)
|
526 |
+
|
527 |
+
def test_tadpole_graph_non_builtin_integers(self):
|
528 |
+
np = pytest.importorskip("numpy")
|
529 |
+
G = nx.tadpole_graph(np.int32(4), np.int64(3))
|
530 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
531 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
532 |
+
assert is_isomorphic(G, expected)
|
533 |
+
|
534 |
+
def test_trivial_graph(self):
|
535 |
+
assert nx.number_of_nodes(nx.trivial_graph()) == 1
|
536 |
+
|
537 |
+
def test_turan_graph(self):
|
538 |
+
assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63
|
539 |
+
assert is_isomorphic(
|
540 |
+
nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3)
|
541 |
+
)
|
542 |
+
|
543 |
+
def test_wheel_graph(self):
|
544 |
+
for n, G in [
|
545 |
+
("", nx.null_graph()),
|
546 |
+
(0, nx.null_graph()),
|
547 |
+
(1, nx.empty_graph(1)),
|
548 |
+
(2, nx.path_graph(2)),
|
549 |
+
(3, nx.complete_graph(3)),
|
550 |
+
(4, nx.complete_graph(4)),
|
551 |
+
]:
|
552 |
+
g = nx.wheel_graph(n)
|
553 |
+
assert is_isomorphic(g, G)
|
554 |
+
|
555 |
+
g = nx.wheel_graph(10)
|
556 |
+
assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]
|
557 |
+
|
558 |
+
pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph)
|
559 |
+
|
560 |
+
mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
|
561 |
+
assert edges_equal(mg.edges(), g.edges())
|
562 |
+
|
563 |
+
G = nx.wheel_graph("abc")
|
564 |
+
assert len(G) == 3
|
565 |
+
assert G.size() == 3
|
566 |
+
|
567 |
+
G = nx.wheel_graph("abcb")
|
568 |
+
assert len(G) == 3
|
569 |
+
assert G.size() == 4
|
570 |
+
G = nx.wheel_graph("abcb", nx.MultiGraph)
|
571 |
+
assert len(G) == 3
|
572 |
+
assert G.size() == 6
|
573 |
+
|
574 |
+
def test_non_int_integers_for_wheel_graph(self):
|
575 |
+
np = pytest.importorskip("numpy")
|
576 |
+
G = nx.wheel_graph(np.int32(3))
|
577 |
+
assert len(G) == 3
|
578 |
+
assert G.size() == 3
|
579 |
+
|
580 |
+
def test_complete_0_partite_graph(self):
|
581 |
+
"""Tests that the complete 0-partite graph is the null graph."""
|
582 |
+
G = nx.complete_multipartite_graph()
|
583 |
+
H = nx.null_graph()
|
584 |
+
assert nodes_equal(G, H)
|
585 |
+
assert edges_equal(G.edges(), H.edges())
|
586 |
+
|
587 |
+
def test_complete_1_partite_graph(self):
|
588 |
+
"""Tests that the complete 1-partite graph is the empty graph."""
|
589 |
+
G = nx.complete_multipartite_graph(3)
|
590 |
+
H = nx.empty_graph(3)
|
591 |
+
assert nodes_equal(G, H)
|
592 |
+
assert edges_equal(G.edges(), H.edges())
|
593 |
+
|
594 |
+
def test_complete_2_partite_graph(self):
|
595 |
+
"""Tests that the complete 2-partite graph is the complete bipartite
|
596 |
+
graph.
|
597 |
+
|
598 |
+
"""
|
599 |
+
G = nx.complete_multipartite_graph(2, 3)
|
600 |
+
H = nx.complete_bipartite_graph(2, 3)
|
601 |
+
assert nodes_equal(G, H)
|
602 |
+
assert edges_equal(G.edges(), H.edges())
|
603 |
+
|
604 |
+
def test_complete_multipartite_graph(self):
|
605 |
+
"""Tests for generating the complete multipartite graph."""
|
606 |
+
G = nx.complete_multipartite_graph(2, 3, 4)
|
607 |
+
blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)]
|
608 |
+
# Within each block, no two vertices should be adjacent.
|
609 |
+
for block in blocks:
|
610 |
+
for u, v in itertools.combinations_with_replacement(block, 2):
|
611 |
+
assert v not in G[u]
|
612 |
+
assert G.nodes[u] == G.nodes[v]
|
613 |
+
# Across blocks, all vertices should be adjacent.
|
614 |
+
for block1, block2 in itertools.combinations(blocks, 2):
|
615 |
+
for u, v in itertools.product(block1, block2):
|
616 |
+
assert v in G[u]
|
617 |
+
assert G.nodes[u] != G.nodes[v]
|
618 |
+
with pytest.raises(nx.NetworkXError, match="Negative number of nodes"):
|
619 |
+
nx.complete_multipartite_graph(2, -3, 4)
|
620 |
+
|
621 |
+
def test_kneser_graph(self):
|
622 |
+
# the petersen graph is a special case of the kneser graph when n=5 and k=2
|
623 |
+
assert is_isomorphic(nx.kneser_graph(5, 2), nx.petersen_graph())
|
624 |
+
|
625 |
+
# when k is 1, the kneser graph returns a complete graph with n vertices
|
626 |
+
for i in range(1, 7):
|
627 |
+
assert is_isomorphic(nx.kneser_graph(i, 1), nx.complete_graph(i))
|
628 |
+
|
629 |
+
# the kneser graph of n and n-1 is the empty graph with n vertices
|
630 |
+
for j in range(3, 7):
|
631 |
+
assert is_isomorphic(nx.kneser_graph(j, j - 1), nx.empty_graph(j))
|
632 |
+
|
633 |
+
# in general the number of edges of the kneser graph is equal to
|
634 |
+
# (n choose k) times (n-k choose k) divided by 2
|
635 |
+
assert nx.number_of_edges(nx.kneser_graph(8, 3)) == 280
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_cographs.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the :mod:`networkx.generators.cographs` module.
|
2 |
+
|
3 |
+
"""
|
4 |
+
|
5 |
+
import networkx as nx
|
6 |
+
|
7 |
+
|
8 |
+
def test_random_cograph():
|
9 |
+
n = 3
|
10 |
+
G = nx.random_cograph(n)
|
11 |
+
|
12 |
+
assert len(G) == 2**n
|
13 |
+
|
14 |
+
# Every connected subgraph of G has diameter <= 2
|
15 |
+
if nx.is_connected(G):
|
16 |
+
assert nx.diameter(G) <= 2
|
17 |
+
else:
|
18 |
+
components = nx.connected_components(G)
|
19 |
+
for component in components:
|
20 |
+
assert nx.diameter(G.subgraph(component)) <= 2
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_community.py
ADDED
@@ -0,0 +1,362 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import networkx as nx
|
4 |
+
|
5 |
+
|
6 |
+
def test_random_partition_graph():
|
7 |
+
G = nx.random_partition_graph([3, 3, 3], 1, 0, seed=42)
|
8 |
+
C = G.graph["partition"]
|
9 |
+
assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}]
|
10 |
+
assert len(G) == 9
|
11 |
+
assert len(list(G.edges())) == 9
|
12 |
+
|
13 |
+
G = nx.random_partition_graph([3, 3, 3], 0, 1)
|
14 |
+
C = G.graph["partition"]
|
15 |
+
assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}]
|
16 |
+
assert len(G) == 9
|
17 |
+
assert len(list(G.edges())) == 27
|
18 |
+
|
19 |
+
G = nx.random_partition_graph([3, 3, 3], 1, 0, directed=True)
|
20 |
+
C = G.graph["partition"]
|
21 |
+
assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}]
|
22 |
+
assert len(G) == 9
|
23 |
+
assert len(list(G.edges())) == 18
|
24 |
+
|
25 |
+
G = nx.random_partition_graph([3, 3, 3], 0, 1, directed=True)
|
26 |
+
C = G.graph["partition"]
|
27 |
+
assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}]
|
28 |
+
assert len(G) == 9
|
29 |
+
assert len(list(G.edges())) == 54
|
30 |
+
|
31 |
+
G = nx.random_partition_graph([1, 2, 3, 4, 5], 0.5, 0.1)
|
32 |
+
C = G.graph["partition"]
|
33 |
+
assert C == [{0}, {1, 2}, {3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13, 14}]
|
34 |
+
assert len(G) == 15
|
35 |
+
|
36 |
+
rpg = nx.random_partition_graph
|
37 |
+
pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 1.1, 0.1)
|
38 |
+
pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], -0.1, 0.1)
|
39 |
+
pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, 1.1)
|
40 |
+
pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, -0.1)
|
41 |
+
|
42 |
+
|
43 |
+
def test_planted_partition_graph():
|
44 |
+
G = nx.planted_partition_graph(4, 3, 1, 0, seed=42)
|
45 |
+
C = G.graph["partition"]
|
46 |
+
assert len(C) == 4
|
47 |
+
assert len(G) == 12
|
48 |
+
assert len(list(G.edges())) == 12
|
49 |
+
|
50 |
+
G = nx.planted_partition_graph(4, 3, 0, 1)
|
51 |
+
C = G.graph["partition"]
|
52 |
+
assert len(C) == 4
|
53 |
+
assert len(G) == 12
|
54 |
+
assert len(list(G.edges())) == 54
|
55 |
+
|
56 |
+
G = nx.planted_partition_graph(10, 4, 0.5, 0.1, seed=42)
|
57 |
+
C = G.graph["partition"]
|
58 |
+
assert len(C) == 10
|
59 |
+
assert len(G) == 40
|
60 |
+
|
61 |
+
G = nx.planted_partition_graph(4, 3, 1, 0, directed=True)
|
62 |
+
C = G.graph["partition"]
|
63 |
+
assert len(C) == 4
|
64 |
+
assert len(G) == 12
|
65 |
+
assert len(list(G.edges())) == 24
|
66 |
+
|
67 |
+
G = nx.planted_partition_graph(4, 3, 0, 1, directed=True)
|
68 |
+
C = G.graph["partition"]
|
69 |
+
assert len(C) == 4
|
70 |
+
assert len(G) == 12
|
71 |
+
assert len(list(G.edges())) == 108
|
72 |
+
|
73 |
+
G = nx.planted_partition_graph(10, 4, 0.5, 0.1, seed=42, directed=True)
|
74 |
+
C = G.graph["partition"]
|
75 |
+
assert len(C) == 10
|
76 |
+
assert len(G) == 40
|
77 |
+
|
78 |
+
ppg = nx.planted_partition_graph
|
79 |
+
pytest.raises(nx.NetworkXError, ppg, 3, 3, 1.1, 0.1)
|
80 |
+
pytest.raises(nx.NetworkXError, ppg, 3, 3, -0.1, 0.1)
|
81 |
+
pytest.raises(nx.NetworkXError, ppg, 3, 3, 0.1, 1.1)
|
82 |
+
pytest.raises(nx.NetworkXError, ppg, 3, 3, 0.1, -0.1)
|
83 |
+
|
84 |
+
|
85 |
+
def test_relaxed_caveman_graph():
|
86 |
+
G = nx.relaxed_caveman_graph(4, 3, 0)
|
87 |
+
assert len(G) == 12
|
88 |
+
G = nx.relaxed_caveman_graph(4, 3, 1)
|
89 |
+
assert len(G) == 12
|
90 |
+
G = nx.relaxed_caveman_graph(4, 3, 0.5)
|
91 |
+
assert len(G) == 12
|
92 |
+
G = nx.relaxed_caveman_graph(4, 3, 0.5, seed=42)
|
93 |
+
assert len(G) == 12
|
94 |
+
|
95 |
+
|
96 |
+
def test_connected_caveman_graph():
|
97 |
+
G = nx.connected_caveman_graph(4, 3)
|
98 |
+
assert len(G) == 12
|
99 |
+
|
100 |
+
G = nx.connected_caveman_graph(1, 5)
|
101 |
+
K5 = nx.complete_graph(5)
|
102 |
+
K5.remove_edge(3, 4)
|
103 |
+
assert nx.is_isomorphic(G, K5)
|
104 |
+
|
105 |
+
# need at least 2 nodes in each clique
|
106 |
+
pytest.raises(nx.NetworkXError, nx.connected_caveman_graph, 4, 1)
|
107 |
+
|
108 |
+
|
109 |
+
def test_caveman_graph():
|
110 |
+
G = nx.caveman_graph(4, 3)
|
111 |
+
assert len(G) == 12
|
112 |
+
|
113 |
+
G = nx.caveman_graph(5, 1)
|
114 |
+
E5 = nx.empty_graph(5)
|
115 |
+
assert nx.is_isomorphic(G, E5)
|
116 |
+
|
117 |
+
G = nx.caveman_graph(1, 5)
|
118 |
+
K5 = nx.complete_graph(5)
|
119 |
+
assert nx.is_isomorphic(G, K5)
|
120 |
+
|
121 |
+
|
122 |
+
def test_gaussian_random_partition_graph():
|
123 |
+
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01)
|
124 |
+
assert len(G) == 100
|
125 |
+
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01, directed=True)
|
126 |
+
assert len(G) == 100
|
127 |
+
G = nx.gaussian_random_partition_graph(
|
128 |
+
100, 10, 10, 0.3, 0.01, directed=False, seed=42
|
129 |
+
)
|
130 |
+
assert len(G) == 100
|
131 |
+
assert not isinstance(G, nx.DiGraph)
|
132 |
+
G = nx.gaussian_random_partition_graph(
|
133 |
+
100, 10, 10, 0.3, 0.01, directed=True, seed=42
|
134 |
+
)
|
135 |
+
assert len(G) == 100
|
136 |
+
assert isinstance(G, nx.DiGraph)
|
137 |
+
pytest.raises(
|
138 |
+
nx.NetworkXError, nx.gaussian_random_partition_graph, 100, 101, 10, 1, 0
|
139 |
+
)
|
140 |
+
# Test when clusters are likely less than 1
|
141 |
+
G = nx.gaussian_random_partition_graph(10, 0.5, 0.5, 0.5, 0.5, seed=1)
|
142 |
+
assert len(G) == 10
|
143 |
+
|
144 |
+
|
145 |
+
def test_ring_of_cliques():
|
146 |
+
for i in range(2, 20, 3):
|
147 |
+
for j in range(2, 20, 3):
|
148 |
+
G = nx.ring_of_cliques(i, j)
|
149 |
+
assert G.number_of_nodes() == i * j
|
150 |
+
if i != 2 or j != 1:
|
151 |
+
expected_num_edges = i * (((j * (j - 1)) // 2) + 1)
|
152 |
+
else:
|
153 |
+
# the edge that already exists cannot be duplicated
|
154 |
+
expected_num_edges = i * (((j * (j - 1)) // 2) + 1) - 1
|
155 |
+
assert G.number_of_edges() == expected_num_edges
|
156 |
+
with pytest.raises(
|
157 |
+
nx.NetworkXError, match="A ring of cliques must have at least two cliques"
|
158 |
+
):
|
159 |
+
nx.ring_of_cliques(1, 5)
|
160 |
+
with pytest.raises(
|
161 |
+
nx.NetworkXError, match="The cliques must have at least two nodes"
|
162 |
+
):
|
163 |
+
nx.ring_of_cliques(3, 0)
|
164 |
+
|
165 |
+
|
166 |
+
def test_windmill_graph():
|
167 |
+
for n in range(2, 20, 3):
|
168 |
+
for k in range(2, 20, 3):
|
169 |
+
G = nx.windmill_graph(n, k)
|
170 |
+
assert G.number_of_nodes() == (k - 1) * n + 1
|
171 |
+
assert G.number_of_edges() == n * k * (k - 1) / 2
|
172 |
+
assert G.degree(0) == G.number_of_nodes() - 1
|
173 |
+
for i in range(1, G.number_of_nodes()):
|
174 |
+
assert G.degree(i) == k - 1
|
175 |
+
with pytest.raises(
|
176 |
+
nx.NetworkXError, match="A windmill graph must have at least two cliques"
|
177 |
+
):
|
178 |
+
nx.windmill_graph(1, 3)
|
179 |
+
with pytest.raises(
|
180 |
+
nx.NetworkXError, match="The cliques must have at least two nodes"
|
181 |
+
):
|
182 |
+
nx.windmill_graph(3, 0)
|
183 |
+
|
184 |
+
|
185 |
+
def test_stochastic_block_model():
|
186 |
+
sizes = [75, 75, 300]
|
187 |
+
probs = [[0.25, 0.05, 0.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]]
|
188 |
+
G = nx.stochastic_block_model(sizes, probs, seed=0)
|
189 |
+
C = G.graph["partition"]
|
190 |
+
assert len(C) == 3
|
191 |
+
assert len(G) == 450
|
192 |
+
assert G.size() == 22160
|
193 |
+
|
194 |
+
GG = nx.stochastic_block_model(sizes, probs, range(450), seed=0)
|
195 |
+
assert G.nodes == GG.nodes
|
196 |
+
|
197 |
+
# Test Exceptions
|
198 |
+
sbm = nx.stochastic_block_model
|
199 |
+
badnodelist = list(range(400)) # not enough nodes to match sizes
|
200 |
+
badprobs1 = [[0.25, 0.05, 1.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]]
|
201 |
+
badprobs2 = [[0.25, 0.05, 0.02], [0.05, -0.35, 0.07], [0.02, 0.07, 0.40]]
|
202 |
+
probs_rect1 = [[0.25, 0.05, 0.02], [0.05, -0.35, 0.07]]
|
203 |
+
probs_rect2 = [[0.25, 0.05], [0.05, -0.35], [0.02, 0.07]]
|
204 |
+
asymprobs = [[0.25, 0.05, 0.01], [0.05, -0.35, 0.07], [0.02, 0.07, 0.40]]
|
205 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, badprobs1)
|
206 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, badprobs2)
|
207 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, probs_rect1, directed=True)
|
208 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, probs_rect2, directed=True)
|
209 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, asymprobs, directed=False)
|
210 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, probs, badnodelist)
|
211 |
+
nodelist = [0] + list(range(449)) # repeated node name in nodelist
|
212 |
+
pytest.raises(nx.NetworkXException, sbm, sizes, probs, nodelist)
|
213 |
+
|
214 |
+
# Extra keyword arguments test
|
215 |
+
GG = nx.stochastic_block_model(sizes, probs, seed=0, selfloops=True)
|
216 |
+
assert G.nodes == GG.nodes
|
217 |
+
GG = nx.stochastic_block_model(sizes, probs, selfloops=True, directed=True)
|
218 |
+
assert G.nodes == GG.nodes
|
219 |
+
GG = nx.stochastic_block_model(sizes, probs, seed=0, sparse=False)
|
220 |
+
assert G.nodes == GG.nodes
|
221 |
+
|
222 |
+
|
223 |
+
def test_generator():
|
224 |
+
n = 250
|
225 |
+
tau1 = 3
|
226 |
+
tau2 = 1.5
|
227 |
+
mu = 0.1
|
228 |
+
G = nx.LFR_benchmark_graph(
|
229 |
+
n, tau1, tau2, mu, average_degree=5, min_community=20, seed=10
|
230 |
+
)
|
231 |
+
assert len(G) == 250
|
232 |
+
C = {frozenset(G.nodes[v]["community"]) for v in G}
|
233 |
+
assert nx.community.is_partition(G.nodes(), C)
|
234 |
+
|
235 |
+
|
236 |
+
def test_invalid_tau1():
|
237 |
+
with pytest.raises(nx.NetworkXError, match="tau2 must be greater than one"):
|
238 |
+
n = 100
|
239 |
+
tau1 = 2
|
240 |
+
tau2 = 1
|
241 |
+
mu = 0.1
|
242 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
|
243 |
+
|
244 |
+
|
245 |
+
def test_invalid_tau2():
|
246 |
+
with pytest.raises(nx.NetworkXError, match="tau1 must be greater than one"):
|
247 |
+
n = 100
|
248 |
+
tau1 = 1
|
249 |
+
tau2 = 2
|
250 |
+
mu = 0.1
|
251 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
|
252 |
+
|
253 |
+
|
254 |
+
def test_mu_too_large():
|
255 |
+
with pytest.raises(nx.NetworkXError, match="mu must be in the interval \\[0, 1\\]"):
|
256 |
+
n = 100
|
257 |
+
tau1 = 2
|
258 |
+
tau2 = 2
|
259 |
+
mu = 1.1
|
260 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
|
261 |
+
|
262 |
+
|
263 |
+
def test_mu_too_small():
|
264 |
+
with pytest.raises(nx.NetworkXError, match="mu must be in the interval \\[0, 1\\]"):
|
265 |
+
n = 100
|
266 |
+
tau1 = 2
|
267 |
+
tau2 = 2
|
268 |
+
mu = -1
|
269 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)
|
270 |
+
|
271 |
+
|
272 |
+
def test_both_degrees_none():
|
273 |
+
with pytest.raises(
|
274 |
+
nx.NetworkXError,
|
275 |
+
match="Must assign exactly one of min_degree and average_degree",
|
276 |
+
):
|
277 |
+
n = 100
|
278 |
+
tau1 = 2
|
279 |
+
tau2 = 2
|
280 |
+
mu = 1
|
281 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu)
|
282 |
+
|
283 |
+
|
284 |
+
def test_neither_degrees_none():
|
285 |
+
with pytest.raises(
|
286 |
+
nx.NetworkXError,
|
287 |
+
match="Must assign exactly one of min_degree and average_degree",
|
288 |
+
):
|
289 |
+
n = 100
|
290 |
+
tau1 = 2
|
291 |
+
tau2 = 2
|
292 |
+
mu = 1
|
293 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, average_degree=5)
|
294 |
+
|
295 |
+
|
296 |
+
def test_max_iters_exceeded():
|
297 |
+
with pytest.raises(
|
298 |
+
nx.ExceededMaxIterations,
|
299 |
+
match="Could not assign communities; try increasing min_community",
|
300 |
+
):
|
301 |
+
n = 10
|
302 |
+
tau1 = 2
|
303 |
+
tau2 = 2
|
304 |
+
mu = 0.1
|
305 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, max_iters=10, seed=1)
|
306 |
+
|
307 |
+
|
308 |
+
def test_max_deg_out_of_range():
|
309 |
+
with pytest.raises(
|
310 |
+
nx.NetworkXError, match="max_degree must be in the interval \\(0, n\\]"
|
311 |
+
):
|
312 |
+
n = 10
|
313 |
+
tau1 = 2
|
314 |
+
tau2 = 2
|
315 |
+
mu = 0.1
|
316 |
+
nx.LFR_benchmark_graph(
|
317 |
+
n, tau1, tau2, mu, max_degree=n + 1, max_iters=10, seed=1
|
318 |
+
)
|
319 |
+
|
320 |
+
|
321 |
+
def test_max_community():
|
322 |
+
n = 250
|
323 |
+
tau1 = 3
|
324 |
+
tau2 = 1.5
|
325 |
+
mu = 0.1
|
326 |
+
G = nx.LFR_benchmark_graph(
|
327 |
+
n,
|
328 |
+
tau1,
|
329 |
+
tau2,
|
330 |
+
mu,
|
331 |
+
average_degree=5,
|
332 |
+
max_degree=100,
|
333 |
+
min_community=50,
|
334 |
+
max_community=200,
|
335 |
+
seed=10,
|
336 |
+
)
|
337 |
+
assert len(G) == 250
|
338 |
+
C = {frozenset(G.nodes[v]["community"]) for v in G}
|
339 |
+
assert nx.community.is_partition(G.nodes(), C)
|
340 |
+
|
341 |
+
|
342 |
+
def test_powerlaw_iterations_exceeded():
|
343 |
+
with pytest.raises(
|
344 |
+
nx.ExceededMaxIterations, match="Could not create power law sequence"
|
345 |
+
):
|
346 |
+
n = 100
|
347 |
+
tau1 = 2
|
348 |
+
tau2 = 2
|
349 |
+
mu = 1
|
350 |
+
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, max_iters=0)
|
351 |
+
|
352 |
+
|
353 |
+
def test_no_scipy_zeta():
|
354 |
+
zeta2 = 1.6449340668482264
|
355 |
+
assert abs(zeta2 - nx.generators.community._hurwitz_zeta(2, 1, 0.0001)) < 0.01
|
356 |
+
|
357 |
+
|
358 |
+
def test_generate_min_degree_itr():
|
359 |
+
with pytest.raises(
|
360 |
+
nx.ExceededMaxIterations, match="Could not match average_degree"
|
361 |
+
):
|
362 |
+
nx.generators.community._generate_min_degree(2, 2, 1, 0.01, 0)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_degree_seq.py
ADDED
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import networkx as nx
|
4 |
+
|
5 |
+
|
6 |
+
class TestConfigurationModel:
|
7 |
+
"""Unit tests for the :func:`~networkx.configuration_model`
|
8 |
+
function.
|
9 |
+
|
10 |
+
"""
|
11 |
+
|
12 |
+
def test_empty_degree_sequence(self):
|
13 |
+
"""Tests that an empty degree sequence yields the null graph."""
|
14 |
+
G = nx.configuration_model([])
|
15 |
+
assert len(G) == 0
|
16 |
+
|
17 |
+
def test_degree_zero(self):
|
18 |
+
"""Tests that a degree sequence of all zeros yields the empty
|
19 |
+
graph.
|
20 |
+
|
21 |
+
"""
|
22 |
+
G = nx.configuration_model([0, 0, 0])
|
23 |
+
assert len(G) == 3
|
24 |
+
assert G.number_of_edges() == 0
|
25 |
+
|
26 |
+
def test_degree_sequence(self):
|
27 |
+
"""Tests that the degree sequence of the generated graph matches
|
28 |
+
the input degree sequence.
|
29 |
+
|
30 |
+
"""
|
31 |
+
deg_seq = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
32 |
+
G = nx.configuration_model(deg_seq, seed=12345678)
|
33 |
+
assert sorted((d for n, d in G.degree()), reverse=True) == [
|
34 |
+
5,
|
35 |
+
3,
|
36 |
+
3,
|
37 |
+
3,
|
38 |
+
3,
|
39 |
+
2,
|
40 |
+
2,
|
41 |
+
2,
|
42 |
+
1,
|
43 |
+
1,
|
44 |
+
1,
|
45 |
+
]
|
46 |
+
assert sorted((d for n, d in G.degree(range(len(deg_seq)))), reverse=True) == [
|
47 |
+
5,
|
48 |
+
3,
|
49 |
+
3,
|
50 |
+
3,
|
51 |
+
3,
|
52 |
+
2,
|
53 |
+
2,
|
54 |
+
2,
|
55 |
+
1,
|
56 |
+
1,
|
57 |
+
1,
|
58 |
+
]
|
59 |
+
|
60 |
+
def test_random_seed(self):
|
61 |
+
"""Tests that each call with the same random seed generates the
|
62 |
+
same graph.
|
63 |
+
|
64 |
+
"""
|
65 |
+
deg_seq = [3] * 12
|
66 |
+
G1 = nx.configuration_model(deg_seq, seed=1000)
|
67 |
+
G2 = nx.configuration_model(deg_seq, seed=1000)
|
68 |
+
assert nx.is_isomorphic(G1, G2)
|
69 |
+
G1 = nx.configuration_model(deg_seq, seed=10)
|
70 |
+
G2 = nx.configuration_model(deg_seq, seed=10)
|
71 |
+
assert nx.is_isomorphic(G1, G2)
|
72 |
+
|
73 |
+
def test_directed_disallowed(self):
|
74 |
+
"""Tests that attempting to create a configuration model graph
|
75 |
+
using a directed graph yields an exception.
|
76 |
+
|
77 |
+
"""
|
78 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
79 |
+
nx.configuration_model([], create_using=nx.DiGraph())
|
80 |
+
|
81 |
+
def test_odd_degree_sum(self):
|
82 |
+
"""Tests that a degree sequence whose sum is odd yields an
|
83 |
+
exception.
|
84 |
+
|
85 |
+
"""
|
86 |
+
with pytest.raises(nx.NetworkXError):
|
87 |
+
nx.configuration_model([1, 2])
|
88 |
+
|
89 |
+
|
90 |
+
def test_directed_configuration_raise_unequal():
|
91 |
+
with pytest.raises(nx.NetworkXError):
|
92 |
+
zin = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1]
|
93 |
+
zout = [5, 3, 3, 3, 3, 2, 2, 2, 1, 2]
|
94 |
+
nx.directed_configuration_model(zin, zout)
|
95 |
+
|
96 |
+
|
97 |
+
def test_directed_configuration_model():
|
98 |
+
G = nx.directed_configuration_model([], [], seed=0)
|
99 |
+
assert len(G) == 0
|
100 |
+
|
101 |
+
|
102 |
+
def test_simple_directed_configuration_model():
|
103 |
+
G = nx.directed_configuration_model([1, 1], [1, 1], seed=0)
|
104 |
+
assert len(G) == 2
|
105 |
+
|
106 |
+
|
107 |
+
def test_expected_degree_graph_empty():
|
108 |
+
# empty graph has empty degree sequence
|
109 |
+
deg_seq = []
|
110 |
+
G = nx.expected_degree_graph(deg_seq)
|
111 |
+
assert dict(G.degree()) == {}
|
112 |
+
|
113 |
+
|
114 |
+
def test_expected_degree_graph():
|
115 |
+
# test that fixed seed delivers the same graph
|
116 |
+
deg_seq = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
|
117 |
+
G1 = nx.expected_degree_graph(deg_seq, seed=1000)
|
118 |
+
assert len(G1) == 12
|
119 |
+
|
120 |
+
G2 = nx.expected_degree_graph(deg_seq, seed=1000)
|
121 |
+
assert nx.is_isomorphic(G1, G2)
|
122 |
+
|
123 |
+
G1 = nx.expected_degree_graph(deg_seq, seed=10)
|
124 |
+
G2 = nx.expected_degree_graph(deg_seq, seed=10)
|
125 |
+
assert nx.is_isomorphic(G1, G2)
|
126 |
+
|
127 |
+
|
128 |
+
def test_expected_degree_graph_selfloops():
|
129 |
+
deg_seq = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
|
130 |
+
G1 = nx.expected_degree_graph(deg_seq, seed=1000, selfloops=False)
|
131 |
+
G2 = nx.expected_degree_graph(deg_seq, seed=1000, selfloops=False)
|
132 |
+
assert nx.is_isomorphic(G1, G2)
|
133 |
+
assert len(G1) == 12
|
134 |
+
|
135 |
+
|
136 |
+
def test_expected_degree_graph_skew():
|
137 |
+
deg_seq = [10, 2, 2, 2, 2]
|
138 |
+
G1 = nx.expected_degree_graph(deg_seq, seed=1000)
|
139 |
+
G2 = nx.expected_degree_graph(deg_seq, seed=1000)
|
140 |
+
assert nx.is_isomorphic(G1, G2)
|
141 |
+
assert len(G1) == 5
|
142 |
+
|
143 |
+
|
144 |
+
def test_havel_hakimi_construction():
|
145 |
+
G = nx.havel_hakimi_graph([])
|
146 |
+
assert len(G) == 0
|
147 |
+
|
148 |
+
z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
149 |
+
pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
|
150 |
+
z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
151 |
+
pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
|
152 |
+
|
153 |
+
z = [5, 4, 3, 3, 3, 2, 2, 2]
|
154 |
+
G = nx.havel_hakimi_graph(z)
|
155 |
+
G = nx.configuration_model(z)
|
156 |
+
z = [6, 5, 4, 4, 2, 1, 1, 1]
|
157 |
+
pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z)
|
158 |
+
|
159 |
+
z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
|
160 |
+
|
161 |
+
G = nx.havel_hakimi_graph(z)
|
162 |
+
|
163 |
+
pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z, create_using=nx.DiGraph())
|
164 |
+
|
165 |
+
|
166 |
+
def test_directed_havel_hakimi():
|
167 |
+
# Test range of valid directed degree sequences
|
168 |
+
n, r = 100, 10
|
169 |
+
p = 1.0 / r
|
170 |
+
for i in range(r):
|
171 |
+
G1 = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
|
172 |
+
din1 = [d for n, d in G1.in_degree()]
|
173 |
+
dout1 = [d for n, d in G1.out_degree()]
|
174 |
+
G2 = nx.directed_havel_hakimi_graph(din1, dout1)
|
175 |
+
din2 = [d for n, d in G2.in_degree()]
|
176 |
+
dout2 = [d for n, d in G2.out_degree()]
|
177 |
+
assert sorted(din1) == sorted(din2)
|
178 |
+
assert sorted(dout1) == sorted(dout2)
|
179 |
+
|
180 |
+
# Test non-graphical sequence
|
181 |
+
dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
182 |
+
din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
|
183 |
+
pytest.raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph, din, dout)
|
184 |
+
# Test valid sequences
|
185 |
+
dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
186 |
+
din = [2, 2, 2, 2, 2, 2, 2, 2, 0, 2]
|
187 |
+
G2 = nx.directed_havel_hakimi_graph(din, dout)
|
188 |
+
dout2 = (d for n, d in G2.out_degree())
|
189 |
+
din2 = (d for n, d in G2.in_degree())
|
190 |
+
assert sorted(dout) == sorted(dout2)
|
191 |
+
assert sorted(din) == sorted(din2)
|
192 |
+
# Test unequal sums
|
193 |
+
din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
|
194 |
+
pytest.raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph, din, dout)
|
195 |
+
# Test for negative values
|
196 |
+
din = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -2]
|
197 |
+
pytest.raises(nx.exception.NetworkXError, nx.directed_havel_hakimi_graph, din, dout)
|
198 |
+
|
199 |
+
|
200 |
+
def test_degree_sequence_tree():
|
201 |
+
z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
202 |
+
G = nx.degree_sequence_tree(z)
|
203 |
+
assert len(G) == len(z)
|
204 |
+
assert len(list(G.edges())) == sum(z) / 2
|
205 |
+
|
206 |
+
pytest.raises(
|
207 |
+
nx.NetworkXError, nx.degree_sequence_tree, z, create_using=nx.DiGraph()
|
208 |
+
)
|
209 |
+
|
210 |
+
z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
211 |
+
pytest.raises(nx.NetworkXError, nx.degree_sequence_tree, z)
|
212 |
+
|
213 |
+
|
214 |
+
def test_random_degree_sequence_graph():
|
215 |
+
d = [1, 2, 2, 3]
|
216 |
+
G = nx.random_degree_sequence_graph(d, seed=42)
|
217 |
+
assert d == sorted(d for n, d in G.degree())
|
218 |
+
|
219 |
+
|
220 |
+
def test_random_degree_sequence_graph_raise():
|
221 |
+
z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
222 |
+
pytest.raises(nx.NetworkXUnfeasible, nx.random_degree_sequence_graph, z)
|
223 |
+
|
224 |
+
|
225 |
+
def test_random_degree_sequence_large():
|
226 |
+
G1 = nx.fast_gnp_random_graph(100, 0.1, seed=42)
|
227 |
+
d1 = (d for n, d in G1.degree())
|
228 |
+
G2 = nx.random_degree_sequence_graph(d1, seed=42)
|
229 |
+
d2 = (d for n, d in G2.degree())
|
230 |
+
assert sorted(d1) == sorted(d2)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_directed.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Generators - Directed Graphs
|
2 |
+
----------------------------
|
3 |
+
"""
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import networkx as nx
|
7 |
+
from networkx.classes import Graph, MultiDiGraph
|
8 |
+
from networkx.generators.directed import (
|
9 |
+
gn_graph,
|
10 |
+
gnc_graph,
|
11 |
+
gnr_graph,
|
12 |
+
random_k_out_graph,
|
13 |
+
random_uniform_k_out_graph,
|
14 |
+
scale_free_graph,
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
class TestGeneratorsDirected:
|
19 |
+
def test_smoke_test_random_graphs(self):
|
20 |
+
gn_graph(100)
|
21 |
+
gnr_graph(100, 0.5)
|
22 |
+
gnc_graph(100)
|
23 |
+
scale_free_graph(100)
|
24 |
+
|
25 |
+
gn_graph(100, seed=42)
|
26 |
+
gnr_graph(100, 0.5, seed=42)
|
27 |
+
gnc_graph(100, seed=42)
|
28 |
+
scale_free_graph(100, seed=42)
|
29 |
+
|
30 |
+
def test_create_using_keyword_arguments(self):
|
31 |
+
pytest.raises(nx.NetworkXError, gn_graph, 100, create_using=Graph())
|
32 |
+
pytest.raises(nx.NetworkXError, gnr_graph, 100, 0.5, create_using=Graph())
|
33 |
+
pytest.raises(nx.NetworkXError, gnc_graph, 100, create_using=Graph())
|
34 |
+
G = gn_graph(100, seed=1)
|
35 |
+
MG = gn_graph(100, create_using=MultiDiGraph(), seed=1)
|
36 |
+
assert sorted(G.edges()) == sorted(MG.edges())
|
37 |
+
G = gnr_graph(100, 0.5, seed=1)
|
38 |
+
MG = gnr_graph(100, 0.5, create_using=MultiDiGraph(), seed=1)
|
39 |
+
assert sorted(G.edges()) == sorted(MG.edges())
|
40 |
+
G = gnc_graph(100, seed=1)
|
41 |
+
MG = gnc_graph(100, create_using=MultiDiGraph(), seed=1)
|
42 |
+
assert sorted(G.edges()) == sorted(MG.edges())
|
43 |
+
|
44 |
+
G = scale_free_graph(
|
45 |
+
100,
|
46 |
+
alpha=0.3,
|
47 |
+
beta=0.4,
|
48 |
+
gamma=0.3,
|
49 |
+
delta_in=0.3,
|
50 |
+
delta_out=0.1,
|
51 |
+
initial_graph=nx.cycle_graph(4, create_using=MultiDiGraph),
|
52 |
+
seed=1,
|
53 |
+
)
|
54 |
+
pytest.raises(ValueError, scale_free_graph, 100, 0.5, 0.4, 0.3)
|
55 |
+
pytest.raises(ValueError, scale_free_graph, 100, alpha=-0.3)
|
56 |
+
pytest.raises(ValueError, scale_free_graph, 100, beta=-0.3)
|
57 |
+
pytest.raises(ValueError, scale_free_graph, 100, gamma=-0.3)
|
58 |
+
|
59 |
+
def test_parameters(self):
|
60 |
+
G = nx.DiGraph()
|
61 |
+
G.add_node(0)
|
62 |
+
|
63 |
+
def kernel(x):
|
64 |
+
return x
|
65 |
+
|
66 |
+
assert nx.is_isomorphic(gn_graph(1), G)
|
67 |
+
assert nx.is_isomorphic(gn_graph(1, kernel=kernel), G)
|
68 |
+
assert nx.is_isomorphic(gnc_graph(1), G)
|
69 |
+
assert nx.is_isomorphic(gnr_graph(1, 0.5), G)
|
70 |
+
|
71 |
+
|
72 |
+
def test_scale_free_graph_negative_delta():
|
73 |
+
with pytest.raises(ValueError, match="delta_in must be >= 0."):
|
74 |
+
scale_free_graph(10, delta_in=-1)
|
75 |
+
with pytest.raises(ValueError, match="delta_out must be >= 0."):
|
76 |
+
scale_free_graph(10, delta_out=-1)
|
77 |
+
|
78 |
+
|
79 |
+
def test_non_numeric_ordering():
|
80 |
+
G = MultiDiGraph([("a", "b"), ("b", "c"), ("c", "a")])
|
81 |
+
s = scale_free_graph(3, initial_graph=G)
|
82 |
+
assert len(s) == 3
|
83 |
+
assert len(s.edges) == 3
|
84 |
+
|
85 |
+
|
86 |
+
@pytest.mark.parametrize("ig", (nx.Graph(), nx.DiGraph([(0, 1)])))
|
87 |
+
def test_scale_free_graph_initial_graph_kwarg(ig):
|
88 |
+
with pytest.raises(nx.NetworkXError):
|
89 |
+
scale_free_graph(100, initial_graph=ig)
|
90 |
+
|
91 |
+
|
92 |
+
class TestRandomKOutGraph:
|
93 |
+
"""Unit tests for the
|
94 |
+
:func:`~networkx.generators.directed.random_k_out_graph` function.
|
95 |
+
|
96 |
+
"""
|
97 |
+
|
98 |
+
def test_regularity(self):
|
99 |
+
"""Tests that the generated graph is `k`-out-regular."""
|
100 |
+
n = 10
|
101 |
+
k = 3
|
102 |
+
alpha = 1
|
103 |
+
G = random_k_out_graph(n, k, alpha)
|
104 |
+
assert all(d == k for v, d in G.out_degree())
|
105 |
+
G = random_k_out_graph(n, k, alpha, seed=42)
|
106 |
+
assert all(d == k for v, d in G.out_degree())
|
107 |
+
|
108 |
+
def test_no_self_loops(self):
|
109 |
+
"""Tests for forbidding self-loops."""
|
110 |
+
n = 10
|
111 |
+
k = 3
|
112 |
+
alpha = 1
|
113 |
+
G = random_k_out_graph(n, k, alpha, self_loops=False)
|
114 |
+
assert nx.number_of_selfloops(G) == 0
|
115 |
+
|
116 |
+
def test_negative_alpha(self):
|
117 |
+
with pytest.raises(ValueError, match="alpha must be positive"):
|
118 |
+
random_k_out_graph(10, 3, -1)
|
119 |
+
|
120 |
+
|
121 |
+
class TestUniformRandomKOutGraph:
|
122 |
+
"""Unit tests for the
|
123 |
+
:func:`~networkx.generators.directed.random_uniform_k_out_graph`
|
124 |
+
function.
|
125 |
+
|
126 |
+
"""
|
127 |
+
|
128 |
+
def test_regularity(self):
|
129 |
+
"""Tests that the generated graph is `k`-out-regular."""
|
130 |
+
n = 10
|
131 |
+
k = 3
|
132 |
+
G = random_uniform_k_out_graph(n, k)
|
133 |
+
assert all(d == k for v, d in G.out_degree())
|
134 |
+
G = random_uniform_k_out_graph(n, k, seed=42)
|
135 |
+
assert all(d == k for v, d in G.out_degree())
|
136 |
+
|
137 |
+
def test_no_self_loops(self):
|
138 |
+
"""Tests for forbidding self-loops."""
|
139 |
+
n = 10
|
140 |
+
k = 3
|
141 |
+
G = random_uniform_k_out_graph(n, k, self_loops=False)
|
142 |
+
assert nx.number_of_selfloops(G) == 0
|
143 |
+
assert all(d == k for v, d in G.out_degree())
|
144 |
+
|
145 |
+
def test_with_replacement(self):
|
146 |
+
n = 10
|
147 |
+
k = 3
|
148 |
+
G = random_uniform_k_out_graph(n, k, with_replacement=True)
|
149 |
+
assert G.is_multigraph()
|
150 |
+
assert all(d == k for v, d in G.out_degree())
|
151 |
+
n = 10
|
152 |
+
k = 9
|
153 |
+
G = random_uniform_k_out_graph(n, k, with_replacement=False, self_loops=False)
|
154 |
+
assert nx.number_of_selfloops(G) == 0
|
155 |
+
assert all(d == k for v, d in G.out_degree())
|
156 |
+
|
157 |
+
def test_without_replacement(self):
|
158 |
+
n = 10
|
159 |
+
k = 3
|
160 |
+
G = random_uniform_k_out_graph(n, k, with_replacement=False)
|
161 |
+
assert not G.is_multigraph()
|
162 |
+
assert all(d == k for v, d in G.out_degree())
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_duplication.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the :mod:`networkx.generators.duplication` module.
|
2 |
+
|
3 |
+
"""
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from networkx.exception import NetworkXError
|
7 |
+
from networkx.generators.duplication import (
|
8 |
+
duplication_divergence_graph,
|
9 |
+
partial_duplication_graph,
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
class TestDuplicationDivergenceGraph:
|
14 |
+
"""Unit tests for the
|
15 |
+
:func:`networkx.generators.duplication.duplication_divergence_graph`
|
16 |
+
function.
|
17 |
+
|
18 |
+
"""
|
19 |
+
|
20 |
+
def test_final_size(self):
|
21 |
+
G = duplication_divergence_graph(3, p=1)
|
22 |
+
assert len(G) == 3
|
23 |
+
G = duplication_divergence_graph(3, p=1, seed=42)
|
24 |
+
assert len(G) == 3
|
25 |
+
|
26 |
+
def test_probability_too_large(self):
|
27 |
+
with pytest.raises(NetworkXError):
|
28 |
+
duplication_divergence_graph(3, p=2)
|
29 |
+
|
30 |
+
def test_probability_too_small(self):
|
31 |
+
with pytest.raises(NetworkXError):
|
32 |
+
duplication_divergence_graph(3, p=-1)
|
33 |
+
|
34 |
+
def test_non_extreme_probability_value(self):
|
35 |
+
G = duplication_divergence_graph(6, p=0.3, seed=42)
|
36 |
+
assert len(G) == 6
|
37 |
+
assert list(G.degree()) == [(0, 2), (1, 3), (2, 2), (3, 3), (4, 1), (5, 1)]
|
38 |
+
|
39 |
+
def test_minimum_desired_nodes(self):
|
40 |
+
with pytest.raises(
|
41 |
+
NetworkXError, match=".*n must be greater than or equal to 2"
|
42 |
+
):
|
43 |
+
duplication_divergence_graph(1, p=1)
|
44 |
+
|
45 |
+
|
46 |
+
class TestPartialDuplicationGraph:
|
47 |
+
"""Unit tests for the
|
48 |
+
:func:`networkx.generators.duplication.partial_duplication_graph`
|
49 |
+
function.
|
50 |
+
|
51 |
+
"""
|
52 |
+
|
53 |
+
def test_final_size(self):
|
54 |
+
N = 10
|
55 |
+
n = 5
|
56 |
+
p = 0.5
|
57 |
+
q = 0.5
|
58 |
+
G = partial_duplication_graph(N, n, p, q)
|
59 |
+
assert len(G) == N
|
60 |
+
G = partial_duplication_graph(N, n, p, q, seed=42)
|
61 |
+
assert len(G) == N
|
62 |
+
|
63 |
+
def test_initial_clique_size(self):
|
64 |
+
N = 10
|
65 |
+
n = 10
|
66 |
+
p = 0.5
|
67 |
+
q = 0.5
|
68 |
+
G = partial_duplication_graph(N, n, p, q)
|
69 |
+
assert len(G) == n
|
70 |
+
|
71 |
+
def test_invalid_initial_size(self):
|
72 |
+
with pytest.raises(NetworkXError):
|
73 |
+
N = 5
|
74 |
+
n = 10
|
75 |
+
p = 0.5
|
76 |
+
q = 0.5
|
77 |
+
G = partial_duplication_graph(N, n, p, q)
|
78 |
+
|
79 |
+
def test_invalid_probabilities(self):
|
80 |
+
N = 1
|
81 |
+
n = 1
|
82 |
+
for p, q in [(0.5, 2), (0.5, -1), (2, 0.5), (-1, 0.5)]:
|
83 |
+
args = (N, n, p, q)
|
84 |
+
pytest.raises(NetworkXError, partial_duplication_graph, *args)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_ego.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
ego graph
|
3 |
+
---------
|
4 |
+
"""
|
5 |
+
|
6 |
+
import networkx as nx
|
7 |
+
from networkx.utils import edges_equal, nodes_equal
|
8 |
+
|
9 |
+
|
10 |
+
class TestGeneratorEgo:
|
11 |
+
def test_ego(self):
|
12 |
+
G = nx.star_graph(3)
|
13 |
+
H = nx.ego_graph(G, 0)
|
14 |
+
assert nx.is_isomorphic(G, H)
|
15 |
+
G.add_edge(1, 11)
|
16 |
+
G.add_edge(2, 22)
|
17 |
+
G.add_edge(3, 33)
|
18 |
+
H = nx.ego_graph(G, 0)
|
19 |
+
assert nx.is_isomorphic(nx.star_graph(3), H)
|
20 |
+
G = nx.path_graph(3)
|
21 |
+
H = nx.ego_graph(G, 0)
|
22 |
+
assert edges_equal(H.edges(), [(0, 1)])
|
23 |
+
H = nx.ego_graph(G, 0, undirected=True)
|
24 |
+
assert edges_equal(H.edges(), [(0, 1)])
|
25 |
+
H = nx.ego_graph(G, 0, center=False)
|
26 |
+
assert edges_equal(H.edges(), [])
|
27 |
+
|
28 |
+
def test_ego_distance(self):
|
29 |
+
G = nx.Graph()
|
30 |
+
G.add_edge(0, 1, weight=2, distance=1)
|
31 |
+
G.add_edge(1, 2, weight=2, distance=2)
|
32 |
+
G.add_edge(2, 3, weight=2, distance=1)
|
33 |
+
assert nodes_equal(nx.ego_graph(G, 0, radius=3).nodes(), [0, 1, 2, 3])
|
34 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="weight")
|
35 |
+
assert nodes_equal(eg.nodes(), [0, 1])
|
36 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="weight", undirected=True)
|
37 |
+
assert nodes_equal(eg.nodes(), [0, 1])
|
38 |
+
eg = nx.ego_graph(G, 0, radius=3, distance="distance")
|
39 |
+
assert nodes_equal(eg.nodes(), [0, 1, 2])
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_expanders.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the :mod:`networkx.generators.expanders` module.
|
2 |
+
|
3 |
+
"""
|
4 |
+
|
5 |
+
import pytest
|
6 |
+
|
7 |
+
import networkx as nx
|
8 |
+
|
9 |
+
|
10 |
+
@pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
|
11 |
+
def test_margulis_gabber_galil_graph_properties(n):
|
12 |
+
g = nx.margulis_gabber_galil_graph(n)
|
13 |
+
assert g.number_of_nodes() == n * n
|
14 |
+
for node in g:
|
15 |
+
assert g.degree(node) == 8
|
16 |
+
assert len(node) == 2
|
17 |
+
for i in node:
|
18 |
+
assert int(i) == i
|
19 |
+
assert 0 <= i < n
|
20 |
+
|
21 |
+
|
22 |
+
@pytest.mark.parametrize("n", (2, 3, 5, 6, 10))
|
23 |
+
def test_margulis_gabber_galil_graph_eigvals(n):
|
24 |
+
np = pytest.importorskip("numpy")
|
25 |
+
sp = pytest.importorskip("scipy")
|
26 |
+
|
27 |
+
g = nx.margulis_gabber_galil_graph(n)
|
28 |
+
# Eigenvalues are already sorted using the scipy eigvalsh,
|
29 |
+
# but the implementation in numpy does not guarantee order.
|
30 |
+
w = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(g).toarray()))
|
31 |
+
assert w[-2] < 5 * np.sqrt(2)
|
32 |
+
|
33 |
+
|
34 |
+
@pytest.mark.parametrize("p", (3, 5, 7, 11)) # Primes
|
35 |
+
def test_chordal_cycle_graph(p):
|
36 |
+
"""Test for the :func:`networkx.chordal_cycle_graph` function."""
|
37 |
+
G = nx.chordal_cycle_graph(p)
|
38 |
+
assert len(G) == p
|
39 |
+
# TODO The second largest eigenvalue should be smaller than a constant,
|
40 |
+
# independent of the number of nodes in the graph:
|
41 |
+
#
|
42 |
+
# eigs = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(G).toarray()))
|
43 |
+
# assert_less(eigs[-2], ...)
|
44 |
+
#
|
45 |
+
|
46 |
+
|
47 |
+
@pytest.mark.parametrize("p", (3, 5, 7, 11, 13)) # Primes
|
48 |
+
def test_paley_graph(p):
|
49 |
+
"""Test for the :func:`networkx.paley_graph` function."""
|
50 |
+
G = nx.paley_graph(p)
|
51 |
+
# G has p nodes
|
52 |
+
assert len(G) == p
|
53 |
+
# G is (p-1)/2-regular
|
54 |
+
in_degrees = {G.in_degree(node) for node in G.nodes}
|
55 |
+
out_degrees = {G.out_degree(node) for node in G.nodes}
|
56 |
+
assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2
|
57 |
+
assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2
|
58 |
+
|
59 |
+
# If p = 1 mod 4, -1 is a square mod 4 and therefore the
|
60 |
+
# edge in the Paley graph are symmetric.
|
61 |
+
if p % 4 == 1:
|
62 |
+
for u, v in G.edges:
|
63 |
+
assert (v, u) in G.edges
|
64 |
+
|
65 |
+
|
66 |
+
@pytest.mark.parametrize("d, n", [(2, 7), (4, 10), (4, 16)])
|
67 |
+
def test_maybe_regular_expander(d, n):
|
68 |
+
pytest.importorskip("numpy")
|
69 |
+
G = nx.maybe_regular_expander(n, d)
|
70 |
+
|
71 |
+
assert len(G) == n, "Should have n nodes"
|
72 |
+
assert len(G.edges) == n * d / 2, "Should have n*d/2 edges"
|
73 |
+
assert nx.is_k_regular(G, d), "Should be d-regular"
|
74 |
+
|
75 |
+
|
76 |
+
@pytest.mark.parametrize("n", (3, 5, 6, 10))
|
77 |
+
def test_is_regular_expander(n):
|
78 |
+
pytest.importorskip("numpy")
|
79 |
+
pytest.importorskip("scipy")
|
80 |
+
G = nx.complete_graph(n)
|
81 |
+
|
82 |
+
assert nx.is_regular_expander(G) == True, "Should be a regular expander"
|
83 |
+
|
84 |
+
|
85 |
+
@pytest.mark.parametrize("d, n", [(2, 7), (4, 10), (4, 16)])
|
86 |
+
def test_random_regular_expander(d, n):
|
87 |
+
pytest.importorskip("numpy")
|
88 |
+
pytest.importorskip("scipy")
|
89 |
+
G = nx.random_regular_expander_graph(n, d)
|
90 |
+
|
91 |
+
assert len(G) == n, "Should have n nodes"
|
92 |
+
assert len(G.edges) == n * d / 2, "Should have n*d/2 edges"
|
93 |
+
assert nx.is_k_regular(G, d), "Should be d-regular"
|
94 |
+
assert nx.is_regular_expander(G) == True, "Should be a regular expander"
|
95 |
+
|
96 |
+
|
97 |
+
def test_random_regular_expander_explicit_construction():
|
98 |
+
pytest.importorskip("numpy")
|
99 |
+
pytest.importorskip("scipy")
|
100 |
+
G = nx.random_regular_expander_graph(d=4, n=5)
|
101 |
+
|
102 |
+
assert len(G) == 5 and len(G.edges) == 10, "Should be a complete graph"
|
103 |
+
|
104 |
+
|
105 |
+
@pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
|
106 |
+
def test_margulis_gabber_galil_graph_badinput(graph_type):
|
107 |
+
with pytest.raises(
|
108 |
+
nx.NetworkXError, match="`create_using` must be an undirected multigraph"
|
109 |
+
):
|
110 |
+
nx.margulis_gabber_galil_graph(3, create_using=graph_type)
|
111 |
+
|
112 |
+
|
113 |
+
@pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph, nx.MultiDiGraph))
|
114 |
+
def test_chordal_cycle_graph_badinput(graph_type):
|
115 |
+
with pytest.raises(
|
116 |
+
nx.NetworkXError, match="`create_using` must be an undirected multigraph"
|
117 |
+
):
|
118 |
+
nx.chordal_cycle_graph(3, create_using=graph_type)
|
119 |
+
|
120 |
+
|
121 |
+
def test_paley_graph_badinput():
|
122 |
+
with pytest.raises(
|
123 |
+
nx.NetworkXError, match="`create_using` cannot be a multigraph."
|
124 |
+
):
|
125 |
+
nx.paley_graph(3, create_using=nx.MultiGraph)
|
126 |
+
|
127 |
+
|
128 |
+
def test_maybe_regular_expander_badinput():
|
129 |
+
pytest.importorskip("numpy")
|
130 |
+
pytest.importorskip("scipy")
|
131 |
+
|
132 |
+
with pytest.raises(nx.NetworkXError, match="n must be a positive integer"):
|
133 |
+
nx.maybe_regular_expander(n=-1, d=2)
|
134 |
+
|
135 |
+
with pytest.raises(nx.NetworkXError, match="d must be greater than or equal to 2"):
|
136 |
+
nx.maybe_regular_expander(n=10, d=0)
|
137 |
+
|
138 |
+
with pytest.raises(nx.NetworkXError, match="Need n-1>= d to have room"):
|
139 |
+
nx.maybe_regular_expander(n=5, d=6)
|
140 |
+
|
141 |
+
|
142 |
+
def test_is_regular_expander_badinput():
|
143 |
+
pytest.importorskip("numpy")
|
144 |
+
pytest.importorskip("scipy")
|
145 |
+
|
146 |
+
with pytest.raises(nx.NetworkXError, match="epsilon must be non negative"):
|
147 |
+
nx.is_regular_expander(nx.Graph(), epsilon=-1)
|
148 |
+
|
149 |
+
|
150 |
+
def test_random_regular_expander_badinput():
|
151 |
+
pytest.importorskip("numpy")
|
152 |
+
pytest.importorskip("scipy")
|
153 |
+
|
154 |
+
with pytest.raises(nx.NetworkXError, match="n must be a positive integer"):
|
155 |
+
nx.random_regular_expander_graph(n=-1, d=2)
|
156 |
+
|
157 |
+
with pytest.raises(nx.NetworkXError, match="d must be greater than or equal to 2"):
|
158 |
+
nx.random_regular_expander_graph(n=10, d=0)
|
159 |
+
|
160 |
+
with pytest.raises(nx.NetworkXError, match="Need n-1>= d to have room"):
|
161 |
+
nx.random_regular_expander_graph(n=5, d=6)
|
162 |
+
|
163 |
+
with pytest.raises(nx.NetworkXError, match="epsilon must be non negative"):
|
164 |
+
nx.random_regular_expander_graph(n=4, d=2, epsilon=-1)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_geometric.py
ADDED
@@ -0,0 +1,488 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import random
|
3 |
+
from itertools import combinations
|
4 |
+
|
5 |
+
import pytest
|
6 |
+
|
7 |
+
import networkx as nx
|
8 |
+
|
9 |
+
|
10 |
+
def l1dist(x, y):
|
11 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
12 |
+
|
13 |
+
|
14 |
+
class TestRandomGeometricGraph:
|
15 |
+
"""Unit tests for :func:`~networkx.random_geometric_graph`"""
|
16 |
+
|
17 |
+
def test_number_of_nodes(self):
|
18 |
+
G = nx.random_geometric_graph(50, 0.25, seed=42)
|
19 |
+
assert len(G) == 50
|
20 |
+
G = nx.random_geometric_graph(range(50), 0.25, seed=42)
|
21 |
+
assert len(G) == 50
|
22 |
+
|
23 |
+
def test_distances(self):
|
24 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
25 |
+
within the prescribed radius.
|
26 |
+
"""
|
27 |
+
# Use the Euclidean metric, the default according to the
|
28 |
+
# documentation.
|
29 |
+
G = nx.random_geometric_graph(50, 0.25)
|
30 |
+
for u, v in combinations(G, 2):
|
31 |
+
# Adjacent vertices must be within the given distance.
|
32 |
+
if v in G[u]:
|
33 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
34 |
+
# Nonadjacent vertices must be at greater distance.
|
35 |
+
else:
|
36 |
+
assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
37 |
+
|
38 |
+
def test_p(self):
|
39 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
40 |
+
# Use the L1 metric.
|
41 |
+
G = nx.random_geometric_graph(50, 0.25, p=1)
|
42 |
+
for u, v in combinations(G, 2):
|
43 |
+
# Adjacent vertices must be within the given distance.
|
44 |
+
if v in G[u]:
|
45 |
+
assert l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
46 |
+
# Nonadjacent vertices must be at greater distance.
|
47 |
+
else:
|
48 |
+
assert not l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
49 |
+
|
50 |
+
def test_node_names(self):
|
51 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
52 |
+
import string
|
53 |
+
|
54 |
+
nodes = list(string.ascii_lowercase)
|
55 |
+
G = nx.random_geometric_graph(nodes, 0.25)
|
56 |
+
assert len(G) == len(nodes)
|
57 |
+
|
58 |
+
for u, v in combinations(G, 2):
|
59 |
+
# Adjacent vertices must be within the given distance.
|
60 |
+
if v in G[u]:
|
61 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
62 |
+
# Nonadjacent vertices must be at greater distance.
|
63 |
+
else:
|
64 |
+
assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
65 |
+
|
66 |
+
def test_pos_name(self):
|
67 |
+
G = nx.random_geometric_graph(50, 0.25, seed=42, pos_name="coords")
|
68 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
69 |
+
|
70 |
+
|
71 |
+
class TestSoftRandomGeometricGraph:
|
72 |
+
"""Unit tests for :func:`~networkx.soft_random_geometric_graph`"""
|
73 |
+
|
74 |
+
def test_number_of_nodes(self):
|
75 |
+
G = nx.soft_random_geometric_graph(50, 0.25, seed=42)
|
76 |
+
assert len(G) == 50
|
77 |
+
G = nx.soft_random_geometric_graph(range(50), 0.25, seed=42)
|
78 |
+
assert len(G) == 50
|
79 |
+
|
80 |
+
def test_distances(self):
|
81 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
82 |
+
within the prescribed radius.
|
83 |
+
"""
|
84 |
+
# Use the Euclidean metric, the default according to the
|
85 |
+
# documentation.
|
86 |
+
G = nx.soft_random_geometric_graph(50, 0.25)
|
87 |
+
for u, v in combinations(G, 2):
|
88 |
+
# Adjacent vertices must be within the given distance.
|
89 |
+
if v in G[u]:
|
90 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
91 |
+
|
92 |
+
def test_p(self):
|
93 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
94 |
+
|
95 |
+
# Use the L1 metric.
|
96 |
+
def dist(x, y):
|
97 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
98 |
+
|
99 |
+
G = nx.soft_random_geometric_graph(50, 0.25, p=1)
|
100 |
+
for u, v in combinations(G, 2):
|
101 |
+
# Adjacent vertices must be within the given distance.
|
102 |
+
if v in G[u]:
|
103 |
+
assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
104 |
+
|
105 |
+
def test_node_names(self):
|
106 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
107 |
+
import string
|
108 |
+
|
109 |
+
nodes = list(string.ascii_lowercase)
|
110 |
+
G = nx.soft_random_geometric_graph(nodes, 0.25)
|
111 |
+
assert len(G) == len(nodes)
|
112 |
+
|
113 |
+
for u, v in combinations(G, 2):
|
114 |
+
# Adjacent vertices must be within the given distance.
|
115 |
+
if v in G[u]:
|
116 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
117 |
+
|
118 |
+
def test_p_dist_default(self):
|
119 |
+
"""Tests default p_dict = 0.5 returns graph with edge count <= RGG with
|
120 |
+
same n, radius, dim and positions
|
121 |
+
"""
|
122 |
+
nodes = 50
|
123 |
+
dim = 2
|
124 |
+
pos = {v: [random.random() for i in range(dim)] for v in range(nodes)}
|
125 |
+
RGG = nx.random_geometric_graph(50, 0.25, pos=pos)
|
126 |
+
SRGG = nx.soft_random_geometric_graph(50, 0.25, pos=pos)
|
127 |
+
assert len(SRGG.edges()) <= len(RGG.edges())
|
128 |
+
|
129 |
+
def test_p_dist_zero(self):
|
130 |
+
"""Tests if p_dict = 0 returns disconnected graph with 0 edges"""
|
131 |
+
|
132 |
+
def p_dist(dist):
|
133 |
+
return 0
|
134 |
+
|
135 |
+
G = nx.soft_random_geometric_graph(50, 0.25, p_dist=p_dist)
|
136 |
+
assert len(G.edges) == 0
|
137 |
+
|
138 |
+
def test_pos_name(self):
|
139 |
+
G = nx.soft_random_geometric_graph(50, 0.25, seed=42, pos_name="coords")
|
140 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
141 |
+
|
142 |
+
|
143 |
+
def join(G, u, v, theta, alpha, metric):
|
144 |
+
"""Returns ``True`` if and only if the nodes whose attributes are
|
145 |
+
``du`` and ``dv`` should be joined, according to the threshold
|
146 |
+
condition for geographical threshold graphs.
|
147 |
+
|
148 |
+
``G`` is an undirected NetworkX graph, and ``u`` and ``v`` are nodes
|
149 |
+
in that graph. The nodes must have node attributes ``'pos'`` and
|
150 |
+
``'weight'``.
|
151 |
+
|
152 |
+
``metric`` is a distance metric.
|
153 |
+
"""
|
154 |
+
du, dv = G.nodes[u], G.nodes[v]
|
155 |
+
u_pos, v_pos = du["pos"], dv["pos"]
|
156 |
+
u_weight, v_weight = du["weight"], dv["weight"]
|
157 |
+
return (u_weight + v_weight) * metric(u_pos, v_pos) ** alpha >= theta
|
158 |
+
|
159 |
+
|
160 |
+
class TestGeographicalThresholdGraph:
|
161 |
+
"""Unit tests for :func:`~networkx.geographical_threshold_graph`"""
|
162 |
+
|
163 |
+
def test_number_of_nodes(self):
|
164 |
+
G = nx.geographical_threshold_graph(50, 100, seed=42)
|
165 |
+
assert len(G) == 50
|
166 |
+
G = nx.geographical_threshold_graph(range(50), 100, seed=42)
|
167 |
+
assert len(G) == 50
|
168 |
+
|
169 |
+
def test_distances(self):
|
170 |
+
"""Tests that pairs of vertices adjacent if and only if their
|
171 |
+
distances meet the given threshold.
|
172 |
+
"""
|
173 |
+
# Use the Euclidean metric and alpha = -2
|
174 |
+
# the default according to the documentation.
|
175 |
+
G = nx.geographical_threshold_graph(50, 10)
|
176 |
+
for u, v in combinations(G, 2):
|
177 |
+
# Adjacent vertices must exceed the threshold.
|
178 |
+
if v in G[u]:
|
179 |
+
assert join(G, u, v, 10, -2, math.dist)
|
180 |
+
# Nonadjacent vertices must not exceed the threshold.
|
181 |
+
else:
|
182 |
+
assert not join(G, u, v, 10, -2, math.dist)
|
183 |
+
|
184 |
+
def test_metric(self):
|
185 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
186 |
+
# Use the L1 metric.
|
187 |
+
G = nx.geographical_threshold_graph(50, 10, metric=l1dist)
|
188 |
+
for u, v in combinations(G, 2):
|
189 |
+
# Adjacent vertices must exceed the threshold.
|
190 |
+
if v in G[u]:
|
191 |
+
assert join(G, u, v, 10, -2, l1dist)
|
192 |
+
# Nonadjacent vertices must not exceed the threshold.
|
193 |
+
else:
|
194 |
+
assert not join(G, u, v, 10, -2, l1dist)
|
195 |
+
|
196 |
+
def test_p_dist_zero(self):
|
197 |
+
"""Tests if p_dict = 0 returns disconnected graph with 0 edges"""
|
198 |
+
|
199 |
+
def p_dist(dist):
|
200 |
+
return 0
|
201 |
+
|
202 |
+
G = nx.geographical_threshold_graph(50, 1, p_dist=p_dist)
|
203 |
+
assert len(G.edges) == 0
|
204 |
+
|
205 |
+
def test_pos_weight_name(self):
|
206 |
+
gtg = nx.geographical_threshold_graph
|
207 |
+
G = gtg(50, 100, seed=42, pos_name="coords", weight_name="wt")
|
208 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
209 |
+
assert all(d["wt"] > 0 for n, d in G.nodes.items())
|
210 |
+
|
211 |
+
|
212 |
+
class TestWaxmanGraph:
|
213 |
+
"""Unit tests for the :func:`~networkx.waxman_graph` function."""
|
214 |
+
|
215 |
+
def test_number_of_nodes_1(self):
|
216 |
+
G = nx.waxman_graph(50, 0.5, 0.1, seed=42)
|
217 |
+
assert len(G) == 50
|
218 |
+
G = nx.waxman_graph(range(50), 0.5, 0.1, seed=42)
|
219 |
+
assert len(G) == 50
|
220 |
+
|
221 |
+
def test_number_of_nodes_2(self):
|
222 |
+
G = nx.waxman_graph(50, 0.5, 0.1, L=1)
|
223 |
+
assert len(G) == 50
|
224 |
+
G = nx.waxman_graph(range(50), 0.5, 0.1, L=1)
|
225 |
+
assert len(G) == 50
|
226 |
+
|
227 |
+
def test_metric(self):
|
228 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
229 |
+
# Use the L1 metric.
|
230 |
+
G = nx.waxman_graph(50, 0.5, 0.1, metric=l1dist)
|
231 |
+
assert len(G) == 50
|
232 |
+
|
233 |
+
def test_pos_name(self):
|
234 |
+
G = nx.waxman_graph(50, 0.5, 0.1, seed=42, pos_name="coords")
|
235 |
+
assert all(len(d["coords"]) == 2 for n, d in G.nodes.items())
|
236 |
+
|
237 |
+
|
238 |
+
class TestNavigableSmallWorldGraph:
|
239 |
+
def test_navigable_small_world(self):
|
240 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, seed=42)
|
241 |
+
gg = nx.grid_2d_graph(5, 5).to_directed()
|
242 |
+
assert nx.is_isomorphic(G, gg)
|
243 |
+
|
244 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, dim=3)
|
245 |
+
gg = nx.grid_graph([5, 5, 5]).to_directed()
|
246 |
+
assert nx.is_isomorphic(G, gg)
|
247 |
+
|
248 |
+
G = nx.navigable_small_world_graph(5, p=1, q=0, dim=1)
|
249 |
+
gg = nx.grid_graph([5]).to_directed()
|
250 |
+
assert nx.is_isomorphic(G, gg)
|
251 |
+
|
252 |
+
def test_invalid_diameter_value(self):
|
253 |
+
with pytest.raises(nx.NetworkXException, match=".*p must be >= 1"):
|
254 |
+
nx.navigable_small_world_graph(5, p=0, q=0, dim=1)
|
255 |
+
|
256 |
+
def test_invalid_long_range_connections_value(self):
|
257 |
+
with pytest.raises(nx.NetworkXException, match=".*q must be >= 0"):
|
258 |
+
nx.navigable_small_world_graph(5, p=1, q=-1, dim=1)
|
259 |
+
|
260 |
+
def test_invalid_exponent_for_decaying_probability_value(self):
|
261 |
+
with pytest.raises(nx.NetworkXException, match=".*r must be >= 0"):
|
262 |
+
nx.navigable_small_world_graph(5, p=1, q=0, r=-1, dim=1)
|
263 |
+
|
264 |
+
def test_r_between_0_and_1(self):
|
265 |
+
"""Smoke test for radius in range [0, 1]"""
|
266 |
+
# q=0 means no long-range connections
|
267 |
+
G = nx.navigable_small_world_graph(3, p=1, q=0, r=0.5, dim=2, seed=42)
|
268 |
+
expected = nx.grid_2d_graph(3, 3, create_using=nx.DiGraph)
|
269 |
+
assert nx.utils.graphs_equal(G, expected)
|
270 |
+
|
271 |
+
@pytest.mark.parametrize("seed", range(2478, 2578, 10))
|
272 |
+
def test_r_general_scaling(self, seed):
|
273 |
+
"""The probability of adding a long-range edge scales with `1 / dist**r`,
|
274 |
+
so a navigable_small_world graph created with r < 1 should generally
|
275 |
+
result in more edges than a navigable_small_world graph with r >= 1
|
276 |
+
(for 0 < q << n).
|
277 |
+
|
278 |
+
N.B. this is probabilistic, so this test may not hold for all seeds."""
|
279 |
+
G1 = nx.navigable_small_world_graph(7, q=3, r=0.5, seed=seed)
|
280 |
+
G2 = nx.navigable_small_world_graph(7, q=3, r=1, seed=seed)
|
281 |
+
G3 = nx.navigable_small_world_graph(7, q=3, r=2, seed=seed)
|
282 |
+
assert G1.number_of_edges() > G2.number_of_edges()
|
283 |
+
assert G2.number_of_edges() > G3.number_of_edges()
|
284 |
+
|
285 |
+
|
286 |
+
class TestThresholdedRandomGeometricGraph:
|
287 |
+
"""Unit tests for :func:`~networkx.thresholded_random_geometric_graph`"""
|
288 |
+
|
289 |
+
def test_number_of_nodes(self):
|
290 |
+
G = nx.thresholded_random_geometric_graph(50, 0.2, 0.1, seed=42)
|
291 |
+
assert len(G) == 50
|
292 |
+
G = nx.thresholded_random_geometric_graph(range(50), 0.2, 0.1, seed=42)
|
293 |
+
assert len(G) == 50
|
294 |
+
|
295 |
+
def test_distances(self):
|
296 |
+
"""Tests that pairs of vertices adjacent if and only if they are
|
297 |
+
within the prescribed radius.
|
298 |
+
"""
|
299 |
+
# Use the Euclidean metric, the default according to the
|
300 |
+
# documentation.
|
301 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, seed=42)
|
302 |
+
for u, v in combinations(G, 2):
|
303 |
+
# Adjacent vertices must be within the given distance.
|
304 |
+
if v in G[u]:
|
305 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
306 |
+
|
307 |
+
def test_p(self):
|
308 |
+
"""Tests for providing an alternate distance metric to the generator."""
|
309 |
+
|
310 |
+
# Use the L1 metric.
|
311 |
+
def dist(x, y):
|
312 |
+
return sum(abs(a - b) for a, b in zip(x, y))
|
313 |
+
|
314 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, p=1, seed=42)
|
315 |
+
for u, v in combinations(G, 2):
|
316 |
+
# Adjacent vertices must be within the given distance.
|
317 |
+
if v in G[u]:
|
318 |
+
assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
319 |
+
|
320 |
+
def test_node_names(self):
|
321 |
+
"""Tests using values other than sequential numbers as node IDs."""
|
322 |
+
import string
|
323 |
+
|
324 |
+
nodes = list(string.ascii_lowercase)
|
325 |
+
G = nx.thresholded_random_geometric_graph(nodes, 0.25, 0.1, seed=42)
|
326 |
+
assert len(G) == len(nodes)
|
327 |
+
|
328 |
+
for u, v in combinations(G, 2):
|
329 |
+
# Adjacent vertices must be within the given distance.
|
330 |
+
if v in G[u]:
|
331 |
+
assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
|
332 |
+
|
333 |
+
def test_theta(self):
|
334 |
+
"""Tests that pairs of vertices adjacent if and only if their sum
|
335 |
+
weights exceeds the threshold parameter theta.
|
336 |
+
"""
|
337 |
+
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, seed=42)
|
338 |
+
|
339 |
+
for u, v in combinations(G, 2):
|
340 |
+
# Adjacent vertices must be within the given distance.
|
341 |
+
if v in G[u]:
|
342 |
+
assert (G.nodes[u]["weight"] + G.nodes[v]["weight"]) >= 0.1
|
343 |
+
|
344 |
+
def test_pos_name(self):
|
345 |
+
trgg = nx.thresholded_random_geometric_graph
|
346 |
+
G = trgg(50, 0.25, 0.1, seed=42, pos_name="p", weight_name="wt")
|
347 |
+
assert all(len(d["p"]) == 2 for n, d in G.nodes.items())
|
348 |
+
assert all(d["wt"] > 0 for n, d in G.nodes.items())
|
349 |
+
|
350 |
+
|
351 |
+
def test_geometric_edges_pos_attribute():
|
352 |
+
G = nx.Graph()
|
353 |
+
G.add_nodes_from(
|
354 |
+
[
|
355 |
+
(0, {"position": (0, 0)}),
|
356 |
+
(1, {"position": (0, 1)}),
|
357 |
+
(2, {"position": (1, 0)}),
|
358 |
+
]
|
359 |
+
)
|
360 |
+
expected_edges = [(0, 1), (0, 2)]
|
361 |
+
assert expected_edges == nx.geometric_edges(G, radius=1, pos_name="position")
|
362 |
+
|
363 |
+
|
364 |
+
def test_geometric_edges_raises_no_pos():
|
365 |
+
G = nx.path_graph(3)
|
366 |
+
msg = "all nodes. must have a '"
|
367 |
+
with pytest.raises(nx.NetworkXError, match=msg):
|
368 |
+
nx.geometric_edges(G, radius=1)
|
369 |
+
|
370 |
+
|
371 |
+
def test_number_of_nodes_S1():
|
372 |
+
G = nx.geometric_soft_configuration_graph(
|
373 |
+
beta=1.5, n=100, gamma=2.7, mean_degree=10, seed=42
|
374 |
+
)
|
375 |
+
assert len(G) == 100
|
376 |
+
|
377 |
+
|
378 |
+
def test_set_attributes_S1():
|
379 |
+
G = nx.geometric_soft_configuration_graph(
|
380 |
+
beta=1.5, n=100, gamma=2.7, mean_degree=10, seed=42
|
381 |
+
)
|
382 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
383 |
+
assert len(kappas) == 100
|
384 |
+
thetas = nx.get_node_attributes(G, "theta")
|
385 |
+
assert len(thetas) == 100
|
386 |
+
radii = nx.get_node_attributes(G, "radius")
|
387 |
+
assert len(radii) == 100
|
388 |
+
|
389 |
+
|
390 |
+
def test_mean_kappas_mean_degree_S1():
|
391 |
+
G = nx.geometric_soft_configuration_graph(
|
392 |
+
beta=2.5, n=50, gamma=2.7, mean_degree=10, seed=8023
|
393 |
+
)
|
394 |
+
|
395 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
396 |
+
mean_kappas = sum(kappas.values()) / len(kappas)
|
397 |
+
assert math.fabs(mean_kappas - 10) < 0.5
|
398 |
+
|
399 |
+
degrees = dict(G.degree())
|
400 |
+
mean_degree = sum(degrees.values()) / len(degrees)
|
401 |
+
assert math.fabs(mean_degree - 10) < 1
|
402 |
+
|
403 |
+
|
404 |
+
def test_dict_kappas_S1():
|
405 |
+
kappas = {i: 10 for i in range(1000)}
|
406 |
+
G = nx.geometric_soft_configuration_graph(beta=1, kappas=kappas)
|
407 |
+
assert len(G) == 1000
|
408 |
+
kappas = nx.get_node_attributes(G, "kappa")
|
409 |
+
assert all(kappa == 10 for kappa in kappas.values())
|
410 |
+
|
411 |
+
|
412 |
+
def test_beta_clustering_S1():
|
413 |
+
G1 = nx.geometric_soft_configuration_graph(
|
414 |
+
beta=1.5, n=100, gamma=3.5, mean_degree=10, seed=42
|
415 |
+
)
|
416 |
+
G2 = nx.geometric_soft_configuration_graph(
|
417 |
+
beta=3.0, n=100, gamma=3.5, mean_degree=10, seed=42
|
418 |
+
)
|
419 |
+
assert nx.average_clustering(G1) < nx.average_clustering(G2)
|
420 |
+
|
421 |
+
|
422 |
+
def test_wrong_parameters_S1():
|
423 |
+
with pytest.raises(
|
424 |
+
nx.NetworkXError,
|
425 |
+
match="Please provide either kappas, or all 3 of: n, gamma and mean_degree.",
|
426 |
+
):
|
427 |
+
G = nx.geometric_soft_configuration_graph(
|
428 |
+
beta=1.5, gamma=3.5, mean_degree=10, seed=42
|
429 |
+
)
|
430 |
+
|
431 |
+
with pytest.raises(
|
432 |
+
nx.NetworkXError,
|
433 |
+
match="When kappas is input, n, gamma and mean_degree must not be.",
|
434 |
+
):
|
435 |
+
kappas = {i: 10 for i in range(1000)}
|
436 |
+
G = nx.geometric_soft_configuration_graph(
|
437 |
+
beta=1.5, kappas=kappas, gamma=2.3, seed=42
|
438 |
+
)
|
439 |
+
|
440 |
+
with pytest.raises(
|
441 |
+
nx.NetworkXError,
|
442 |
+
match="Please provide either kappas, or all 3 of: n, gamma and mean_degree.",
|
443 |
+
):
|
444 |
+
G = nx.geometric_soft_configuration_graph(beta=1.5, seed=42)
|
445 |
+
|
446 |
+
|
447 |
+
def test_negative_beta_S1():
|
448 |
+
with pytest.raises(
|
449 |
+
nx.NetworkXError, match="The parameter beta cannot be smaller or equal to 0."
|
450 |
+
):
|
451 |
+
G = nx.geometric_soft_configuration_graph(
|
452 |
+
beta=-1, n=100, gamma=2.3, mean_degree=10, seed=42
|
453 |
+
)
|
454 |
+
|
455 |
+
|
456 |
+
def test_non_zero_clustering_beta_lower_one_S1():
|
457 |
+
G = nx.geometric_soft_configuration_graph(
|
458 |
+
beta=0.5, n=100, gamma=3.5, mean_degree=10, seed=42
|
459 |
+
)
|
460 |
+
assert nx.average_clustering(G) > 0
|
461 |
+
|
462 |
+
|
463 |
+
def test_mean_degree_influence_on_connectivity_S1():
|
464 |
+
low_mean_degree = 2
|
465 |
+
high_mean_degree = 20
|
466 |
+
G_low = nx.geometric_soft_configuration_graph(
|
467 |
+
beta=1.2, n=100, gamma=2.7, mean_degree=low_mean_degree, seed=42
|
468 |
+
)
|
469 |
+
G_high = nx.geometric_soft_configuration_graph(
|
470 |
+
beta=1.2, n=100, gamma=2.7, mean_degree=high_mean_degree, seed=42
|
471 |
+
)
|
472 |
+
assert nx.number_connected_components(G_low) > nx.number_connected_components(
|
473 |
+
G_high
|
474 |
+
)
|
475 |
+
|
476 |
+
|
477 |
+
def test_compare_mean_kappas_different_gammas_S1():
|
478 |
+
G1 = nx.geometric_soft_configuration_graph(
|
479 |
+
beta=1.5, n=20, gamma=2.7, mean_degree=5, seed=42
|
480 |
+
)
|
481 |
+
G2 = nx.geometric_soft_configuration_graph(
|
482 |
+
beta=1.5, n=20, gamma=3.5, mean_degree=5, seed=42
|
483 |
+
)
|
484 |
+
kappas1 = nx.get_node_attributes(G1, "kappa")
|
485 |
+
mean_kappas1 = sum(kappas1.values()) / len(kappas1)
|
486 |
+
kappas2 = nx.get_node_attributes(G2, "kappa")
|
487 |
+
mean_kappas2 = sum(kappas2.values()) / len(kappas2)
|
488 |
+
assert math.fabs(mean_kappas1 - mean_kappas2) < 1
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_harary_graph.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the :mod:`networkx.generators.harary_graph` module.
|
2 |
+
"""
|
3 |
+
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import networkx as nx
|
7 |
+
from networkx.algorithms.isomorphism.isomorph import is_isomorphic
|
8 |
+
from networkx.generators.harary_graph import hkn_harary_graph, hnm_harary_graph
|
9 |
+
|
10 |
+
|
11 |
+
class TestHararyGraph:
|
12 |
+
"""
|
13 |
+
Suppose n nodes, m >= n-1 edges, d = 2m // n, r = 2m % n
|
14 |
+
"""
|
15 |
+
|
16 |
+
def test_hnm_harary_graph(self):
|
17 |
+
# When d is even and r = 0, the hnm_harary_graph(n,m) is
|
18 |
+
# the circulant_graph(n, list(range(1,d/2+1)))
|
19 |
+
for n, m in [(5, 5), (6, 12), (7, 14)]:
|
20 |
+
G1 = hnm_harary_graph(n, m)
|
21 |
+
d = 2 * m // n
|
22 |
+
G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
|
23 |
+
assert is_isomorphic(G1, G2)
|
24 |
+
|
25 |
+
# When d is even and r > 0, the hnm_harary_graph(n,m) is
|
26 |
+
# the circulant_graph(n, list(range(1,d/2+1)))
|
27 |
+
# with r edges added arbitrarily
|
28 |
+
for n, m in [(5, 7), (6, 13), (7, 16)]:
|
29 |
+
G1 = hnm_harary_graph(n, m)
|
30 |
+
d = 2 * m // n
|
31 |
+
G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1)))
|
32 |
+
assert set(G2.edges) < set(G1.edges)
|
33 |
+
assert G1.number_of_edges() == m
|
34 |
+
|
35 |
+
# When d is odd and n is even and r = 0, the hnm_harary_graph(n,m)
|
36 |
+
# is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
|
37 |
+
for n, m in [(6, 9), (8, 12), (10, 15)]:
|
38 |
+
G1 = hnm_harary_graph(n, m)
|
39 |
+
d = 2 * m // n
|
40 |
+
L = list(range(1, (d + 1) // 2))
|
41 |
+
L.append(n // 2)
|
42 |
+
G2 = nx.circulant_graph(n, L)
|
43 |
+
assert is_isomorphic(G1, G2)
|
44 |
+
|
45 |
+
# When d is odd and n is even and r > 0, the hnm_harary_graph(n,m)
|
46 |
+
# is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2])
|
47 |
+
# with r edges added arbitrarily
|
48 |
+
for n, m in [(6, 10), (8, 13), (10, 17)]:
|
49 |
+
G1 = hnm_harary_graph(n, m)
|
50 |
+
d = 2 * m // n
|
51 |
+
L = list(range(1, (d + 1) // 2))
|
52 |
+
L.append(n // 2)
|
53 |
+
G2 = nx.circulant_graph(n, L)
|
54 |
+
assert set(G2.edges) < set(G1.edges)
|
55 |
+
assert G1.number_of_edges() == m
|
56 |
+
|
57 |
+
# When d is odd and n is odd, the hnm_harary_graph(n,m) is
|
58 |
+
# the circulant_graph(n, list(range(1,(d+1)/2))
|
59 |
+
# with m - n*(d-1)/2 edges added arbitrarily
|
60 |
+
for n, m in [(5, 4), (7, 12), (9, 14)]:
|
61 |
+
G1 = hnm_harary_graph(n, m)
|
62 |
+
d = 2 * m // n
|
63 |
+
L = list(range(1, (d + 1) // 2))
|
64 |
+
G2 = nx.circulant_graph(n, L)
|
65 |
+
assert set(G2.edges) < set(G1.edges)
|
66 |
+
assert G1.number_of_edges() == m
|
67 |
+
|
68 |
+
# Raise NetworkXError if n<1
|
69 |
+
n = 0
|
70 |
+
m = 0
|
71 |
+
pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
|
72 |
+
|
73 |
+
# Raise NetworkXError if m < n-1
|
74 |
+
n = 6
|
75 |
+
m = 4
|
76 |
+
pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
|
77 |
+
|
78 |
+
# Raise NetworkXError if m > n(n-1)/2
|
79 |
+
n = 6
|
80 |
+
m = 16
|
81 |
+
pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
|
82 |
+
|
83 |
+
"""
|
84 |
+
Suppose connectivity k, number of nodes n
|
85 |
+
"""
|
86 |
+
|
87 |
+
def test_hkn_harary_graph(self):
|
88 |
+
# When k == 1, the hkn_harary_graph(k,n) is
|
89 |
+
# the path_graph(n)
|
90 |
+
for k, n in [(1, 6), (1, 7)]:
|
91 |
+
G1 = hkn_harary_graph(k, n)
|
92 |
+
G2 = nx.path_graph(n)
|
93 |
+
assert is_isomorphic(G1, G2)
|
94 |
+
|
95 |
+
# When k is even, the hkn_harary_graph(k,n) is
|
96 |
+
# the circulant_graph(n, list(range(1,k/2+1)))
|
97 |
+
for k, n in [(2, 6), (2, 7), (4, 6), (4, 7)]:
|
98 |
+
G1 = hkn_harary_graph(k, n)
|
99 |
+
G2 = nx.circulant_graph(n, list(range(1, k // 2 + 1)))
|
100 |
+
assert is_isomorphic(G1, G2)
|
101 |
+
|
102 |
+
# When k is odd and n is even, the hkn_harary_graph(k,n) is
|
103 |
+
# the circulant_graph(n, list(range(1,(k+1)/2)) plus [n/2])
|
104 |
+
for k, n in [(3, 6), (5, 8), (7, 10)]:
|
105 |
+
G1 = hkn_harary_graph(k, n)
|
106 |
+
L = list(range(1, (k + 1) // 2))
|
107 |
+
L.append(n // 2)
|
108 |
+
G2 = nx.circulant_graph(n, L)
|
109 |
+
assert is_isomorphic(G1, G2)
|
110 |
+
|
111 |
+
# When k is odd and n is odd, the hkn_harary_graph(k,n) is
|
112 |
+
# the circulant_graph(n, list(range(1,(k+1)/2))) with
|
113 |
+
# n//2+1 edges added between node i and node i+n//2+1
|
114 |
+
for k, n in [(3, 5), (5, 9), (7, 11)]:
|
115 |
+
G1 = hkn_harary_graph(k, n)
|
116 |
+
G2 = nx.circulant_graph(n, list(range(1, (k + 1) // 2)))
|
117 |
+
eSet1 = set(G1.edges)
|
118 |
+
eSet2 = set(G2.edges)
|
119 |
+
eSet3 = set()
|
120 |
+
half = n // 2
|
121 |
+
for i in range(half + 1):
|
122 |
+
# add half+1 edges between i and i+half
|
123 |
+
eSet3.add((i, (i + half) % n))
|
124 |
+
assert eSet1 == eSet2 | eSet3
|
125 |
+
|
126 |
+
# Raise NetworkXError if k<1
|
127 |
+
k = 0
|
128 |
+
n = 0
|
129 |
+
pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
|
130 |
+
|
131 |
+
# Raise NetworkXError if n<k+1
|
132 |
+
k = 6
|
133 |
+
n = 6
|
134 |
+
pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_internet_as_graphs.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pytest import approx
|
2 |
+
|
3 |
+
from networkx import is_connected, neighbors
|
4 |
+
from networkx.generators.internet_as_graphs import random_internet_as_graph
|
5 |
+
|
6 |
+
|
7 |
+
class TestInternetASTopology:
|
8 |
+
@classmethod
|
9 |
+
def setup_class(cls):
|
10 |
+
cls.n = 1000
|
11 |
+
cls.seed = 42
|
12 |
+
cls.G = random_internet_as_graph(cls.n, cls.seed)
|
13 |
+
cls.T = []
|
14 |
+
cls.M = []
|
15 |
+
cls.C = []
|
16 |
+
cls.CP = []
|
17 |
+
cls.customers = {}
|
18 |
+
cls.providers = {}
|
19 |
+
|
20 |
+
for i in cls.G.nodes():
|
21 |
+
if cls.G.nodes[i]["type"] == "T":
|
22 |
+
cls.T.append(i)
|
23 |
+
elif cls.G.nodes[i]["type"] == "M":
|
24 |
+
cls.M.append(i)
|
25 |
+
elif cls.G.nodes[i]["type"] == "C":
|
26 |
+
cls.C.append(i)
|
27 |
+
elif cls.G.nodes[i]["type"] == "CP":
|
28 |
+
cls.CP.append(i)
|
29 |
+
else:
|
30 |
+
raise ValueError("Inconsistent data in the graph node attributes")
|
31 |
+
cls.set_customers(i)
|
32 |
+
cls.set_providers(i)
|
33 |
+
|
34 |
+
@classmethod
|
35 |
+
def set_customers(cls, i):
|
36 |
+
if i not in cls.customers:
|
37 |
+
cls.customers[i] = set()
|
38 |
+
for j in neighbors(cls.G, i):
|
39 |
+
e = cls.G.edges[(i, j)]
|
40 |
+
if e["type"] == "transit":
|
41 |
+
customer = int(e["customer"])
|
42 |
+
if j == customer:
|
43 |
+
cls.set_customers(j)
|
44 |
+
cls.customers[i] = cls.customers[i].union(cls.customers[j])
|
45 |
+
cls.customers[i].add(j)
|
46 |
+
elif i != customer:
|
47 |
+
raise ValueError(
|
48 |
+
"Inconsistent data in the graph edge attributes"
|
49 |
+
)
|
50 |
+
|
51 |
+
@classmethod
|
52 |
+
def set_providers(cls, i):
|
53 |
+
if i not in cls.providers:
|
54 |
+
cls.providers[i] = set()
|
55 |
+
for j in neighbors(cls.G, i):
|
56 |
+
e = cls.G.edges[(i, j)]
|
57 |
+
if e["type"] == "transit":
|
58 |
+
customer = int(e["customer"])
|
59 |
+
if i == customer:
|
60 |
+
cls.set_providers(j)
|
61 |
+
cls.providers[i] = cls.providers[i].union(cls.providers[j])
|
62 |
+
cls.providers[i].add(j)
|
63 |
+
elif j != customer:
|
64 |
+
raise ValueError(
|
65 |
+
"Inconsistent data in the graph edge attributes"
|
66 |
+
)
|
67 |
+
|
68 |
+
def test_wrong_input(self):
|
69 |
+
G = random_internet_as_graph(0)
|
70 |
+
assert len(G.nodes()) == 0
|
71 |
+
|
72 |
+
G = random_internet_as_graph(-1)
|
73 |
+
assert len(G.nodes()) == 0
|
74 |
+
|
75 |
+
G = random_internet_as_graph(1)
|
76 |
+
assert len(G.nodes()) == 1
|
77 |
+
|
78 |
+
def test_node_numbers(self):
|
79 |
+
assert len(self.G.nodes()) == self.n
|
80 |
+
assert len(self.T) < 7
|
81 |
+
assert len(self.M) == round(self.n * 0.15)
|
82 |
+
assert len(self.CP) == round(self.n * 0.05)
|
83 |
+
numb = self.n - len(self.T) - len(self.M) - len(self.CP)
|
84 |
+
assert len(self.C) == numb
|
85 |
+
|
86 |
+
def test_connectivity(self):
|
87 |
+
assert is_connected(self.G)
|
88 |
+
|
89 |
+
def test_relationships(self):
|
90 |
+
# T nodes are not customers of anyone
|
91 |
+
for i in self.T:
|
92 |
+
assert len(self.providers[i]) == 0
|
93 |
+
|
94 |
+
# C nodes are not providers of anyone
|
95 |
+
for i in self.C:
|
96 |
+
assert len(self.customers[i]) == 0
|
97 |
+
|
98 |
+
# CP nodes are not providers of anyone
|
99 |
+
for i in self.CP:
|
100 |
+
assert len(self.customers[i]) == 0
|
101 |
+
|
102 |
+
# test whether there is a customer-provider loop
|
103 |
+
for i in self.G.nodes():
|
104 |
+
assert len(self.customers[i].intersection(self.providers[i])) == 0
|
105 |
+
|
106 |
+
# test whether there is a peering with a customer or provider
|
107 |
+
for i, j in self.G.edges():
|
108 |
+
if self.G.edges[(i, j)]["type"] == "peer":
|
109 |
+
assert j not in self.customers[i]
|
110 |
+
assert i not in self.customers[j]
|
111 |
+
assert j not in self.providers[i]
|
112 |
+
assert i not in self.providers[j]
|
113 |
+
|
114 |
+
def test_degree_values(self):
|
115 |
+
d_m = 0 # multihoming degree for M nodes
|
116 |
+
d_cp = 0 # multihoming degree for CP nodes
|
117 |
+
d_c = 0 # multihoming degree for C nodes
|
118 |
+
p_m_m = 0 # avg number of peering edges between M and M
|
119 |
+
p_cp_m = 0 # avg number of peering edges between CP and M
|
120 |
+
p_cp_cp = 0 # avg number of peering edges between CP and CP
|
121 |
+
t_m = 0 # probability M's provider is T
|
122 |
+
t_cp = 0 # probability CP's provider is T
|
123 |
+
t_c = 0 # probability C's provider is T
|
124 |
+
|
125 |
+
for i, j in self.G.edges():
|
126 |
+
e = self.G.edges[(i, j)]
|
127 |
+
if e["type"] == "transit":
|
128 |
+
cust = int(e["customer"])
|
129 |
+
if i == cust:
|
130 |
+
prov = j
|
131 |
+
elif j == cust:
|
132 |
+
prov = i
|
133 |
+
else:
|
134 |
+
raise ValueError("Inconsistent data in the graph edge attributes")
|
135 |
+
if cust in self.M:
|
136 |
+
d_m += 1
|
137 |
+
if self.G.nodes[prov]["type"] == "T":
|
138 |
+
t_m += 1
|
139 |
+
elif cust in self.C:
|
140 |
+
d_c += 1
|
141 |
+
if self.G.nodes[prov]["type"] == "T":
|
142 |
+
t_c += 1
|
143 |
+
elif cust in self.CP:
|
144 |
+
d_cp += 1
|
145 |
+
if self.G.nodes[prov]["type"] == "T":
|
146 |
+
t_cp += 1
|
147 |
+
else:
|
148 |
+
raise ValueError("Inconsistent data in the graph edge attributes")
|
149 |
+
elif e["type"] == "peer":
|
150 |
+
if self.G.nodes[i]["type"] == "M" and self.G.nodes[j]["type"] == "M":
|
151 |
+
p_m_m += 1
|
152 |
+
if self.G.nodes[i]["type"] == "CP" and self.G.nodes[j]["type"] == "CP":
|
153 |
+
p_cp_cp += 1
|
154 |
+
if (
|
155 |
+
self.G.nodes[i]["type"] == "M"
|
156 |
+
and self.G.nodes[j]["type"] == "CP"
|
157 |
+
or self.G.nodes[i]["type"] == "CP"
|
158 |
+
and self.G.nodes[j]["type"] == "M"
|
159 |
+
):
|
160 |
+
p_cp_m += 1
|
161 |
+
else:
|
162 |
+
raise ValueError("Unexpected data in the graph edge attributes")
|
163 |
+
|
164 |
+
assert d_m / len(self.M) == approx((2 + (2.5 * self.n) / 10000), abs=1e-0)
|
165 |
+
assert d_cp / len(self.CP) == approx((2 + (1.5 * self.n) / 10000), abs=1e-0)
|
166 |
+
assert d_c / len(self.C) == approx((1 + (5 * self.n) / 100000), abs=1e-0)
|
167 |
+
|
168 |
+
assert p_m_m / len(self.M) == approx((1 + (2 * self.n) / 10000), abs=1e-0)
|
169 |
+
assert p_cp_m / len(self.CP) == approx((0.2 + (2 * self.n) / 10000), abs=1e-0)
|
170 |
+
assert p_cp_cp / len(self.CP) == approx(
|
171 |
+
(0.05 + (2 * self.n) / 100000), abs=1e-0
|
172 |
+
)
|
173 |
+
|
174 |
+
assert t_m / d_m == approx(0.375, abs=1e-1)
|
175 |
+
assert t_cp / d_cp == approx(0.375, abs=1e-1)
|
176 |
+
assert t_c / d_c == approx(0.125, abs=1e-1)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_intersection.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import networkx as nx
|
4 |
+
|
5 |
+
|
6 |
+
class TestIntersectionGraph:
|
7 |
+
def test_random_intersection_graph(self):
|
8 |
+
G = nx.uniform_random_intersection_graph(10, 5, 0.5)
|
9 |
+
assert len(G) == 10
|
10 |
+
|
11 |
+
def test_k_random_intersection_graph(self):
|
12 |
+
G = nx.k_random_intersection_graph(10, 5, 2)
|
13 |
+
assert len(G) == 10
|
14 |
+
|
15 |
+
def test_k_random_intersection_graph_seeded(self):
|
16 |
+
G = nx.k_random_intersection_graph(10, 5, 2, seed=1234)
|
17 |
+
assert len(G) == 10
|
18 |
+
|
19 |
+
def test_general_random_intersection_graph(self):
|
20 |
+
G = nx.general_random_intersection_graph(10, 5, [0.1, 0.2, 0.2, 0.1, 0.1])
|
21 |
+
assert len(G) == 10
|
22 |
+
pytest.raises(
|
23 |
+
ValueError,
|
24 |
+
nx.general_random_intersection_graph,
|
25 |
+
10,
|
26 |
+
5,
|
27 |
+
[0.1, 0.2, 0.2, 0.1],
|
28 |
+
)
|
venv/lib/python3.10/site-packages/networkx/generators/tests/test_interval_graph.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Unit tests for the :mod:`networkx.generators.interval_graph` module.
|
2 |
+
|
3 |
+
"""
|
4 |
+
import math
|
5 |
+
|
6 |
+
import pytest
|
7 |
+
|
8 |
+
import networkx as nx
|
9 |
+
from networkx.generators.interval_graph import interval_graph
|
10 |
+
from networkx.utils import edges_equal
|
11 |
+
|
12 |
+
|
13 |
+
class TestIntervalGraph:
|
14 |
+
"""Unit tests for :func:`networkx.generators.interval_graph.interval_graph`"""
|
15 |
+
|
16 |
+
def test_empty(self):
|
17 |
+
"""Tests for trivial case of empty input"""
|
18 |
+
assert len(interval_graph([])) == 0
|
19 |
+
|
20 |
+
def test_interval_graph_check_invalid(self):
|
21 |
+
"""Tests for conditions that raise Exceptions"""
|
22 |
+
|
23 |
+
invalids_having_none = [None, (1, 2)]
|
24 |
+
with pytest.raises(TypeError):
|
25 |
+
interval_graph(invalids_having_none)
|
26 |
+
|
27 |
+
invalids_having_set = [{1, 2}]
|
28 |
+
with pytest.raises(TypeError):
|
29 |
+
interval_graph(invalids_having_set)
|
30 |
+
|
31 |
+
invalids_having_seq_but_not_length2 = [(1, 2, 3)]
|
32 |
+
with pytest.raises(TypeError):
|
33 |
+
interval_graph(invalids_having_seq_but_not_length2)
|
34 |
+
|
35 |
+
invalids_interval = [[3, 2]]
|
36 |
+
with pytest.raises(ValueError):
|
37 |
+
interval_graph(invalids_interval)
|
38 |
+
|
39 |
+
def test_interval_graph_0(self):
|
40 |
+
intervals = [(1, 2), (1, 3)]
|
41 |
+
|
42 |
+
expected_graph = nx.Graph()
|
43 |
+
expected_graph.add_edge(*intervals)
|
44 |
+
|
45 |
+
actual_g = interval_graph(intervals)
|
46 |
+
|
47 |
+
assert set(actual_g.nodes) == set(expected_graph.nodes)
|
48 |
+
assert edges_equal(expected_graph, actual_g)
|
49 |
+
|
50 |
+
def test_interval_graph_1(self):
|
51 |
+
intervals = [(1, 2), (2, 3), (3, 4), (1, 4)]
|
52 |
+
|
53 |
+
expected_graph = nx.Graph()
|
54 |
+
expected_graph.add_nodes_from(intervals)
|
55 |
+
e1 = ((1, 4), (1, 2))
|
56 |
+
e2 = ((1, 4), (2, 3))
|
57 |
+
e3 = ((1, 4), (3, 4))
|
58 |
+
e4 = ((3, 4), (2, 3))
|
59 |
+
e5 = ((1, 2), (2, 3))
|
60 |
+
|
61 |
+
expected_graph.add_edges_from([e1, e2, e3, e4, e5])
|
62 |
+
|
63 |
+
actual_g = interval_graph(intervals)
|
64 |
+
|
65 |
+
assert set(actual_g.nodes) == set(expected_graph.nodes)
|
66 |
+
assert edges_equal(expected_graph, actual_g)
|
67 |
+
|
68 |
+
def test_interval_graph_2(self):
|
69 |
+
intervals = [(1, 2), [3, 5], [6, 8], (9, 10)]
|
70 |
+
|
71 |
+
expected_graph = nx.Graph()
|
72 |
+
expected_graph.add_nodes_from([(1, 2), (3, 5), (6, 8), (9, 10)])
|
73 |
+
|
74 |
+
actual_g = interval_graph(intervals)
|
75 |
+
|
76 |
+
assert set(actual_g.nodes) == set(expected_graph.nodes)
|
77 |
+
assert edges_equal(expected_graph, actual_g)
|
78 |
+
|
79 |
+
def test_interval_graph_3(self):
|
80 |
+
intervals = [(1, 4), [3, 5], [2.5, 4]]
|
81 |
+
|
82 |
+
expected_graph = nx.Graph()
|
83 |
+
expected_graph.add_nodes_from([(1, 4), (3, 5), (2.5, 4)])
|
84 |
+
e1 = ((1, 4), (3, 5))
|
85 |
+
e2 = ((1, 4), (2.5, 4))
|
86 |
+
e3 = ((3, 5), (2.5, 4))
|
87 |
+
|
88 |
+
expected_graph.add_edges_from([e1, e2, e3])
|
89 |
+
|
90 |
+
actual_g = interval_graph(intervals)
|
91 |
+
|
92 |
+
assert set(actual_g.nodes) == set(expected_graph.nodes)
|
93 |
+
assert edges_equal(expected_graph, actual_g)
|
94 |
+
|
95 |
+
def test_interval_graph_4(self):
|
96 |
+
"""test all possible overlaps"""
|
97 |
+
intervals = [
|
98 |
+
(0, 2),
|
99 |
+
(-2, -1),
|
100 |
+
(-2, 0),
|
101 |
+
(-2, 1),
|
102 |
+
(-2, 2),
|
103 |
+
(-2, 3),
|
104 |
+
(0, 1),
|
105 |
+
(0, 2),
|
106 |
+
(0, 3),
|
107 |
+
(1, 2),
|
108 |
+
(1, 3),
|
109 |
+
(2, 3),
|
110 |
+
(3, 4),
|
111 |
+
]
|
112 |
+
|
113 |
+
expected_graph = nx.Graph()
|
114 |
+
expected_graph.add_nodes_from(intervals)
|
115 |
+
expected_nbrs = {
|
116 |
+
(-2, 0),
|
117 |
+
(-2, 1),
|
118 |
+
(-2, 2),
|
119 |
+
(-2, 3),
|
120 |
+
(0, 1),
|
121 |
+
(0, 2),
|
122 |
+
(0, 3),
|
123 |
+
(1, 2),
|
124 |
+
(1, 3),
|
125 |
+
(2, 3),
|
126 |
+
}
|
127 |
+
actual_g = nx.interval_graph(intervals)
|
128 |
+
actual_nbrs = nx.neighbors(actual_g, (0, 2))
|
129 |
+
|
130 |
+
assert set(actual_nbrs) == expected_nbrs
|
131 |
+
|
132 |
+
def test_interval_graph_5(self):
|
133 |
+
"""this test is to see that an interval supports infinite number"""
|
134 |
+
intervals = {(-math.inf, 0), (-1, -1), (0.5, 0.5), (1, 1), (1, math.inf)}
|
135 |
+
|
136 |
+
expected_graph = nx.Graph()
|
137 |
+
expected_graph.add_nodes_from(intervals)
|
138 |
+
e1 = ((-math.inf, 0), (-1, -1))
|
139 |
+
e2 = ((1, 1), (1, math.inf))
|
140 |
+
|
141 |
+
expected_graph.add_edges_from([e1, e2])
|
142 |
+
actual_g = interval_graph(intervals)
|
143 |
+
|
144 |
+
assert set(actual_g.nodes) == set(expected_graph.nodes)
|
145 |
+
assert edges_equal(expected_graph, actual_g)
|