Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/__init__.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/atlas.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/classic.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/cographs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/community.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/degree_seq.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/ego.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/expanders.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/harary_graph.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/intersection.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/interval_graph.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/lattice.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/line.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/nonisomorphic_trees.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_clustered.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_graphs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/small.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/stochastic.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/sudoku.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/time_series.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/__pycache__/triads.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/atlas.dat.gz +3 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__init__.py +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_classic.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_nonisomorphic_trees.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_spectral_graph_forge.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_triads.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/test_atlas.py +75 -0
- .venv/lib/python3.11/site-packages/networkx/generators/tests/test_classic.py +640 -0
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (1.78 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/atlas.cpython-311.pyc
ADDED
|
Binary file (6.12 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/classic.cpython-311.pyc
ADDED
|
Binary file (40.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/cographs.cpython-311.pyc
ADDED
|
Binary file (2.81 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/community.cpython-311.pyc
ADDED
|
Binary file (42.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/degree_seq.cpython-311.pyc
ADDED
|
Binary file (37.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/ego.cpython-311.pyc
ADDED
|
Binary file (2.48 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/expanders.cpython-311.pyc
ADDED
|
Binary file (16.7 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/harary_graph.cpython-311.pyc
ADDED
|
Binary file (7.15 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-311.pyc
ADDED
|
Binary file (20.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/intersection.cpython-311.pyc
ADDED
|
Binary file (5.69 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/interval_graph.cpython-311.pyc
ADDED
|
Binary file (3.08 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/lattice.cpython-311.pyc
ADDED
|
Binary file (22.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/line.cpython-311.pyc
ADDED
|
Binary file (21.7 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/nonisomorphic_trees.cpython-311.pyc
ADDED
|
Binary file (8.77 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_clustered.cpython-311.pyc
ADDED
|
Binary file (5.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_graphs.cpython-311.pyc
ADDED
|
Binary file (58.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/small.cpython-311.pyc
ADDED
|
Binary file (33.1 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/stochastic.cpython-311.pyc
ADDED
|
Binary file (2.65 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/sudoku.cpython-311.pyc
ADDED
|
Binary file (5.07 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/time_series.cpython-311.pyc
ADDED
|
Binary file (3.37 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/triads.cpython-311.pyc
ADDED
|
Binary file (2.88 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/atlas.dat.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:73fc416df0164923607751cb759f4ae81deb5f6550bf25be59c86de3b747e41d
|
| 3 |
+
size 8887
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_atlas.cpython-311.pyc
ADDED
|
Binary file (6.14 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_classic.cpython-311.pyc
ADDED
|
Binary file (50.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-311.pyc
ADDED
|
Binary file (23.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_degree_seq.cpython-311.pyc
ADDED
|
Binary file (15.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-311.pyc
ADDED
|
Binary file (8.35 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-311.pyc
ADDED
|
Binary file (3.36 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-311.pyc
ADDED
|
Binary file (12.8 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_geometric.cpython-311.pyc
ADDED
|
Binary file (36.7 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_harary_graph.cpython-311.pyc
ADDED
|
Binary file (6.24 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-311.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-311.pyc
ADDED
|
Binary file (2.23 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_interval_graph.cpython-311.pyc
ADDED
|
Binary file (7.43 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-311.pyc
ADDED
|
Binary file (4.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_lattice.cpython-311.pyc
ADDED
|
Binary file (19.8 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-311.pyc
ADDED
|
Binary file (21.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-311.pyc
ADDED
|
Binary file (2.84 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_nonisomorphic_trees.cpython-311.pyc
ADDED
|
Binary file (6.19 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_random_clustered.cpython-311.pyc
ADDED
|
Binary file (3.43 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_random_graphs.cpython-311.pyc
ADDED
|
Binary file (35 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_small.cpython-311.pyc
ADDED
|
Binary file (19.8 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_spectral_graph_forge.cpython-311.pyc
ADDED
|
Binary file (2.13 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-311.pyc
ADDED
|
Binary file (2.89 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_trees.cpython-311.pyc
ADDED
|
Binary file (13.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_triads.cpython-311.pyc
ADDED
|
Binary file (1.35 kB). View file
|
|
|
.venv/lib/python3.11/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.11/site-packages/networkx/generators/tests/test_classic.py
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
====================
|
| 3 |
+
Generators - Classic
|
| 4 |
+
====================
|
| 5 |
+
|
| 6 |
+
Unit tests for various classic graph generators in generators/classic.py
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import itertools
|
| 10 |
+
import typing
|
| 11 |
+
|
| 12 |
+
import pytest
|
| 13 |
+
|
| 14 |
+
import networkx as nx
|
| 15 |
+
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
|
| 16 |
+
from networkx.utils import edges_equal, nodes_equal
|
| 17 |
+
|
| 18 |
+
is_isomorphic = graph_could_be_isomorphic
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TestGeneratorClassic:
|
| 22 |
+
def test_balanced_tree(self):
|
| 23 |
+
# balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges
|
| 24 |
+
for r, h in [(2, 2), (3, 3), (6, 2)]:
|
| 25 |
+
t = nx.balanced_tree(r, h)
|
| 26 |
+
order = t.order()
|
| 27 |
+
assert order == (r ** (h + 1) - 1) / (r - 1)
|
| 28 |
+
assert nx.is_connected(t)
|
| 29 |
+
assert t.size() == order - 1
|
| 30 |
+
dh = nx.degree_histogram(t)
|
| 31 |
+
assert dh[0] == 0 # no nodes of 0
|
| 32 |
+
assert dh[1] == r**h # nodes of degree 1 are leaves
|
| 33 |
+
assert dh[r] == 1 # root is degree r
|
| 34 |
+
assert dh[r + 1] == order - r**h - 1 # everyone else is degree r+1
|
| 35 |
+
assert len(dh) == r + 2
|
| 36 |
+
|
| 37 |
+
def test_balanced_tree_star(self):
|
| 38 |
+
# balanced_tree(r,1) is the r-star
|
| 39 |
+
t = nx.balanced_tree(r=2, h=1)
|
| 40 |
+
assert is_isomorphic(t, nx.star_graph(2))
|
| 41 |
+
t = nx.balanced_tree(r=5, h=1)
|
| 42 |
+
assert is_isomorphic(t, nx.star_graph(5))
|
| 43 |
+
t = nx.balanced_tree(r=10, h=1)
|
| 44 |
+
assert is_isomorphic(t, nx.star_graph(10))
|
| 45 |
+
|
| 46 |
+
def test_balanced_tree_path(self):
|
| 47 |
+
"""Tests that the balanced tree with branching factor one is the
|
| 48 |
+
path graph.
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
# A tree of height four has five levels.
|
| 52 |
+
T = nx.balanced_tree(1, 4)
|
| 53 |
+
P = nx.path_graph(5)
|
| 54 |
+
assert is_isomorphic(T, P)
|
| 55 |
+
|
| 56 |
+
def test_full_rary_tree(self):
|
| 57 |
+
r = 2
|
| 58 |
+
n = 9
|
| 59 |
+
t = nx.full_rary_tree(r, n)
|
| 60 |
+
assert t.order() == n
|
| 61 |
+
assert nx.is_connected(t)
|
| 62 |
+
dh = nx.degree_histogram(t)
|
| 63 |
+
assert dh[0] == 0 # no nodes of 0
|
| 64 |
+
assert dh[1] == 5 # nodes of degree 1 are leaves
|
| 65 |
+
assert dh[r] == 1 # root is degree r
|
| 66 |
+
assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1
|
| 67 |
+
assert len(dh) == r + 2
|
| 68 |
+
|
| 69 |
+
def test_full_rary_tree_balanced(self):
|
| 70 |
+
t = nx.full_rary_tree(2, 15)
|
| 71 |
+
th = nx.balanced_tree(2, 3)
|
| 72 |
+
assert is_isomorphic(t, th)
|
| 73 |
+
|
| 74 |
+
def test_full_rary_tree_path(self):
|
| 75 |
+
t = nx.full_rary_tree(1, 10)
|
| 76 |
+
assert is_isomorphic(t, nx.path_graph(10))
|
| 77 |
+
|
| 78 |
+
def test_full_rary_tree_empty(self):
|
| 79 |
+
t = nx.full_rary_tree(0, 10)
|
| 80 |
+
assert is_isomorphic(t, nx.empty_graph(10))
|
| 81 |
+
t = nx.full_rary_tree(3, 0)
|
| 82 |
+
assert is_isomorphic(t, nx.empty_graph(0))
|
| 83 |
+
|
| 84 |
+
def test_full_rary_tree_3_20(self):
|
| 85 |
+
t = nx.full_rary_tree(3, 20)
|
| 86 |
+
assert t.order() == 20
|
| 87 |
+
|
| 88 |
+
def test_barbell_graph(self):
|
| 89 |
+
# number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges)
|
| 90 |
+
# number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1
|
| 91 |
+
m1 = 3
|
| 92 |
+
m2 = 5
|
| 93 |
+
b = nx.barbell_graph(m1, m2)
|
| 94 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 95 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 96 |
+
|
| 97 |
+
m1 = 4
|
| 98 |
+
m2 = 10
|
| 99 |
+
b = nx.barbell_graph(m1, m2)
|
| 100 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 101 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 102 |
+
|
| 103 |
+
m1 = 3
|
| 104 |
+
m2 = 20
|
| 105 |
+
b = nx.barbell_graph(m1, m2)
|
| 106 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 107 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 108 |
+
|
| 109 |
+
# Raise NetworkXError if m1<2
|
| 110 |
+
m1 = 1
|
| 111 |
+
m2 = 20
|
| 112 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 113 |
+
|
| 114 |
+
# Raise NetworkXError if m2<0
|
| 115 |
+
m1 = 5
|
| 116 |
+
m2 = -2
|
| 117 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 118 |
+
|
| 119 |
+
# nx.barbell_graph(2,m) = nx.path_graph(m+4)
|
| 120 |
+
m1 = 2
|
| 121 |
+
m2 = 5
|
| 122 |
+
b = nx.barbell_graph(m1, m2)
|
| 123 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 124 |
+
|
| 125 |
+
m1 = 2
|
| 126 |
+
m2 = 10
|
| 127 |
+
b = nx.barbell_graph(m1, m2)
|
| 128 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 129 |
+
|
| 130 |
+
m1 = 2
|
| 131 |
+
m2 = 20
|
| 132 |
+
b = nx.barbell_graph(m1, m2)
|
| 133 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 134 |
+
|
| 135 |
+
pytest.raises(
|
| 136 |
+
nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph()
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph())
|
| 140 |
+
assert edges_equal(mb.edges(), b.edges())
|
| 141 |
+
|
| 142 |
+
def test_binomial_tree(self):
|
| 143 |
+
graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
|
| 144 |
+
for create_using in graphs:
|
| 145 |
+
for n in range(4):
|
| 146 |
+
b = nx.binomial_tree(n, create_using)
|
| 147 |
+
assert nx.number_of_nodes(b) == 2**n
|
| 148 |
+
assert nx.number_of_edges(b) == (2**n - 1)
|
| 149 |
+
|
| 150 |
+
def test_complete_graph(self):
|
| 151 |
+
# complete_graph(m) is a connected graph with
|
| 152 |
+
# m nodes and m*(m+1)/2 edges
|
| 153 |
+
for m in [0, 1, 3, 5]:
|
| 154 |
+
g = nx.complete_graph(m)
|
| 155 |
+
assert nx.number_of_nodes(g) == m
|
| 156 |
+
assert nx.number_of_edges(g) == m * (m - 1) // 2
|
| 157 |
+
|
| 158 |
+
mg = nx.complete_graph(m, create_using=nx.MultiGraph)
|
| 159 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 160 |
+
|
| 161 |
+
g = nx.complete_graph("abc")
|
| 162 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 163 |
+
assert g.size() == 3
|
| 164 |
+
|
| 165 |
+
# creates a self-loop... should it? <backward compatible says yes>
|
| 166 |
+
g = nx.complete_graph("abcb")
|
| 167 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 168 |
+
assert g.size() == 4
|
| 169 |
+
|
| 170 |
+
g = nx.complete_graph("abcb", create_using=nx.MultiGraph)
|
| 171 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 172 |
+
assert g.size() == 6
|
| 173 |
+
|
| 174 |
+
def test_complete_digraph(self):
|
| 175 |
+
# complete_graph(m) is a connected graph with
|
| 176 |
+
# m nodes and m*(m+1)/2 edges
|
| 177 |
+
for m in [0, 1, 3, 5]:
|
| 178 |
+
g = nx.complete_graph(m, create_using=nx.DiGraph)
|
| 179 |
+
assert nx.number_of_nodes(g) == m
|
| 180 |
+
assert nx.number_of_edges(g) == m * (m - 1)
|
| 181 |
+
|
| 182 |
+
g = nx.complete_graph("abc", create_using=nx.DiGraph)
|
| 183 |
+
assert len(g) == 3
|
| 184 |
+
assert g.size() == 6
|
| 185 |
+
assert g.is_directed()
|
| 186 |
+
|
| 187 |
+
def test_circular_ladder_graph(self):
|
| 188 |
+
G = nx.circular_ladder_graph(5)
|
| 189 |
+
pytest.raises(
|
| 190 |
+
nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph
|
| 191 |
+
)
|
| 192 |
+
mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph)
|
| 193 |
+
assert edges_equal(mG.edges(), G.edges())
|
| 194 |
+
|
| 195 |
+
def test_circulant_graph(self):
|
| 196 |
+
# Ci_n(1) is the cycle graph for all n
|
| 197 |
+
Ci6_1 = nx.circulant_graph(6, [1])
|
| 198 |
+
C6 = nx.cycle_graph(6)
|
| 199 |
+
assert edges_equal(Ci6_1.edges(), C6.edges())
|
| 200 |
+
|
| 201 |
+
# Ci_n(1, 2, ..., n div 2) is the complete graph for all n
|
| 202 |
+
Ci7 = nx.circulant_graph(7, [1, 2, 3])
|
| 203 |
+
K7 = nx.complete_graph(7)
|
| 204 |
+
assert edges_equal(Ci7.edges(), K7.edges())
|
| 205 |
+
|
| 206 |
+
# Ci_6(1, 3) is K_3,3 i.e. the utility graph
|
| 207 |
+
Ci6_1_3 = nx.circulant_graph(6, [1, 3])
|
| 208 |
+
K3_3 = nx.complete_bipartite_graph(3, 3)
|
| 209 |
+
assert is_isomorphic(Ci6_1_3, K3_3)
|
| 210 |
+
|
| 211 |
+
def test_cycle_graph(self):
|
| 212 |
+
G = nx.cycle_graph(4)
|
| 213 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 214 |
+
mG = nx.cycle_graph(4, create_using=nx.MultiGraph)
|
| 215 |
+
assert edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 216 |
+
G = nx.cycle_graph(4, create_using=nx.DiGraph)
|
| 217 |
+
assert not G.has_edge(2, 1)
|
| 218 |
+
assert G.has_edge(1, 2)
|
| 219 |
+
assert G.is_directed()
|
| 220 |
+
|
| 221 |
+
G = nx.cycle_graph("abc")
|
| 222 |
+
assert len(G) == 3
|
| 223 |
+
assert G.size() == 3
|
| 224 |
+
G = nx.cycle_graph("abcb")
|
| 225 |
+
assert len(G) == 3
|
| 226 |
+
assert G.size() == 2
|
| 227 |
+
g = nx.cycle_graph("abc", nx.DiGraph)
|
| 228 |
+
assert len(g) == 3
|
| 229 |
+
assert g.size() == 3
|
| 230 |
+
assert g.is_directed()
|
| 231 |
+
g = nx.cycle_graph("abcb", nx.DiGraph)
|
| 232 |
+
assert len(g) == 3
|
| 233 |
+
assert g.size() == 4
|
| 234 |
+
|
| 235 |
+
def test_dorogovtsev_goltsev_mendes_graph(self):
|
| 236 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(0)
|
| 237 |
+
assert edges_equal(G.edges(), [(0, 1)])
|
| 238 |
+
assert nodes_equal(list(G), [0, 1])
|
| 239 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(1)
|
| 240 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
|
| 241 |
+
assert nx.average_clustering(G) == 1.0
|
| 242 |
+
assert nx.average_shortest_path_length(G) == 1.0
|
| 243 |
+
assert sorted(nx.triangles(G).values()) == [1, 1, 1]
|
| 244 |
+
assert nx.is_planar(G)
|
| 245 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(2)
|
| 246 |
+
assert nx.number_of_nodes(G) == 6
|
| 247 |
+
assert nx.number_of_edges(G) == 9
|
| 248 |
+
assert nx.average_clustering(G) == 0.75
|
| 249 |
+
assert nx.average_shortest_path_length(G) == 1.4
|
| 250 |
+
assert nx.is_planar(G)
|
| 251 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(10)
|
| 252 |
+
assert nx.number_of_nodes(G) == 29526
|
| 253 |
+
assert nx.number_of_edges(G) == 59049
|
| 254 |
+
assert G.degree(0) == 1024
|
| 255 |
+
assert G.degree(1) == 1024
|
| 256 |
+
assert G.degree(2) == 1024
|
| 257 |
+
|
| 258 |
+
with pytest.raises(nx.NetworkXError, match=r"n must be greater than"):
|
| 259 |
+
nx.dorogovtsev_goltsev_mendes_graph(-1)
|
| 260 |
+
with pytest.raises(nx.NetworkXError, match=r"directed graph not supported"):
|
| 261 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.DiGraph)
|
| 262 |
+
with pytest.raises(nx.NetworkXError, match=r"multigraph not supported"):
|
| 263 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.MultiGraph)
|
| 264 |
+
with pytest.raises(nx.NetworkXError):
|
| 265 |
+
nx.dorogovtsev_goltsev_mendes_graph(7, create_using=nx.MultiDiGraph)
|
| 266 |
+
|
| 267 |
+
def test_create_using(self):
|
| 268 |
+
G = nx.empty_graph()
|
| 269 |
+
assert isinstance(G, nx.Graph)
|
| 270 |
+
pytest.raises(TypeError, nx.empty_graph, create_using=0.0)
|
| 271 |
+
pytest.raises(TypeError, nx.empty_graph, create_using="Graph")
|
| 272 |
+
|
| 273 |
+
G = nx.empty_graph(create_using=nx.MultiGraph)
|
| 274 |
+
assert isinstance(G, nx.MultiGraph)
|
| 275 |
+
G = nx.empty_graph(create_using=nx.DiGraph)
|
| 276 |
+
assert isinstance(G, nx.DiGraph)
|
| 277 |
+
|
| 278 |
+
G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph)
|
| 279 |
+
assert isinstance(G, nx.DiGraph)
|
| 280 |
+
G = nx.empty_graph(create_using=None, default=nx.MultiGraph)
|
| 281 |
+
assert isinstance(G, nx.MultiGraph)
|
| 282 |
+
G = nx.empty_graph(default=nx.MultiGraph)
|
| 283 |
+
assert isinstance(G, nx.MultiGraph)
|
| 284 |
+
|
| 285 |
+
G = nx.path_graph(5)
|
| 286 |
+
H = nx.empty_graph(create_using=G)
|
| 287 |
+
assert not H.is_multigraph()
|
| 288 |
+
assert not H.is_directed()
|
| 289 |
+
assert len(H) == 0
|
| 290 |
+
assert G is H
|
| 291 |
+
|
| 292 |
+
H = nx.empty_graph(create_using=nx.MultiGraph())
|
| 293 |
+
assert H.is_multigraph()
|
| 294 |
+
assert not H.is_directed()
|
| 295 |
+
assert G is not H
|
| 296 |
+
|
| 297 |
+
# test for subclasses that also use typing.Protocol. See gh-6243
|
| 298 |
+
class Mixin(typing.Protocol):
|
| 299 |
+
pass
|
| 300 |
+
|
| 301 |
+
class MyGraph(Mixin, nx.DiGraph):
|
| 302 |
+
pass
|
| 303 |
+
|
| 304 |
+
G = nx.empty_graph(create_using=MyGraph)
|
| 305 |
+
|
| 306 |
+
def test_empty_graph(self):
|
| 307 |
+
G = nx.empty_graph()
|
| 308 |
+
assert nx.number_of_nodes(G) == 0
|
| 309 |
+
G = nx.empty_graph(42)
|
| 310 |
+
assert nx.number_of_nodes(G) == 42
|
| 311 |
+
assert nx.number_of_edges(G) == 0
|
| 312 |
+
|
| 313 |
+
G = nx.empty_graph("abc")
|
| 314 |
+
assert len(G) == 3
|
| 315 |
+
assert G.size() == 0
|
| 316 |
+
|
| 317 |
+
# create empty digraph
|
| 318 |
+
G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh"))
|
| 319 |
+
assert nx.number_of_nodes(G) == 42
|
| 320 |
+
assert nx.number_of_edges(G) == 0
|
| 321 |
+
assert isinstance(G, nx.DiGraph)
|
| 322 |
+
|
| 323 |
+
# create empty multigraph
|
| 324 |
+
G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh"))
|
| 325 |
+
assert nx.number_of_nodes(G) == 42
|
| 326 |
+
assert nx.number_of_edges(G) == 0
|
| 327 |
+
assert isinstance(G, nx.MultiGraph)
|
| 328 |
+
|
| 329 |
+
# create empty graph from another
|
| 330 |
+
pete = nx.petersen_graph()
|
| 331 |
+
G = nx.empty_graph(42, create_using=pete)
|
| 332 |
+
assert nx.number_of_nodes(G) == 42
|
| 333 |
+
assert nx.number_of_edges(G) == 0
|
| 334 |
+
assert isinstance(G, nx.Graph)
|
| 335 |
+
|
| 336 |
+
def test_ladder_graph(self):
|
| 337 |
+
for i, G in [
|
| 338 |
+
(0, nx.empty_graph(0)),
|
| 339 |
+
(1, nx.path_graph(2)),
|
| 340 |
+
(2, nx.hypercube_graph(2)),
|
| 341 |
+
(10, nx.grid_graph([2, 10])),
|
| 342 |
+
]:
|
| 343 |
+
assert is_isomorphic(nx.ladder_graph(i), G)
|
| 344 |
+
|
| 345 |
+
pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph)
|
| 346 |
+
|
| 347 |
+
g = nx.ladder_graph(2)
|
| 348 |
+
mg = nx.ladder_graph(2, create_using=nx.MultiGraph)
|
| 349 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 350 |
+
|
| 351 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 5), (4, 10), (3, 20)])
|
| 352 |
+
def test_lollipop_graph_right_sizes(self, m, n):
|
| 353 |
+
G = nx.lollipop_graph(m, n)
|
| 354 |
+
assert nx.number_of_nodes(G) == m + n
|
| 355 |
+
assert nx.number_of_edges(G) == m * (m - 1) / 2 + n
|
| 356 |
+
|
| 357 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("abc", "defg")])
|
| 358 |
+
def test_lollipop_graph_size_node_sequence(self, m, n):
|
| 359 |
+
G = nx.lollipop_graph(m, n)
|
| 360 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
| 361 |
+
assert nx.number_of_edges(G) == len(m) * (len(m) - 1) / 2 + len(n)
|
| 362 |
+
|
| 363 |
+
def test_lollipop_graph_exceptions(self):
|
| 364 |
+
# Raise NetworkXError if m<2
|
| 365 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, -1, 2)
|
| 366 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20)
|
| 367 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "", 20)
|
| 368 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "a", 20)
|
| 369 |
+
|
| 370 |
+
# Raise NetworkXError if n<0
|
| 371 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2)
|
| 372 |
+
|
| 373 |
+
# raise NetworkXError if create_using is directed
|
| 374 |
+
with pytest.raises(nx.NetworkXError):
|
| 375 |
+
nx.lollipop_graph(2, 20, create_using=nx.DiGraph)
|
| 376 |
+
with pytest.raises(nx.NetworkXError):
|
| 377 |
+
nx.lollipop_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 378 |
+
|
| 379 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
| 380 |
+
def test_lollipop_graph_same_as_path_when_m1_is_2(self, m, n):
|
| 381 |
+
G = nx.lollipop_graph(m, n)
|
| 382 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
| 383 |
+
|
| 384 |
+
def test_lollipop_graph_for_multigraph(self):
|
| 385 |
+
G = nx.lollipop_graph(5, 20)
|
| 386 |
+
MG = nx.lollipop_graph(5, 20, create_using=nx.MultiGraph)
|
| 387 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 388 |
+
|
| 389 |
+
@pytest.mark.parametrize(
|
| 390 |
+
("m", "n"),
|
| 391 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
| 392 |
+
)
|
| 393 |
+
def test_lollipop_graph_mixing_input_types(self, m, n):
|
| 394 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
| 395 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
| 396 |
+
assert is_isomorphic(nx.lollipop_graph(m, n), expected)
|
| 397 |
+
|
| 398 |
+
def test_lollipop_graph_non_builtin_ints(self):
|
| 399 |
+
np = pytest.importorskip("numpy")
|
| 400 |
+
G = nx.lollipop_graph(np.int32(4), np.int64(3))
|
| 401 |
+
expected = nx.compose(nx.complete_graph(4), nx.path_graph(range(100, 103)))
|
| 402 |
+
expected.add_edge(0, 100) # Connect complete graph and path graph
|
| 403 |
+
assert is_isomorphic(G, expected)
|
| 404 |
+
|
| 405 |
+
def test_null_graph(self):
|
| 406 |
+
assert nx.number_of_nodes(nx.null_graph()) == 0
|
| 407 |
+
|
| 408 |
+
def test_path_graph(self):
|
| 409 |
+
p = nx.path_graph(0)
|
| 410 |
+
assert is_isomorphic(p, nx.null_graph())
|
| 411 |
+
|
| 412 |
+
p = nx.path_graph(1)
|
| 413 |
+
assert is_isomorphic(p, nx.empty_graph(1))
|
| 414 |
+
|
| 415 |
+
p = nx.path_graph(10)
|
| 416 |
+
assert nx.is_connected(p)
|
| 417 |
+
assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
|
| 418 |
+
assert p.order() - 1 == p.size()
|
| 419 |
+
|
| 420 |
+
dp = nx.path_graph(3, create_using=nx.DiGraph)
|
| 421 |
+
assert dp.has_edge(0, 1)
|
| 422 |
+
assert not dp.has_edge(1, 0)
|
| 423 |
+
|
| 424 |
+
mp = nx.path_graph(10, create_using=nx.MultiGraph)
|
| 425 |
+
assert edges_equal(mp.edges(), p.edges())
|
| 426 |
+
|
| 427 |
+
G = nx.path_graph("abc")
|
| 428 |
+
assert len(G) == 3
|
| 429 |
+
assert G.size() == 2
|
| 430 |
+
G = nx.path_graph("abcb")
|
| 431 |
+
assert len(G) == 3
|
| 432 |
+
assert G.size() == 2
|
| 433 |
+
g = nx.path_graph("abc", nx.DiGraph)
|
| 434 |
+
assert len(g) == 3
|
| 435 |
+
assert g.size() == 2
|
| 436 |
+
assert g.is_directed()
|
| 437 |
+
g = nx.path_graph("abcb", nx.DiGraph)
|
| 438 |
+
assert len(g) == 3
|
| 439 |
+
assert g.size() == 3
|
| 440 |
+
|
| 441 |
+
G = nx.path_graph((1, 2, 3, 2, 4))
|
| 442 |
+
assert G.has_edge(2, 4)
|
| 443 |
+
|
| 444 |
+
def test_star_graph(self):
|
| 445 |
+
assert is_isomorphic(nx.star_graph(""), nx.empty_graph(0))
|
| 446 |
+
assert is_isomorphic(nx.star_graph([]), nx.empty_graph(0))
|
| 447 |
+
assert is_isomorphic(nx.star_graph(0), nx.empty_graph(1))
|
| 448 |
+
assert is_isomorphic(nx.star_graph(1), nx.path_graph(2))
|
| 449 |
+
assert is_isomorphic(nx.star_graph(2), nx.path_graph(3))
|
| 450 |
+
assert is_isomorphic(nx.star_graph(5), nx.complete_bipartite_graph(1, 5))
|
| 451 |
+
|
| 452 |
+
s = nx.star_graph(10)
|
| 453 |
+
assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10]
|
| 454 |
+
|
| 455 |
+
pytest.raises(nx.NetworkXError, nx.star_graph, 10, create_using=nx.DiGraph)
|
| 456 |
+
|
| 457 |
+
ms = nx.star_graph(10, create_using=nx.MultiGraph)
|
| 458 |
+
assert edges_equal(ms.edges(), s.edges())
|
| 459 |
+
|
| 460 |
+
G = nx.star_graph("abc")
|
| 461 |
+
assert len(G) == 3
|
| 462 |
+
assert G.size() == 2
|
| 463 |
+
|
| 464 |
+
G = nx.star_graph("abcb")
|
| 465 |
+
assert len(G) == 3
|
| 466 |
+
assert G.size() == 2
|
| 467 |
+
G = nx.star_graph("abcb", create_using=nx.MultiGraph)
|
| 468 |
+
assert len(G) == 3
|
| 469 |
+
assert G.size() == 3
|
| 470 |
+
|
| 471 |
+
G = nx.star_graph("abcdefg")
|
| 472 |
+
assert len(G) == 7
|
| 473 |
+
assert G.size() == 6
|
| 474 |
+
|
| 475 |
+
def test_non_int_integers_for_star_graph(self):
|
| 476 |
+
np = pytest.importorskip("numpy")
|
| 477 |
+
G = nx.star_graph(np.int32(3))
|
| 478 |
+
assert len(G) == 4
|
| 479 |
+
assert G.size() == 3
|
| 480 |
+
|
| 481 |
+
@pytest.mark.parametrize(("m", "n"), [(3, 0), (3, 5), (4, 10), (3, 20)])
|
| 482 |
+
def test_tadpole_graph_right_sizes(self, m, n):
|
| 483 |
+
G = nx.tadpole_graph(m, n)
|
| 484 |
+
assert nx.number_of_nodes(G) == m + n
|
| 485 |
+
assert nx.number_of_edges(G) == m + n - (m == 2)
|
| 486 |
+
|
| 487 |
+
@pytest.mark.parametrize(("m", "n"), [("ab", ""), ("ab", "c"), ("abc", "defg")])
|
| 488 |
+
def test_tadpole_graph_size_node_sequences(self, m, n):
|
| 489 |
+
G = nx.tadpole_graph(m, n)
|
| 490 |
+
assert nx.number_of_nodes(G) == len(m) + len(n)
|
| 491 |
+
assert nx.number_of_edges(G) == len(m) + len(n) - (len(m) == 2)
|
| 492 |
+
|
| 493 |
+
def test_tadpole_graph_exceptions(self):
|
| 494 |
+
# Raise NetworkXError if m<2
|
| 495 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, -1, 3)
|
| 496 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 0, 3)
|
| 497 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 1, 3)
|
| 498 |
+
|
| 499 |
+
# Raise NetworkXError if n<0
|
| 500 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 5, -2)
|
| 501 |
+
|
| 502 |
+
# Raise NetworkXError for digraphs
|
| 503 |
+
with pytest.raises(nx.NetworkXError):
|
| 504 |
+
nx.tadpole_graph(2, 20, create_using=nx.DiGraph)
|
| 505 |
+
with pytest.raises(nx.NetworkXError):
|
| 506 |
+
nx.tadpole_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 507 |
+
|
| 508 |
+
@pytest.mark.parametrize(("m", "n"), [(2, 0), (2, 5), (2, 10), ("ab", 20)])
|
| 509 |
+
def test_tadpole_graph_same_as_path_when_m_is_2(self, m, n):
|
| 510 |
+
G = nx.tadpole_graph(m, n)
|
| 511 |
+
assert is_isomorphic(G, nx.path_graph(n + 2))
|
| 512 |
+
|
| 513 |
+
@pytest.mark.parametrize("m", [4, 7])
|
| 514 |
+
def test_tadpole_graph_same_as_cycle_when_m2_is_0(self, m):
|
| 515 |
+
G = nx.tadpole_graph(m, 0)
|
| 516 |
+
assert is_isomorphic(G, nx.cycle_graph(m))
|
| 517 |
+
|
| 518 |
+
def test_tadpole_graph_for_multigraph(self):
|
| 519 |
+
G = nx.tadpole_graph(5, 20)
|
| 520 |
+
MG = nx.tadpole_graph(5, 20, create_using=nx.MultiGraph)
|
| 521 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 522 |
+
|
| 523 |
+
@pytest.mark.parametrize(
|
| 524 |
+
("m", "n"),
|
| 525 |
+
[(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])],
|
| 526 |
+
)
|
| 527 |
+
def test_tadpole_graph_mixing_input_types(self, m, n):
|
| 528 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
| 529 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
| 530 |
+
assert is_isomorphic(nx.tadpole_graph(m, n), expected)
|
| 531 |
+
|
| 532 |
+
def test_tadpole_graph_non_builtin_integers(self):
|
| 533 |
+
np = pytest.importorskip("numpy")
|
| 534 |
+
G = nx.tadpole_graph(np.int32(4), np.int64(3))
|
| 535 |
+
expected = nx.compose(nx.cycle_graph(4), nx.path_graph(range(100, 103)))
|
| 536 |
+
expected.add_edge(0, 100) # Connect cycle and path
|
| 537 |
+
assert is_isomorphic(G, expected)
|
| 538 |
+
|
| 539 |
+
def test_trivial_graph(self):
|
| 540 |
+
assert nx.number_of_nodes(nx.trivial_graph()) == 1
|
| 541 |
+
|
| 542 |
+
def test_turan_graph(self):
|
| 543 |
+
assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63
|
| 544 |
+
assert is_isomorphic(
|
| 545 |
+
nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3)
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
def test_wheel_graph(self):
|
| 549 |
+
for n, G in [
|
| 550 |
+
("", nx.null_graph()),
|
| 551 |
+
(0, nx.null_graph()),
|
| 552 |
+
(1, nx.empty_graph(1)),
|
| 553 |
+
(2, nx.path_graph(2)),
|
| 554 |
+
(3, nx.complete_graph(3)),
|
| 555 |
+
(4, nx.complete_graph(4)),
|
| 556 |
+
]:
|
| 557 |
+
g = nx.wheel_graph(n)
|
| 558 |
+
assert is_isomorphic(g, G)
|
| 559 |
+
|
| 560 |
+
g = nx.wheel_graph(10)
|
| 561 |
+
assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]
|
| 562 |
+
|
| 563 |
+
pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph)
|
| 564 |
+
|
| 565 |
+
mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
|
| 566 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 567 |
+
|
| 568 |
+
G = nx.wheel_graph("abc")
|
| 569 |
+
assert len(G) == 3
|
| 570 |
+
assert G.size() == 3
|
| 571 |
+
|
| 572 |
+
G = nx.wheel_graph("abcb")
|
| 573 |
+
assert len(G) == 3
|
| 574 |
+
assert G.size() == 4
|
| 575 |
+
G = nx.wheel_graph("abcb", nx.MultiGraph)
|
| 576 |
+
assert len(G) == 3
|
| 577 |
+
assert G.size() == 6
|
| 578 |
+
|
| 579 |
+
def test_non_int_integers_for_wheel_graph(self):
|
| 580 |
+
np = pytest.importorskip("numpy")
|
| 581 |
+
G = nx.wheel_graph(np.int32(3))
|
| 582 |
+
assert len(G) == 3
|
| 583 |
+
assert G.size() == 3
|
| 584 |
+
|
| 585 |
+
def test_complete_0_partite_graph(self):
|
| 586 |
+
"""Tests that the complete 0-partite graph is the null graph."""
|
| 587 |
+
G = nx.complete_multipartite_graph()
|
| 588 |
+
H = nx.null_graph()
|
| 589 |
+
assert nodes_equal(G, H)
|
| 590 |
+
assert edges_equal(G.edges(), H.edges())
|
| 591 |
+
|
| 592 |
+
def test_complete_1_partite_graph(self):
|
| 593 |
+
"""Tests that the complete 1-partite graph is the empty graph."""
|
| 594 |
+
G = nx.complete_multipartite_graph(3)
|
| 595 |
+
H = nx.empty_graph(3)
|
| 596 |
+
assert nodes_equal(G, H)
|
| 597 |
+
assert edges_equal(G.edges(), H.edges())
|
| 598 |
+
|
| 599 |
+
def test_complete_2_partite_graph(self):
|
| 600 |
+
"""Tests that the complete 2-partite graph is the complete bipartite
|
| 601 |
+
graph.
|
| 602 |
+
|
| 603 |
+
"""
|
| 604 |
+
G = nx.complete_multipartite_graph(2, 3)
|
| 605 |
+
H = nx.complete_bipartite_graph(2, 3)
|
| 606 |
+
assert nodes_equal(G, H)
|
| 607 |
+
assert edges_equal(G.edges(), H.edges())
|
| 608 |
+
|
| 609 |
+
def test_complete_multipartite_graph(self):
|
| 610 |
+
"""Tests for generating the complete multipartite graph."""
|
| 611 |
+
G = nx.complete_multipartite_graph(2, 3, 4)
|
| 612 |
+
blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)]
|
| 613 |
+
# Within each block, no two vertices should be adjacent.
|
| 614 |
+
for block in blocks:
|
| 615 |
+
for u, v in itertools.combinations_with_replacement(block, 2):
|
| 616 |
+
assert v not in G[u]
|
| 617 |
+
assert G.nodes[u] == G.nodes[v]
|
| 618 |
+
# Across blocks, all vertices should be adjacent.
|
| 619 |
+
for block1, block2 in itertools.combinations(blocks, 2):
|
| 620 |
+
for u, v in itertools.product(block1, block2):
|
| 621 |
+
assert v in G[u]
|
| 622 |
+
assert G.nodes[u] != G.nodes[v]
|
| 623 |
+
with pytest.raises(nx.NetworkXError, match="Negative number of nodes"):
|
| 624 |
+
nx.complete_multipartite_graph(2, -3, 4)
|
| 625 |
+
|
| 626 |
+
def test_kneser_graph(self):
|
| 627 |
+
# the petersen graph is a special case of the kneser graph when n=5 and k=2
|
| 628 |
+
assert is_isomorphic(nx.kneser_graph(5, 2), nx.petersen_graph())
|
| 629 |
+
|
| 630 |
+
# when k is 1, the kneser graph returns a complete graph with n vertices
|
| 631 |
+
for i in range(1, 7):
|
| 632 |
+
assert is_isomorphic(nx.kneser_graph(i, 1), nx.complete_graph(i))
|
| 633 |
+
|
| 634 |
+
# the kneser graph of n and n-1 is the empty graph with n vertices
|
| 635 |
+
for j in range(3, 7):
|
| 636 |
+
assert is_isomorphic(nx.kneser_graph(j, j - 1), nx.empty_graph(j))
|
| 637 |
+
|
| 638 |
+
# in general the number of edges of the kneser graph is equal to
|
| 639 |
+
# (n choose k) times (n-k choose k) divided by 2
|
| 640 |
+
assert nx.number_of_edges(nx.kneser_graph(8, 3)) == 280
|