Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc +0 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc +0 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_threshold.cpython-310.pyc +0 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_chains.py +140 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_core.py +266 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_cycles.py +974 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_measures.py +756 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_regular.py +85 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominance.py +285 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominating.py +46 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_efficiency.py +58 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_graphical.py +163 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_matching.py +605 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_polynomials.py +57 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_reciprocity.py +37 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_simple_paths.py +792 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_threshold.py +269 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_time_dependent.py +431 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_triads.py +289 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_vitality.py +41 -0
- valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_voronoi.py +103 -0
- wemm/lib/python3.10/lib2to3/Grammar.txt +196 -0
- wemm/lib/python3.10/lib2to3/__main__.py +4 -0
- wemm/lib/python3.10/lib2to3/btm_utils.py +281 -0
- wemm/lib/python3.10/lib2to3/fixer_base.py +186 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_buffer.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_dict.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_exitfunc.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_filter.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_future.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_has_key.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_imports.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_imports2.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_input.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_itertools.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_itertools_imports.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_metaclass.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_ne.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_next.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_nonzero.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_operator.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_raw_input.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_reduce.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_renames.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_repr.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_set_literal.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_standarderror.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_types.cpython-310.pyc +0 -0
- wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_unicode.cpython-310.pyc +0 -0
valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-310.pyc
ADDED
|
Binary file (9.59 kB). View file
|
|
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-310.pyc
ADDED
|
Binary file (7.91 kB). View file
|
|
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/__pycache__/test_threshold.cpython-310.pyc
ADDED
|
Binary file (11.2 kB). View file
|
|
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_chains.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the chain decomposition functions."""
|
| 2 |
+
from itertools import cycle, islice
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
import networkx as nx
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def cycles(seq):
|
| 10 |
+
"""Yields cyclic permutations of the given sequence.
|
| 11 |
+
|
| 12 |
+
For example::
|
| 13 |
+
|
| 14 |
+
>>> list(cycles("abc"))
|
| 15 |
+
[('a', 'b', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b')]
|
| 16 |
+
|
| 17 |
+
"""
|
| 18 |
+
n = len(seq)
|
| 19 |
+
cycled_seq = cycle(seq)
|
| 20 |
+
for x in seq:
|
| 21 |
+
yield tuple(islice(cycled_seq, n))
|
| 22 |
+
next(cycled_seq)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def cyclic_equals(seq1, seq2):
|
| 26 |
+
"""Decide whether two sequences are equal up to cyclic permutations.
|
| 27 |
+
|
| 28 |
+
For example::
|
| 29 |
+
|
| 30 |
+
>>> cyclic_equals("xyz", "zxy")
|
| 31 |
+
True
|
| 32 |
+
>>> cyclic_equals("xyz", "zyx")
|
| 33 |
+
False
|
| 34 |
+
|
| 35 |
+
"""
|
| 36 |
+
# Cast seq2 to a tuple since `cycles()` yields tuples.
|
| 37 |
+
seq2 = tuple(seq2)
|
| 38 |
+
return any(x == tuple(seq2) for x in cycles(seq1))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class TestChainDecomposition:
|
| 42 |
+
"""Unit tests for the chain decomposition function."""
|
| 43 |
+
|
| 44 |
+
def assertContainsChain(self, chain, expected):
|
| 45 |
+
# A cycle could be expressed in two different orientations, one
|
| 46 |
+
# forward and one backward, so we need to check for cyclic
|
| 47 |
+
# equality in both orientations.
|
| 48 |
+
reversed_chain = list(reversed([tuple(reversed(e)) for e in chain]))
|
| 49 |
+
for candidate in expected:
|
| 50 |
+
if cyclic_equals(chain, candidate):
|
| 51 |
+
break
|
| 52 |
+
if cyclic_equals(reversed_chain, candidate):
|
| 53 |
+
break
|
| 54 |
+
else:
|
| 55 |
+
self.fail("chain not found")
|
| 56 |
+
|
| 57 |
+
def test_decomposition(self):
|
| 58 |
+
edges = [
|
| 59 |
+
# DFS tree edges.
|
| 60 |
+
(1, 2),
|
| 61 |
+
(2, 3),
|
| 62 |
+
(3, 4),
|
| 63 |
+
(3, 5),
|
| 64 |
+
(5, 6),
|
| 65 |
+
(6, 7),
|
| 66 |
+
(7, 8),
|
| 67 |
+
(5, 9),
|
| 68 |
+
(9, 10),
|
| 69 |
+
# Nontree edges.
|
| 70 |
+
(1, 3),
|
| 71 |
+
(1, 4),
|
| 72 |
+
(2, 5),
|
| 73 |
+
(5, 10),
|
| 74 |
+
(6, 8),
|
| 75 |
+
]
|
| 76 |
+
G = nx.Graph(edges)
|
| 77 |
+
expected = [
|
| 78 |
+
[(1, 3), (3, 2), (2, 1)],
|
| 79 |
+
[(1, 4), (4, 3)],
|
| 80 |
+
[(2, 5), (5, 3)],
|
| 81 |
+
[(5, 10), (10, 9), (9, 5)],
|
| 82 |
+
[(6, 8), (8, 7), (7, 6)],
|
| 83 |
+
]
|
| 84 |
+
chains = list(nx.chain_decomposition(G, root=1))
|
| 85 |
+
assert len(chains) == len(expected)
|
| 86 |
+
|
| 87 |
+
# This chain decomposition isn't unique
|
| 88 |
+
# for chain in chains:
|
| 89 |
+
# print(chain)
|
| 90 |
+
# self.assertContainsChain(chain, expected)
|
| 91 |
+
|
| 92 |
+
def test_barbell_graph(self):
|
| 93 |
+
# The (3, 0) barbell graph has two triangles joined by a single edge.
|
| 94 |
+
G = nx.barbell_graph(3, 0)
|
| 95 |
+
chains = list(nx.chain_decomposition(G, root=0))
|
| 96 |
+
expected = [[(0, 1), (1, 2), (2, 0)], [(3, 4), (4, 5), (5, 3)]]
|
| 97 |
+
assert len(chains) == len(expected)
|
| 98 |
+
for chain in chains:
|
| 99 |
+
self.assertContainsChain(chain, expected)
|
| 100 |
+
|
| 101 |
+
def test_disconnected_graph(self):
|
| 102 |
+
"""Test for a graph with multiple connected components."""
|
| 103 |
+
G = nx.barbell_graph(3, 0)
|
| 104 |
+
H = nx.barbell_graph(3, 0)
|
| 105 |
+
mapping = dict(zip(range(6), "abcdef"))
|
| 106 |
+
nx.relabel_nodes(H, mapping, copy=False)
|
| 107 |
+
G = nx.union(G, H)
|
| 108 |
+
chains = list(nx.chain_decomposition(G))
|
| 109 |
+
expected = [
|
| 110 |
+
[(0, 1), (1, 2), (2, 0)],
|
| 111 |
+
[(3, 4), (4, 5), (5, 3)],
|
| 112 |
+
[("a", "b"), ("b", "c"), ("c", "a")],
|
| 113 |
+
[("d", "e"), ("e", "f"), ("f", "d")],
|
| 114 |
+
]
|
| 115 |
+
assert len(chains) == len(expected)
|
| 116 |
+
for chain in chains:
|
| 117 |
+
self.assertContainsChain(chain, expected)
|
| 118 |
+
|
| 119 |
+
def test_disconnected_graph_root_node(self):
|
| 120 |
+
"""Test for a single component of a disconnected graph."""
|
| 121 |
+
G = nx.barbell_graph(3, 0)
|
| 122 |
+
H = nx.barbell_graph(3, 0)
|
| 123 |
+
mapping = dict(zip(range(6), "abcdef"))
|
| 124 |
+
nx.relabel_nodes(H, mapping, copy=False)
|
| 125 |
+
G = nx.union(G, H)
|
| 126 |
+
chains = list(nx.chain_decomposition(G, root="a"))
|
| 127 |
+
expected = [
|
| 128 |
+
[("a", "b"), ("b", "c"), ("c", "a")],
|
| 129 |
+
[("d", "e"), ("e", "f"), ("f", "d")],
|
| 130 |
+
]
|
| 131 |
+
assert len(chains) == len(expected)
|
| 132 |
+
for chain in chains:
|
| 133 |
+
self.assertContainsChain(chain, expected)
|
| 134 |
+
|
| 135 |
+
def test_chain_decomposition_root_not_in_G(self):
|
| 136 |
+
"""Test chain decomposition when root is not in graph"""
|
| 137 |
+
G = nx.Graph()
|
| 138 |
+
G.add_nodes_from([1, 2, 3])
|
| 139 |
+
with pytest.raises(nx.NodeNotFound):
|
| 140 |
+
nx.has_bridges(G, root=6)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_core.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils import nodes_equal
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestCore:
|
| 8 |
+
@classmethod
|
| 9 |
+
def setup_class(cls):
|
| 10 |
+
# G is the example graph in Figure 1 from Batagelj and
|
| 11 |
+
# Zaversnik's paper titled An O(m) Algorithm for Cores
|
| 12 |
+
# Decomposition of Networks, 2003,
|
| 13 |
+
# http://arXiv.org/abs/cs/0310049. With nodes labeled as
|
| 14 |
+
# shown, the 3-core is given by nodes 1-8, the 2-core by nodes
|
| 15 |
+
# 9-16, the 1-core by nodes 17-20 and node 21 is in the
|
| 16 |
+
# 0-core.
|
| 17 |
+
t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
|
| 18 |
+
t2 = nx.convert_node_labels_to_integers(t1, 5)
|
| 19 |
+
G = nx.union(t1, t2)
|
| 20 |
+
G.add_edges_from(
|
| 21 |
+
[
|
| 22 |
+
(3, 7),
|
| 23 |
+
(2, 11),
|
| 24 |
+
(11, 5),
|
| 25 |
+
(11, 12),
|
| 26 |
+
(5, 12),
|
| 27 |
+
(12, 19),
|
| 28 |
+
(12, 18),
|
| 29 |
+
(3, 9),
|
| 30 |
+
(7, 9),
|
| 31 |
+
(7, 10),
|
| 32 |
+
(9, 10),
|
| 33 |
+
(9, 20),
|
| 34 |
+
(17, 13),
|
| 35 |
+
(13, 14),
|
| 36 |
+
(14, 15),
|
| 37 |
+
(15, 16),
|
| 38 |
+
(16, 13),
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
G.add_node(21)
|
| 42 |
+
cls.G = G
|
| 43 |
+
|
| 44 |
+
# Create the graph H resulting from the degree sequence
|
| 45 |
+
# [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.
|
| 46 |
+
|
| 47 |
+
degseq = [0, 1, 2, 2, 2, 2, 3]
|
| 48 |
+
H = nx.havel_hakimi_graph(degseq)
|
| 49 |
+
mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
|
| 50 |
+
cls.H = nx.relabel_nodes(H, mapping)
|
| 51 |
+
|
| 52 |
+
def test_trivial(self):
|
| 53 |
+
"""Empty graph"""
|
| 54 |
+
G = nx.Graph()
|
| 55 |
+
assert nx.core_number(G) == {}
|
| 56 |
+
|
| 57 |
+
def test_core_number(self):
|
| 58 |
+
core = nx.core_number(self.G)
|
| 59 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)]
|
| 60 |
+
assert nodes_equal(nodes_by_core[0], [21])
|
| 61 |
+
assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20])
|
| 62 |
+
assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16])
|
| 63 |
+
assert nodes_equal(nodes_by_core[3], [1, 2, 3, 4, 5, 6, 7, 8])
|
| 64 |
+
|
| 65 |
+
def test_core_number2(self):
|
| 66 |
+
core = nx.core_number(self.H)
|
| 67 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(3)]
|
| 68 |
+
assert nodes_equal(nodes_by_core[0], [0])
|
| 69 |
+
assert nodes_equal(nodes_by_core[1], [1, 3])
|
| 70 |
+
assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6])
|
| 71 |
+
|
| 72 |
+
def test_core_number_multigraph(self):
|
| 73 |
+
G = nx.complete_graph(3)
|
| 74 |
+
G = nx.MultiGraph(G)
|
| 75 |
+
G.add_edge(1, 2)
|
| 76 |
+
with pytest.raises(
|
| 77 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 78 |
+
):
|
| 79 |
+
nx.core_number(G)
|
| 80 |
+
|
| 81 |
+
def test_core_number_self_loop(self):
|
| 82 |
+
G = nx.cycle_graph(3)
|
| 83 |
+
G.add_edge(0, 0)
|
| 84 |
+
with pytest.raises(
|
| 85 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
| 86 |
+
):
|
| 87 |
+
nx.core_number(G)
|
| 88 |
+
|
| 89 |
+
def test_directed_core_number(self):
|
| 90 |
+
"""core number had a bug for directed graphs found in issue #1959"""
|
| 91 |
+
# small example where too timid edge removal can make cn[2] = 3
|
| 92 |
+
G = nx.DiGraph()
|
| 93 |
+
edges = [(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)]
|
| 94 |
+
G.add_edges_from(edges)
|
| 95 |
+
assert nx.core_number(G) == {1: 2, 2: 2, 3: 2, 4: 2}
|
| 96 |
+
# small example where too aggressive edge removal can make cn[2] = 2
|
| 97 |
+
more_edges = [(1, 5), (3, 5), (4, 5), (3, 6), (4, 6), (5, 6)]
|
| 98 |
+
G.add_edges_from(more_edges)
|
| 99 |
+
assert nx.core_number(G) == {1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3}
|
| 100 |
+
|
| 101 |
+
def test_main_core(self):
|
| 102 |
+
main_core_subgraph = nx.k_core(self.H)
|
| 103 |
+
assert sorted(main_core_subgraph.nodes()) == [2, 4, 5, 6]
|
| 104 |
+
|
| 105 |
+
def test_k_core(self):
|
| 106 |
+
# k=0
|
| 107 |
+
k_core_subgraph = nx.k_core(self.H, k=0)
|
| 108 |
+
assert sorted(k_core_subgraph.nodes()) == sorted(self.H.nodes())
|
| 109 |
+
# k=1
|
| 110 |
+
k_core_subgraph = nx.k_core(self.H, k=1)
|
| 111 |
+
assert sorted(k_core_subgraph.nodes()) == [1, 2, 3, 4, 5, 6]
|
| 112 |
+
# k = 2
|
| 113 |
+
k_core_subgraph = nx.k_core(self.H, k=2)
|
| 114 |
+
assert sorted(k_core_subgraph.nodes()) == [2, 4, 5, 6]
|
| 115 |
+
|
| 116 |
+
def test_k_core_multigraph(self):
|
| 117 |
+
core_number = nx.core_number(self.H)
|
| 118 |
+
H = nx.MultiGraph(self.H)
|
| 119 |
+
with pytest.deprecated_call():
|
| 120 |
+
nx.k_core(H, k=0, core_number=core_number)
|
| 121 |
+
|
| 122 |
+
def test_main_crust(self):
|
| 123 |
+
main_crust_subgraph = nx.k_crust(self.H)
|
| 124 |
+
assert sorted(main_crust_subgraph.nodes()) == [0, 1, 3]
|
| 125 |
+
|
| 126 |
+
def test_k_crust(self):
|
| 127 |
+
# k = 0
|
| 128 |
+
k_crust_subgraph = nx.k_crust(self.H, k=2)
|
| 129 |
+
assert sorted(k_crust_subgraph.nodes()) == sorted(self.H.nodes())
|
| 130 |
+
# k=1
|
| 131 |
+
k_crust_subgraph = nx.k_crust(self.H, k=1)
|
| 132 |
+
assert sorted(k_crust_subgraph.nodes()) == [0, 1, 3]
|
| 133 |
+
# k=2
|
| 134 |
+
k_crust_subgraph = nx.k_crust(self.H, k=0)
|
| 135 |
+
assert sorted(k_crust_subgraph.nodes()) == [0]
|
| 136 |
+
|
| 137 |
+
def test_k_crust_multigraph(self):
|
| 138 |
+
core_number = nx.core_number(self.H)
|
| 139 |
+
H = nx.MultiGraph(self.H)
|
| 140 |
+
with pytest.deprecated_call():
|
| 141 |
+
nx.k_crust(H, k=0, core_number=core_number)
|
| 142 |
+
|
| 143 |
+
def test_main_shell(self):
|
| 144 |
+
main_shell_subgraph = nx.k_shell(self.H)
|
| 145 |
+
assert sorted(main_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
| 146 |
+
|
| 147 |
+
def test_k_shell(self):
|
| 148 |
+
# k=0
|
| 149 |
+
k_shell_subgraph = nx.k_shell(self.H, k=2)
|
| 150 |
+
assert sorted(k_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
| 151 |
+
# k=1
|
| 152 |
+
k_shell_subgraph = nx.k_shell(self.H, k=1)
|
| 153 |
+
assert sorted(k_shell_subgraph.nodes()) == [1, 3]
|
| 154 |
+
# k=2
|
| 155 |
+
k_shell_subgraph = nx.k_shell(self.H, k=0)
|
| 156 |
+
assert sorted(k_shell_subgraph.nodes()) == [0]
|
| 157 |
+
|
| 158 |
+
def test_k_shell_multigraph(self):
|
| 159 |
+
core_number = nx.core_number(self.H)
|
| 160 |
+
H = nx.MultiGraph(self.H)
|
| 161 |
+
with pytest.deprecated_call():
|
| 162 |
+
nx.k_shell(H, k=0, core_number=core_number)
|
| 163 |
+
|
| 164 |
+
def test_k_corona(self):
|
| 165 |
+
# k=0
|
| 166 |
+
k_corona_subgraph = nx.k_corona(self.H, k=2)
|
| 167 |
+
assert sorted(k_corona_subgraph.nodes()) == [2, 4, 5, 6]
|
| 168 |
+
# k=1
|
| 169 |
+
k_corona_subgraph = nx.k_corona(self.H, k=1)
|
| 170 |
+
assert sorted(k_corona_subgraph.nodes()) == [1]
|
| 171 |
+
# k=2
|
| 172 |
+
k_corona_subgraph = nx.k_corona(self.H, k=0)
|
| 173 |
+
assert sorted(k_corona_subgraph.nodes()) == [0]
|
| 174 |
+
|
| 175 |
+
def test_k_corona_multigraph(self):
|
| 176 |
+
core_number = nx.core_number(self.H)
|
| 177 |
+
H = nx.MultiGraph(self.H)
|
| 178 |
+
with pytest.deprecated_call():
|
| 179 |
+
nx.k_corona(H, k=0, core_number=core_number)
|
| 180 |
+
|
| 181 |
+
def test_k_truss(self):
|
| 182 |
+
# k=-1
|
| 183 |
+
k_truss_subgraph = nx.k_truss(self.G, -1)
|
| 184 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 185 |
+
# k=0
|
| 186 |
+
k_truss_subgraph = nx.k_truss(self.G, 0)
|
| 187 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 188 |
+
# k=1
|
| 189 |
+
k_truss_subgraph = nx.k_truss(self.G, 1)
|
| 190 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 191 |
+
# k=2
|
| 192 |
+
k_truss_subgraph = nx.k_truss(self.G, 2)
|
| 193 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 194 |
+
# k=3
|
| 195 |
+
k_truss_subgraph = nx.k_truss(self.G, 3)
|
| 196 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 13))
|
| 197 |
+
|
| 198 |
+
k_truss_subgraph = nx.k_truss(self.G, 4)
|
| 199 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 9))
|
| 200 |
+
|
| 201 |
+
k_truss_subgraph = nx.k_truss(self.G, 5)
|
| 202 |
+
assert sorted(k_truss_subgraph.nodes()) == []
|
| 203 |
+
|
| 204 |
+
def test_k_truss_digraph(self):
|
| 205 |
+
G = nx.complete_graph(3)
|
| 206 |
+
G = nx.DiGraph(G)
|
| 207 |
+
G.add_edge(2, 1)
|
| 208 |
+
with pytest.raises(
|
| 209 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
| 210 |
+
):
|
| 211 |
+
nx.k_truss(G, k=1)
|
| 212 |
+
|
| 213 |
+
def test_k_truss_multigraph(self):
|
| 214 |
+
G = nx.complete_graph(3)
|
| 215 |
+
G = nx.MultiGraph(G)
|
| 216 |
+
G.add_edge(1, 2)
|
| 217 |
+
with pytest.raises(
|
| 218 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 219 |
+
):
|
| 220 |
+
nx.k_truss(G, k=1)
|
| 221 |
+
|
| 222 |
+
def test_k_truss_self_loop(self):
|
| 223 |
+
G = nx.cycle_graph(3)
|
| 224 |
+
G.add_edge(0, 0)
|
| 225 |
+
with pytest.raises(
|
| 226 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
| 227 |
+
):
|
| 228 |
+
nx.k_truss(G, k=1)
|
| 229 |
+
|
| 230 |
+
def test_onion_layers(self):
|
| 231 |
+
layers = nx.onion_layers(self.G)
|
| 232 |
+
nodes_by_layer = [
|
| 233 |
+
sorted(n for n in layers if layers[n] == val) for val in range(1, 7)
|
| 234 |
+
]
|
| 235 |
+
assert nodes_equal(nodes_by_layer[0], [21])
|
| 236 |
+
assert nodes_equal(nodes_by_layer[1], [17, 18, 19, 20])
|
| 237 |
+
assert nodes_equal(nodes_by_layer[2], [10, 12, 13, 14, 15, 16])
|
| 238 |
+
assert nodes_equal(nodes_by_layer[3], [9, 11])
|
| 239 |
+
assert nodes_equal(nodes_by_layer[4], [1, 2, 4, 5, 6, 8])
|
| 240 |
+
assert nodes_equal(nodes_by_layer[5], [3, 7])
|
| 241 |
+
|
| 242 |
+
def test_onion_digraph(self):
|
| 243 |
+
G = nx.complete_graph(3)
|
| 244 |
+
G = nx.DiGraph(G)
|
| 245 |
+
G.add_edge(2, 1)
|
| 246 |
+
with pytest.raises(
|
| 247 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
| 248 |
+
):
|
| 249 |
+
nx.onion_layers(G)
|
| 250 |
+
|
| 251 |
+
def test_onion_multigraph(self):
|
| 252 |
+
G = nx.complete_graph(3)
|
| 253 |
+
G = nx.MultiGraph(G)
|
| 254 |
+
G.add_edge(1, 2)
|
| 255 |
+
with pytest.raises(
|
| 256 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 257 |
+
):
|
| 258 |
+
nx.onion_layers(G)
|
| 259 |
+
|
| 260 |
+
def test_onion_self_loop(self):
|
| 261 |
+
G = nx.cycle_graph(3)
|
| 262 |
+
G.add_edge(0, 0)
|
| 263 |
+
with pytest.raises(
|
| 264 |
+
nx.NetworkXNotImplemented, match="Input graph contains self loops"
|
| 265 |
+
):
|
| 266 |
+
nx.onion_layers(G)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_cycles.py
ADDED
|
@@ -0,0 +1,974 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from itertools import chain, islice, tee
|
| 2 |
+
from math import inf
|
| 3 |
+
from random import shuffle
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
from networkx.algorithms.traversal.edgedfs import FORWARD, REVERSE
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def check_independent(basis):
|
| 12 |
+
if len(basis) == 0:
|
| 13 |
+
return
|
| 14 |
+
|
| 15 |
+
np = pytest.importorskip("numpy")
|
| 16 |
+
sp = pytest.importorskip("scipy") # Required by incidence_matrix
|
| 17 |
+
|
| 18 |
+
H = nx.Graph()
|
| 19 |
+
for b in basis:
|
| 20 |
+
nx.add_cycle(H, b)
|
| 21 |
+
inc = nx.incidence_matrix(H, oriented=True)
|
| 22 |
+
rank = np.linalg.matrix_rank(inc.toarray(), tol=None, hermitian=False)
|
| 23 |
+
assert inc.shape[1] - rank == len(basis)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TestCycles:
|
| 27 |
+
@classmethod
|
| 28 |
+
def setup_class(cls):
|
| 29 |
+
G = nx.Graph()
|
| 30 |
+
nx.add_cycle(G, [0, 1, 2, 3])
|
| 31 |
+
nx.add_cycle(G, [0, 3, 4, 5])
|
| 32 |
+
nx.add_cycle(G, [0, 1, 6, 7, 8])
|
| 33 |
+
G.add_edge(8, 9)
|
| 34 |
+
cls.G = G
|
| 35 |
+
|
| 36 |
+
def is_cyclic_permutation(self, a, b):
|
| 37 |
+
n = len(a)
|
| 38 |
+
if len(b) != n:
|
| 39 |
+
return False
|
| 40 |
+
l = a + a
|
| 41 |
+
return any(l[i : i + n] == b for i in range(n))
|
| 42 |
+
|
| 43 |
+
def test_cycle_basis(self):
|
| 44 |
+
G = self.G
|
| 45 |
+
cy = nx.cycle_basis(G, 0)
|
| 46 |
+
sort_cy = sorted(sorted(c) for c in cy)
|
| 47 |
+
assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
|
| 48 |
+
cy = nx.cycle_basis(G, 1)
|
| 49 |
+
sort_cy = sorted(sorted(c) for c in cy)
|
| 50 |
+
assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
|
| 51 |
+
cy = nx.cycle_basis(G, 9)
|
| 52 |
+
sort_cy = sorted(sorted(c) for c in cy)
|
| 53 |
+
assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]
|
| 54 |
+
# test disconnected graphs
|
| 55 |
+
nx.add_cycle(G, "ABC")
|
| 56 |
+
cy = nx.cycle_basis(G, 9)
|
| 57 |
+
sort_cy = sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
|
| 58 |
+
assert sort_cy == [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5], ["A", "B", "C"]]
|
| 59 |
+
|
| 60 |
+
def test_cycle_basis2(self):
|
| 61 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 62 |
+
G = nx.DiGraph()
|
| 63 |
+
cy = nx.cycle_basis(G, 0)
|
| 64 |
+
|
| 65 |
+
def test_cycle_basis3(self):
|
| 66 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 67 |
+
G = nx.MultiGraph()
|
| 68 |
+
cy = nx.cycle_basis(G, 0)
|
| 69 |
+
|
| 70 |
+
def test_cycle_basis_ordered(self):
|
| 71 |
+
# see gh-6654 replace sets with (ordered) dicts
|
| 72 |
+
G = nx.cycle_graph(5)
|
| 73 |
+
G.update(nx.cycle_graph(range(3, 8)))
|
| 74 |
+
cbG = nx.cycle_basis(G)
|
| 75 |
+
|
| 76 |
+
perm = {1: 0, 0: 1} # switch 0 and 1
|
| 77 |
+
H = nx.relabel_nodes(G, perm)
|
| 78 |
+
cbH = [[perm.get(n, n) for n in cyc] for cyc in nx.cycle_basis(H)]
|
| 79 |
+
assert cbG == cbH
|
| 80 |
+
|
| 81 |
+
def test_cycle_basis_self_loop(self):
|
| 82 |
+
"""Tests the function for graphs with self loops"""
|
| 83 |
+
G = nx.Graph()
|
| 84 |
+
nx.add_cycle(G, [0, 1, 2, 3])
|
| 85 |
+
nx.add_cycle(G, [0, 0, 6, 2])
|
| 86 |
+
cy = nx.cycle_basis(G)
|
| 87 |
+
sort_cy = sorted(sorted(c) for c in cy)
|
| 88 |
+
assert sort_cy == [[0], [0, 1, 2], [0, 2, 3], [0, 2, 6]]
|
| 89 |
+
|
| 90 |
+
def test_simple_cycles(self):
|
| 91 |
+
edges = [(0, 0), (0, 1), (0, 2), (1, 2), (2, 0), (2, 1), (2, 2)]
|
| 92 |
+
G = nx.DiGraph(edges)
|
| 93 |
+
cc = sorted(nx.simple_cycles(G))
|
| 94 |
+
ca = [[0], [0, 1, 2], [0, 2], [1, 2], [2]]
|
| 95 |
+
assert len(cc) == len(ca)
|
| 96 |
+
for c in cc:
|
| 97 |
+
assert any(self.is_cyclic_permutation(c, rc) for rc in ca)
|
| 98 |
+
|
| 99 |
+
def test_simple_cycles_singleton(self):
|
| 100 |
+
G = nx.Graph([(0, 0)]) # self-loop
|
| 101 |
+
assert list(nx.simple_cycles(G)) == [[0]]
|
| 102 |
+
|
| 103 |
+
def test_unsortable(self):
|
| 104 |
+
# this test ensures that graphs whose nodes without an intrinsic
|
| 105 |
+
# ordering do not cause issues
|
| 106 |
+
G = nx.DiGraph()
|
| 107 |
+
nx.add_cycle(G, ["a", 1])
|
| 108 |
+
c = list(nx.simple_cycles(G))
|
| 109 |
+
assert len(c) == 1
|
| 110 |
+
|
| 111 |
+
def test_simple_cycles_small(self):
|
| 112 |
+
G = nx.DiGraph()
|
| 113 |
+
nx.add_cycle(G, [1, 2, 3])
|
| 114 |
+
c = sorted(nx.simple_cycles(G))
|
| 115 |
+
assert len(c) == 1
|
| 116 |
+
assert self.is_cyclic_permutation(c[0], [1, 2, 3])
|
| 117 |
+
nx.add_cycle(G, [10, 20, 30])
|
| 118 |
+
cc = sorted(nx.simple_cycles(G))
|
| 119 |
+
assert len(cc) == 2
|
| 120 |
+
ca = [[1, 2, 3], [10, 20, 30]]
|
| 121 |
+
for c in cc:
|
| 122 |
+
assert any(self.is_cyclic_permutation(c, rc) for rc in ca)
|
| 123 |
+
|
| 124 |
+
def test_simple_cycles_empty(self):
|
| 125 |
+
G = nx.DiGraph()
|
| 126 |
+
assert list(nx.simple_cycles(G)) == []
|
| 127 |
+
|
| 128 |
+
def worst_case_graph(self, k):
|
| 129 |
+
# see figure 1 in Johnson's paper
|
| 130 |
+
# this graph has exactly 3k simple cycles
|
| 131 |
+
G = nx.DiGraph()
|
| 132 |
+
for n in range(2, k + 2):
|
| 133 |
+
G.add_edge(1, n)
|
| 134 |
+
G.add_edge(n, k + 2)
|
| 135 |
+
G.add_edge(2 * k + 1, 1)
|
| 136 |
+
for n in range(k + 2, 2 * k + 2):
|
| 137 |
+
G.add_edge(n, 2 * k + 2)
|
| 138 |
+
G.add_edge(n, n + 1)
|
| 139 |
+
G.add_edge(2 * k + 3, k + 2)
|
| 140 |
+
for n in range(2 * k + 3, 3 * k + 3):
|
| 141 |
+
G.add_edge(2 * k + 2, n)
|
| 142 |
+
G.add_edge(n, 3 * k + 3)
|
| 143 |
+
G.add_edge(3 * k + 3, 2 * k + 2)
|
| 144 |
+
return G
|
| 145 |
+
|
| 146 |
+
def test_worst_case_graph(self):
|
| 147 |
+
# see figure 1 in Johnson's paper
|
| 148 |
+
for k in range(3, 10):
|
| 149 |
+
G = self.worst_case_graph(k)
|
| 150 |
+
l = len(list(nx.simple_cycles(G)))
|
| 151 |
+
assert l == 3 * k
|
| 152 |
+
|
| 153 |
+
def test_recursive_simple_and_not(self):
|
| 154 |
+
for k in range(2, 10):
|
| 155 |
+
G = self.worst_case_graph(k)
|
| 156 |
+
cc = sorted(nx.simple_cycles(G))
|
| 157 |
+
rcc = sorted(nx.recursive_simple_cycles(G))
|
| 158 |
+
assert len(cc) == len(rcc)
|
| 159 |
+
for c in cc:
|
| 160 |
+
assert any(self.is_cyclic_permutation(c, r) for r in rcc)
|
| 161 |
+
for rc in rcc:
|
| 162 |
+
assert any(self.is_cyclic_permutation(rc, c) for c in cc)
|
| 163 |
+
|
| 164 |
+
def test_simple_graph_with_reported_bug(self):
|
| 165 |
+
G = nx.DiGraph()
|
| 166 |
+
edges = [
|
| 167 |
+
(0, 2),
|
| 168 |
+
(0, 3),
|
| 169 |
+
(1, 0),
|
| 170 |
+
(1, 3),
|
| 171 |
+
(2, 1),
|
| 172 |
+
(2, 4),
|
| 173 |
+
(3, 2),
|
| 174 |
+
(3, 4),
|
| 175 |
+
(4, 0),
|
| 176 |
+
(4, 1),
|
| 177 |
+
(4, 5),
|
| 178 |
+
(5, 0),
|
| 179 |
+
(5, 1),
|
| 180 |
+
(5, 2),
|
| 181 |
+
(5, 3),
|
| 182 |
+
]
|
| 183 |
+
G.add_edges_from(edges)
|
| 184 |
+
cc = sorted(nx.simple_cycles(G))
|
| 185 |
+
assert len(cc) == 26
|
| 186 |
+
rcc = sorted(nx.recursive_simple_cycles(G))
|
| 187 |
+
assert len(cc) == len(rcc)
|
| 188 |
+
for c in cc:
|
| 189 |
+
assert any(self.is_cyclic_permutation(c, rc) for rc in rcc)
|
| 190 |
+
for rc in rcc:
|
| 191 |
+
assert any(self.is_cyclic_permutation(rc, c) for c in cc)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def pairwise(iterable):
|
| 195 |
+
a, b = tee(iterable)
|
| 196 |
+
next(b, None)
|
| 197 |
+
return zip(a, b)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def cycle_edges(c):
|
| 201 |
+
return pairwise(chain(c, islice(c, 1)))
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def directed_cycle_edgeset(c):
|
| 205 |
+
return frozenset(cycle_edges(c))
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def undirected_cycle_edgeset(c):
|
| 209 |
+
if len(c) == 1:
|
| 210 |
+
return frozenset(cycle_edges(c))
|
| 211 |
+
return frozenset(map(frozenset, cycle_edges(c)))
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def multigraph_cycle_edgeset(c):
|
| 215 |
+
if len(c) <= 2:
|
| 216 |
+
return frozenset(cycle_edges(c))
|
| 217 |
+
else:
|
| 218 |
+
return frozenset(map(frozenset, cycle_edges(c)))
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
class TestCycleEnumeration:
|
| 222 |
+
@staticmethod
|
| 223 |
+
def K(n):
|
| 224 |
+
return nx.complete_graph(n)
|
| 225 |
+
|
| 226 |
+
@staticmethod
|
| 227 |
+
def D(n):
|
| 228 |
+
return nx.complete_graph(n).to_directed()
|
| 229 |
+
|
| 230 |
+
@staticmethod
|
| 231 |
+
def edgeset_function(g):
|
| 232 |
+
if g.is_directed():
|
| 233 |
+
return directed_cycle_edgeset
|
| 234 |
+
elif g.is_multigraph():
|
| 235 |
+
return multigraph_cycle_edgeset
|
| 236 |
+
else:
|
| 237 |
+
return undirected_cycle_edgeset
|
| 238 |
+
|
| 239 |
+
def check_cycle(self, g, c, es, cache, source, original_c, length_bound, chordless):
|
| 240 |
+
if length_bound is not None and len(c) > length_bound:
|
| 241 |
+
raise RuntimeError(
|
| 242 |
+
f"computed cycle {original_c} exceeds length bound {length_bound}"
|
| 243 |
+
)
|
| 244 |
+
if source == "computed":
|
| 245 |
+
if es in cache:
|
| 246 |
+
raise RuntimeError(
|
| 247 |
+
f"computed cycle {original_c} has already been found!"
|
| 248 |
+
)
|
| 249 |
+
else:
|
| 250 |
+
cache[es] = tuple(original_c)
|
| 251 |
+
else:
|
| 252 |
+
if es in cache:
|
| 253 |
+
cache.pop(es)
|
| 254 |
+
else:
|
| 255 |
+
raise RuntimeError(f"expected cycle {original_c} was not computed")
|
| 256 |
+
|
| 257 |
+
if not all(g.has_edge(*e) for e in es):
|
| 258 |
+
raise RuntimeError(
|
| 259 |
+
f"{source} claimed cycle {original_c} is not a cycle of g"
|
| 260 |
+
)
|
| 261 |
+
if chordless and len(g.subgraph(c).edges) > len(c):
|
| 262 |
+
raise RuntimeError(f"{source} cycle {original_c} is not chordless")
|
| 263 |
+
|
| 264 |
+
def check_cycle_algorithm(
|
| 265 |
+
self,
|
| 266 |
+
g,
|
| 267 |
+
expected_cycles,
|
| 268 |
+
length_bound=None,
|
| 269 |
+
chordless=False,
|
| 270 |
+
algorithm=None,
|
| 271 |
+
):
|
| 272 |
+
if algorithm is None:
|
| 273 |
+
algorithm = nx.chordless_cycles if chordless else nx.simple_cycles
|
| 274 |
+
|
| 275 |
+
# note: we shuffle the labels of g to rule out accidentally-correct
|
| 276 |
+
# behavior which occurred during the development of chordless cycle
|
| 277 |
+
# enumeration algorithms
|
| 278 |
+
|
| 279 |
+
relabel = list(range(len(g)))
|
| 280 |
+
shuffle(relabel)
|
| 281 |
+
label = dict(zip(g, relabel))
|
| 282 |
+
unlabel = dict(zip(relabel, g))
|
| 283 |
+
h = nx.relabel_nodes(g, label, copy=True)
|
| 284 |
+
|
| 285 |
+
edgeset = self.edgeset_function(h)
|
| 286 |
+
|
| 287 |
+
params = {}
|
| 288 |
+
if length_bound is not None:
|
| 289 |
+
params["length_bound"] = length_bound
|
| 290 |
+
|
| 291 |
+
cycle_cache = {}
|
| 292 |
+
for c in algorithm(h, **params):
|
| 293 |
+
original_c = [unlabel[x] for x in c]
|
| 294 |
+
es = edgeset(c)
|
| 295 |
+
self.check_cycle(
|
| 296 |
+
h, c, es, cycle_cache, "computed", original_c, length_bound, chordless
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
if isinstance(expected_cycles, int):
|
| 300 |
+
if len(cycle_cache) != expected_cycles:
|
| 301 |
+
raise RuntimeError(
|
| 302 |
+
f"expected {expected_cycles} cycles, got {len(cycle_cache)}"
|
| 303 |
+
)
|
| 304 |
+
return
|
| 305 |
+
for original_c in expected_cycles:
|
| 306 |
+
c = [label[x] for x in original_c]
|
| 307 |
+
es = edgeset(c)
|
| 308 |
+
self.check_cycle(
|
| 309 |
+
h, c, es, cycle_cache, "expected", original_c, length_bound, chordless
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
if len(cycle_cache):
|
| 313 |
+
for c in cycle_cache.values():
|
| 314 |
+
raise RuntimeError(
|
| 315 |
+
f"computed cycle {c} is valid but not in the expected cycle set!"
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
def check_cycle_enumeration_integer_sequence(
|
| 319 |
+
self,
|
| 320 |
+
g_family,
|
| 321 |
+
cycle_counts,
|
| 322 |
+
length_bound=None,
|
| 323 |
+
chordless=False,
|
| 324 |
+
algorithm=None,
|
| 325 |
+
):
|
| 326 |
+
for g, num_cycles in zip(g_family, cycle_counts):
|
| 327 |
+
self.check_cycle_algorithm(
|
| 328 |
+
g,
|
| 329 |
+
num_cycles,
|
| 330 |
+
length_bound=length_bound,
|
| 331 |
+
chordless=chordless,
|
| 332 |
+
algorithm=algorithm,
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
def test_directed_chordless_cycle_digons(self):
|
| 336 |
+
g = nx.DiGraph()
|
| 337 |
+
nx.add_cycle(g, range(5))
|
| 338 |
+
nx.add_cycle(g, range(5)[::-1])
|
| 339 |
+
g.add_edge(0, 0)
|
| 340 |
+
expected_cycles = [(0,), (1, 2), (2, 3), (3, 4)]
|
| 341 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 342 |
+
|
| 343 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True, length_bound=2)
|
| 344 |
+
|
| 345 |
+
expected_cycles = [c for c in expected_cycles if len(c) < 2]
|
| 346 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True, length_bound=1)
|
| 347 |
+
|
| 348 |
+
def test_directed_chordless_cycle_undirected(self):
|
| 349 |
+
g = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (5, 1), (0, 2)])
|
| 350 |
+
expected_cycles = [(0, 2, 3, 4, 5), (1, 2, 3, 4, 5)]
|
| 351 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 352 |
+
|
| 353 |
+
g = nx.DiGraph()
|
| 354 |
+
nx.add_cycle(g, range(5))
|
| 355 |
+
nx.add_cycle(g, range(4, 9))
|
| 356 |
+
g.add_edge(7, 3)
|
| 357 |
+
expected_cycles = [(0, 1, 2, 3, 4), (3, 4, 5, 6, 7), (4, 5, 6, 7, 8)]
|
| 358 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 359 |
+
|
| 360 |
+
g.add_edge(3, 7)
|
| 361 |
+
expected_cycles = [(0, 1, 2, 3, 4), (3, 7), (4, 5, 6, 7, 8)]
|
| 362 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 363 |
+
|
| 364 |
+
expected_cycles = [(3, 7)]
|
| 365 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True, length_bound=4)
|
| 366 |
+
|
| 367 |
+
g.remove_edge(7, 3)
|
| 368 |
+
expected_cycles = [(0, 1, 2, 3, 4), (4, 5, 6, 7, 8)]
|
| 369 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 370 |
+
|
| 371 |
+
g = nx.DiGraph((i, j) for i in range(10) for j in range(i))
|
| 372 |
+
expected_cycles = []
|
| 373 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 374 |
+
|
| 375 |
+
def test_chordless_cycles_directed(self):
|
| 376 |
+
G = nx.DiGraph()
|
| 377 |
+
nx.add_cycle(G, range(5))
|
| 378 |
+
nx.add_cycle(G, range(4, 12))
|
| 379 |
+
expected = [[*range(5)], [*range(4, 12)]]
|
| 380 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 381 |
+
self.check_cycle_algorithm(
|
| 382 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
G.add_edge(7, 3)
|
| 386 |
+
expected.append([*range(3, 8)])
|
| 387 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 388 |
+
self.check_cycle_algorithm(
|
| 389 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
G.add_edge(3, 7)
|
| 393 |
+
expected[-1] = [7, 3]
|
| 394 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 395 |
+
self.check_cycle_algorithm(
|
| 396 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
expected.pop()
|
| 400 |
+
G.remove_edge(7, 3)
|
| 401 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 402 |
+
self.check_cycle_algorithm(
|
| 403 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
def test_directed_chordless_cycle_diclique(self):
|
| 407 |
+
g_family = [self.D(n) for n in range(10)]
|
| 408 |
+
expected_cycles = [(n * n - n) // 2 for n in range(10)]
|
| 409 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 410 |
+
g_family, expected_cycles, chordless=True
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
expected_cycles = [(n * n - n) // 2 for n in range(10)]
|
| 414 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 415 |
+
g_family, expected_cycles, length_bound=2
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
def test_directed_chordless_loop_blockade(self):
|
| 419 |
+
g = nx.DiGraph((i, i) for i in range(10))
|
| 420 |
+
nx.add_cycle(g, range(10))
|
| 421 |
+
expected_cycles = [(i,) for i in range(10)]
|
| 422 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 423 |
+
|
| 424 |
+
self.check_cycle_algorithm(g, expected_cycles, length_bound=1)
|
| 425 |
+
|
| 426 |
+
g = nx.MultiDiGraph(g)
|
| 427 |
+
g.add_edges_from((i, i) for i in range(0, 10, 2))
|
| 428 |
+
expected_cycles = [(i,) for i in range(1, 10, 2)]
|
| 429 |
+
self.check_cycle_algorithm(g, expected_cycles, chordless=True)
|
| 430 |
+
|
| 431 |
+
def test_simple_cycles_notable_clique_sequences(self):
|
| 432 |
+
# A000292: Number of labeled graphs on n+3 nodes that are triangles.
|
| 433 |
+
g_family = [self.K(n) for n in range(2, 12)]
|
| 434 |
+
expected = [0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220]
|
| 435 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 436 |
+
g_family, expected, length_bound=3
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
def triangles(g, **kwargs):
|
| 440 |
+
yield from (c for c in nx.simple_cycles(g, **kwargs) if len(c) == 3)
|
| 441 |
+
|
| 442 |
+
# directed complete graphs have twice as many triangles thanks to reversal
|
| 443 |
+
g_family = [self.D(n) for n in range(2, 12)]
|
| 444 |
+
expected = [2 * e for e in expected]
|
| 445 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 446 |
+
g_family, expected, length_bound=3, algorithm=triangles
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
def four_cycles(g, **kwargs):
|
| 450 |
+
yield from (c for c in nx.simple_cycles(g, **kwargs) if len(c) == 4)
|
| 451 |
+
|
| 452 |
+
# A050534: the number of 4-cycles in the complete graph K_{n+1}
|
| 453 |
+
expected = [0, 0, 0, 3, 15, 45, 105, 210, 378, 630, 990]
|
| 454 |
+
g_family = [self.K(n) for n in range(1, 12)]
|
| 455 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 456 |
+
g_family, expected, length_bound=4, algorithm=four_cycles
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
# directed complete graphs have twice as many 4-cycles thanks to reversal
|
| 460 |
+
expected = [2 * e for e in expected]
|
| 461 |
+
g_family = [self.D(n) for n in range(1, 15)]
|
| 462 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 463 |
+
g_family, expected, length_bound=4, algorithm=four_cycles
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
# A006231: the number of elementary circuits in a complete directed graph with n nodes
|
| 467 |
+
expected = [0, 1, 5, 20, 84, 409, 2365]
|
| 468 |
+
g_family = [self.D(n) for n in range(1, 8)]
|
| 469 |
+
self.check_cycle_enumeration_integer_sequence(g_family, expected)
|
| 470 |
+
|
| 471 |
+
# A002807: Number of cycles in the complete graph on n nodes K_{n}.
|
| 472 |
+
expected = [0, 0, 0, 1, 7, 37, 197, 1172]
|
| 473 |
+
g_family = [self.K(n) for n in range(8)]
|
| 474 |
+
self.check_cycle_enumeration_integer_sequence(g_family, expected)
|
| 475 |
+
|
| 476 |
+
def test_directed_chordless_cycle_parallel_multiedges(self):
|
| 477 |
+
g = nx.MultiGraph()
|
| 478 |
+
|
| 479 |
+
nx.add_cycle(g, range(5))
|
| 480 |
+
expected = [[*range(5)]]
|
| 481 |
+
self.check_cycle_algorithm(g, expected, chordless=True)
|
| 482 |
+
|
| 483 |
+
nx.add_cycle(g, range(5))
|
| 484 |
+
expected = [*cycle_edges(range(5))]
|
| 485 |
+
self.check_cycle_algorithm(g, expected, chordless=True)
|
| 486 |
+
|
| 487 |
+
nx.add_cycle(g, range(5))
|
| 488 |
+
expected = []
|
| 489 |
+
self.check_cycle_algorithm(g, expected, chordless=True)
|
| 490 |
+
|
| 491 |
+
g = nx.MultiDiGraph()
|
| 492 |
+
|
| 493 |
+
nx.add_cycle(g, range(5))
|
| 494 |
+
expected = [[*range(5)]]
|
| 495 |
+
self.check_cycle_algorithm(g, expected, chordless=True)
|
| 496 |
+
|
| 497 |
+
nx.add_cycle(g, range(5))
|
| 498 |
+
self.check_cycle_algorithm(g, [], chordless=True)
|
| 499 |
+
|
| 500 |
+
nx.add_cycle(g, range(5))
|
| 501 |
+
self.check_cycle_algorithm(g, [], chordless=True)
|
| 502 |
+
|
| 503 |
+
g = nx.MultiDiGraph()
|
| 504 |
+
|
| 505 |
+
nx.add_cycle(g, range(5))
|
| 506 |
+
nx.add_cycle(g, range(5)[::-1])
|
| 507 |
+
expected = [*cycle_edges(range(5))]
|
| 508 |
+
self.check_cycle_algorithm(g, expected, chordless=True)
|
| 509 |
+
|
| 510 |
+
nx.add_cycle(g, range(5))
|
| 511 |
+
self.check_cycle_algorithm(g, [], chordless=True)
|
| 512 |
+
|
| 513 |
+
def test_chordless_cycles_graph(self):
|
| 514 |
+
G = nx.Graph()
|
| 515 |
+
nx.add_cycle(G, range(5))
|
| 516 |
+
nx.add_cycle(G, range(4, 12))
|
| 517 |
+
expected = [[*range(5)], [*range(4, 12)]]
|
| 518 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 519 |
+
self.check_cycle_algorithm(
|
| 520 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
G.add_edge(7, 3)
|
| 524 |
+
expected.append([*range(3, 8)])
|
| 525 |
+
expected.append([4, 3, 7, 8, 9, 10, 11])
|
| 526 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 527 |
+
self.check_cycle_algorithm(
|
| 528 |
+
G, [c for c in expected if len(c) <= 5], length_bound=5, chordless=True
|
| 529 |
+
)
|
| 530 |
+
|
| 531 |
+
def test_chordless_cycles_giant_hamiltonian(self):
|
| 532 |
+
# ... o - e - o - e - o ... # o = odd, e = even
|
| 533 |
+
# ... ---/ \-----/ \--- ... # <-- "long" edges
|
| 534 |
+
#
|
| 535 |
+
# each long edge belongs to exactly one triangle, and one giant cycle
|
| 536 |
+
# of length n/2. The remaining edges each belong to a triangle
|
| 537 |
+
|
| 538 |
+
n = 1000
|
| 539 |
+
assert n % 2 == 0
|
| 540 |
+
G = nx.Graph()
|
| 541 |
+
for v in range(n):
|
| 542 |
+
if not v % 2:
|
| 543 |
+
G.add_edge(v, (v + 2) % n)
|
| 544 |
+
G.add_edge(v, (v + 1) % n)
|
| 545 |
+
|
| 546 |
+
expected = [[*range(0, n, 2)]] + [
|
| 547 |
+
[x % n for x in range(i, i + 3)] for i in range(0, n, 2)
|
| 548 |
+
]
|
| 549 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 550 |
+
self.check_cycle_algorithm(
|
| 551 |
+
G, [c for c in expected if len(c) <= 3], length_bound=3, chordless=True
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
# ... o -> e -> o -> e -> o ... # o = odd, e = even
|
| 555 |
+
# ... <---/ \---<---/ \---< ... # <-- "long" edges
|
| 556 |
+
#
|
| 557 |
+
# this time, we orient the short and long edges in opposition
|
| 558 |
+
# the cycle structure of this graph is the same, but we need to reverse
|
| 559 |
+
# the long one in our representation. Also, we need to drop the size
|
| 560 |
+
# because our partitioning algorithm uses strongly connected components
|
| 561 |
+
# instead of separating graphs by their strong articulation points
|
| 562 |
+
|
| 563 |
+
n = 100
|
| 564 |
+
assert n % 2 == 0
|
| 565 |
+
G = nx.DiGraph()
|
| 566 |
+
for v in range(n):
|
| 567 |
+
G.add_edge(v, (v + 1) % n)
|
| 568 |
+
if not v % 2:
|
| 569 |
+
G.add_edge((v + 2) % n, v)
|
| 570 |
+
|
| 571 |
+
expected = [[*range(n - 2, -2, -2)]] + [
|
| 572 |
+
[x % n for x in range(i, i + 3)] for i in range(0, n, 2)
|
| 573 |
+
]
|
| 574 |
+
self.check_cycle_algorithm(G, expected, chordless=True)
|
| 575 |
+
self.check_cycle_algorithm(
|
| 576 |
+
G, [c for c in expected if len(c) <= 3], length_bound=3, chordless=True
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
def test_simple_cycles_acyclic_tournament(self):
|
| 580 |
+
n = 10
|
| 581 |
+
G = nx.DiGraph((x, y) for x in range(n) for y in range(x))
|
| 582 |
+
self.check_cycle_algorithm(G, [])
|
| 583 |
+
self.check_cycle_algorithm(G, [], chordless=True)
|
| 584 |
+
|
| 585 |
+
for k in range(n + 1):
|
| 586 |
+
self.check_cycle_algorithm(G, [], length_bound=k)
|
| 587 |
+
self.check_cycle_algorithm(G, [], length_bound=k, chordless=True)
|
| 588 |
+
|
| 589 |
+
def test_simple_cycles_graph(self):
|
| 590 |
+
testG = nx.cycle_graph(8)
|
| 591 |
+
cyc1 = tuple(range(8))
|
| 592 |
+
self.check_cycle_algorithm(testG, [cyc1])
|
| 593 |
+
|
| 594 |
+
testG.add_edge(4, -1)
|
| 595 |
+
nx.add_path(testG, [3, -2, -3, -4])
|
| 596 |
+
self.check_cycle_algorithm(testG, [cyc1])
|
| 597 |
+
|
| 598 |
+
testG.update(nx.cycle_graph(range(8, 16)))
|
| 599 |
+
cyc2 = tuple(range(8, 16))
|
| 600 |
+
self.check_cycle_algorithm(testG, [cyc1, cyc2])
|
| 601 |
+
|
| 602 |
+
testG.update(nx.cycle_graph(range(4, 12)))
|
| 603 |
+
cyc3 = tuple(range(4, 12))
|
| 604 |
+
expected = {
|
| 605 |
+
(0, 1, 2, 3, 4, 5, 6, 7), # cyc1
|
| 606 |
+
(8, 9, 10, 11, 12, 13, 14, 15), # cyc2
|
| 607 |
+
(4, 5, 6, 7, 8, 9, 10, 11), # cyc3
|
| 608 |
+
(4, 5, 6, 7, 8, 15, 14, 13, 12, 11), # cyc2 + cyc3
|
| 609 |
+
(0, 1, 2, 3, 4, 11, 10, 9, 8, 7), # cyc1 + cyc3
|
| 610 |
+
(0, 1, 2, 3, 4, 11, 12, 13, 14, 15, 8, 7), # cyc1 + cyc2 + cyc3
|
| 611 |
+
}
|
| 612 |
+
self.check_cycle_algorithm(testG, expected)
|
| 613 |
+
assert len(expected) == (2**3 - 1) - 1 # 1 disjoint comb: cyc1 + cyc2
|
| 614 |
+
|
| 615 |
+
# Basis size = 5 (2 loops overlapping gives 5 small loops
|
| 616 |
+
# E
|
| 617 |
+
# / \ Note: A-F = 10-15
|
| 618 |
+
# 1-2-3-4-5
|
| 619 |
+
# / | | \ cyc1=012DAB -- left
|
| 620 |
+
# 0 D F 6 cyc2=234E -- top
|
| 621 |
+
# \ | | / cyc3=45678F -- right
|
| 622 |
+
# B-A-9-8-7 cyc4=89AC -- bottom
|
| 623 |
+
# \ / cyc5=234F89AD -- middle
|
| 624 |
+
# C
|
| 625 |
+
#
|
| 626 |
+
# combinations of 5 basis elements: 2^5 - 1 (one includes no cycles)
|
| 627 |
+
#
|
| 628 |
+
# disjoint combs: (11 total) not simple cycles
|
| 629 |
+
# Any pair not including cyc5 => choose(4, 2) = 6
|
| 630 |
+
# Any triple not including cyc5 => choose(4, 3) = 4
|
| 631 |
+
# Any quad not including cyc5 => choose(4, 4) = 1
|
| 632 |
+
#
|
| 633 |
+
# we expect 31 - 11 = 20 simple cycles
|
| 634 |
+
#
|
| 635 |
+
testG = nx.cycle_graph(12)
|
| 636 |
+
testG.update(nx.cycle_graph([12, 10, 13, 2, 14, 4, 15, 8]).edges)
|
| 637 |
+
expected = (2**5 - 1) - 11 # 11 disjoint combinations
|
| 638 |
+
self.check_cycle_algorithm(testG, expected)
|
| 639 |
+
|
| 640 |
+
def test_simple_cycles_bounded(self):
|
| 641 |
+
# iteratively construct a cluster of nested cycles running in the same direction
|
| 642 |
+
# there should be one cycle of every length
|
| 643 |
+
d = nx.DiGraph()
|
| 644 |
+
expected = []
|
| 645 |
+
for n in range(10):
|
| 646 |
+
nx.add_cycle(d, range(n))
|
| 647 |
+
expected.append(n)
|
| 648 |
+
for k, e in enumerate(expected):
|
| 649 |
+
self.check_cycle_algorithm(d, e, length_bound=k)
|
| 650 |
+
|
| 651 |
+
# iteratively construct a path of undirected cycles, connected at articulation
|
| 652 |
+
# points. there should be one cycle of every length except 2: no digons
|
| 653 |
+
g = nx.Graph()
|
| 654 |
+
top = 0
|
| 655 |
+
expected = []
|
| 656 |
+
for n in range(10):
|
| 657 |
+
expected.append(n if n < 2 else n - 1)
|
| 658 |
+
if n == 2:
|
| 659 |
+
# no digons in undirected graphs
|
| 660 |
+
continue
|
| 661 |
+
nx.add_cycle(g, range(top, top + n))
|
| 662 |
+
top += n
|
| 663 |
+
for k, e in enumerate(expected):
|
| 664 |
+
self.check_cycle_algorithm(g, e, length_bound=k)
|
| 665 |
+
|
| 666 |
+
def test_simple_cycles_bound_corner_cases(self):
|
| 667 |
+
G = nx.cycle_graph(4)
|
| 668 |
+
DG = nx.cycle_graph(4, create_using=nx.DiGraph)
|
| 669 |
+
assert list(nx.simple_cycles(G, length_bound=0)) == []
|
| 670 |
+
assert list(nx.simple_cycles(DG, length_bound=0)) == []
|
| 671 |
+
assert list(nx.chordless_cycles(G, length_bound=0)) == []
|
| 672 |
+
assert list(nx.chordless_cycles(DG, length_bound=0)) == []
|
| 673 |
+
|
| 674 |
+
def test_simple_cycles_bound_error(self):
|
| 675 |
+
with pytest.raises(ValueError):
|
| 676 |
+
G = nx.DiGraph()
|
| 677 |
+
for c in nx.simple_cycles(G, -1):
|
| 678 |
+
assert False
|
| 679 |
+
|
| 680 |
+
with pytest.raises(ValueError):
|
| 681 |
+
G = nx.Graph()
|
| 682 |
+
for c in nx.simple_cycles(G, -1):
|
| 683 |
+
assert False
|
| 684 |
+
|
| 685 |
+
with pytest.raises(ValueError):
|
| 686 |
+
G = nx.Graph()
|
| 687 |
+
for c in nx.chordless_cycles(G, -1):
|
| 688 |
+
assert False
|
| 689 |
+
|
| 690 |
+
with pytest.raises(ValueError):
|
| 691 |
+
G = nx.DiGraph()
|
| 692 |
+
for c in nx.chordless_cycles(G, -1):
|
| 693 |
+
assert False
|
| 694 |
+
|
| 695 |
+
def test_chordless_cycles_clique(self):
|
| 696 |
+
g_family = [self.K(n) for n in range(2, 15)]
|
| 697 |
+
expected = [0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364]
|
| 698 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 699 |
+
g_family, expected, chordless=True
|
| 700 |
+
)
|
| 701 |
+
|
| 702 |
+
# directed cliques have as many digons as undirected graphs have edges
|
| 703 |
+
expected = [(n * n - n) // 2 for n in range(15)]
|
| 704 |
+
g_family = [self.D(n) for n in range(15)]
|
| 705 |
+
self.check_cycle_enumeration_integer_sequence(
|
| 706 |
+
g_family, expected, chordless=True
|
| 707 |
+
)
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
# These tests might fail with hash randomization since they depend on
|
| 711 |
+
# edge_dfs. For more information, see the comments in:
|
| 712 |
+
# networkx/algorithms/traversal/tests/test_edgedfs.py
|
| 713 |
+
|
| 714 |
+
|
| 715 |
+
class TestFindCycle:
|
| 716 |
+
@classmethod
|
| 717 |
+
def setup_class(cls):
|
| 718 |
+
cls.nodes = [0, 1, 2, 3]
|
| 719 |
+
cls.edges = [(-1, 0), (0, 1), (1, 0), (1, 0), (2, 1), (3, 1)]
|
| 720 |
+
|
| 721 |
+
def test_graph_nocycle(self):
|
| 722 |
+
G = nx.Graph(self.edges)
|
| 723 |
+
pytest.raises(nx.exception.NetworkXNoCycle, nx.find_cycle, G, self.nodes)
|
| 724 |
+
|
| 725 |
+
def test_graph_cycle(self):
|
| 726 |
+
G = nx.Graph(self.edges)
|
| 727 |
+
G.add_edge(2, 0)
|
| 728 |
+
x = list(nx.find_cycle(G, self.nodes))
|
| 729 |
+
x_ = [(0, 1), (1, 2), (2, 0)]
|
| 730 |
+
assert x == x_
|
| 731 |
+
|
| 732 |
+
def test_graph_orientation_none(self):
|
| 733 |
+
G = nx.Graph(self.edges)
|
| 734 |
+
G.add_edge(2, 0)
|
| 735 |
+
x = list(nx.find_cycle(G, self.nodes, orientation=None))
|
| 736 |
+
x_ = [(0, 1), (1, 2), (2, 0)]
|
| 737 |
+
assert x == x_
|
| 738 |
+
|
| 739 |
+
def test_graph_orientation_original(self):
|
| 740 |
+
G = nx.Graph(self.edges)
|
| 741 |
+
G.add_edge(2, 0)
|
| 742 |
+
x = list(nx.find_cycle(G, self.nodes, orientation="original"))
|
| 743 |
+
x_ = [(0, 1, FORWARD), (1, 2, FORWARD), (2, 0, FORWARD)]
|
| 744 |
+
assert x == x_
|
| 745 |
+
|
| 746 |
+
def test_digraph(self):
|
| 747 |
+
G = nx.DiGraph(self.edges)
|
| 748 |
+
x = list(nx.find_cycle(G, self.nodes))
|
| 749 |
+
x_ = [(0, 1), (1, 0)]
|
| 750 |
+
assert x == x_
|
| 751 |
+
|
| 752 |
+
def test_digraph_orientation_none(self):
|
| 753 |
+
G = nx.DiGraph(self.edges)
|
| 754 |
+
x = list(nx.find_cycle(G, self.nodes, orientation=None))
|
| 755 |
+
x_ = [(0, 1), (1, 0)]
|
| 756 |
+
assert x == x_
|
| 757 |
+
|
| 758 |
+
def test_digraph_orientation_original(self):
|
| 759 |
+
G = nx.DiGraph(self.edges)
|
| 760 |
+
x = list(nx.find_cycle(G, self.nodes, orientation="original"))
|
| 761 |
+
x_ = [(0, 1, FORWARD), (1, 0, FORWARD)]
|
| 762 |
+
assert x == x_
|
| 763 |
+
|
| 764 |
+
def test_multigraph(self):
|
| 765 |
+
G = nx.MultiGraph(self.edges)
|
| 766 |
+
x = list(nx.find_cycle(G, self.nodes))
|
| 767 |
+
x_ = [(0, 1, 0), (1, 0, 1)] # or (1, 0, 2)
|
| 768 |
+
# Hash randomization...could be any edge.
|
| 769 |
+
assert x[0] == x_[0]
|
| 770 |
+
assert x[1][:2] == x_[1][:2]
|
| 771 |
+
|
| 772 |
+
def test_multidigraph(self):
|
| 773 |
+
G = nx.MultiDiGraph(self.edges)
|
| 774 |
+
x = list(nx.find_cycle(G, self.nodes))
|
| 775 |
+
x_ = [(0, 1, 0), (1, 0, 0)] # (1, 0, 1)
|
| 776 |
+
assert x[0] == x_[0]
|
| 777 |
+
assert x[1][:2] == x_[1][:2]
|
| 778 |
+
|
| 779 |
+
def test_digraph_ignore(self):
|
| 780 |
+
G = nx.DiGraph(self.edges)
|
| 781 |
+
x = list(nx.find_cycle(G, self.nodes, orientation="ignore"))
|
| 782 |
+
x_ = [(0, 1, FORWARD), (1, 0, FORWARD)]
|
| 783 |
+
assert x == x_
|
| 784 |
+
|
| 785 |
+
def test_digraph_reverse(self):
|
| 786 |
+
G = nx.DiGraph(self.edges)
|
| 787 |
+
x = list(nx.find_cycle(G, self.nodes, orientation="reverse"))
|
| 788 |
+
x_ = [(1, 0, REVERSE), (0, 1, REVERSE)]
|
| 789 |
+
assert x == x_
|
| 790 |
+
|
| 791 |
+
def test_multidigraph_ignore(self):
|
| 792 |
+
G = nx.MultiDiGraph(self.edges)
|
| 793 |
+
x = list(nx.find_cycle(G, self.nodes, orientation="ignore"))
|
| 794 |
+
x_ = [(0, 1, 0, FORWARD), (1, 0, 0, FORWARD)] # or (1, 0, 1, 1)
|
| 795 |
+
assert x[0] == x_[0]
|
| 796 |
+
assert x[1][:2] == x_[1][:2]
|
| 797 |
+
assert x[1][3] == x_[1][3]
|
| 798 |
+
|
| 799 |
+
def test_multidigraph_ignore2(self):
|
| 800 |
+
# Loop traversed an edge while ignoring its orientation.
|
| 801 |
+
G = nx.MultiDiGraph([(0, 1), (1, 2), (1, 2)])
|
| 802 |
+
x = list(nx.find_cycle(G, [0, 1, 2], orientation="ignore"))
|
| 803 |
+
x_ = [(1, 2, 0, FORWARD), (1, 2, 1, REVERSE)]
|
| 804 |
+
assert x == x_
|
| 805 |
+
|
| 806 |
+
def test_multidigraph_original(self):
|
| 807 |
+
# Node 2 doesn't need to be searched again from visited from 4.
|
| 808 |
+
# The goal here is to cover the case when 2 to be researched from 4,
|
| 809 |
+
# when 4 is visited from the first time (so we must make sure that 4
|
| 810 |
+
# is not visited from 2, and hence, we respect the edge orientation).
|
| 811 |
+
G = nx.MultiDiGraph([(0, 1), (1, 2), (2, 3), (4, 2)])
|
| 812 |
+
pytest.raises(
|
| 813 |
+
nx.exception.NetworkXNoCycle,
|
| 814 |
+
nx.find_cycle,
|
| 815 |
+
G,
|
| 816 |
+
[0, 1, 2, 3, 4],
|
| 817 |
+
orientation="original",
|
| 818 |
+
)
|
| 819 |
+
|
| 820 |
+
def test_dag(self):
|
| 821 |
+
G = nx.DiGraph([(0, 1), (0, 2), (1, 2)])
|
| 822 |
+
pytest.raises(
|
| 823 |
+
nx.exception.NetworkXNoCycle, nx.find_cycle, G, orientation="original"
|
| 824 |
+
)
|
| 825 |
+
x = list(nx.find_cycle(G, orientation="ignore"))
|
| 826 |
+
assert x == [(0, 1, FORWARD), (1, 2, FORWARD), (0, 2, REVERSE)]
|
| 827 |
+
|
| 828 |
+
def test_prev_explored(self):
|
| 829 |
+
# https://github.com/networkx/networkx/issues/2323
|
| 830 |
+
|
| 831 |
+
G = nx.DiGraph()
|
| 832 |
+
G.add_edges_from([(1, 0), (2, 0), (1, 2), (2, 1)])
|
| 833 |
+
pytest.raises(nx.NetworkXNoCycle, nx.find_cycle, G, source=0)
|
| 834 |
+
x = list(nx.find_cycle(G, 1))
|
| 835 |
+
x_ = [(1, 2), (2, 1)]
|
| 836 |
+
assert x == x_
|
| 837 |
+
|
| 838 |
+
x = list(nx.find_cycle(G, 2))
|
| 839 |
+
x_ = [(2, 1), (1, 2)]
|
| 840 |
+
assert x == x_
|
| 841 |
+
|
| 842 |
+
x = list(nx.find_cycle(G))
|
| 843 |
+
x_ = [(1, 2), (2, 1)]
|
| 844 |
+
assert x == x_
|
| 845 |
+
|
| 846 |
+
def test_no_cycle(self):
|
| 847 |
+
# https://github.com/networkx/networkx/issues/2439
|
| 848 |
+
|
| 849 |
+
G = nx.DiGraph()
|
| 850 |
+
G.add_edges_from([(1, 2), (2, 0), (3, 1), (3, 2)])
|
| 851 |
+
pytest.raises(nx.NetworkXNoCycle, nx.find_cycle, G, source=0)
|
| 852 |
+
pytest.raises(nx.NetworkXNoCycle, nx.find_cycle, G)
|
| 853 |
+
|
| 854 |
+
|
| 855 |
+
def assert_basis_equal(a, b):
|
| 856 |
+
assert sorted(a) == sorted(b)
|
| 857 |
+
|
| 858 |
+
|
| 859 |
+
class TestMinimumCycleBasis:
|
| 860 |
+
@classmethod
|
| 861 |
+
def setup_class(cls):
|
| 862 |
+
T = nx.Graph()
|
| 863 |
+
nx.add_cycle(T, [1, 2, 3, 4], weight=1)
|
| 864 |
+
T.add_edge(2, 4, weight=5)
|
| 865 |
+
cls.diamond_graph = T
|
| 866 |
+
|
| 867 |
+
def test_unweighted_diamond(self):
|
| 868 |
+
mcb = nx.minimum_cycle_basis(self.diamond_graph)
|
| 869 |
+
assert_basis_equal(mcb, [[2, 4, 1], [3, 4, 2]])
|
| 870 |
+
|
| 871 |
+
def test_weighted_diamond(self):
|
| 872 |
+
mcb = nx.minimum_cycle_basis(self.diamond_graph, weight="weight")
|
| 873 |
+
assert_basis_equal(mcb, [[2, 4, 1], [4, 3, 2, 1]])
|
| 874 |
+
|
| 875 |
+
def test_dimensionality(self):
|
| 876 |
+
# checks |MCB|=|E|-|V|+|NC|
|
| 877 |
+
ntrial = 10
|
| 878 |
+
for seed in range(1234, 1234 + ntrial):
|
| 879 |
+
rg = nx.erdos_renyi_graph(10, 0.3, seed=seed)
|
| 880 |
+
nnodes = rg.number_of_nodes()
|
| 881 |
+
nedges = rg.number_of_edges()
|
| 882 |
+
ncomp = nx.number_connected_components(rg)
|
| 883 |
+
|
| 884 |
+
mcb = nx.minimum_cycle_basis(rg)
|
| 885 |
+
assert len(mcb) == nedges - nnodes + ncomp
|
| 886 |
+
check_independent(mcb)
|
| 887 |
+
|
| 888 |
+
def test_complete_graph(self):
|
| 889 |
+
cg = nx.complete_graph(5)
|
| 890 |
+
mcb = nx.minimum_cycle_basis(cg)
|
| 891 |
+
assert all(len(cycle) == 3 for cycle in mcb)
|
| 892 |
+
check_independent(mcb)
|
| 893 |
+
|
| 894 |
+
def test_tree_graph(self):
|
| 895 |
+
tg = nx.balanced_tree(3, 3)
|
| 896 |
+
assert not nx.minimum_cycle_basis(tg)
|
| 897 |
+
|
| 898 |
+
def test_petersen_graph(self):
|
| 899 |
+
G = nx.petersen_graph()
|
| 900 |
+
mcb = list(nx.minimum_cycle_basis(G))
|
| 901 |
+
expected = [
|
| 902 |
+
[4, 9, 7, 5, 0],
|
| 903 |
+
[1, 2, 3, 4, 0],
|
| 904 |
+
[1, 6, 8, 5, 0],
|
| 905 |
+
[4, 3, 8, 5, 0],
|
| 906 |
+
[1, 6, 9, 4, 0],
|
| 907 |
+
[1, 2, 7, 5, 0],
|
| 908 |
+
]
|
| 909 |
+
assert len(mcb) == len(expected)
|
| 910 |
+
assert all(c in expected for c in mcb)
|
| 911 |
+
|
| 912 |
+
# check that order of the nodes is a path
|
| 913 |
+
for c in mcb:
|
| 914 |
+
assert all(G.has_edge(u, v) for u, v in nx.utils.pairwise(c, cyclic=True))
|
| 915 |
+
# check independence of the basis
|
| 916 |
+
check_independent(mcb)
|
| 917 |
+
|
| 918 |
+
def test_gh6787_variable_weighted_complete_graph(self):
|
| 919 |
+
N = 8
|
| 920 |
+
cg = nx.complete_graph(N)
|
| 921 |
+
cg.add_weighted_edges_from([(u, v, 9) for u, v in cg.edges])
|
| 922 |
+
cg.add_weighted_edges_from([(u, v, 1) for u, v in nx.cycle_graph(N).edges])
|
| 923 |
+
mcb = nx.minimum_cycle_basis(cg, weight="weight")
|
| 924 |
+
check_independent(mcb)
|
| 925 |
+
|
| 926 |
+
def test_gh6787_and_edge_attribute_names(self):
|
| 927 |
+
G = nx.cycle_graph(4)
|
| 928 |
+
G.add_weighted_edges_from([(0, 2, 10), (1, 3, 10)], weight="dist")
|
| 929 |
+
expected = [[1, 3, 0], [3, 2, 1, 0], [1, 2, 0]]
|
| 930 |
+
mcb = list(nx.minimum_cycle_basis(G, weight="dist"))
|
| 931 |
+
assert len(mcb) == len(expected)
|
| 932 |
+
assert all(c in expected for c in mcb)
|
| 933 |
+
|
| 934 |
+
# test not using a weight with weight attributes
|
| 935 |
+
expected = [[1, 3, 0], [1, 2, 0], [3, 2, 0]]
|
| 936 |
+
mcb = list(nx.minimum_cycle_basis(G))
|
| 937 |
+
assert len(mcb) == len(expected)
|
| 938 |
+
assert all(c in expected for c in mcb)
|
| 939 |
+
|
| 940 |
+
|
| 941 |
+
class TestGirth:
|
| 942 |
+
@pytest.mark.parametrize(
|
| 943 |
+
("G", "expected"),
|
| 944 |
+
(
|
| 945 |
+
(nx.chvatal_graph(), 4),
|
| 946 |
+
(nx.tutte_graph(), 4),
|
| 947 |
+
(nx.petersen_graph(), 5),
|
| 948 |
+
(nx.heawood_graph(), 6),
|
| 949 |
+
(nx.pappus_graph(), 6),
|
| 950 |
+
(nx.random_tree(10, seed=42), inf),
|
| 951 |
+
(nx.empty_graph(10), inf),
|
| 952 |
+
(nx.Graph(chain(cycle_edges(range(5)), cycle_edges(range(6, 10)))), 4),
|
| 953 |
+
(
|
| 954 |
+
nx.Graph(
|
| 955 |
+
[
|
| 956 |
+
(0, 6),
|
| 957 |
+
(0, 8),
|
| 958 |
+
(0, 9),
|
| 959 |
+
(1, 8),
|
| 960 |
+
(2, 8),
|
| 961 |
+
(2, 9),
|
| 962 |
+
(4, 9),
|
| 963 |
+
(5, 9),
|
| 964 |
+
(6, 8),
|
| 965 |
+
(6, 9),
|
| 966 |
+
(7, 8),
|
| 967 |
+
]
|
| 968 |
+
),
|
| 969 |
+
3,
|
| 970 |
+
),
|
| 971 |
+
),
|
| 972 |
+
)
|
| 973 |
+
def test_girth(self, G, expected):
|
| 974 |
+
assert nx.girth(G) == expected
|
valley/l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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from random import Random
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
| 7 |
+
from networkx.algorithms.distance_measures import _extrema_bounding
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test__extrema_bounding_invalid_compute_kwarg():
|
| 11 |
+
G = nx.path_graph(3)
|
| 12 |
+
with pytest.raises(ValueError, match="compute must be one of"):
|
| 13 |
+
_extrema_bounding(G, compute="spam")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestDistance:
|
| 17 |
+
def setup_method(self):
|
| 18 |
+
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
|
| 19 |
+
self.G = G
|
| 20 |
+
|
| 21 |
+
def test_eccentricity(self):
|
| 22 |
+
assert nx.eccentricity(self.G, 1) == 6
|
| 23 |
+
e = nx.eccentricity(self.G)
|
| 24 |
+
assert e[1] == 6
|
| 25 |
+
|
| 26 |
+
sp = dict(nx.shortest_path_length(self.G))
|
| 27 |
+
e = nx.eccentricity(self.G, sp=sp)
|
| 28 |
+
assert e[1] == 6
|
| 29 |
+
|
| 30 |
+
e = nx.eccentricity(self.G, v=1)
|
| 31 |
+
assert e == 6
|
| 32 |
+
|
| 33 |
+
# This behavior changed in version 1.8 (ticket #739)
|
| 34 |
+
e = nx.eccentricity(self.G, v=[1, 1])
|
| 35 |
+
assert e[1] == 6
|
| 36 |
+
e = nx.eccentricity(self.G, v=[1, 2])
|
| 37 |
+
assert e[1] == 6
|
| 38 |
+
|
| 39 |
+
# test against graph with one node
|
| 40 |
+
G = nx.path_graph(1)
|
| 41 |
+
e = nx.eccentricity(G)
|
| 42 |
+
assert e[0] == 0
|
| 43 |
+
e = nx.eccentricity(G, v=0)
|
| 44 |
+
assert e == 0
|
| 45 |
+
pytest.raises(nx.NetworkXError, nx.eccentricity, G, 1)
|
| 46 |
+
|
| 47 |
+
# test against empty graph
|
| 48 |
+
G = nx.empty_graph()
|
| 49 |
+
e = nx.eccentricity(G)
|
| 50 |
+
assert e == {}
|
| 51 |
+
|
| 52 |
+
def test_diameter(self):
|
| 53 |
+
assert nx.diameter(self.G) == 6
|
| 54 |
+
|
| 55 |
+
def test_radius(self):
|
| 56 |
+
assert nx.radius(self.G) == 4
|
| 57 |
+
|
| 58 |
+
def test_periphery(self):
|
| 59 |
+
assert set(nx.periphery(self.G)) == {1, 4, 13, 16}
|
| 60 |
+
|
| 61 |
+
def test_center(self):
|
| 62 |
+
assert set(nx.center(self.G)) == {6, 7, 10, 11}
|
| 63 |
+
|
| 64 |
+
def test_bound_diameter(self):
|
| 65 |
+
assert nx.diameter(self.G, usebounds=True) == 6
|
| 66 |
+
|
| 67 |
+
def test_bound_radius(self):
|
| 68 |
+
assert nx.radius(self.G, usebounds=True) == 4
|
| 69 |
+
|
| 70 |
+
def test_bound_periphery(self):
|
| 71 |
+
result = {1, 4, 13, 16}
|
| 72 |
+
assert set(nx.periphery(self.G, usebounds=True)) == result
|
| 73 |
+
|
| 74 |
+
def test_bound_center(self):
|
| 75 |
+
result = {6, 7, 10, 11}
|
| 76 |
+
assert set(nx.center(self.G, usebounds=True)) == result
|
| 77 |
+
|
| 78 |
+
def test_radius_exception(self):
|
| 79 |
+
G = nx.Graph()
|
| 80 |
+
G.add_edge(1, 2)
|
| 81 |
+
G.add_edge(3, 4)
|
| 82 |
+
pytest.raises(nx.NetworkXError, nx.diameter, G)
|
| 83 |
+
|
| 84 |
+
def test_eccentricity_infinite(self):
|
| 85 |
+
with pytest.raises(nx.NetworkXError):
|
| 86 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
| 87 |
+
e = nx.eccentricity(G)
|
| 88 |
+
|
| 89 |
+
def test_eccentricity_undirected_not_connected(self):
|
| 90 |
+
with pytest.raises(nx.NetworkXError):
|
| 91 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
| 92 |
+
e = nx.eccentricity(G, sp=1)
|
| 93 |
+
|
| 94 |
+
def test_eccentricity_directed_weakly_connected(self):
|
| 95 |
+
with pytest.raises(nx.NetworkXError):
|
| 96 |
+
DG = nx.DiGraph([(1, 2), (1, 3)])
|
| 97 |
+
nx.eccentricity(DG)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class TestWeightedDistance:
|
| 101 |
+
def setup_method(self):
|
| 102 |
+
G = nx.Graph()
|
| 103 |
+
G.add_edge(0, 1, weight=0.6, cost=0.6, high_cost=6)
|
| 104 |
+
G.add_edge(0, 2, weight=0.2, cost=0.2, high_cost=2)
|
| 105 |
+
G.add_edge(2, 3, weight=0.1, cost=0.1, high_cost=1)
|
| 106 |
+
G.add_edge(2, 4, weight=0.7, cost=0.7, high_cost=7)
|
| 107 |
+
G.add_edge(2, 5, weight=0.9, cost=0.9, high_cost=9)
|
| 108 |
+
G.add_edge(1, 5, weight=0.3, cost=0.3, high_cost=3)
|
| 109 |
+
self.G = G
|
| 110 |
+
self.weight_fn = lambda v, u, e: 2
|
| 111 |
+
|
| 112 |
+
def test_eccentricity_weight_None(self):
|
| 113 |
+
assert nx.eccentricity(self.G, 1, weight=None) == 3
|
| 114 |
+
e = nx.eccentricity(self.G, weight=None)
|
| 115 |
+
assert e[1] == 3
|
| 116 |
+
|
| 117 |
+
e = nx.eccentricity(self.G, v=1, weight=None)
|
| 118 |
+
assert e == 3
|
| 119 |
+
|
| 120 |
+
# This behavior changed in version 1.8 (ticket #739)
|
| 121 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight=None)
|
| 122 |
+
assert e[1] == 3
|
| 123 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight=None)
|
| 124 |
+
assert e[1] == 3
|
| 125 |
+
|
| 126 |
+
def test_eccentricity_weight_attr(self):
|
| 127 |
+
assert nx.eccentricity(self.G, 1, weight="weight") == 1.5
|
| 128 |
+
e = nx.eccentricity(self.G, weight="weight")
|
| 129 |
+
assert (
|
| 130 |
+
e
|
| 131 |
+
== nx.eccentricity(self.G, weight="cost")
|
| 132 |
+
!= nx.eccentricity(self.G, weight="high_cost")
|
| 133 |
+
)
|
| 134 |
+
assert e[1] == 1.5
|
| 135 |
+
|
| 136 |
+
e = nx.eccentricity(self.G, v=1, weight="weight")
|
| 137 |
+
assert e == 1.5
|
| 138 |
+
|
| 139 |
+
# This behavior changed in version 1.8 (ticket #739)
|
| 140 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight="weight")
|
| 141 |
+
assert e[1] == 1.5
|
| 142 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight="weight")
|
| 143 |
+
assert e[1] == 1.5
|
| 144 |
+
|
| 145 |
+
def test_eccentricity_weight_fn(self):
|
| 146 |
+
assert nx.eccentricity(self.G, 1, weight=self.weight_fn) == 6
|
| 147 |
+
e = nx.eccentricity(self.G, weight=self.weight_fn)
|
| 148 |
+
assert e[1] == 6
|
| 149 |
+
|
| 150 |
+
e = nx.eccentricity(self.G, v=1, weight=self.weight_fn)
|
| 151 |
+
assert e == 6
|
| 152 |
+
|
| 153 |
+
# This behavior changed in version 1.8 (ticket #739)
|
| 154 |
+
e = nx.eccentricity(self.G, v=[1, 1], weight=self.weight_fn)
|
| 155 |
+
assert e[1] == 6
|
| 156 |
+
e = nx.eccentricity(self.G, v=[1, 2], weight=self.weight_fn)
|
| 157 |
+
assert e[1] == 6
|
| 158 |
+
|
| 159 |
+
def test_diameter_weight_None(self):
|
| 160 |
+
assert nx.diameter(self.G, weight=None) == 3
|
| 161 |
+
|
| 162 |
+
def test_diameter_weight_attr(self):
|
| 163 |
+
assert (
|
| 164 |
+
nx.diameter(self.G, weight="weight")
|
| 165 |
+
== nx.diameter(self.G, weight="cost")
|
| 166 |
+
== 1.6
|
| 167 |
+
!= nx.diameter(self.G, weight="high_cost")
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
def test_diameter_weight_fn(self):
|
| 171 |
+
assert nx.diameter(self.G, weight=self.weight_fn) == 6
|
| 172 |
+
|
| 173 |
+
def test_radius_weight_None(self):
|
| 174 |
+
assert pytest.approx(nx.radius(self.G, weight=None)) == 2
|
| 175 |
+
|
| 176 |
+
def test_radius_weight_attr(self):
|
| 177 |
+
assert (
|
| 178 |
+
pytest.approx(nx.radius(self.G, weight="weight"))
|
| 179 |
+
== pytest.approx(nx.radius(self.G, weight="cost"))
|
| 180 |
+
== 0.9
|
| 181 |
+
!= nx.radius(self.G, weight="high_cost")
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
def test_radius_weight_fn(self):
|
| 185 |
+
assert nx.radius(self.G, weight=self.weight_fn) == 4
|
| 186 |
+
|
| 187 |
+
def test_periphery_weight_None(self):
|
| 188 |
+
for v in set(nx.periphery(self.G, weight=None)):
|
| 189 |
+
assert nx.eccentricity(self.G, v, weight=None) == nx.diameter(
|
| 190 |
+
self.G, weight=None
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
def test_periphery_weight_attr(self):
|
| 194 |
+
periphery = set(nx.periphery(self.G, weight="weight"))
|
| 195 |
+
assert (
|
| 196 |
+
periphery
|
| 197 |
+
== set(nx.periphery(self.G, weight="cost"))
|
| 198 |
+
== set(nx.periphery(self.G, weight="high_cost"))
|
| 199 |
+
)
|
| 200 |
+
for v in periphery:
|
| 201 |
+
assert (
|
| 202 |
+
nx.eccentricity(self.G, v, weight="high_cost")
|
| 203 |
+
!= nx.eccentricity(self.G, v, weight="weight")
|
| 204 |
+
== nx.eccentricity(self.G, v, weight="cost")
|
| 205 |
+
== nx.diameter(self.G, weight="weight")
|
| 206 |
+
== nx.diameter(self.G, weight="cost")
|
| 207 |
+
!= nx.diameter(self.G, weight="high_cost")
|
| 208 |
+
)
|
| 209 |
+
assert nx.eccentricity(self.G, v, weight="high_cost") == nx.diameter(
|
| 210 |
+
self.G, weight="high_cost"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
def test_periphery_weight_fn(self):
|
| 214 |
+
for v in set(nx.periphery(self.G, weight=self.weight_fn)):
|
| 215 |
+
assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.diameter(
|
| 216 |
+
self.G, weight=self.weight_fn
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
def test_center_weight_None(self):
|
| 220 |
+
for v in set(nx.center(self.G, weight=None)):
|
| 221 |
+
assert pytest.approx(nx.eccentricity(self.G, v, weight=None)) == nx.radius(
|
| 222 |
+
self.G, weight=None
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
def test_center_weight_attr(self):
|
| 226 |
+
center = set(nx.center(self.G, weight="weight"))
|
| 227 |
+
assert (
|
| 228 |
+
center
|
| 229 |
+
== set(nx.center(self.G, weight="cost"))
|
| 230 |
+
!= set(nx.center(self.G, weight="high_cost"))
|
| 231 |
+
)
|
| 232 |
+
for v in center:
|
| 233 |
+
assert (
|
| 234 |
+
nx.eccentricity(self.G, v, weight="high_cost")
|
| 235 |
+
!= pytest.approx(nx.eccentricity(self.G, v, weight="weight"))
|
| 236 |
+
== pytest.approx(nx.eccentricity(self.G, v, weight="cost"))
|
| 237 |
+
== nx.radius(self.G, weight="weight")
|
| 238 |
+
== nx.radius(self.G, weight="cost")
|
| 239 |
+
!= nx.radius(self.G, weight="high_cost")
|
| 240 |
+
)
|
| 241 |
+
assert nx.eccentricity(self.G, v, weight="high_cost") == nx.radius(
|
| 242 |
+
self.G, weight="high_cost"
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
def test_center_weight_fn(self):
|
| 246 |
+
for v in set(nx.center(self.G, weight=self.weight_fn)):
|
| 247 |
+
assert nx.eccentricity(self.G, v, weight=self.weight_fn) == nx.radius(
|
| 248 |
+
self.G, weight=self.weight_fn
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
def test_bound_diameter_weight_None(self):
|
| 252 |
+
assert nx.diameter(self.G, usebounds=True, weight=None) == 3
|
| 253 |
+
|
| 254 |
+
def test_bound_diameter_weight_attr(self):
|
| 255 |
+
assert (
|
| 256 |
+
nx.diameter(self.G, usebounds=True, weight="high_cost")
|
| 257 |
+
!= nx.diameter(self.G, usebounds=True, weight="weight")
|
| 258 |
+
== nx.diameter(self.G, usebounds=True, weight="cost")
|
| 259 |
+
== 1.6
|
| 260 |
+
!= nx.diameter(self.G, usebounds=True, weight="high_cost")
|
| 261 |
+
)
|
| 262 |
+
assert nx.diameter(self.G, usebounds=True, weight="high_cost") == nx.diameter(
|
| 263 |
+
self.G, usebounds=True, weight="high_cost"
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
def test_bound_diameter_weight_fn(self):
|
| 267 |
+
assert nx.diameter(self.G, usebounds=True, weight=self.weight_fn) == 6
|
| 268 |
+
|
| 269 |
+
def test_bound_radius_weight_None(self):
|
| 270 |
+
assert pytest.approx(nx.radius(self.G, usebounds=True, weight=None)) == 2
|
| 271 |
+
|
| 272 |
+
def test_bound_radius_weight_attr(self):
|
| 273 |
+
assert (
|
| 274 |
+
nx.radius(self.G, usebounds=True, weight="high_cost")
|
| 275 |
+
!= pytest.approx(nx.radius(self.G, usebounds=True, weight="weight"))
|
| 276 |
+
== pytest.approx(nx.radius(self.G, usebounds=True, weight="cost"))
|
| 277 |
+
== 0.9
|
| 278 |
+
!= nx.radius(self.G, usebounds=True, weight="high_cost")
|
| 279 |
+
)
|
| 280 |
+
assert nx.radius(self.G, usebounds=True, weight="high_cost") == nx.radius(
|
| 281 |
+
self.G, usebounds=True, weight="high_cost"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
def test_bound_radius_weight_fn(self):
|
| 285 |
+
assert nx.radius(self.G, usebounds=True, weight=self.weight_fn) == 4
|
| 286 |
+
|
| 287 |
+
def test_bound_periphery_weight_None(self):
|
| 288 |
+
result = {1, 3, 4}
|
| 289 |
+
assert set(nx.periphery(self.G, usebounds=True, weight=None)) == result
|
| 290 |
+
|
| 291 |
+
def test_bound_periphery_weight_attr(self):
|
| 292 |
+
result = {4, 5}
|
| 293 |
+
assert (
|
| 294 |
+
set(nx.periphery(self.G, usebounds=True, weight="weight"))
|
| 295 |
+
== set(nx.periphery(self.G, usebounds=True, weight="cost"))
|
| 296 |
+
== result
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
def test_bound_periphery_weight_fn(self):
|
| 300 |
+
result = {1, 3, 4}
|
| 301 |
+
assert (
|
| 302 |
+
set(nx.periphery(self.G, usebounds=True, weight=self.weight_fn)) == result
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
def test_bound_center_weight_None(self):
|
| 306 |
+
result = {0, 2, 5}
|
| 307 |
+
assert set(nx.center(self.G, usebounds=True, weight=None)) == result
|
| 308 |
+
|
| 309 |
+
def test_bound_center_weight_attr(self):
|
| 310 |
+
result = {0}
|
| 311 |
+
assert (
|
| 312 |
+
set(nx.center(self.G, usebounds=True, weight="weight"))
|
| 313 |
+
== set(nx.center(self.G, usebounds=True, weight="cost"))
|
| 314 |
+
== result
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
def test_bound_center_weight_fn(self):
|
| 318 |
+
result = {0, 2, 5}
|
| 319 |
+
assert set(nx.center(self.G, usebounds=True, weight=self.weight_fn)) == result
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
class TestResistanceDistance:
|
| 323 |
+
@classmethod
|
| 324 |
+
def setup_class(cls):
|
| 325 |
+
global np
|
| 326 |
+
np = pytest.importorskip("numpy")
|
| 327 |
+
sp = pytest.importorskip("scipy")
|
| 328 |
+
|
| 329 |
+
def setup_method(self):
|
| 330 |
+
G = nx.Graph()
|
| 331 |
+
G.add_edge(1, 2, weight=2)
|
| 332 |
+
G.add_edge(2, 3, weight=4)
|
| 333 |
+
G.add_edge(3, 4, weight=1)
|
| 334 |
+
G.add_edge(1, 4, weight=3)
|
| 335 |
+
self.G = G
|
| 336 |
+
|
| 337 |
+
def test_resistance_distance_directed_graph(self):
|
| 338 |
+
G = nx.DiGraph()
|
| 339 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 340 |
+
nx.resistance_distance(G)
|
| 341 |
+
|
| 342 |
+
def test_resistance_distance_empty(self):
|
| 343 |
+
G = nx.Graph()
|
| 344 |
+
with pytest.raises(nx.NetworkXError):
|
| 345 |
+
nx.resistance_distance(G)
|
| 346 |
+
|
| 347 |
+
def test_resistance_distance_not_connected(self):
|
| 348 |
+
with pytest.raises(nx.NetworkXError):
|
| 349 |
+
self.G.add_node(5)
|
| 350 |
+
nx.resistance_distance(self.G, 1, 5)
|
| 351 |
+
|
| 352 |
+
def test_resistance_distance_nodeA_not_in_graph(self):
|
| 353 |
+
with pytest.raises(nx.NetworkXError):
|
| 354 |
+
nx.resistance_distance(self.G, 9, 1)
|
| 355 |
+
|
| 356 |
+
def test_resistance_distance_nodeB_not_in_graph(self):
|
| 357 |
+
with pytest.raises(nx.NetworkXError):
|
| 358 |
+
nx.resistance_distance(self.G, 1, 9)
|
| 359 |
+
|
| 360 |
+
def test_resistance_distance(self):
|
| 361 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
|
| 362 |
+
test_data = 1 / (1 / (2 + 4) + 1 / (1 + 3))
|
| 363 |
+
assert round(rd, 5) == round(test_data, 5)
|
| 364 |
+
|
| 365 |
+
def test_resistance_distance_noinv(self):
|
| 366 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", False)
|
| 367 |
+
test_data = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1 + 1 / 3))
|
| 368 |
+
assert round(rd, 5) == round(test_data, 5)
|
| 369 |
+
|
| 370 |
+
def test_resistance_distance_no_weight(self):
|
| 371 |
+
rd = nx.resistance_distance(self.G, 1, 3)
|
| 372 |
+
assert round(rd, 5) == 1
|
| 373 |
+
|
| 374 |
+
def test_resistance_distance_neg_weight(self):
|
| 375 |
+
self.G[2][3]["weight"] = -4
|
| 376 |
+
rd = nx.resistance_distance(self.G, 1, 3, "weight", True)
|
| 377 |
+
test_data = 1 / (1 / (2 + -4) + 1 / (1 + 3))
|
| 378 |
+
assert round(rd, 5) == round(test_data, 5)
|
| 379 |
+
|
| 380 |
+
def test_multigraph(self):
|
| 381 |
+
G = nx.MultiGraph()
|
| 382 |
+
G.add_edge(1, 2, weight=2)
|
| 383 |
+
G.add_edge(2, 3, weight=4)
|
| 384 |
+
G.add_edge(3, 4, weight=1)
|
| 385 |
+
G.add_edge(1, 4, weight=3)
|
| 386 |
+
rd = nx.resistance_distance(G, 1, 3, "weight", True)
|
| 387 |
+
assert np.isclose(rd, 1 / (1 / (2 + 4) + 1 / (1 + 3)))
|
| 388 |
+
|
| 389 |
+
def test_resistance_distance_div0(self):
|
| 390 |
+
with pytest.raises(ZeroDivisionError):
|
| 391 |
+
self.G[1][2]["weight"] = 0
|
| 392 |
+
nx.resistance_distance(self.G, 1, 3, "weight")
|
| 393 |
+
|
| 394 |
+
def test_resistance_distance_same_node(self):
|
| 395 |
+
assert nx.resistance_distance(self.G, 1, 1) == 0
|
| 396 |
+
|
| 397 |
+
def test_resistance_distance_only_nodeA(self):
|
| 398 |
+
rd = nx.resistance_distance(self.G, nodeA=1)
|
| 399 |
+
test_data = {}
|
| 400 |
+
test_data[1] = 0
|
| 401 |
+
test_data[2] = 0.75
|
| 402 |
+
test_data[3] = 1
|
| 403 |
+
test_data[4] = 0.75
|
| 404 |
+
assert type(rd) == dict
|
| 405 |
+
assert sorted(rd.keys()) == sorted(test_data.keys())
|
| 406 |
+
for key in rd:
|
| 407 |
+
assert np.isclose(rd[key], test_data[key])
|
| 408 |
+
|
| 409 |
+
def test_resistance_distance_only_nodeB(self):
|
| 410 |
+
rd = nx.resistance_distance(self.G, nodeB=1)
|
| 411 |
+
test_data = {}
|
| 412 |
+
test_data[1] = 0
|
| 413 |
+
test_data[2] = 0.75
|
| 414 |
+
test_data[3] = 1
|
| 415 |
+
test_data[4] = 0.75
|
| 416 |
+
assert type(rd) == dict
|
| 417 |
+
assert sorted(rd.keys()) == sorted(test_data.keys())
|
| 418 |
+
for key in rd:
|
| 419 |
+
assert np.isclose(rd[key], test_data[key])
|
| 420 |
+
|
| 421 |
+
def test_resistance_distance_all(self):
|
| 422 |
+
rd = nx.resistance_distance(self.G)
|
| 423 |
+
assert type(rd) == dict
|
| 424 |
+
assert round(rd[1][3], 5) == 1
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
class TestEffectiveGraphResistance:
|
| 428 |
+
@classmethod
|
| 429 |
+
def setup_class(cls):
|
| 430 |
+
global np
|
| 431 |
+
np = pytest.importorskip("numpy")
|
| 432 |
+
sp = pytest.importorskip("scipy")
|
| 433 |
+
|
| 434 |
+
def setup_method(self):
|
| 435 |
+
G = nx.Graph()
|
| 436 |
+
G.add_edge(1, 2, weight=2)
|
| 437 |
+
G.add_edge(1, 3, weight=1)
|
| 438 |
+
G.add_edge(2, 3, weight=4)
|
| 439 |
+
self.G = G
|
| 440 |
+
|
| 441 |
+
def test_effective_graph_resistance_directed_graph(self):
|
| 442 |
+
G = nx.DiGraph()
|
| 443 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 444 |
+
nx.effective_graph_resistance(G)
|
| 445 |
+
|
| 446 |
+
def test_effective_graph_resistance_empty(self):
|
| 447 |
+
G = nx.Graph()
|
| 448 |
+
with pytest.raises(nx.NetworkXError):
|
| 449 |
+
nx.effective_graph_resistance(G)
|
| 450 |
+
|
| 451 |
+
def test_effective_graph_resistance_not_connected(self):
|
| 452 |
+
G = nx.Graph([(1, 2), (3, 4)])
|
| 453 |
+
RG = nx.effective_graph_resistance(G)
|
| 454 |
+
assert np.isinf(RG)
|
| 455 |
+
|
| 456 |
+
def test_effective_graph_resistance(self):
|
| 457 |
+
RG = nx.effective_graph_resistance(self.G, "weight", True)
|
| 458 |
+
rd12 = 1 / (1 / (1 + 4) + 1 / 2)
|
| 459 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / 4)
|
| 460 |
+
rd23 = 1 / (1 / (2 + 4) + 1 / 1)
|
| 461 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
| 462 |
+
|
| 463 |
+
def test_effective_graph_resistance_noinv(self):
|
| 464 |
+
RG = nx.effective_graph_resistance(self.G, "weight", False)
|
| 465 |
+
rd12 = 1 / (1 / (1 / 1 + 1 / 4) + 1 / (1 / 2))
|
| 466 |
+
rd13 = 1 / (1 / (1 / 1 + 1 / 2) + 1 / (1 / 4))
|
| 467 |
+
rd23 = 1 / (1 / (1 / 2 + 1 / 4) + 1 / (1 / 1))
|
| 468 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
| 469 |
+
|
| 470 |
+
def test_effective_graph_resistance_no_weight(self):
|
| 471 |
+
RG = nx.effective_graph_resistance(self.G)
|
| 472 |
+
assert np.isclose(RG, 2)
|
| 473 |
+
|
| 474 |
+
def test_effective_graph_resistance_neg_weight(self):
|
| 475 |
+
self.G[2][3]["weight"] = -4
|
| 476 |
+
RG = nx.effective_graph_resistance(self.G, "weight", True)
|
| 477 |
+
rd12 = 1 / (1 / (1 + -4) + 1 / 2)
|
| 478 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / (-4))
|
| 479 |
+
rd23 = 1 / (1 / (2 + -4) + 1 / 1)
|
| 480 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
| 481 |
+
|
| 482 |
+
def test_effective_graph_resistance_multigraph(self):
|
| 483 |
+
G = nx.MultiGraph()
|
| 484 |
+
G.add_edge(1, 2, weight=2)
|
| 485 |
+
G.add_edge(1, 3, weight=1)
|
| 486 |
+
G.add_edge(2, 3, weight=1)
|
| 487 |
+
G.add_edge(2, 3, weight=3)
|
| 488 |
+
RG = nx.effective_graph_resistance(G, "weight", True)
|
| 489 |
+
edge23 = 1 / (1 / 1 + 1 / 3)
|
| 490 |
+
rd12 = 1 / (1 / (1 + edge23) + 1 / 2)
|
| 491 |
+
rd13 = 1 / (1 / (1 + 2) + 1 / edge23)
|
| 492 |
+
rd23 = 1 / (1 / (2 + edge23) + 1 / 1)
|
| 493 |
+
assert np.isclose(RG, rd12 + rd13 + rd23)
|
| 494 |
+
|
| 495 |
+
def test_effective_graph_resistance_div0(self):
|
| 496 |
+
with pytest.raises(ZeroDivisionError):
|
| 497 |
+
self.G[1][2]["weight"] = 0
|
| 498 |
+
nx.effective_graph_resistance(self.G, "weight")
|
| 499 |
+
|
| 500 |
+
def test_effective_graph_resistance_complete_graph(self):
|
| 501 |
+
N = 10
|
| 502 |
+
G = nx.complete_graph(N)
|
| 503 |
+
RG = nx.effective_graph_resistance(G)
|
| 504 |
+
assert np.isclose(RG, N - 1)
|
| 505 |
+
|
| 506 |
+
def test_effective_graph_resistance_path_graph(self):
|
| 507 |
+
N = 10
|
| 508 |
+
G = nx.path_graph(N)
|
| 509 |
+
RG = nx.effective_graph_resistance(G)
|
| 510 |
+
assert np.isclose(RG, (N - 1) * N * (N + 1) // 6)
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
class TestBarycenter:
|
| 514 |
+
"""Test :func:`networkx.algorithms.distance_measures.barycenter`."""
|
| 515 |
+
|
| 516 |
+
def barycenter_as_subgraph(self, g, **kwargs):
|
| 517 |
+
"""Return the subgraph induced on the barycenter of g"""
|
| 518 |
+
b = nx.barycenter(g, **kwargs)
|
| 519 |
+
assert isinstance(b, list)
|
| 520 |
+
assert set(b) <= set(g)
|
| 521 |
+
return g.subgraph(b)
|
| 522 |
+
|
| 523 |
+
def test_must_be_connected(self):
|
| 524 |
+
pytest.raises(nx.NetworkXNoPath, nx.barycenter, nx.empty_graph(5))
|
| 525 |
+
|
| 526 |
+
def test_sp_kwarg(self):
|
| 527 |
+
# Complete graph K_5. Normally it works...
|
| 528 |
+
K_5 = nx.complete_graph(5)
|
| 529 |
+
sp = dict(nx.shortest_path_length(K_5))
|
| 530 |
+
assert nx.barycenter(K_5, sp=sp) == list(K_5)
|
| 531 |
+
|
| 532 |
+
# ...but not with the weight argument
|
| 533 |
+
for u, v, data in K_5.edges.data():
|
| 534 |
+
data["weight"] = 1
|
| 535 |
+
pytest.raises(ValueError, nx.barycenter, K_5, sp=sp, weight="weight")
|
| 536 |
+
|
| 537 |
+
# ...and a corrupted sp can make it seem like K_5 is disconnected
|
| 538 |
+
del sp[0][1]
|
| 539 |
+
pytest.raises(nx.NetworkXNoPath, nx.barycenter, K_5, sp=sp)
|
| 540 |
+
|
| 541 |
+
def test_trees(self):
|
| 542 |
+
"""The barycenter of a tree is a single vertex or an edge.
|
| 543 |
+
|
| 544 |
+
See [West01]_, p. 78.
|
| 545 |
+
"""
|
| 546 |
+
prng = Random(0xDEADBEEF)
|
| 547 |
+
for i in range(50):
|
| 548 |
+
RT = nx.random_labeled_tree(prng.randint(1, 75), seed=prng)
|
| 549 |
+
b = self.barycenter_as_subgraph(RT)
|
| 550 |
+
if len(b) == 2:
|
| 551 |
+
assert b.size() == 1
|
| 552 |
+
else:
|
| 553 |
+
assert len(b) == 1
|
| 554 |
+
assert b.size() == 0
|
| 555 |
+
|
| 556 |
+
def test_this_one_specific_tree(self):
|
| 557 |
+
"""Test the tree pictured at the bottom of [West01]_, p. 78."""
|
| 558 |
+
g = nx.Graph(
|
| 559 |
+
{
|
| 560 |
+
"a": ["b"],
|
| 561 |
+
"b": ["a", "x"],
|
| 562 |
+
"x": ["b", "y"],
|
| 563 |
+
"y": ["x", "z"],
|
| 564 |
+
"z": ["y", 0, 1, 2, 3, 4],
|
| 565 |
+
0: ["z"],
|
| 566 |
+
1: ["z"],
|
| 567 |
+
2: ["z"],
|
| 568 |
+
3: ["z"],
|
| 569 |
+
4: ["z"],
|
| 570 |
+
}
|
| 571 |
+
)
|
| 572 |
+
b = self.barycenter_as_subgraph(g, attr="barycentricity")
|
| 573 |
+
assert list(b) == ["z"]
|
| 574 |
+
assert not b.edges
|
| 575 |
+
expected_barycentricity = {
|
| 576 |
+
0: 23,
|
| 577 |
+
1: 23,
|
| 578 |
+
2: 23,
|
| 579 |
+
3: 23,
|
| 580 |
+
4: 23,
|
| 581 |
+
"a": 35,
|
| 582 |
+
"b": 27,
|
| 583 |
+
"x": 21,
|
| 584 |
+
"y": 17,
|
| 585 |
+
"z": 15,
|
| 586 |
+
}
|
| 587 |
+
for node, barycentricity in expected_barycentricity.items():
|
| 588 |
+
assert g.nodes[node]["barycentricity"] == barycentricity
|
| 589 |
+
|
| 590 |
+
# Doubling weights should do nothing but double the barycentricities
|
| 591 |
+
for edge in g.edges:
|
| 592 |
+
g.edges[edge]["weight"] = 2
|
| 593 |
+
b = self.barycenter_as_subgraph(g, weight="weight", attr="barycentricity2")
|
| 594 |
+
assert list(b) == ["z"]
|
| 595 |
+
assert not b.edges
|
| 596 |
+
for node, barycentricity in expected_barycentricity.items():
|
| 597 |
+
assert g.nodes[node]["barycentricity2"] == barycentricity * 2
|
| 598 |
+
|
| 599 |
+
|
| 600 |
+
class TestKemenyConstant:
|
| 601 |
+
@classmethod
|
| 602 |
+
def setup_class(cls):
|
| 603 |
+
global np
|
| 604 |
+
np = pytest.importorskip("numpy")
|
| 605 |
+
sp = pytest.importorskip("scipy")
|
| 606 |
+
|
| 607 |
+
def setup_method(self):
|
| 608 |
+
G = nx.Graph()
|
| 609 |
+
w12 = 2
|
| 610 |
+
w13 = 3
|
| 611 |
+
w23 = 4
|
| 612 |
+
G.add_edge(1, 2, weight=w12)
|
| 613 |
+
G.add_edge(1, 3, weight=w13)
|
| 614 |
+
G.add_edge(2, 3, weight=w23)
|
| 615 |
+
self.G = G
|
| 616 |
+
|
| 617 |
+
def test_kemeny_constant_directed(self):
|
| 618 |
+
G = nx.DiGraph()
|
| 619 |
+
G.add_edge(1, 2)
|
| 620 |
+
G.add_edge(1, 3)
|
| 621 |
+
G.add_edge(2, 3)
|
| 622 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 623 |
+
nx.kemeny_constant(G)
|
| 624 |
+
|
| 625 |
+
def test_kemeny_constant_not_connected(self):
|
| 626 |
+
self.G.add_node(5)
|
| 627 |
+
with pytest.raises(nx.NetworkXError):
|
| 628 |
+
nx.kemeny_constant(self.G)
|
| 629 |
+
|
| 630 |
+
def test_kemeny_constant_no_nodes(self):
|
| 631 |
+
G = nx.Graph()
|
| 632 |
+
with pytest.raises(nx.NetworkXError):
|
| 633 |
+
nx.kemeny_constant(G)
|
| 634 |
+
|
| 635 |
+
def test_kemeny_constant_negative_weight(self):
|
| 636 |
+
G = nx.Graph()
|
| 637 |
+
w12 = 2
|
| 638 |
+
w13 = 3
|
| 639 |
+
w23 = -10
|
| 640 |
+
G.add_edge(1, 2, weight=w12)
|
| 641 |
+
G.add_edge(1, 3, weight=w13)
|
| 642 |
+
G.add_edge(2, 3, weight=w23)
|
| 643 |
+
with pytest.raises(nx.NetworkXError):
|
| 644 |
+
nx.kemeny_constant(G, weight="weight")
|
| 645 |
+
|
| 646 |
+
def test_kemeny_constant(self):
|
| 647 |
+
K = nx.kemeny_constant(self.G, weight="weight")
|
| 648 |
+
w12 = 2
|
| 649 |
+
w13 = 3
|
| 650 |
+
w23 = 4
|
| 651 |
+
test_data = (
|
| 652 |
+
3
|
| 653 |
+
/ 2
|
| 654 |
+
* (w12 + w13)
|
| 655 |
+
* (w12 + w23)
|
| 656 |
+
* (w13 + w23)
|
| 657 |
+
/ (
|
| 658 |
+
w12**2 * (w13 + w23)
|
| 659 |
+
+ w13**2 * (w12 + w23)
|
| 660 |
+
+ w23**2 * (w12 + w13)
|
| 661 |
+
+ 3 * w12 * w13 * w23
|
| 662 |
+
)
|
| 663 |
+
)
|
| 664 |
+
assert np.isclose(K, test_data)
|
| 665 |
+
|
| 666 |
+
def test_kemeny_constant_no_weight(self):
|
| 667 |
+
K = nx.kemeny_constant(self.G)
|
| 668 |
+
assert np.isclose(K, 4 / 3)
|
| 669 |
+
|
| 670 |
+
def test_kemeny_constant_multigraph(self):
|
| 671 |
+
G = nx.MultiGraph()
|
| 672 |
+
w12_1 = 2
|
| 673 |
+
w12_2 = 1
|
| 674 |
+
w13 = 3
|
| 675 |
+
w23 = 4
|
| 676 |
+
G.add_edge(1, 2, weight=w12_1)
|
| 677 |
+
G.add_edge(1, 2, weight=w12_2)
|
| 678 |
+
G.add_edge(1, 3, weight=w13)
|
| 679 |
+
G.add_edge(2, 3, weight=w23)
|
| 680 |
+
K = nx.kemeny_constant(G, weight="weight")
|
| 681 |
+
w12 = w12_1 + w12_2
|
| 682 |
+
test_data = (
|
| 683 |
+
3
|
| 684 |
+
/ 2
|
| 685 |
+
* (w12 + w13)
|
| 686 |
+
* (w12 + w23)
|
| 687 |
+
* (w13 + w23)
|
| 688 |
+
/ (
|
| 689 |
+
w12**2 * (w13 + w23)
|
| 690 |
+
+ w13**2 * (w12 + w23)
|
| 691 |
+
+ w23**2 * (w12 + w13)
|
| 692 |
+
+ 3 * w12 * w13 * w23
|
| 693 |
+
)
|
| 694 |
+
)
|
| 695 |
+
assert np.isclose(K, test_data)
|
| 696 |
+
|
| 697 |
+
def test_kemeny_constant_weight0(self):
|
| 698 |
+
G = nx.Graph()
|
| 699 |
+
w12 = 0
|
| 700 |
+
w13 = 3
|
| 701 |
+
w23 = 4
|
| 702 |
+
G.add_edge(1, 2, weight=w12)
|
| 703 |
+
G.add_edge(1, 3, weight=w13)
|
| 704 |
+
G.add_edge(2, 3, weight=w23)
|
| 705 |
+
K = nx.kemeny_constant(G, weight="weight")
|
| 706 |
+
test_data = (
|
| 707 |
+
3
|
| 708 |
+
/ 2
|
| 709 |
+
* (w12 + w13)
|
| 710 |
+
* (w12 + w23)
|
| 711 |
+
* (w13 + w23)
|
| 712 |
+
/ (
|
| 713 |
+
w12**2 * (w13 + w23)
|
| 714 |
+
+ w13**2 * (w12 + w23)
|
| 715 |
+
+ w23**2 * (w12 + w13)
|
| 716 |
+
+ 3 * w12 * w13 * w23
|
| 717 |
+
)
|
| 718 |
+
)
|
| 719 |
+
assert np.isclose(K, test_data)
|
| 720 |
+
|
| 721 |
+
def test_kemeny_constant_selfloop(self):
|
| 722 |
+
G = nx.Graph()
|
| 723 |
+
w11 = 1
|
| 724 |
+
w12 = 2
|
| 725 |
+
w13 = 3
|
| 726 |
+
w23 = 4
|
| 727 |
+
G.add_edge(1, 1, weight=w11)
|
| 728 |
+
G.add_edge(1, 2, weight=w12)
|
| 729 |
+
G.add_edge(1, 3, weight=w13)
|
| 730 |
+
G.add_edge(2, 3, weight=w23)
|
| 731 |
+
K = nx.kemeny_constant(G, weight="weight")
|
| 732 |
+
test_data = (
|
| 733 |
+
(2 * w11 + 3 * w12 + 3 * w13)
|
| 734 |
+
* (w12 + w23)
|
| 735 |
+
* (w13 + w23)
|
| 736 |
+
/ (
|
| 737 |
+
(w12 * w13 + w12 * w23 + w13 * w23)
|
| 738 |
+
* (w11 + 2 * w12 + 2 * w13 + 2 * w23)
|
| 739 |
+
)
|
| 740 |
+
)
|
| 741 |
+
assert np.isclose(K, test_data)
|
| 742 |
+
|
| 743 |
+
def test_kemeny_constant_complete_bipartite_graph(self):
|
| 744 |
+
# Theorem 1 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
|
| 745 |
+
n1 = 5
|
| 746 |
+
n2 = 4
|
| 747 |
+
G = nx.complete_bipartite_graph(n1, n2)
|
| 748 |
+
K = nx.kemeny_constant(G)
|
| 749 |
+
assert np.isclose(K, n1 + n2 - 3 / 2)
|
| 750 |
+
|
| 751 |
+
def test_kemeny_constant_path_graph(self):
|
| 752 |
+
# Theorem 2 in https://www.sciencedirect.com/science/article/pii/S0166218X20302912
|
| 753 |
+
n = 10
|
| 754 |
+
G = nx.path_graph(n)
|
| 755 |
+
K = nx.kemeny_constant(G)
|
| 756 |
+
assert np.isclose(K, n**2 / 3 - 2 * n / 3 + 1 / 2)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_distance_regular.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx import is_strongly_regular
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.mark.parametrize(
|
| 8 |
+
"f", (nx.is_distance_regular, nx.intersection_array, nx.is_strongly_regular)
|
| 9 |
+
)
|
| 10 |
+
@pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
|
| 11 |
+
def test_raises_on_directed_and_multigraphs(f, graph_constructor):
|
| 12 |
+
G = graph_constructor([(0, 1), (1, 2)])
|
| 13 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 14 |
+
f(G)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TestDistanceRegular:
|
| 18 |
+
def test_is_distance_regular(self):
|
| 19 |
+
assert nx.is_distance_regular(nx.icosahedral_graph())
|
| 20 |
+
assert nx.is_distance_regular(nx.petersen_graph())
|
| 21 |
+
assert nx.is_distance_regular(nx.cubical_graph())
|
| 22 |
+
assert nx.is_distance_regular(nx.complete_bipartite_graph(3, 3))
|
| 23 |
+
assert nx.is_distance_regular(nx.tetrahedral_graph())
|
| 24 |
+
assert nx.is_distance_regular(nx.dodecahedral_graph())
|
| 25 |
+
assert nx.is_distance_regular(nx.pappus_graph())
|
| 26 |
+
assert nx.is_distance_regular(nx.heawood_graph())
|
| 27 |
+
assert nx.is_distance_regular(nx.cycle_graph(3))
|
| 28 |
+
# no distance regular
|
| 29 |
+
assert not nx.is_distance_regular(nx.path_graph(4))
|
| 30 |
+
|
| 31 |
+
def test_not_connected(self):
|
| 32 |
+
G = nx.cycle_graph(4)
|
| 33 |
+
nx.add_cycle(G, [5, 6, 7])
|
| 34 |
+
assert not nx.is_distance_regular(G)
|
| 35 |
+
|
| 36 |
+
def test_global_parameters(self):
|
| 37 |
+
b, c = nx.intersection_array(nx.cycle_graph(5))
|
| 38 |
+
g = nx.global_parameters(b, c)
|
| 39 |
+
assert list(g) == [(0, 0, 2), (1, 0, 1), (1, 1, 0)]
|
| 40 |
+
b, c = nx.intersection_array(nx.cycle_graph(3))
|
| 41 |
+
g = nx.global_parameters(b, c)
|
| 42 |
+
assert list(g) == [(0, 0, 2), (1, 1, 0)]
|
| 43 |
+
|
| 44 |
+
def test_intersection_array(self):
|
| 45 |
+
b, c = nx.intersection_array(nx.cycle_graph(5))
|
| 46 |
+
assert b == [2, 1]
|
| 47 |
+
assert c == [1, 1]
|
| 48 |
+
b, c = nx.intersection_array(nx.dodecahedral_graph())
|
| 49 |
+
assert b == [3, 2, 1, 1, 1]
|
| 50 |
+
assert c == [1, 1, 1, 2, 3]
|
| 51 |
+
b, c = nx.intersection_array(nx.icosahedral_graph())
|
| 52 |
+
assert b == [5, 2, 1]
|
| 53 |
+
assert c == [1, 2, 5]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@pytest.mark.parametrize("f", (nx.is_distance_regular, nx.is_strongly_regular))
|
| 57 |
+
def test_empty_graph_raises(f):
|
| 58 |
+
G = nx.Graph()
|
| 59 |
+
with pytest.raises(nx.NetworkXPointlessConcept, match="Graph has no nodes"):
|
| 60 |
+
f(G)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class TestStronglyRegular:
|
| 64 |
+
"""Unit tests for the :func:`~networkx.is_strongly_regular`
|
| 65 |
+
function.
|
| 66 |
+
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
def test_cycle_graph(self):
|
| 70 |
+
"""Tests that the cycle graph on five vertices is strongly
|
| 71 |
+
regular.
|
| 72 |
+
|
| 73 |
+
"""
|
| 74 |
+
G = nx.cycle_graph(5)
|
| 75 |
+
assert is_strongly_regular(G)
|
| 76 |
+
|
| 77 |
+
def test_petersen_graph(self):
|
| 78 |
+
"""Tests that the Petersen graph is strongly regular."""
|
| 79 |
+
G = nx.petersen_graph()
|
| 80 |
+
assert is_strongly_regular(G)
|
| 81 |
+
|
| 82 |
+
def test_path_graph(self):
|
| 83 |
+
"""Tests that the path graph is not strongly regular."""
|
| 84 |
+
G = nx.path_graph(4)
|
| 85 |
+
assert not is_strongly_regular(G)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominance.py
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestImmediateDominators:
|
| 7 |
+
def test_exceptions(self):
|
| 8 |
+
G = nx.Graph()
|
| 9 |
+
G.add_node(0)
|
| 10 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
|
| 11 |
+
G = nx.MultiGraph(G)
|
| 12 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.immediate_dominators, G, 0)
|
| 13 |
+
G = nx.DiGraph([[0, 0]])
|
| 14 |
+
pytest.raises(nx.NetworkXError, nx.immediate_dominators, G, 1)
|
| 15 |
+
|
| 16 |
+
def test_singleton(self):
|
| 17 |
+
G = nx.DiGraph()
|
| 18 |
+
G.add_node(0)
|
| 19 |
+
assert nx.immediate_dominators(G, 0) == {0: 0}
|
| 20 |
+
G.add_edge(0, 0)
|
| 21 |
+
assert nx.immediate_dominators(G, 0) == {0: 0}
|
| 22 |
+
|
| 23 |
+
def test_path(self):
|
| 24 |
+
n = 5
|
| 25 |
+
G = nx.path_graph(n, create_using=nx.DiGraph())
|
| 26 |
+
assert nx.immediate_dominators(G, 0) == {i: max(i - 1, 0) for i in range(n)}
|
| 27 |
+
|
| 28 |
+
def test_cycle(self):
|
| 29 |
+
n = 5
|
| 30 |
+
G = nx.cycle_graph(n, create_using=nx.DiGraph())
|
| 31 |
+
assert nx.immediate_dominators(G, 0) == {i: max(i - 1, 0) for i in range(n)}
|
| 32 |
+
|
| 33 |
+
def test_unreachable(self):
|
| 34 |
+
n = 5
|
| 35 |
+
assert n > 1
|
| 36 |
+
G = nx.path_graph(n, create_using=nx.DiGraph())
|
| 37 |
+
assert nx.immediate_dominators(G, n // 2) == {
|
| 38 |
+
i: max(i - 1, n // 2) for i in range(n // 2, n)
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
def test_irreducible1(self):
|
| 42 |
+
# Graph taken from Figure 2 of
|
| 43 |
+
# K. D. Cooper, T. J. Harvey, and K. Kennedy.
|
| 44 |
+
# A simple, fast dominance algorithm.
|
| 45 |
+
# Software Practice & Experience, 4:110, 2001.
|
| 46 |
+
edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
|
| 47 |
+
G = nx.DiGraph(edges)
|
| 48 |
+
assert nx.immediate_dominators(G, 5) == {i: 5 for i in range(1, 6)}
|
| 49 |
+
|
| 50 |
+
def test_irreducible2(self):
|
| 51 |
+
# Graph taken from Figure 4 of
|
| 52 |
+
# K. D. Cooper, T. J. Harvey, and K. Kennedy.
|
| 53 |
+
# A simple, fast dominance algorithm.
|
| 54 |
+
# Software Practice & Experience, 4:110, 2001.
|
| 55 |
+
edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1), (6, 4), (6, 5)]
|
| 56 |
+
G = nx.DiGraph(edges)
|
| 57 |
+
result = nx.immediate_dominators(G, 6)
|
| 58 |
+
assert result == {i: 6 for i in range(1, 7)}
|
| 59 |
+
|
| 60 |
+
def test_domrel_png(self):
|
| 61 |
+
# Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
|
| 62 |
+
edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
|
| 63 |
+
G = nx.DiGraph(edges)
|
| 64 |
+
result = nx.immediate_dominators(G, 1)
|
| 65 |
+
assert result == {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2}
|
| 66 |
+
# Test postdominance.
|
| 67 |
+
result = nx.immediate_dominators(G.reverse(copy=False), 6)
|
| 68 |
+
assert result == {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}
|
| 69 |
+
|
| 70 |
+
def test_boost_example(self):
|
| 71 |
+
# Graph taken from Figure 1 of
|
| 72 |
+
# http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
|
| 73 |
+
edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6), (5, 7), (6, 4)]
|
| 74 |
+
G = nx.DiGraph(edges)
|
| 75 |
+
result = nx.immediate_dominators(G, 0)
|
| 76 |
+
assert result == {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1}
|
| 77 |
+
# Test postdominance.
|
| 78 |
+
result = nx.immediate_dominators(G.reverse(copy=False), 7)
|
| 79 |
+
assert result == {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class TestDominanceFrontiers:
|
| 83 |
+
def test_exceptions(self):
|
| 84 |
+
G = nx.Graph()
|
| 85 |
+
G.add_node(0)
|
| 86 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.dominance_frontiers, G, 0)
|
| 87 |
+
G = nx.MultiGraph(G)
|
| 88 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.dominance_frontiers, G, 0)
|
| 89 |
+
G = nx.DiGraph([[0, 0]])
|
| 90 |
+
pytest.raises(nx.NetworkXError, nx.dominance_frontiers, G, 1)
|
| 91 |
+
|
| 92 |
+
def test_singleton(self):
|
| 93 |
+
G = nx.DiGraph()
|
| 94 |
+
G.add_node(0)
|
| 95 |
+
assert nx.dominance_frontiers(G, 0) == {0: set()}
|
| 96 |
+
G.add_edge(0, 0)
|
| 97 |
+
assert nx.dominance_frontiers(G, 0) == {0: set()}
|
| 98 |
+
|
| 99 |
+
def test_path(self):
|
| 100 |
+
n = 5
|
| 101 |
+
G = nx.path_graph(n, create_using=nx.DiGraph())
|
| 102 |
+
assert nx.dominance_frontiers(G, 0) == {i: set() for i in range(n)}
|
| 103 |
+
|
| 104 |
+
def test_cycle(self):
|
| 105 |
+
n = 5
|
| 106 |
+
G = nx.cycle_graph(n, create_using=nx.DiGraph())
|
| 107 |
+
assert nx.dominance_frontiers(G, 0) == {i: set() for i in range(n)}
|
| 108 |
+
|
| 109 |
+
def test_unreachable(self):
|
| 110 |
+
n = 5
|
| 111 |
+
assert n > 1
|
| 112 |
+
G = nx.path_graph(n, create_using=nx.DiGraph())
|
| 113 |
+
assert nx.dominance_frontiers(G, n // 2) == {i: set() for i in range(n // 2, n)}
|
| 114 |
+
|
| 115 |
+
def test_irreducible1(self):
|
| 116 |
+
# Graph taken from Figure 2 of
|
| 117 |
+
# K. D. Cooper, T. J. Harvey, and K. Kennedy.
|
| 118 |
+
# A simple, fast dominance algorithm.
|
| 119 |
+
# Software Practice & Experience, 4:110, 2001.
|
| 120 |
+
edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)]
|
| 121 |
+
G = nx.DiGraph(edges)
|
| 122 |
+
assert dict(nx.dominance_frontiers(G, 5).items()) == {
|
| 123 |
+
1: {2},
|
| 124 |
+
2: {1},
|
| 125 |
+
3: {2},
|
| 126 |
+
4: {1},
|
| 127 |
+
5: set(),
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
def test_irreducible2(self):
|
| 131 |
+
# Graph taken from Figure 4 of
|
| 132 |
+
# K. D. Cooper, T. J. Harvey, and K. Kennedy.
|
| 133 |
+
# A simple, fast dominance algorithm.
|
| 134 |
+
# Software Practice & Experience, 4:110, 2001.
|
| 135 |
+
edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1), (6, 4), (6, 5)]
|
| 136 |
+
G = nx.DiGraph(edges)
|
| 137 |
+
assert nx.dominance_frontiers(G, 6) == {
|
| 138 |
+
1: {2},
|
| 139 |
+
2: {1, 3},
|
| 140 |
+
3: {2},
|
| 141 |
+
4: {2, 3},
|
| 142 |
+
5: {1},
|
| 143 |
+
6: set(),
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
def test_domrel_png(self):
|
| 147 |
+
# Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png
|
| 148 |
+
edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)]
|
| 149 |
+
G = nx.DiGraph(edges)
|
| 150 |
+
assert nx.dominance_frontiers(G, 1) == {
|
| 151 |
+
1: set(),
|
| 152 |
+
2: {2},
|
| 153 |
+
3: {5},
|
| 154 |
+
4: {5},
|
| 155 |
+
5: {2},
|
| 156 |
+
6: set(),
|
| 157 |
+
}
|
| 158 |
+
# Test postdominance.
|
| 159 |
+
result = nx.dominance_frontiers(G.reverse(copy=False), 6)
|
| 160 |
+
assert result == {1: set(), 2: {2}, 3: {2}, 4: {2}, 5: {2}, 6: set()}
|
| 161 |
+
|
| 162 |
+
def test_boost_example(self):
|
| 163 |
+
# Graph taken from Figure 1 of
|
| 164 |
+
# http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm
|
| 165 |
+
edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6), (5, 7), (6, 4)]
|
| 166 |
+
G = nx.DiGraph(edges)
|
| 167 |
+
assert nx.dominance_frontiers(G, 0) == {
|
| 168 |
+
0: set(),
|
| 169 |
+
1: set(),
|
| 170 |
+
2: {7},
|
| 171 |
+
3: {7},
|
| 172 |
+
4: {4, 7},
|
| 173 |
+
5: {7},
|
| 174 |
+
6: {4},
|
| 175 |
+
7: set(),
|
| 176 |
+
}
|
| 177 |
+
# Test postdominance.
|
| 178 |
+
result = nx.dominance_frontiers(G.reverse(copy=False), 7)
|
| 179 |
+
expected = {
|
| 180 |
+
0: set(),
|
| 181 |
+
1: set(),
|
| 182 |
+
2: {1},
|
| 183 |
+
3: {1},
|
| 184 |
+
4: {1, 4},
|
| 185 |
+
5: {1},
|
| 186 |
+
6: {4},
|
| 187 |
+
7: set(),
|
| 188 |
+
}
|
| 189 |
+
assert result == expected
|
| 190 |
+
|
| 191 |
+
def test_discard_issue(self):
|
| 192 |
+
# https://github.com/networkx/networkx/issues/2071
|
| 193 |
+
g = nx.DiGraph()
|
| 194 |
+
g.add_edges_from(
|
| 195 |
+
[
|
| 196 |
+
("b0", "b1"),
|
| 197 |
+
("b1", "b2"),
|
| 198 |
+
("b2", "b3"),
|
| 199 |
+
("b3", "b1"),
|
| 200 |
+
("b1", "b5"),
|
| 201 |
+
("b5", "b6"),
|
| 202 |
+
("b5", "b8"),
|
| 203 |
+
("b6", "b7"),
|
| 204 |
+
("b8", "b7"),
|
| 205 |
+
("b7", "b3"),
|
| 206 |
+
("b3", "b4"),
|
| 207 |
+
]
|
| 208 |
+
)
|
| 209 |
+
df = nx.dominance_frontiers(g, "b0")
|
| 210 |
+
assert df == {
|
| 211 |
+
"b4": set(),
|
| 212 |
+
"b5": {"b3"},
|
| 213 |
+
"b6": {"b7"},
|
| 214 |
+
"b7": {"b3"},
|
| 215 |
+
"b0": set(),
|
| 216 |
+
"b1": {"b1"},
|
| 217 |
+
"b2": {"b3"},
|
| 218 |
+
"b3": {"b1"},
|
| 219 |
+
"b8": {"b7"},
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
def test_loop(self):
|
| 223 |
+
g = nx.DiGraph()
|
| 224 |
+
g.add_edges_from([("a", "b"), ("b", "c"), ("b", "a")])
|
| 225 |
+
df = nx.dominance_frontiers(g, "a")
|
| 226 |
+
assert df == {"a": set(), "b": set(), "c": set()}
|
| 227 |
+
|
| 228 |
+
def test_missing_immediate_doms(self):
|
| 229 |
+
# see https://github.com/networkx/networkx/issues/2070
|
| 230 |
+
g = nx.DiGraph()
|
| 231 |
+
edges = [
|
| 232 |
+
("entry_1", "b1"),
|
| 233 |
+
("b1", "b2"),
|
| 234 |
+
("b2", "b3"),
|
| 235 |
+
("b3", "exit"),
|
| 236 |
+
("entry_2", "b3"),
|
| 237 |
+
]
|
| 238 |
+
|
| 239 |
+
# entry_1
|
| 240 |
+
# |
|
| 241 |
+
# b1
|
| 242 |
+
# |
|
| 243 |
+
# b2 entry_2
|
| 244 |
+
# | /
|
| 245 |
+
# b3
|
| 246 |
+
# |
|
| 247 |
+
# exit
|
| 248 |
+
|
| 249 |
+
g.add_edges_from(edges)
|
| 250 |
+
# formerly raised KeyError on entry_2 when parsing b3
|
| 251 |
+
# because entry_2 does not have immediate doms (no path)
|
| 252 |
+
nx.dominance_frontiers(g, "entry_1")
|
| 253 |
+
|
| 254 |
+
def test_loops_larger(self):
|
| 255 |
+
# from
|
| 256 |
+
# http://ecee.colorado.edu/~waite/Darmstadt/motion.html
|
| 257 |
+
g = nx.DiGraph()
|
| 258 |
+
edges = [
|
| 259 |
+
("entry", "exit"),
|
| 260 |
+
("entry", "1"),
|
| 261 |
+
("1", "2"),
|
| 262 |
+
("2", "3"),
|
| 263 |
+
("3", "4"),
|
| 264 |
+
("4", "5"),
|
| 265 |
+
("5", "6"),
|
| 266 |
+
("6", "exit"),
|
| 267 |
+
("6", "2"),
|
| 268 |
+
("5", "3"),
|
| 269 |
+
("4", "4"),
|
| 270 |
+
]
|
| 271 |
+
|
| 272 |
+
g.add_edges_from(edges)
|
| 273 |
+
df = nx.dominance_frontiers(g, "entry")
|
| 274 |
+
answer = {
|
| 275 |
+
"entry": set(),
|
| 276 |
+
"1": {"exit"},
|
| 277 |
+
"2": {"exit", "2"},
|
| 278 |
+
"3": {"exit", "3", "2"},
|
| 279 |
+
"4": {"exit", "4", "3", "2"},
|
| 280 |
+
"5": {"exit", "3", "2"},
|
| 281 |
+
"6": {"exit", "2"},
|
| 282 |
+
"exit": set(),
|
| 283 |
+
}
|
| 284 |
+
for n in df:
|
| 285 |
+
assert set(df[n]) == set(answer[n])
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_dominating.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_dominating_set():
|
| 7 |
+
G = nx.gnp_random_graph(100, 0.1)
|
| 8 |
+
D = nx.dominating_set(G)
|
| 9 |
+
assert nx.is_dominating_set(G, D)
|
| 10 |
+
D = nx.dominating_set(G, start_with=0)
|
| 11 |
+
assert nx.is_dominating_set(G, D)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_complete():
|
| 15 |
+
"""In complete graphs each node is a dominating set.
|
| 16 |
+
Thus the dominating set has to be of cardinality 1.
|
| 17 |
+
"""
|
| 18 |
+
K4 = nx.complete_graph(4)
|
| 19 |
+
assert len(nx.dominating_set(K4)) == 1
|
| 20 |
+
K5 = nx.complete_graph(5)
|
| 21 |
+
assert len(nx.dominating_set(K5)) == 1
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_raise_dominating_set():
|
| 25 |
+
with pytest.raises(nx.NetworkXError):
|
| 26 |
+
G = nx.path_graph(4)
|
| 27 |
+
D = nx.dominating_set(G, start_with=10)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_is_dominating_set():
|
| 31 |
+
G = nx.path_graph(4)
|
| 32 |
+
d = {1, 3}
|
| 33 |
+
assert nx.is_dominating_set(G, d)
|
| 34 |
+
d = {0, 2}
|
| 35 |
+
assert nx.is_dominating_set(G, d)
|
| 36 |
+
d = {1}
|
| 37 |
+
assert not nx.is_dominating_set(G, d)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_wikipedia_is_dominating_set():
|
| 41 |
+
"""Example from https://en.wikipedia.org/wiki/Dominating_set"""
|
| 42 |
+
G = nx.cycle_graph(4)
|
| 43 |
+
G.add_edges_from([(0, 4), (1, 4), (2, 5)])
|
| 44 |
+
assert nx.is_dominating_set(G, {4, 3, 5})
|
| 45 |
+
assert nx.is_dominating_set(G, {0, 2})
|
| 46 |
+
assert nx.is_dominating_set(G, {1, 2})
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_efficiency.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the :mod:`networkx.algorithms.efficiency` module."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestEfficiency:
|
| 7 |
+
def setup_method(self):
|
| 8 |
+
# G1 is a disconnected graph
|
| 9 |
+
self.G1 = nx.Graph()
|
| 10 |
+
self.G1.add_nodes_from([1, 2, 3])
|
| 11 |
+
# G2 is a cycle graph
|
| 12 |
+
self.G2 = nx.cycle_graph(4)
|
| 13 |
+
# G3 is the triangle graph with one additional edge
|
| 14 |
+
self.G3 = nx.lollipop_graph(3, 1)
|
| 15 |
+
|
| 16 |
+
def test_efficiency_disconnected_nodes(self):
|
| 17 |
+
"""
|
| 18 |
+
When nodes are disconnected, efficiency is 0
|
| 19 |
+
"""
|
| 20 |
+
assert nx.efficiency(self.G1, 1, 2) == 0
|
| 21 |
+
|
| 22 |
+
def test_local_efficiency_disconnected_graph(self):
|
| 23 |
+
"""
|
| 24 |
+
In a disconnected graph the efficiency is 0
|
| 25 |
+
"""
|
| 26 |
+
assert nx.local_efficiency(self.G1) == 0
|
| 27 |
+
|
| 28 |
+
def test_efficiency(self):
|
| 29 |
+
assert nx.efficiency(self.G2, 0, 1) == 1
|
| 30 |
+
assert nx.efficiency(self.G2, 0, 2) == 1 / 2
|
| 31 |
+
|
| 32 |
+
def test_global_efficiency(self):
|
| 33 |
+
assert nx.global_efficiency(self.G2) == 5 / 6
|
| 34 |
+
|
| 35 |
+
def test_global_efficiency_complete_graph(self):
|
| 36 |
+
"""
|
| 37 |
+
Tests that the average global efficiency of the complete graph is one.
|
| 38 |
+
"""
|
| 39 |
+
for n in range(2, 10):
|
| 40 |
+
G = nx.complete_graph(n)
|
| 41 |
+
assert nx.global_efficiency(G) == 1
|
| 42 |
+
|
| 43 |
+
def test_local_efficiency_complete_graph(self):
|
| 44 |
+
"""
|
| 45 |
+
Test that the local efficiency for a complete graph with at least 3
|
| 46 |
+
nodes should be one. For a graph with only 2 nodes, the induced
|
| 47 |
+
subgraph has no edges.
|
| 48 |
+
"""
|
| 49 |
+
for n in range(3, 10):
|
| 50 |
+
G = nx.complete_graph(n)
|
| 51 |
+
assert nx.local_efficiency(G) == 1
|
| 52 |
+
|
| 53 |
+
def test_using_ego_graph(self):
|
| 54 |
+
"""
|
| 55 |
+
Test that the ego graph is used when computing local efficiency.
|
| 56 |
+
For more information, see GitHub issue #2710.
|
| 57 |
+
"""
|
| 58 |
+
assert nx.local_efficiency(self.G3) == 7 / 12
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_graphical.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_valid_degree_sequence1():
|
| 7 |
+
n = 100
|
| 8 |
+
p = 0.3
|
| 9 |
+
for i in range(10):
|
| 10 |
+
G = nx.erdos_renyi_graph(n, p)
|
| 11 |
+
deg = (d for n, d in G.degree())
|
| 12 |
+
assert nx.is_graphical(deg, method="eg")
|
| 13 |
+
assert nx.is_graphical(deg, method="hh")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_valid_degree_sequence2():
|
| 17 |
+
n = 100
|
| 18 |
+
for i in range(10):
|
| 19 |
+
G = nx.barabasi_albert_graph(n, 1)
|
| 20 |
+
deg = (d for n, d in G.degree())
|
| 21 |
+
assert nx.is_graphical(deg, method="eg")
|
| 22 |
+
assert nx.is_graphical(deg, method="hh")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_string_input():
|
| 26 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, [], "foo")
|
| 27 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "hh")
|
| 28 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "eg")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_non_integer_input():
|
| 32 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "eg")
|
| 33 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "hh")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_negative_input():
|
| 37 |
+
assert not nx.is_graphical([-1], "hh")
|
| 38 |
+
assert not nx.is_graphical([-1], "eg")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class TestAtlas:
|
| 42 |
+
@classmethod
|
| 43 |
+
def setup_class(cls):
|
| 44 |
+
global atlas
|
| 45 |
+
from networkx.generators import atlas
|
| 46 |
+
|
| 47 |
+
cls.GAG = atlas.graph_atlas_g()
|
| 48 |
+
|
| 49 |
+
def test_atlas(self):
|
| 50 |
+
for graph in self.GAG:
|
| 51 |
+
deg = (d for n, d in graph.degree())
|
| 52 |
+
assert nx.is_graphical(deg, method="eg")
|
| 53 |
+
assert nx.is_graphical(deg, method="hh")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_small_graph_true():
|
| 57 |
+
z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
| 58 |
+
assert nx.is_graphical(z, method="hh")
|
| 59 |
+
assert nx.is_graphical(z, method="eg")
|
| 60 |
+
z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
|
| 61 |
+
assert nx.is_graphical(z, method="hh")
|
| 62 |
+
assert nx.is_graphical(z, method="eg")
|
| 63 |
+
z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
| 64 |
+
assert nx.is_graphical(z, method="hh")
|
| 65 |
+
assert nx.is_graphical(z, method="eg")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_small_graph_false():
|
| 69 |
+
z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
| 70 |
+
assert not nx.is_graphical(z, method="hh")
|
| 71 |
+
assert not nx.is_graphical(z, method="eg")
|
| 72 |
+
z = [6, 5, 4, 4, 2, 1, 1, 1]
|
| 73 |
+
assert not nx.is_graphical(z, method="hh")
|
| 74 |
+
assert not nx.is_graphical(z, method="eg")
|
| 75 |
+
z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
| 76 |
+
assert not nx.is_graphical(z, method="hh")
|
| 77 |
+
assert not nx.is_graphical(z, method="eg")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_directed_degree_sequence():
|
| 81 |
+
# Test a range of valid directed degree sequences
|
| 82 |
+
n, r = 100, 10
|
| 83 |
+
p = 1.0 / r
|
| 84 |
+
for i in range(r):
|
| 85 |
+
G = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
|
| 86 |
+
din = (d for n, d in G.in_degree())
|
| 87 |
+
dout = (d for n, d in G.out_degree())
|
| 88 |
+
assert nx.is_digraphical(din, dout)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_small_directed_sequences():
|
| 92 |
+
dout = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
| 93 |
+
din = [3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1]
|
| 94 |
+
assert nx.is_digraphical(din, dout)
|
| 95 |
+
# Test nongraphical directed sequence
|
| 96 |
+
dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
| 97 |
+
din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
|
| 98 |
+
assert not nx.is_digraphical(din, dout)
|
| 99 |
+
# Test digraphical small sequence
|
| 100 |
+
dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
| 101 |
+
din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1]
|
| 102 |
+
assert nx.is_digraphical(din, dout)
|
| 103 |
+
# Test nonmatching sum
|
| 104 |
+
din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
|
| 105 |
+
assert not nx.is_digraphical(din, dout)
|
| 106 |
+
# Test for negative integer in sequence
|
| 107 |
+
din = [2, 2, 2, -2, 2, 2, 2, 2, 1, 1, 4]
|
| 108 |
+
assert not nx.is_digraphical(din, dout)
|
| 109 |
+
# Test for noninteger
|
| 110 |
+
din = dout = [1, 1, 1.1, 1]
|
| 111 |
+
assert not nx.is_digraphical(din, dout)
|
| 112 |
+
din = dout = [1, 1, "rer", 1]
|
| 113 |
+
assert not nx.is_digraphical(din, dout)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_multi_sequence():
|
| 117 |
+
# Test nongraphical multi sequence
|
| 118 |
+
seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
|
| 119 |
+
assert not nx.is_multigraphical(seq)
|
| 120 |
+
# Test small graphical multi sequence
|
| 121 |
+
seq = [6, 5, 4, 4, 2, 1, 1, 1]
|
| 122 |
+
assert nx.is_multigraphical(seq)
|
| 123 |
+
# Test for negative integer in sequence
|
| 124 |
+
seq = [6, 5, 4, -4, 2, 1, 1, 1]
|
| 125 |
+
assert not nx.is_multigraphical(seq)
|
| 126 |
+
# Test for sequence with odd sum
|
| 127 |
+
seq = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
|
| 128 |
+
assert not nx.is_multigraphical(seq)
|
| 129 |
+
# Test for noninteger
|
| 130 |
+
seq = [1, 1, 1.1, 1]
|
| 131 |
+
assert not nx.is_multigraphical(seq)
|
| 132 |
+
seq = [1, 1, "rer", 1]
|
| 133 |
+
assert not nx.is_multigraphical(seq)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def test_pseudo_sequence():
|
| 137 |
+
# Test small valid pseudo sequence
|
| 138 |
+
seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
|
| 139 |
+
assert nx.is_pseudographical(seq)
|
| 140 |
+
# Test for sequence with odd sum
|
| 141 |
+
seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
| 142 |
+
assert not nx.is_pseudographical(seq)
|
| 143 |
+
# Test for negative integer in sequence
|
| 144 |
+
seq = [1000, 3, 3, 3, 3, 2, 2, -2, 1, 1]
|
| 145 |
+
assert not nx.is_pseudographical(seq)
|
| 146 |
+
# Test for noninteger
|
| 147 |
+
seq = [1, 1, 1.1, 1]
|
| 148 |
+
assert not nx.is_pseudographical(seq)
|
| 149 |
+
seq = [1, 1, "rer", 1]
|
| 150 |
+
assert not nx.is_pseudographical(seq)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def test_numpy_degree_sequence():
|
| 154 |
+
np = pytest.importorskip("numpy")
|
| 155 |
+
ds = np.array([1, 2, 2, 2, 1], dtype=np.int64)
|
| 156 |
+
assert nx.is_graphical(ds, "eg")
|
| 157 |
+
assert nx.is_graphical(ds, "hh")
|
| 158 |
+
ds = np.array([1, 2, 2, 2, 1], dtype=np.float64)
|
| 159 |
+
assert nx.is_graphical(ds, "eg")
|
| 160 |
+
assert nx.is_graphical(ds, "hh")
|
| 161 |
+
ds = np.array([1.1, 2, 2, 2, 1], dtype=np.float64)
|
| 162 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "eg")
|
| 163 |
+
pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "hh")
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_matching.py
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from itertools import permutations
|
| 3 |
+
|
| 4 |
+
from pytest import raises
|
| 5 |
+
|
| 6 |
+
import networkx as nx
|
| 7 |
+
from networkx.algorithms.matching import matching_dict_to_set
|
| 8 |
+
from networkx.utils import edges_equal
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestMaxWeightMatching:
|
| 12 |
+
"""Unit tests for the
|
| 13 |
+
:func:`~networkx.algorithms.matching.max_weight_matching` function.
|
| 14 |
+
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def test_trivial1(self):
|
| 18 |
+
"""Empty graph"""
|
| 19 |
+
G = nx.Graph()
|
| 20 |
+
assert nx.max_weight_matching(G) == set()
|
| 21 |
+
assert nx.min_weight_matching(G) == set()
|
| 22 |
+
|
| 23 |
+
def test_selfloop(self):
|
| 24 |
+
G = nx.Graph()
|
| 25 |
+
G.add_edge(0, 0, weight=100)
|
| 26 |
+
assert nx.max_weight_matching(G) == set()
|
| 27 |
+
assert nx.min_weight_matching(G) == set()
|
| 28 |
+
|
| 29 |
+
def test_single_edge(self):
|
| 30 |
+
G = nx.Graph()
|
| 31 |
+
G.add_edge(0, 1)
|
| 32 |
+
assert edges_equal(
|
| 33 |
+
nx.max_weight_matching(G), matching_dict_to_set({0: 1, 1: 0})
|
| 34 |
+
)
|
| 35 |
+
assert edges_equal(
|
| 36 |
+
nx.min_weight_matching(G), matching_dict_to_set({0: 1, 1: 0})
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def test_two_path(self):
|
| 40 |
+
G = nx.Graph()
|
| 41 |
+
G.add_edge("one", "two", weight=10)
|
| 42 |
+
G.add_edge("two", "three", weight=11)
|
| 43 |
+
assert edges_equal(
|
| 44 |
+
nx.max_weight_matching(G),
|
| 45 |
+
matching_dict_to_set({"three": "two", "two": "three"}),
|
| 46 |
+
)
|
| 47 |
+
assert edges_equal(
|
| 48 |
+
nx.min_weight_matching(G),
|
| 49 |
+
matching_dict_to_set({"one": "two", "two": "one"}),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def test_path(self):
|
| 53 |
+
G = nx.Graph()
|
| 54 |
+
G.add_edge(1, 2, weight=5)
|
| 55 |
+
G.add_edge(2, 3, weight=11)
|
| 56 |
+
G.add_edge(3, 4, weight=5)
|
| 57 |
+
assert edges_equal(
|
| 58 |
+
nx.max_weight_matching(G), matching_dict_to_set({2: 3, 3: 2})
|
| 59 |
+
)
|
| 60 |
+
assert edges_equal(
|
| 61 |
+
nx.max_weight_matching(G, 1), matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3})
|
| 62 |
+
)
|
| 63 |
+
assert edges_equal(
|
| 64 |
+
nx.min_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
|
| 65 |
+
)
|
| 66 |
+
assert edges_equal(
|
| 67 |
+
nx.min_weight_matching(G, 1), matching_dict_to_set({1: 2, 3: 4})
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
def test_square(self):
|
| 71 |
+
G = nx.Graph()
|
| 72 |
+
G.add_edge(1, 4, weight=2)
|
| 73 |
+
G.add_edge(2, 3, weight=2)
|
| 74 |
+
G.add_edge(1, 2, weight=1)
|
| 75 |
+
G.add_edge(3, 4, weight=4)
|
| 76 |
+
assert edges_equal(
|
| 77 |
+
nx.max_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
|
| 78 |
+
)
|
| 79 |
+
assert edges_equal(
|
| 80 |
+
nx.min_weight_matching(G), matching_dict_to_set({1: 4, 2: 3})
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
def test_edge_attribute_name(self):
|
| 84 |
+
G = nx.Graph()
|
| 85 |
+
G.add_edge("one", "two", weight=10, abcd=11)
|
| 86 |
+
G.add_edge("two", "three", weight=11, abcd=10)
|
| 87 |
+
assert edges_equal(
|
| 88 |
+
nx.max_weight_matching(G, weight="abcd"),
|
| 89 |
+
matching_dict_to_set({"one": "two", "two": "one"}),
|
| 90 |
+
)
|
| 91 |
+
assert edges_equal(
|
| 92 |
+
nx.min_weight_matching(G, weight="abcd"),
|
| 93 |
+
matching_dict_to_set({"three": "two"}),
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
def test_floating_point_weights(self):
|
| 97 |
+
G = nx.Graph()
|
| 98 |
+
G.add_edge(1, 2, weight=math.pi)
|
| 99 |
+
G.add_edge(2, 3, weight=math.exp(1))
|
| 100 |
+
G.add_edge(1, 3, weight=3.0)
|
| 101 |
+
G.add_edge(1, 4, weight=math.sqrt(2.0))
|
| 102 |
+
assert edges_equal(
|
| 103 |
+
nx.max_weight_matching(G), matching_dict_to_set({1: 4, 2: 3, 3: 2, 4: 1})
|
| 104 |
+
)
|
| 105 |
+
assert edges_equal(
|
| 106 |
+
nx.min_weight_matching(G), matching_dict_to_set({1: 4, 2: 3, 3: 2, 4: 1})
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def test_negative_weights(self):
|
| 110 |
+
G = nx.Graph()
|
| 111 |
+
G.add_edge(1, 2, weight=2)
|
| 112 |
+
G.add_edge(1, 3, weight=-2)
|
| 113 |
+
G.add_edge(2, 3, weight=1)
|
| 114 |
+
G.add_edge(2, 4, weight=-1)
|
| 115 |
+
G.add_edge(3, 4, weight=-6)
|
| 116 |
+
assert edges_equal(
|
| 117 |
+
nx.max_weight_matching(G), matching_dict_to_set({1: 2, 2: 1})
|
| 118 |
+
)
|
| 119 |
+
assert edges_equal(
|
| 120 |
+
nx.max_weight_matching(G, maxcardinality=True),
|
| 121 |
+
matching_dict_to_set({1: 3, 2: 4, 3: 1, 4: 2}),
|
| 122 |
+
)
|
| 123 |
+
assert edges_equal(
|
| 124 |
+
nx.min_weight_matching(G), matching_dict_to_set({1: 2, 3: 4})
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
def test_s_blossom(self):
|
| 128 |
+
"""Create S-blossom and use it for augmentation:"""
|
| 129 |
+
G = nx.Graph()
|
| 130 |
+
G.add_weighted_edges_from([(1, 2, 8), (1, 3, 9), (2, 3, 10), (3, 4, 7)])
|
| 131 |
+
answer = matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3})
|
| 132 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 133 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 134 |
+
|
| 135 |
+
G.add_weighted_edges_from([(1, 6, 5), (4, 5, 6)])
|
| 136 |
+
answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
|
| 137 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 138 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 139 |
+
|
| 140 |
+
def test_s_t_blossom(self):
|
| 141 |
+
"""Create S-blossom, relabel as T-blossom, use for augmentation:"""
|
| 142 |
+
G = nx.Graph()
|
| 143 |
+
G.add_weighted_edges_from(
|
| 144 |
+
[(1, 2, 9), (1, 3, 8), (2, 3, 10), (1, 4, 5), (4, 5, 4), (1, 6, 3)]
|
| 145 |
+
)
|
| 146 |
+
answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
|
| 147 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 148 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 149 |
+
|
| 150 |
+
G.add_edge(4, 5, weight=3)
|
| 151 |
+
G.add_edge(1, 6, weight=4)
|
| 152 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 153 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 154 |
+
|
| 155 |
+
G.remove_edge(1, 6)
|
| 156 |
+
G.add_edge(3, 6, weight=4)
|
| 157 |
+
answer = matching_dict_to_set({1: 2, 2: 1, 3: 6, 4: 5, 5: 4, 6: 3})
|
| 158 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 159 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 160 |
+
|
| 161 |
+
def test_nested_s_blossom(self):
|
| 162 |
+
"""Create nested S-blossom, use for augmentation:"""
|
| 163 |
+
|
| 164 |
+
G = nx.Graph()
|
| 165 |
+
G.add_weighted_edges_from(
|
| 166 |
+
[
|
| 167 |
+
(1, 2, 9),
|
| 168 |
+
(1, 3, 9),
|
| 169 |
+
(2, 3, 10),
|
| 170 |
+
(2, 4, 8),
|
| 171 |
+
(3, 5, 8),
|
| 172 |
+
(4, 5, 10),
|
| 173 |
+
(5, 6, 6),
|
| 174 |
+
]
|
| 175 |
+
)
|
| 176 |
+
dict_format = {1: 3, 2: 4, 3: 1, 4: 2, 5: 6, 6: 5}
|
| 177 |
+
expected = {frozenset(e) for e in matching_dict_to_set(dict_format)}
|
| 178 |
+
answer = {frozenset(e) for e in nx.max_weight_matching(G)}
|
| 179 |
+
assert answer == expected
|
| 180 |
+
answer = {frozenset(e) for e in nx.min_weight_matching(G)}
|
| 181 |
+
assert answer == expected
|
| 182 |
+
|
| 183 |
+
def test_nested_s_blossom_relabel(self):
|
| 184 |
+
"""Create S-blossom, relabel as S, include in nested S-blossom:"""
|
| 185 |
+
G = nx.Graph()
|
| 186 |
+
G.add_weighted_edges_from(
|
| 187 |
+
[
|
| 188 |
+
(1, 2, 10),
|
| 189 |
+
(1, 7, 10),
|
| 190 |
+
(2, 3, 12),
|
| 191 |
+
(3, 4, 20),
|
| 192 |
+
(3, 5, 20),
|
| 193 |
+
(4, 5, 25),
|
| 194 |
+
(5, 6, 10),
|
| 195 |
+
(6, 7, 10),
|
| 196 |
+
(7, 8, 8),
|
| 197 |
+
]
|
| 198 |
+
)
|
| 199 |
+
answer = matching_dict_to_set({1: 2, 2: 1, 3: 4, 4: 3, 5: 6, 6: 5, 7: 8, 8: 7})
|
| 200 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 201 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 202 |
+
|
| 203 |
+
def test_nested_s_blossom_expand(self):
|
| 204 |
+
"""Create nested S-blossom, augment, expand recursively:"""
|
| 205 |
+
G = nx.Graph()
|
| 206 |
+
G.add_weighted_edges_from(
|
| 207 |
+
[
|
| 208 |
+
(1, 2, 8),
|
| 209 |
+
(1, 3, 8),
|
| 210 |
+
(2, 3, 10),
|
| 211 |
+
(2, 4, 12),
|
| 212 |
+
(3, 5, 12),
|
| 213 |
+
(4, 5, 14),
|
| 214 |
+
(4, 6, 12),
|
| 215 |
+
(5, 7, 12),
|
| 216 |
+
(6, 7, 14),
|
| 217 |
+
(7, 8, 12),
|
| 218 |
+
]
|
| 219 |
+
)
|
| 220 |
+
answer = matching_dict_to_set({1: 2, 2: 1, 3: 5, 4: 6, 5: 3, 6: 4, 7: 8, 8: 7})
|
| 221 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 222 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 223 |
+
|
| 224 |
+
def test_s_blossom_relabel_expand(self):
|
| 225 |
+
"""Create S-blossom, relabel as T, expand:"""
|
| 226 |
+
G = nx.Graph()
|
| 227 |
+
G.add_weighted_edges_from(
|
| 228 |
+
[
|
| 229 |
+
(1, 2, 23),
|
| 230 |
+
(1, 5, 22),
|
| 231 |
+
(1, 6, 15),
|
| 232 |
+
(2, 3, 25),
|
| 233 |
+
(3, 4, 22),
|
| 234 |
+
(4, 5, 25),
|
| 235 |
+
(4, 8, 14),
|
| 236 |
+
(5, 7, 13),
|
| 237 |
+
]
|
| 238 |
+
)
|
| 239 |
+
answer = matching_dict_to_set({1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4})
|
| 240 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 241 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 242 |
+
|
| 243 |
+
def test_nested_s_blossom_relabel_expand(self):
|
| 244 |
+
"""Create nested S-blossom, relabel as T, expand:"""
|
| 245 |
+
G = nx.Graph()
|
| 246 |
+
G.add_weighted_edges_from(
|
| 247 |
+
[
|
| 248 |
+
(1, 2, 19),
|
| 249 |
+
(1, 3, 20),
|
| 250 |
+
(1, 8, 8),
|
| 251 |
+
(2, 3, 25),
|
| 252 |
+
(2, 4, 18),
|
| 253 |
+
(3, 5, 18),
|
| 254 |
+
(4, 5, 13),
|
| 255 |
+
(4, 7, 7),
|
| 256 |
+
(5, 6, 7),
|
| 257 |
+
]
|
| 258 |
+
)
|
| 259 |
+
answer = matching_dict_to_set({1: 8, 2: 3, 3: 2, 4: 7, 5: 6, 6: 5, 7: 4, 8: 1})
|
| 260 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 261 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 262 |
+
|
| 263 |
+
def test_nasty_blossom1(self):
|
| 264 |
+
"""Create blossom, relabel as T in more than one way, expand,
|
| 265 |
+
augment:
|
| 266 |
+
"""
|
| 267 |
+
G = nx.Graph()
|
| 268 |
+
G.add_weighted_edges_from(
|
| 269 |
+
[
|
| 270 |
+
(1, 2, 45),
|
| 271 |
+
(1, 5, 45),
|
| 272 |
+
(2, 3, 50),
|
| 273 |
+
(3, 4, 45),
|
| 274 |
+
(4, 5, 50),
|
| 275 |
+
(1, 6, 30),
|
| 276 |
+
(3, 9, 35),
|
| 277 |
+
(4, 8, 35),
|
| 278 |
+
(5, 7, 26),
|
| 279 |
+
(9, 10, 5),
|
| 280 |
+
]
|
| 281 |
+
)
|
| 282 |
+
ansdict = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
|
| 283 |
+
answer = matching_dict_to_set(ansdict)
|
| 284 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 285 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 286 |
+
|
| 287 |
+
def test_nasty_blossom2(self):
|
| 288 |
+
"""Again but slightly different:"""
|
| 289 |
+
G = nx.Graph()
|
| 290 |
+
G.add_weighted_edges_from(
|
| 291 |
+
[
|
| 292 |
+
(1, 2, 45),
|
| 293 |
+
(1, 5, 45),
|
| 294 |
+
(2, 3, 50),
|
| 295 |
+
(3, 4, 45),
|
| 296 |
+
(4, 5, 50),
|
| 297 |
+
(1, 6, 30),
|
| 298 |
+
(3, 9, 35),
|
| 299 |
+
(4, 8, 26),
|
| 300 |
+
(5, 7, 40),
|
| 301 |
+
(9, 10, 5),
|
| 302 |
+
]
|
| 303 |
+
)
|
| 304 |
+
ans = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
|
| 305 |
+
answer = matching_dict_to_set(ans)
|
| 306 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 307 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 308 |
+
|
| 309 |
+
def test_nasty_blossom_least_slack(self):
|
| 310 |
+
"""Create blossom, relabel as T, expand such that a new
|
| 311 |
+
least-slack S-to-free dge is produced, augment:
|
| 312 |
+
"""
|
| 313 |
+
G = nx.Graph()
|
| 314 |
+
G.add_weighted_edges_from(
|
| 315 |
+
[
|
| 316 |
+
(1, 2, 45),
|
| 317 |
+
(1, 5, 45),
|
| 318 |
+
(2, 3, 50),
|
| 319 |
+
(3, 4, 45),
|
| 320 |
+
(4, 5, 50),
|
| 321 |
+
(1, 6, 30),
|
| 322 |
+
(3, 9, 35),
|
| 323 |
+
(4, 8, 28),
|
| 324 |
+
(5, 7, 26),
|
| 325 |
+
(9, 10, 5),
|
| 326 |
+
]
|
| 327 |
+
)
|
| 328 |
+
ans = {1: 6, 2: 3, 3: 2, 4: 8, 5: 7, 6: 1, 7: 5, 8: 4, 9: 10, 10: 9}
|
| 329 |
+
answer = matching_dict_to_set(ans)
|
| 330 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 331 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 332 |
+
|
| 333 |
+
def test_nasty_blossom_augmenting(self):
|
| 334 |
+
"""Create nested blossom, relabel as T in more than one way"""
|
| 335 |
+
# expand outer blossom such that inner blossom ends up on an
|
| 336 |
+
# augmenting path:
|
| 337 |
+
G = nx.Graph()
|
| 338 |
+
G.add_weighted_edges_from(
|
| 339 |
+
[
|
| 340 |
+
(1, 2, 45),
|
| 341 |
+
(1, 7, 45),
|
| 342 |
+
(2, 3, 50),
|
| 343 |
+
(3, 4, 45),
|
| 344 |
+
(4, 5, 95),
|
| 345 |
+
(4, 6, 94),
|
| 346 |
+
(5, 6, 94),
|
| 347 |
+
(6, 7, 50),
|
| 348 |
+
(1, 8, 30),
|
| 349 |
+
(3, 11, 35),
|
| 350 |
+
(5, 9, 36),
|
| 351 |
+
(7, 10, 26),
|
| 352 |
+
(11, 12, 5),
|
| 353 |
+
]
|
| 354 |
+
)
|
| 355 |
+
ans = {
|
| 356 |
+
1: 8,
|
| 357 |
+
2: 3,
|
| 358 |
+
3: 2,
|
| 359 |
+
4: 6,
|
| 360 |
+
5: 9,
|
| 361 |
+
6: 4,
|
| 362 |
+
7: 10,
|
| 363 |
+
8: 1,
|
| 364 |
+
9: 5,
|
| 365 |
+
10: 7,
|
| 366 |
+
11: 12,
|
| 367 |
+
12: 11,
|
| 368 |
+
}
|
| 369 |
+
answer = matching_dict_to_set(ans)
|
| 370 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 371 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 372 |
+
|
| 373 |
+
def test_nasty_blossom_expand_recursively(self):
|
| 374 |
+
"""Create nested S-blossom, relabel as S, expand recursively:"""
|
| 375 |
+
G = nx.Graph()
|
| 376 |
+
G.add_weighted_edges_from(
|
| 377 |
+
[
|
| 378 |
+
(1, 2, 40),
|
| 379 |
+
(1, 3, 40),
|
| 380 |
+
(2, 3, 60),
|
| 381 |
+
(2, 4, 55),
|
| 382 |
+
(3, 5, 55),
|
| 383 |
+
(4, 5, 50),
|
| 384 |
+
(1, 8, 15),
|
| 385 |
+
(5, 7, 30),
|
| 386 |
+
(7, 6, 10),
|
| 387 |
+
(8, 10, 10),
|
| 388 |
+
(4, 9, 30),
|
| 389 |
+
]
|
| 390 |
+
)
|
| 391 |
+
ans = {1: 2, 2: 1, 3: 5, 4: 9, 5: 3, 6: 7, 7: 6, 8: 10, 9: 4, 10: 8}
|
| 392 |
+
answer = matching_dict_to_set(ans)
|
| 393 |
+
assert edges_equal(nx.max_weight_matching(G), answer)
|
| 394 |
+
assert edges_equal(nx.min_weight_matching(G), answer)
|
| 395 |
+
|
| 396 |
+
def test_wrong_graph_type(self):
|
| 397 |
+
error = nx.NetworkXNotImplemented
|
| 398 |
+
raises(error, nx.max_weight_matching, nx.MultiGraph())
|
| 399 |
+
raises(error, nx.max_weight_matching, nx.MultiDiGraph())
|
| 400 |
+
raises(error, nx.max_weight_matching, nx.DiGraph())
|
| 401 |
+
raises(error, nx.min_weight_matching, nx.DiGraph())
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
class TestIsMatching:
|
| 405 |
+
"""Unit tests for the
|
| 406 |
+
:func:`~networkx.algorithms.matching.is_matching` function.
|
| 407 |
+
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
def test_dict(self):
|
| 411 |
+
G = nx.path_graph(4)
|
| 412 |
+
assert nx.is_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
|
| 413 |
+
|
| 414 |
+
def test_empty_matching(self):
|
| 415 |
+
G = nx.path_graph(4)
|
| 416 |
+
assert nx.is_matching(G, set())
|
| 417 |
+
|
| 418 |
+
def test_single_edge(self):
|
| 419 |
+
G = nx.path_graph(4)
|
| 420 |
+
assert nx.is_matching(G, {(1, 2)})
|
| 421 |
+
|
| 422 |
+
def test_edge_order(self):
|
| 423 |
+
G = nx.path_graph(4)
|
| 424 |
+
assert nx.is_matching(G, {(0, 1), (2, 3)})
|
| 425 |
+
assert nx.is_matching(G, {(1, 0), (2, 3)})
|
| 426 |
+
assert nx.is_matching(G, {(0, 1), (3, 2)})
|
| 427 |
+
assert nx.is_matching(G, {(1, 0), (3, 2)})
|
| 428 |
+
|
| 429 |
+
def test_valid_matching(self):
|
| 430 |
+
G = nx.path_graph(4)
|
| 431 |
+
assert nx.is_matching(G, {(0, 1), (2, 3)})
|
| 432 |
+
|
| 433 |
+
def test_invalid_input(self):
|
| 434 |
+
error = nx.NetworkXError
|
| 435 |
+
G = nx.path_graph(4)
|
| 436 |
+
# edge to node not in G
|
| 437 |
+
raises(error, nx.is_matching, G, {(0, 5), (2, 3)})
|
| 438 |
+
# edge not a 2-tuple
|
| 439 |
+
raises(error, nx.is_matching, G, {(0, 1, 2), (2, 3)})
|
| 440 |
+
raises(error, nx.is_matching, G, {(0,), (2, 3)})
|
| 441 |
+
|
| 442 |
+
def test_selfloops(self):
|
| 443 |
+
error = nx.NetworkXError
|
| 444 |
+
G = nx.path_graph(4)
|
| 445 |
+
# selfloop for node not in G
|
| 446 |
+
raises(error, nx.is_matching, G, {(5, 5), (2, 3)})
|
| 447 |
+
# selfloop edge not in G
|
| 448 |
+
assert not nx.is_matching(G, {(0, 0), (1, 2), (2, 3)})
|
| 449 |
+
# selfloop edge in G
|
| 450 |
+
G.add_edge(0, 0)
|
| 451 |
+
assert not nx.is_matching(G, {(0, 0), (1, 2)})
|
| 452 |
+
|
| 453 |
+
def test_invalid_matching(self):
|
| 454 |
+
G = nx.path_graph(4)
|
| 455 |
+
assert not nx.is_matching(G, {(0, 1), (1, 2), (2, 3)})
|
| 456 |
+
|
| 457 |
+
def test_invalid_edge(self):
|
| 458 |
+
G = nx.path_graph(4)
|
| 459 |
+
assert not nx.is_matching(G, {(0, 3), (1, 2)})
|
| 460 |
+
raises(nx.NetworkXError, nx.is_matching, G, {(0, 55)})
|
| 461 |
+
|
| 462 |
+
G = nx.DiGraph(G.edges)
|
| 463 |
+
assert nx.is_matching(G, {(0, 1)})
|
| 464 |
+
assert not nx.is_matching(G, {(1, 0)})
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
class TestIsMaximalMatching:
|
| 468 |
+
"""Unit tests for the
|
| 469 |
+
:func:`~networkx.algorithms.matching.is_maximal_matching` function.
|
| 470 |
+
|
| 471 |
+
"""
|
| 472 |
+
|
| 473 |
+
def test_dict(self):
|
| 474 |
+
G = nx.path_graph(4)
|
| 475 |
+
assert nx.is_maximal_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
|
| 476 |
+
|
| 477 |
+
def test_invalid_input(self):
|
| 478 |
+
error = nx.NetworkXError
|
| 479 |
+
G = nx.path_graph(4)
|
| 480 |
+
# edge to node not in G
|
| 481 |
+
raises(error, nx.is_maximal_matching, G, {(0, 5)})
|
| 482 |
+
raises(error, nx.is_maximal_matching, G, {(5, 0)})
|
| 483 |
+
# edge not a 2-tuple
|
| 484 |
+
raises(error, nx.is_maximal_matching, G, {(0, 1, 2), (2, 3)})
|
| 485 |
+
raises(error, nx.is_maximal_matching, G, {(0,), (2, 3)})
|
| 486 |
+
|
| 487 |
+
def test_valid(self):
|
| 488 |
+
G = nx.path_graph(4)
|
| 489 |
+
assert nx.is_maximal_matching(G, {(0, 1), (2, 3)})
|
| 490 |
+
|
| 491 |
+
def test_not_matching(self):
|
| 492 |
+
G = nx.path_graph(4)
|
| 493 |
+
assert not nx.is_maximal_matching(G, {(0, 1), (1, 2), (2, 3)})
|
| 494 |
+
assert not nx.is_maximal_matching(G, {(0, 3)})
|
| 495 |
+
G.add_edge(0, 0)
|
| 496 |
+
assert not nx.is_maximal_matching(G, {(0, 0)})
|
| 497 |
+
|
| 498 |
+
def test_not_maximal(self):
|
| 499 |
+
G = nx.path_graph(4)
|
| 500 |
+
assert not nx.is_maximal_matching(G, {(0, 1)})
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
class TestIsPerfectMatching:
|
| 504 |
+
"""Unit tests for the
|
| 505 |
+
:func:`~networkx.algorithms.matching.is_perfect_matching` function.
|
| 506 |
+
|
| 507 |
+
"""
|
| 508 |
+
|
| 509 |
+
def test_dict(self):
|
| 510 |
+
G = nx.path_graph(4)
|
| 511 |
+
assert nx.is_perfect_matching(G, {0: 1, 1: 0, 2: 3, 3: 2})
|
| 512 |
+
|
| 513 |
+
def test_valid(self):
|
| 514 |
+
G = nx.path_graph(4)
|
| 515 |
+
assert nx.is_perfect_matching(G, {(0, 1), (2, 3)})
|
| 516 |
+
|
| 517 |
+
def test_valid_not_path(self):
|
| 518 |
+
G = nx.cycle_graph(4)
|
| 519 |
+
G.add_edge(0, 4)
|
| 520 |
+
G.add_edge(1, 4)
|
| 521 |
+
G.add_edge(5, 2)
|
| 522 |
+
|
| 523 |
+
assert nx.is_perfect_matching(G, {(1, 4), (0, 3), (5, 2)})
|
| 524 |
+
|
| 525 |
+
def test_invalid_input(self):
|
| 526 |
+
error = nx.NetworkXError
|
| 527 |
+
G = nx.path_graph(4)
|
| 528 |
+
# edge to node not in G
|
| 529 |
+
raises(error, nx.is_perfect_matching, G, {(0, 5)})
|
| 530 |
+
raises(error, nx.is_perfect_matching, G, {(5, 0)})
|
| 531 |
+
# edge not a 2-tuple
|
| 532 |
+
raises(error, nx.is_perfect_matching, G, {(0, 1, 2), (2, 3)})
|
| 533 |
+
raises(error, nx.is_perfect_matching, G, {(0,), (2, 3)})
|
| 534 |
+
|
| 535 |
+
def test_selfloops(self):
|
| 536 |
+
error = nx.NetworkXError
|
| 537 |
+
G = nx.path_graph(4)
|
| 538 |
+
# selfloop for node not in G
|
| 539 |
+
raises(error, nx.is_perfect_matching, G, {(5, 5), (2, 3)})
|
| 540 |
+
# selfloop edge not in G
|
| 541 |
+
assert not nx.is_perfect_matching(G, {(0, 0), (1, 2), (2, 3)})
|
| 542 |
+
# selfloop edge in G
|
| 543 |
+
G.add_edge(0, 0)
|
| 544 |
+
assert not nx.is_perfect_matching(G, {(0, 0), (1, 2)})
|
| 545 |
+
|
| 546 |
+
def test_not_matching(self):
|
| 547 |
+
G = nx.path_graph(4)
|
| 548 |
+
assert not nx.is_perfect_matching(G, {(0, 3)})
|
| 549 |
+
assert not nx.is_perfect_matching(G, {(0, 1), (1, 2), (2, 3)})
|
| 550 |
+
|
| 551 |
+
def test_maximal_but_not_perfect(self):
|
| 552 |
+
G = nx.cycle_graph(4)
|
| 553 |
+
G.add_edge(0, 4)
|
| 554 |
+
G.add_edge(1, 4)
|
| 555 |
+
|
| 556 |
+
assert not nx.is_perfect_matching(G, {(1, 4), (0, 3)})
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
class TestMaximalMatching:
|
| 560 |
+
"""Unit tests for the
|
| 561 |
+
:func:`~networkx.algorithms.matching.maximal_matching`.
|
| 562 |
+
|
| 563 |
+
"""
|
| 564 |
+
|
| 565 |
+
def test_valid_matching(self):
|
| 566 |
+
edges = [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 6), (5, 6)]
|
| 567 |
+
G = nx.Graph(edges)
|
| 568 |
+
matching = nx.maximal_matching(G)
|
| 569 |
+
assert nx.is_maximal_matching(G, matching)
|
| 570 |
+
|
| 571 |
+
def test_single_edge_matching(self):
|
| 572 |
+
# In the star graph, any maximal matching has just one edge.
|
| 573 |
+
G = nx.star_graph(5)
|
| 574 |
+
matching = nx.maximal_matching(G)
|
| 575 |
+
assert 1 == len(matching)
|
| 576 |
+
assert nx.is_maximal_matching(G, matching)
|
| 577 |
+
|
| 578 |
+
def test_self_loops(self):
|
| 579 |
+
# Create the path graph with two self-loops.
|
| 580 |
+
G = nx.path_graph(3)
|
| 581 |
+
G.add_edges_from([(0, 0), (1, 1)])
|
| 582 |
+
matching = nx.maximal_matching(G)
|
| 583 |
+
assert len(matching) == 1
|
| 584 |
+
# The matching should never include self-loops.
|
| 585 |
+
assert not any(u == v for u, v in matching)
|
| 586 |
+
assert nx.is_maximal_matching(G, matching)
|
| 587 |
+
|
| 588 |
+
def test_ordering(self):
|
| 589 |
+
"""Tests that a maximal matching is computed correctly
|
| 590 |
+
regardless of the order in which nodes are added to the graph.
|
| 591 |
+
|
| 592 |
+
"""
|
| 593 |
+
for nodes in permutations(range(3)):
|
| 594 |
+
G = nx.Graph()
|
| 595 |
+
G.add_nodes_from(nodes)
|
| 596 |
+
G.add_edges_from([(0, 1), (0, 2)])
|
| 597 |
+
matching = nx.maximal_matching(G)
|
| 598 |
+
assert len(matching) == 1
|
| 599 |
+
assert nx.is_maximal_matching(G, matching)
|
| 600 |
+
|
| 601 |
+
def test_wrong_graph_type(self):
|
| 602 |
+
error = nx.NetworkXNotImplemented
|
| 603 |
+
raises(error, nx.maximal_matching, nx.MultiGraph())
|
| 604 |
+
raises(error, nx.maximal_matching, nx.MultiDiGraph())
|
| 605 |
+
raises(error, nx.maximal_matching, nx.DiGraph())
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_polynomials.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the :mod:`networkx.algorithms.polynomials` module."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
|
| 7 |
+
sympy = pytest.importorskip("sympy")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Mapping of input graphs to a string representation of their tutte polynomials
|
| 11 |
+
_test_tutte_graphs = {
|
| 12 |
+
nx.complete_graph(1): "1",
|
| 13 |
+
nx.complete_graph(4): "x**3 + 3*x**2 + 4*x*y + 2*x + y**3 + 3*y**2 + 2*y",
|
| 14 |
+
nx.cycle_graph(5): "x**4 + x**3 + x**2 + x + y",
|
| 15 |
+
nx.diamond_graph(): "x**3 + 2*x**2 + 2*x*y + x + y**2 + y",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
_test_chromatic_graphs = {
|
| 19 |
+
nx.complete_graph(1): "x",
|
| 20 |
+
nx.complete_graph(4): "x**4 - 6*x**3 + 11*x**2 - 6*x",
|
| 21 |
+
nx.cycle_graph(5): "x**5 - 5*x**4 + 10*x**3 - 10*x**2 + 4*x",
|
| 22 |
+
nx.diamond_graph(): "x**4 - 5*x**3 + 8*x**2 - 4*x",
|
| 23 |
+
nx.path_graph(5): "x**5 - 4*x**4 + 6*x**3 - 4*x**2 + x",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@pytest.mark.parametrize(("G", "expected"), _test_tutte_graphs.items())
|
| 28 |
+
def test_tutte_polynomial(G, expected):
|
| 29 |
+
assert nx.tutte_polynomial(G).equals(expected)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@pytest.mark.parametrize("G", _test_tutte_graphs.keys())
|
| 33 |
+
def test_tutte_polynomial_disjoint(G):
|
| 34 |
+
"""Tutte polynomial factors into the Tutte polynomials of its components.
|
| 35 |
+
Verify this property with the disjoint union of two copies of the input graph.
|
| 36 |
+
"""
|
| 37 |
+
t_g = nx.tutte_polynomial(G)
|
| 38 |
+
H = nx.disjoint_union(G, G)
|
| 39 |
+
t_h = nx.tutte_polynomial(H)
|
| 40 |
+
assert sympy.simplify(t_g * t_g).equals(t_h)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@pytest.mark.parametrize(("G", "expected"), _test_chromatic_graphs.items())
|
| 44 |
+
def test_chromatic_polynomial(G, expected):
|
| 45 |
+
assert nx.chromatic_polynomial(G).equals(expected)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@pytest.mark.parametrize("G", _test_chromatic_graphs.keys())
|
| 49 |
+
def test_chromatic_polynomial_disjoint(G):
|
| 50 |
+
"""Chromatic polynomial factors into the Chromatic polynomials of its
|
| 51 |
+
components. Verify this property with the disjoint union of two copies of
|
| 52 |
+
the input graph.
|
| 53 |
+
"""
|
| 54 |
+
x_g = nx.chromatic_polynomial(G)
|
| 55 |
+
H = nx.disjoint_union(G, G)
|
| 56 |
+
x_h = nx.chromatic_polynomial(H)
|
| 57 |
+
assert sympy.simplify(x_g * x_g).equals(x_h)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_reciprocity.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestReciprocity:
|
| 7 |
+
# test overall reciprocity by passing whole graph
|
| 8 |
+
def test_reciprocity_digraph(self):
|
| 9 |
+
DG = nx.DiGraph([(1, 2), (2, 1)])
|
| 10 |
+
reciprocity = nx.reciprocity(DG)
|
| 11 |
+
assert reciprocity == 1.0
|
| 12 |
+
|
| 13 |
+
# test empty graph's overall reciprocity which will throw an error
|
| 14 |
+
def test_overall_reciprocity_empty_graph(self):
|
| 15 |
+
with pytest.raises(nx.NetworkXError):
|
| 16 |
+
DG = nx.DiGraph()
|
| 17 |
+
nx.overall_reciprocity(DG)
|
| 18 |
+
|
| 19 |
+
# test for reciprocity for a list of nodes
|
| 20 |
+
def test_reciprocity_graph_nodes(self):
|
| 21 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
|
| 22 |
+
reciprocity = nx.reciprocity(DG, [1, 2])
|
| 23 |
+
expected_reciprocity = {1: 0.0, 2: 0.6666666666666666}
|
| 24 |
+
assert reciprocity == expected_reciprocity
|
| 25 |
+
|
| 26 |
+
# test for reciprocity for a single node
|
| 27 |
+
def test_reciprocity_graph_node(self):
|
| 28 |
+
DG = nx.DiGraph([(1, 2), (2, 3), (3, 2)])
|
| 29 |
+
reciprocity = nx.reciprocity(DG, 2)
|
| 30 |
+
assert reciprocity == 0.6666666666666666
|
| 31 |
+
|
| 32 |
+
# test for reciprocity for an isolated node
|
| 33 |
+
def test_reciprocity_graph_isolated_nodes(self):
|
| 34 |
+
with pytest.raises(nx.NetworkXError):
|
| 35 |
+
DG = nx.DiGraph([(1, 2)])
|
| 36 |
+
DG.add_node(4)
|
| 37 |
+
nx.reciprocity(DG, 4)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_simple_paths.py
ADDED
|
@@ -0,0 +1,792 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
| 7 |
+
from networkx.algorithms.simple_paths import (
|
| 8 |
+
_bidirectional_dijkstra,
|
| 9 |
+
_bidirectional_shortest_path,
|
| 10 |
+
)
|
| 11 |
+
from networkx.utils import arbitrary_element, pairwise
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestIsSimplePath:
|
| 15 |
+
"""Unit tests for the
|
| 16 |
+
:func:`networkx.algorithms.simple_paths.is_simple_path` function.
|
| 17 |
+
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def test_empty_list(self):
|
| 21 |
+
"""Tests that the empty list is not a valid path, since there
|
| 22 |
+
should be a one-to-one correspondence between paths as lists of
|
| 23 |
+
nodes and paths as lists of edges.
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
G = nx.trivial_graph()
|
| 27 |
+
assert not nx.is_simple_path(G, [])
|
| 28 |
+
|
| 29 |
+
def test_trivial_path(self):
|
| 30 |
+
"""Tests that the trivial path, a path of length one, is
|
| 31 |
+
considered a simple path in a graph.
|
| 32 |
+
|
| 33 |
+
"""
|
| 34 |
+
G = nx.trivial_graph()
|
| 35 |
+
assert nx.is_simple_path(G, [0])
|
| 36 |
+
|
| 37 |
+
def test_trivial_nonpath(self):
|
| 38 |
+
"""Tests that a list whose sole element is an object not in the
|
| 39 |
+
graph is not considered a simple path.
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
G = nx.trivial_graph()
|
| 43 |
+
assert not nx.is_simple_path(G, ["not a node"])
|
| 44 |
+
|
| 45 |
+
def test_simple_path(self):
|
| 46 |
+
G = nx.path_graph(2)
|
| 47 |
+
assert nx.is_simple_path(G, [0, 1])
|
| 48 |
+
|
| 49 |
+
def test_non_simple_path(self):
|
| 50 |
+
G = nx.path_graph(2)
|
| 51 |
+
assert not nx.is_simple_path(G, [0, 1, 0])
|
| 52 |
+
|
| 53 |
+
def test_cycle(self):
|
| 54 |
+
G = nx.cycle_graph(3)
|
| 55 |
+
assert not nx.is_simple_path(G, [0, 1, 2, 0])
|
| 56 |
+
|
| 57 |
+
def test_missing_node(self):
|
| 58 |
+
G = nx.path_graph(2)
|
| 59 |
+
assert not nx.is_simple_path(G, [0, 2])
|
| 60 |
+
|
| 61 |
+
def test_missing_starting_node(self):
|
| 62 |
+
G = nx.path_graph(2)
|
| 63 |
+
assert not nx.is_simple_path(G, [2, 0])
|
| 64 |
+
|
| 65 |
+
def test_directed_path(self):
|
| 66 |
+
G = nx.DiGraph([(0, 1), (1, 2)])
|
| 67 |
+
assert nx.is_simple_path(G, [0, 1, 2])
|
| 68 |
+
|
| 69 |
+
def test_directed_non_path(self):
|
| 70 |
+
G = nx.DiGraph([(0, 1), (1, 2)])
|
| 71 |
+
assert not nx.is_simple_path(G, [2, 1, 0])
|
| 72 |
+
|
| 73 |
+
def test_directed_cycle(self):
|
| 74 |
+
G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
|
| 75 |
+
assert not nx.is_simple_path(G, [0, 1, 2, 0])
|
| 76 |
+
|
| 77 |
+
def test_multigraph(self):
|
| 78 |
+
G = nx.MultiGraph([(0, 1), (0, 1)])
|
| 79 |
+
assert nx.is_simple_path(G, [0, 1])
|
| 80 |
+
|
| 81 |
+
def test_multidigraph(self):
|
| 82 |
+
G = nx.MultiDiGraph([(0, 1), (0, 1), (1, 0), (1, 0)])
|
| 83 |
+
assert nx.is_simple_path(G, [0, 1])
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Tests for all_simple_paths
|
| 87 |
+
def test_all_simple_paths():
|
| 88 |
+
G = nx.path_graph(4)
|
| 89 |
+
paths = nx.all_simple_paths(G, 0, 3)
|
| 90 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2, 3)}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_all_simple_paths_with_two_targets_emits_two_paths():
|
| 94 |
+
G = nx.path_graph(4)
|
| 95 |
+
G.add_edge(2, 4)
|
| 96 |
+
paths = nx.all_simple_paths(G, 0, [3, 4])
|
| 97 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_digraph_all_simple_paths_with_two_targets_emits_two_paths():
|
| 101 |
+
G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 102 |
+
G.add_edge(2, 4)
|
| 103 |
+
paths = nx.all_simple_paths(G, 0, [3, 4])
|
| 104 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def test_all_simple_paths_with_two_targets_cutoff():
|
| 108 |
+
G = nx.path_graph(4)
|
| 109 |
+
G.add_edge(2, 4)
|
| 110 |
+
paths = nx.all_simple_paths(G, 0, [3, 4], cutoff=3)
|
| 111 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_digraph_all_simple_paths_with_two_targets_cutoff():
|
| 115 |
+
G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 116 |
+
G.add_edge(2, 4)
|
| 117 |
+
paths = nx.all_simple_paths(G, 0, [3, 4], cutoff=3)
|
| 118 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2, 3), (0, 1, 2, 4)}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_all_simple_paths_with_two_targets_in_line_emits_two_paths():
|
| 122 |
+
G = nx.path_graph(4)
|
| 123 |
+
paths = nx.all_simple_paths(G, 0, [2, 3])
|
| 124 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2), (0, 1, 2, 3)}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def test_all_simple_paths_ignores_cycle():
|
| 128 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 129 |
+
G.add_edge(1, 3)
|
| 130 |
+
paths = nx.all_simple_paths(G, 0, 3)
|
| 131 |
+
assert {tuple(p) for p in paths} == {(0, 1, 3)}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def test_all_simple_paths_with_two_targets_inside_cycle_emits_two_paths():
|
| 135 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 136 |
+
G.add_edge(1, 3)
|
| 137 |
+
paths = nx.all_simple_paths(G, 0, [2, 3])
|
| 138 |
+
assert {tuple(p) for p in paths} == {(0, 1, 2), (0, 1, 3)}
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_all_simple_paths_source_target():
|
| 142 |
+
G = nx.path_graph(4)
|
| 143 |
+
assert list(nx.all_simple_paths(G, 1, 1)) == [[1]]
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def test_all_simple_paths_cutoff():
|
| 147 |
+
G = nx.complete_graph(4)
|
| 148 |
+
paths = nx.all_simple_paths(G, 0, 1, cutoff=1)
|
| 149 |
+
assert {tuple(p) for p in paths} == {(0, 1)}
|
| 150 |
+
paths = nx.all_simple_paths(G, 0, 1, cutoff=2)
|
| 151 |
+
assert {tuple(p) for p in paths} == {(0, 1), (0, 2, 1), (0, 3, 1)}
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def test_all_simple_paths_on_non_trivial_graph():
|
| 155 |
+
"""you may need to draw this graph to make sure it is reasonable"""
|
| 156 |
+
G = nx.path_graph(5, create_using=nx.DiGraph())
|
| 157 |
+
G.add_edges_from([(0, 5), (1, 5), (1, 3), (5, 4), (4, 2), (4, 3)])
|
| 158 |
+
paths = nx.all_simple_paths(G, 1, [2, 3])
|
| 159 |
+
assert {tuple(p) for p in paths} == {
|
| 160 |
+
(1, 2),
|
| 161 |
+
(1, 3, 4, 2),
|
| 162 |
+
(1, 5, 4, 2),
|
| 163 |
+
(1, 3),
|
| 164 |
+
(1, 2, 3),
|
| 165 |
+
(1, 5, 4, 3),
|
| 166 |
+
(1, 5, 4, 2, 3),
|
| 167 |
+
}
|
| 168 |
+
paths = nx.all_simple_paths(G, 1, [2, 3], cutoff=3)
|
| 169 |
+
assert {tuple(p) for p in paths} == {
|
| 170 |
+
(1, 2),
|
| 171 |
+
(1, 3, 4, 2),
|
| 172 |
+
(1, 5, 4, 2),
|
| 173 |
+
(1, 3),
|
| 174 |
+
(1, 2, 3),
|
| 175 |
+
(1, 5, 4, 3),
|
| 176 |
+
}
|
| 177 |
+
paths = nx.all_simple_paths(G, 1, [2, 3], cutoff=2)
|
| 178 |
+
assert {tuple(p) for p in paths} == {(1, 2), (1, 3), (1, 2, 3)}
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def test_all_simple_paths_multigraph():
|
| 182 |
+
G = nx.MultiGraph([(1, 2), (1, 2)])
|
| 183 |
+
assert list(nx.all_simple_paths(G, 1, 1)) == [[1]]
|
| 184 |
+
nx.add_path(G, [3, 1, 10, 2])
|
| 185 |
+
paths = list(nx.all_simple_paths(G, 1, 2))
|
| 186 |
+
assert len(paths) == 3
|
| 187 |
+
assert {tuple(p) for p in paths} == {(1, 2), (1, 2), (1, 10, 2)}
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_all_simple_paths_multigraph_with_cutoff():
|
| 191 |
+
G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)])
|
| 192 |
+
paths = list(nx.all_simple_paths(G, 1, 2, cutoff=1))
|
| 193 |
+
assert len(paths) == 2
|
| 194 |
+
assert {tuple(p) for p in paths} == {(1, 2), (1, 2)}
|
| 195 |
+
|
| 196 |
+
# See GitHub issue #6732.
|
| 197 |
+
G = nx.MultiGraph([(0, 1), (0, 2)])
|
| 198 |
+
assert list(nx.all_simple_paths(G, 0, {1, 2}, cutoff=1)) == [[0, 1], [0, 2]]
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def test_all_simple_paths_directed():
|
| 202 |
+
G = nx.DiGraph()
|
| 203 |
+
nx.add_path(G, [1, 2, 3])
|
| 204 |
+
nx.add_path(G, [3, 2, 1])
|
| 205 |
+
paths = nx.all_simple_paths(G, 1, 3)
|
| 206 |
+
assert {tuple(p) for p in paths} == {(1, 2, 3)}
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_all_simple_paths_empty():
|
| 210 |
+
G = nx.path_graph(4)
|
| 211 |
+
paths = nx.all_simple_paths(G, 0, 3, cutoff=2)
|
| 212 |
+
assert list(paths) == []
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def test_all_simple_paths_corner_cases():
|
| 216 |
+
assert list(nx.all_simple_paths(nx.empty_graph(2), 0, 0)) == [[0]]
|
| 217 |
+
assert list(nx.all_simple_paths(nx.empty_graph(2), 0, 1)) == []
|
| 218 |
+
assert list(nx.all_simple_paths(nx.path_graph(9), 0, 8, 0)) == []
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def test_all_simple_paths_source_in_targets():
|
| 222 |
+
# See GitHub issue #6690.
|
| 223 |
+
G = nx.path_graph(3)
|
| 224 |
+
assert list(nx.all_simple_paths(G, 0, {0, 1, 2})) == [[0], [0, 1], [0, 1, 2]]
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def hamiltonian_path(G, source):
|
| 228 |
+
source = arbitrary_element(G)
|
| 229 |
+
neighbors = set(G[source]) - {source}
|
| 230 |
+
n = len(G)
|
| 231 |
+
for target in neighbors:
|
| 232 |
+
for path in nx.all_simple_paths(G, source, target):
|
| 233 |
+
if len(path) == n:
|
| 234 |
+
yield path
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def test_hamiltonian_path():
|
| 238 |
+
from itertools import permutations
|
| 239 |
+
|
| 240 |
+
G = nx.complete_graph(4)
|
| 241 |
+
paths = [list(p) for p in hamiltonian_path(G, 0)]
|
| 242 |
+
exact = [[0] + list(p) for p in permutations([1, 2, 3], 3)]
|
| 243 |
+
assert sorted(paths) == sorted(exact)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def test_cutoff_zero():
|
| 247 |
+
G = nx.complete_graph(4)
|
| 248 |
+
paths = nx.all_simple_paths(G, 0, 3, cutoff=0)
|
| 249 |
+
assert [list(p) for p in paths] == []
|
| 250 |
+
paths = nx.all_simple_paths(nx.MultiGraph(G), 0, 3, cutoff=0)
|
| 251 |
+
assert [list(p) for p in paths] == []
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
def test_source_missing():
|
| 255 |
+
with pytest.raises(nx.NodeNotFound):
|
| 256 |
+
G = nx.Graph()
|
| 257 |
+
nx.add_path(G, [1, 2, 3])
|
| 258 |
+
list(nx.all_simple_paths(nx.MultiGraph(G), 0, 3))
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def test_target_missing():
|
| 262 |
+
with pytest.raises(nx.NodeNotFound):
|
| 263 |
+
G = nx.Graph()
|
| 264 |
+
nx.add_path(G, [1, 2, 3])
|
| 265 |
+
list(nx.all_simple_paths(nx.MultiGraph(G), 1, 4))
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
# Tests for all_simple_edge_paths
|
| 269 |
+
def test_all_simple_edge_paths():
|
| 270 |
+
G = nx.path_graph(4)
|
| 271 |
+
paths = nx.all_simple_edge_paths(G, 0, 3)
|
| 272 |
+
assert {tuple(p) for p in paths} == {((0, 1), (1, 2), (2, 3))}
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def test_all_simple_edge_paths_empty_path():
|
| 276 |
+
G = nx.empty_graph(1)
|
| 277 |
+
assert list(nx.all_simple_edge_paths(G, 0, 0)) == [[]]
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def test_all_simple_edge_paths_with_two_targets_emits_two_paths():
|
| 281 |
+
G = nx.path_graph(4)
|
| 282 |
+
G.add_edge(2, 4)
|
| 283 |
+
paths = nx.all_simple_edge_paths(G, 0, [3, 4])
|
| 284 |
+
assert {tuple(p) for p in paths} == {
|
| 285 |
+
((0, 1), (1, 2), (2, 3)),
|
| 286 |
+
((0, 1), (1, 2), (2, 4)),
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def test_digraph_all_simple_edge_paths_with_two_targets_emits_two_paths():
|
| 291 |
+
G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 292 |
+
G.add_edge(2, 4)
|
| 293 |
+
paths = nx.all_simple_edge_paths(G, 0, [3, 4])
|
| 294 |
+
assert {tuple(p) for p in paths} == {
|
| 295 |
+
((0, 1), (1, 2), (2, 3)),
|
| 296 |
+
((0, 1), (1, 2), (2, 4)),
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
def test_all_simple_edge_paths_with_two_targets_cutoff():
|
| 301 |
+
G = nx.path_graph(4)
|
| 302 |
+
G.add_edge(2, 4)
|
| 303 |
+
paths = nx.all_simple_edge_paths(G, 0, [3, 4], cutoff=3)
|
| 304 |
+
assert {tuple(p) for p in paths} == {
|
| 305 |
+
((0, 1), (1, 2), (2, 3)),
|
| 306 |
+
((0, 1), (1, 2), (2, 4)),
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
def test_digraph_all_simple_edge_paths_with_two_targets_cutoff():
|
| 311 |
+
G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 312 |
+
G.add_edge(2, 4)
|
| 313 |
+
paths = nx.all_simple_edge_paths(G, 0, [3, 4], cutoff=3)
|
| 314 |
+
assert {tuple(p) for p in paths} == {
|
| 315 |
+
((0, 1), (1, 2), (2, 3)),
|
| 316 |
+
((0, 1), (1, 2), (2, 4)),
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
def test_all_simple_edge_paths_with_two_targets_in_line_emits_two_paths():
|
| 321 |
+
G = nx.path_graph(4)
|
| 322 |
+
paths = nx.all_simple_edge_paths(G, 0, [2, 3])
|
| 323 |
+
assert {tuple(p) for p in paths} == {((0, 1), (1, 2)), ((0, 1), (1, 2), (2, 3))}
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
def test_all_simple_edge_paths_ignores_cycle():
|
| 327 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 328 |
+
G.add_edge(1, 3)
|
| 329 |
+
paths = nx.all_simple_edge_paths(G, 0, 3)
|
| 330 |
+
assert {tuple(p) for p in paths} == {((0, 1), (1, 3))}
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
def test_all_simple_edge_paths_with_two_targets_inside_cycle_emits_two_paths():
|
| 334 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 335 |
+
G.add_edge(1, 3)
|
| 336 |
+
paths = nx.all_simple_edge_paths(G, 0, [2, 3])
|
| 337 |
+
assert {tuple(p) for p in paths} == {((0, 1), (1, 2)), ((0, 1), (1, 3))}
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def test_all_simple_edge_paths_source_target():
|
| 341 |
+
G = nx.path_graph(4)
|
| 342 |
+
paths = nx.all_simple_edge_paths(G, 1, 1)
|
| 343 |
+
assert list(paths) == [[]]
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def test_all_simple_edge_paths_cutoff():
|
| 347 |
+
G = nx.complete_graph(4)
|
| 348 |
+
paths = nx.all_simple_edge_paths(G, 0, 1, cutoff=1)
|
| 349 |
+
assert {tuple(p) for p in paths} == {((0, 1),)}
|
| 350 |
+
paths = nx.all_simple_edge_paths(G, 0, 1, cutoff=2)
|
| 351 |
+
assert {tuple(p) for p in paths} == {((0, 1),), ((0, 2), (2, 1)), ((0, 3), (3, 1))}
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
def test_all_simple_edge_paths_on_non_trivial_graph():
|
| 355 |
+
"""you may need to draw this graph to make sure it is reasonable"""
|
| 356 |
+
G = nx.path_graph(5, create_using=nx.DiGraph())
|
| 357 |
+
G.add_edges_from([(0, 5), (1, 5), (1, 3), (5, 4), (4, 2), (4, 3)])
|
| 358 |
+
paths = nx.all_simple_edge_paths(G, 1, [2, 3])
|
| 359 |
+
assert {tuple(p) for p in paths} == {
|
| 360 |
+
((1, 2),),
|
| 361 |
+
((1, 3), (3, 4), (4, 2)),
|
| 362 |
+
((1, 5), (5, 4), (4, 2)),
|
| 363 |
+
((1, 3),),
|
| 364 |
+
((1, 2), (2, 3)),
|
| 365 |
+
((1, 5), (5, 4), (4, 3)),
|
| 366 |
+
((1, 5), (5, 4), (4, 2), (2, 3)),
|
| 367 |
+
}
|
| 368 |
+
paths = nx.all_simple_edge_paths(G, 1, [2, 3], cutoff=3)
|
| 369 |
+
assert {tuple(p) for p in paths} == {
|
| 370 |
+
((1, 2),),
|
| 371 |
+
((1, 3), (3, 4), (4, 2)),
|
| 372 |
+
((1, 5), (5, 4), (4, 2)),
|
| 373 |
+
((1, 3),),
|
| 374 |
+
((1, 2), (2, 3)),
|
| 375 |
+
((1, 5), (5, 4), (4, 3)),
|
| 376 |
+
}
|
| 377 |
+
paths = nx.all_simple_edge_paths(G, 1, [2, 3], cutoff=2)
|
| 378 |
+
assert {tuple(p) for p in paths} == {((1, 2),), ((1, 3),), ((1, 2), (2, 3))}
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def test_all_simple_edge_paths_multigraph():
|
| 382 |
+
G = nx.MultiGraph([(1, 2), (1, 2)])
|
| 383 |
+
paths = nx.all_simple_edge_paths(G, 1, 1)
|
| 384 |
+
assert list(paths) == [[]]
|
| 385 |
+
nx.add_path(G, [3, 1, 10, 2])
|
| 386 |
+
paths = list(nx.all_simple_edge_paths(G, 1, 2))
|
| 387 |
+
assert len(paths) == 3
|
| 388 |
+
assert {tuple(p) for p in paths} == {
|
| 389 |
+
((1, 2, 0),),
|
| 390 |
+
((1, 2, 1),),
|
| 391 |
+
((1, 10, 0), (10, 2, 0)),
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def test_all_simple_edge_paths_multigraph_with_cutoff():
|
| 396 |
+
G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)])
|
| 397 |
+
paths = list(nx.all_simple_edge_paths(G, 1, 2, cutoff=1))
|
| 398 |
+
assert len(paths) == 2
|
| 399 |
+
assert {tuple(p) for p in paths} == {((1, 2, 0),), ((1, 2, 1),)}
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def test_all_simple_edge_paths_directed():
|
| 403 |
+
G = nx.DiGraph()
|
| 404 |
+
nx.add_path(G, [1, 2, 3])
|
| 405 |
+
nx.add_path(G, [3, 2, 1])
|
| 406 |
+
paths = nx.all_simple_edge_paths(G, 1, 3)
|
| 407 |
+
assert {tuple(p) for p in paths} == {((1, 2), (2, 3))}
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def test_all_simple_edge_paths_empty():
|
| 411 |
+
G = nx.path_graph(4)
|
| 412 |
+
paths = nx.all_simple_edge_paths(G, 0, 3, cutoff=2)
|
| 413 |
+
assert list(paths) == []
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def test_all_simple_edge_paths_corner_cases():
|
| 417 |
+
assert list(nx.all_simple_edge_paths(nx.empty_graph(2), 0, 0)) == [[]]
|
| 418 |
+
assert list(nx.all_simple_edge_paths(nx.empty_graph(2), 0, 1)) == []
|
| 419 |
+
assert list(nx.all_simple_edge_paths(nx.path_graph(9), 0, 8, 0)) == []
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def test_all_simple_edge_paths_ignores_self_loop():
|
| 423 |
+
G = nx.Graph([(0, 0), (0, 1), (1, 1), (1, 2)])
|
| 424 |
+
assert list(nx.all_simple_edge_paths(G, 0, 2)) == [[(0, 1), (1, 2)]]
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def hamiltonian_edge_path(G, source):
|
| 428 |
+
source = arbitrary_element(G)
|
| 429 |
+
neighbors = set(G[source]) - {source}
|
| 430 |
+
n = len(G)
|
| 431 |
+
for target in neighbors:
|
| 432 |
+
for path in nx.all_simple_edge_paths(G, source, target):
|
| 433 |
+
if len(path) == n - 1:
|
| 434 |
+
yield path
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def test_hamiltonian__edge_path():
|
| 438 |
+
from itertools import permutations
|
| 439 |
+
|
| 440 |
+
G = nx.complete_graph(4)
|
| 441 |
+
paths = hamiltonian_edge_path(G, 0)
|
| 442 |
+
exact = [list(pairwise([0] + list(p))) for p in permutations([1, 2, 3], 3)]
|
| 443 |
+
assert sorted(exact) == sorted(paths)
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
def test_edge_cutoff_zero():
|
| 447 |
+
G = nx.complete_graph(4)
|
| 448 |
+
paths = nx.all_simple_edge_paths(G, 0, 3, cutoff=0)
|
| 449 |
+
assert [list(p) for p in paths] == []
|
| 450 |
+
paths = nx.all_simple_edge_paths(nx.MultiGraph(G), 0, 3, cutoff=0)
|
| 451 |
+
assert [list(p) for p in paths] == []
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
def test_edge_source_missing():
|
| 455 |
+
with pytest.raises(nx.NodeNotFound):
|
| 456 |
+
G = nx.Graph()
|
| 457 |
+
nx.add_path(G, [1, 2, 3])
|
| 458 |
+
list(nx.all_simple_edge_paths(nx.MultiGraph(G), 0, 3))
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def test_edge_target_missing():
|
| 462 |
+
with pytest.raises(nx.NodeNotFound):
|
| 463 |
+
G = nx.Graph()
|
| 464 |
+
nx.add_path(G, [1, 2, 3])
|
| 465 |
+
list(nx.all_simple_edge_paths(nx.MultiGraph(G), 1, 4))
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
# Tests for shortest_simple_paths
|
| 469 |
+
def test_shortest_simple_paths():
|
| 470 |
+
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
|
| 471 |
+
paths = nx.shortest_simple_paths(G, 1, 12)
|
| 472 |
+
assert next(paths) == [1, 2, 3, 4, 8, 12]
|
| 473 |
+
assert next(paths) == [1, 5, 6, 7, 8, 12]
|
| 474 |
+
assert [len(path) for path in nx.shortest_simple_paths(G, 1, 12)] == sorted(
|
| 475 |
+
len(path) for path in nx.all_simple_paths(G, 1, 12)
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
def test_shortest_simple_paths_singleton_path():
|
| 480 |
+
G = nx.empty_graph(3)
|
| 481 |
+
assert list(nx.shortest_simple_paths(G, 0, 0)) == [[0]]
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
def test_shortest_simple_paths_directed():
|
| 485 |
+
G = nx.cycle_graph(7, create_using=nx.DiGraph())
|
| 486 |
+
paths = nx.shortest_simple_paths(G, 0, 3)
|
| 487 |
+
assert list(paths) == [[0, 1, 2, 3]]
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
def test_shortest_simple_paths_directed_with_weight_function():
|
| 491 |
+
def cost(u, v, x):
|
| 492 |
+
return 1
|
| 493 |
+
|
| 494 |
+
G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
|
| 495 |
+
paths = nx.shortest_simple_paths(G, 1, 12)
|
| 496 |
+
assert next(paths) == [1, 2, 3, 4, 8, 12]
|
| 497 |
+
assert next(paths) == [1, 5, 6, 7, 8, 12]
|
| 498 |
+
assert [
|
| 499 |
+
len(path) for path in nx.shortest_simple_paths(G, 1, 12, weight=cost)
|
| 500 |
+
] == sorted(len(path) for path in nx.all_simple_paths(G, 1, 12))
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def test_shortest_simple_paths_with_weight_function():
|
| 504 |
+
def cost(u, v, x):
|
| 505 |
+
return 1
|
| 506 |
+
|
| 507 |
+
G = nx.cycle_graph(7, create_using=nx.DiGraph())
|
| 508 |
+
paths = nx.shortest_simple_paths(G, 0, 3, weight=cost)
|
| 509 |
+
assert list(paths) == [[0, 1, 2, 3]]
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
def test_Greg_Bernstein():
|
| 513 |
+
g1 = nx.Graph()
|
| 514 |
+
g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
|
| 515 |
+
g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
|
| 516 |
+
g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
|
| 517 |
+
g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
|
| 518 |
+
g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
|
| 519 |
+
g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
|
| 520 |
+
g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
|
| 521 |
+
solution = [["N1", "N0", "N3"], ["N1", "N2", "N3"], ["N1", "N4", "N0", "N3"]]
|
| 522 |
+
result = list(nx.shortest_simple_paths(g1, "N1", "N3", weight="weight"))
|
| 523 |
+
assert result == solution
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
def test_weighted_shortest_simple_path():
|
| 527 |
+
def cost_func(path):
|
| 528 |
+
return sum(G.adj[u][v]["weight"] for (u, v) in zip(path, path[1:]))
|
| 529 |
+
|
| 530 |
+
G = nx.complete_graph(5)
|
| 531 |
+
weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
|
| 532 |
+
nx.set_edge_attributes(G, weight, "weight")
|
| 533 |
+
cost = 0
|
| 534 |
+
for path in nx.shortest_simple_paths(G, 0, 3, weight="weight"):
|
| 535 |
+
this_cost = cost_func(path)
|
| 536 |
+
assert cost <= this_cost
|
| 537 |
+
cost = this_cost
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
def test_directed_weighted_shortest_simple_path():
|
| 541 |
+
def cost_func(path):
|
| 542 |
+
return sum(G.adj[u][v]["weight"] for (u, v) in zip(path, path[1:]))
|
| 543 |
+
|
| 544 |
+
G = nx.complete_graph(5)
|
| 545 |
+
G = G.to_directed()
|
| 546 |
+
weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
|
| 547 |
+
nx.set_edge_attributes(G, weight, "weight")
|
| 548 |
+
cost = 0
|
| 549 |
+
for path in nx.shortest_simple_paths(G, 0, 3, weight="weight"):
|
| 550 |
+
this_cost = cost_func(path)
|
| 551 |
+
assert cost <= this_cost
|
| 552 |
+
cost = this_cost
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
def test_weighted_shortest_simple_path_issue2427():
|
| 556 |
+
G = nx.Graph()
|
| 557 |
+
G.add_edge("IN", "OUT", weight=2)
|
| 558 |
+
G.add_edge("IN", "A", weight=1)
|
| 559 |
+
G.add_edge("IN", "B", weight=2)
|
| 560 |
+
G.add_edge("B", "OUT", weight=2)
|
| 561 |
+
assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
|
| 562 |
+
["IN", "OUT"],
|
| 563 |
+
["IN", "B", "OUT"],
|
| 564 |
+
]
|
| 565 |
+
G = nx.Graph()
|
| 566 |
+
G.add_edge("IN", "OUT", weight=10)
|
| 567 |
+
G.add_edge("IN", "A", weight=1)
|
| 568 |
+
G.add_edge("IN", "B", weight=1)
|
| 569 |
+
G.add_edge("B", "OUT", weight=1)
|
| 570 |
+
assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
|
| 571 |
+
["IN", "B", "OUT"],
|
| 572 |
+
["IN", "OUT"],
|
| 573 |
+
]
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
def test_directed_weighted_shortest_simple_path_issue2427():
|
| 577 |
+
G = nx.DiGraph()
|
| 578 |
+
G.add_edge("IN", "OUT", weight=2)
|
| 579 |
+
G.add_edge("IN", "A", weight=1)
|
| 580 |
+
G.add_edge("IN", "B", weight=2)
|
| 581 |
+
G.add_edge("B", "OUT", weight=2)
|
| 582 |
+
assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
|
| 583 |
+
["IN", "OUT"],
|
| 584 |
+
["IN", "B", "OUT"],
|
| 585 |
+
]
|
| 586 |
+
G = nx.DiGraph()
|
| 587 |
+
G.add_edge("IN", "OUT", weight=10)
|
| 588 |
+
G.add_edge("IN", "A", weight=1)
|
| 589 |
+
G.add_edge("IN", "B", weight=1)
|
| 590 |
+
G.add_edge("B", "OUT", weight=1)
|
| 591 |
+
assert list(nx.shortest_simple_paths(G, "IN", "OUT", weight="weight")) == [
|
| 592 |
+
["IN", "B", "OUT"],
|
| 593 |
+
["IN", "OUT"],
|
| 594 |
+
]
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
def test_weight_name():
|
| 598 |
+
G = nx.cycle_graph(7)
|
| 599 |
+
nx.set_edge_attributes(G, 1, "weight")
|
| 600 |
+
nx.set_edge_attributes(G, 1, "foo")
|
| 601 |
+
G.adj[1][2]["foo"] = 7
|
| 602 |
+
paths = list(nx.shortest_simple_paths(G, 0, 3, weight="foo"))
|
| 603 |
+
solution = [[0, 6, 5, 4, 3], [0, 1, 2, 3]]
|
| 604 |
+
assert paths == solution
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
def test_ssp_source_missing():
|
| 608 |
+
with pytest.raises(nx.NodeNotFound):
|
| 609 |
+
G = nx.Graph()
|
| 610 |
+
nx.add_path(G, [1, 2, 3])
|
| 611 |
+
list(nx.shortest_simple_paths(G, 0, 3))
|
| 612 |
+
|
| 613 |
+
|
| 614 |
+
def test_ssp_target_missing():
|
| 615 |
+
with pytest.raises(nx.NodeNotFound):
|
| 616 |
+
G = nx.Graph()
|
| 617 |
+
nx.add_path(G, [1, 2, 3])
|
| 618 |
+
list(nx.shortest_simple_paths(G, 1, 4))
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
def test_ssp_multigraph():
|
| 622 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 623 |
+
G = nx.MultiGraph()
|
| 624 |
+
nx.add_path(G, [1, 2, 3])
|
| 625 |
+
list(nx.shortest_simple_paths(G, 1, 4))
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
def test_ssp_source_missing2():
|
| 629 |
+
with pytest.raises(nx.NetworkXNoPath):
|
| 630 |
+
G = nx.Graph()
|
| 631 |
+
nx.add_path(G, [0, 1, 2])
|
| 632 |
+
nx.add_path(G, [3, 4, 5])
|
| 633 |
+
list(nx.shortest_simple_paths(G, 0, 3))
|
| 634 |
+
|
| 635 |
+
|
| 636 |
+
def test_bidirectional_shortest_path_restricted_cycle():
|
| 637 |
+
cycle = nx.cycle_graph(7)
|
| 638 |
+
length, path = _bidirectional_shortest_path(cycle, 0, 3)
|
| 639 |
+
assert path == [0, 1, 2, 3]
|
| 640 |
+
length, path = _bidirectional_shortest_path(cycle, 0, 3, ignore_nodes=[1])
|
| 641 |
+
assert path == [0, 6, 5, 4, 3]
|
| 642 |
+
|
| 643 |
+
|
| 644 |
+
def test_bidirectional_shortest_path_restricted_wheel():
|
| 645 |
+
wheel = nx.wheel_graph(6)
|
| 646 |
+
length, path = _bidirectional_shortest_path(wheel, 1, 3)
|
| 647 |
+
assert path in [[1, 0, 3], [1, 2, 3]]
|
| 648 |
+
length, path = _bidirectional_shortest_path(wheel, 1, 3, ignore_nodes=[0])
|
| 649 |
+
assert path == [1, 2, 3]
|
| 650 |
+
length, path = _bidirectional_shortest_path(wheel, 1, 3, ignore_nodes=[0, 2])
|
| 651 |
+
assert path == [1, 5, 4, 3]
|
| 652 |
+
length, path = _bidirectional_shortest_path(
|
| 653 |
+
wheel, 1, 3, ignore_edges=[(1, 0), (5, 0), (2, 3)]
|
| 654 |
+
)
|
| 655 |
+
assert path in [[1, 2, 0, 3], [1, 5, 4, 3]]
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
def test_bidirectional_shortest_path_restricted_directed_cycle():
|
| 659 |
+
directed_cycle = nx.cycle_graph(7, create_using=nx.DiGraph())
|
| 660 |
+
length, path = _bidirectional_shortest_path(directed_cycle, 0, 3)
|
| 661 |
+
assert path == [0, 1, 2, 3]
|
| 662 |
+
pytest.raises(
|
| 663 |
+
nx.NetworkXNoPath,
|
| 664 |
+
_bidirectional_shortest_path,
|
| 665 |
+
directed_cycle,
|
| 666 |
+
0,
|
| 667 |
+
3,
|
| 668 |
+
ignore_nodes=[1],
|
| 669 |
+
)
|
| 670 |
+
length, path = _bidirectional_shortest_path(
|
| 671 |
+
directed_cycle, 0, 3, ignore_edges=[(2, 1)]
|
| 672 |
+
)
|
| 673 |
+
assert path == [0, 1, 2, 3]
|
| 674 |
+
pytest.raises(
|
| 675 |
+
nx.NetworkXNoPath,
|
| 676 |
+
_bidirectional_shortest_path,
|
| 677 |
+
directed_cycle,
|
| 678 |
+
0,
|
| 679 |
+
3,
|
| 680 |
+
ignore_edges=[(1, 2)],
|
| 681 |
+
)
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
def test_bidirectional_shortest_path_ignore():
|
| 685 |
+
G = nx.Graph()
|
| 686 |
+
nx.add_path(G, [1, 2])
|
| 687 |
+
nx.add_path(G, [1, 3])
|
| 688 |
+
nx.add_path(G, [1, 4])
|
| 689 |
+
pytest.raises(
|
| 690 |
+
nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[1]
|
| 691 |
+
)
|
| 692 |
+
pytest.raises(
|
| 693 |
+
nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[2]
|
| 694 |
+
)
|
| 695 |
+
G = nx.Graph()
|
| 696 |
+
nx.add_path(G, [1, 3])
|
| 697 |
+
nx.add_path(G, [1, 4])
|
| 698 |
+
nx.add_path(G, [3, 2])
|
| 699 |
+
pytest.raises(
|
| 700 |
+
nx.NetworkXNoPath, _bidirectional_shortest_path, G, 1, 2, ignore_nodes=[1, 2]
|
| 701 |
+
)
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
def validate_path(G, s, t, soln_len, path):
|
| 705 |
+
assert path[0] == s
|
| 706 |
+
assert path[-1] == t
|
| 707 |
+
assert soln_len == sum(
|
| 708 |
+
G[u][v].get("weight", 1) for u, v in zip(path[:-1], path[1:])
|
| 709 |
+
)
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
def validate_length_path(G, s, t, soln_len, length, path):
|
| 713 |
+
assert soln_len == length
|
| 714 |
+
validate_path(G, s, t, length, path)
|
| 715 |
+
|
| 716 |
+
|
| 717 |
+
def test_bidirectional_dijkstra_restricted():
|
| 718 |
+
XG = nx.DiGraph()
|
| 719 |
+
XG.add_weighted_edges_from(
|
| 720 |
+
[
|
| 721 |
+
("s", "u", 10),
|
| 722 |
+
("s", "x", 5),
|
| 723 |
+
("u", "v", 1),
|
| 724 |
+
("u", "x", 2),
|
| 725 |
+
("v", "y", 1),
|
| 726 |
+
("x", "u", 3),
|
| 727 |
+
("x", "v", 5),
|
| 728 |
+
("x", "y", 2),
|
| 729 |
+
("y", "s", 7),
|
| 730 |
+
("y", "v", 6),
|
| 731 |
+
]
|
| 732 |
+
)
|
| 733 |
+
|
| 734 |
+
XG3 = nx.Graph()
|
| 735 |
+
XG3.add_weighted_edges_from(
|
| 736 |
+
[[0, 1, 2], [1, 2, 12], [2, 3, 1], [3, 4, 5], [4, 5, 1], [5, 0, 10]]
|
| 737 |
+
)
|
| 738 |
+
validate_length_path(XG, "s", "v", 9, *_bidirectional_dijkstra(XG, "s", "v"))
|
| 739 |
+
validate_length_path(
|
| 740 |
+
XG, "s", "v", 10, *_bidirectional_dijkstra(XG, "s", "v", ignore_nodes=["u"])
|
| 741 |
+
)
|
| 742 |
+
validate_length_path(
|
| 743 |
+
XG,
|
| 744 |
+
"s",
|
| 745 |
+
"v",
|
| 746 |
+
11,
|
| 747 |
+
*_bidirectional_dijkstra(XG, "s", "v", ignore_edges=[("s", "x")]),
|
| 748 |
+
)
|
| 749 |
+
pytest.raises(
|
| 750 |
+
nx.NetworkXNoPath,
|
| 751 |
+
_bidirectional_dijkstra,
|
| 752 |
+
XG,
|
| 753 |
+
"s",
|
| 754 |
+
"v",
|
| 755 |
+
ignore_nodes=["u"],
|
| 756 |
+
ignore_edges=[("s", "x")],
|
| 757 |
+
)
|
| 758 |
+
validate_length_path(XG3, 0, 3, 15, *_bidirectional_dijkstra(XG3, 0, 3))
|
| 759 |
+
validate_length_path(
|
| 760 |
+
XG3, 0, 3, 16, *_bidirectional_dijkstra(XG3, 0, 3, ignore_nodes=[1])
|
| 761 |
+
)
|
| 762 |
+
validate_length_path(
|
| 763 |
+
XG3, 0, 3, 16, *_bidirectional_dijkstra(XG3, 0, 3, ignore_edges=[(2, 3)])
|
| 764 |
+
)
|
| 765 |
+
pytest.raises(
|
| 766 |
+
nx.NetworkXNoPath,
|
| 767 |
+
_bidirectional_dijkstra,
|
| 768 |
+
XG3,
|
| 769 |
+
0,
|
| 770 |
+
3,
|
| 771 |
+
ignore_nodes=[1],
|
| 772 |
+
ignore_edges=[(5, 4)],
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
|
| 776 |
+
def test_bidirectional_dijkstra_no_path():
|
| 777 |
+
with pytest.raises(nx.NetworkXNoPath):
|
| 778 |
+
G = nx.Graph()
|
| 779 |
+
nx.add_path(G, [1, 2, 3])
|
| 780 |
+
nx.add_path(G, [4, 5, 6])
|
| 781 |
+
_bidirectional_dijkstra(G, 1, 6)
|
| 782 |
+
|
| 783 |
+
|
| 784 |
+
def test_bidirectional_dijkstra_ignore():
|
| 785 |
+
G = nx.Graph()
|
| 786 |
+
nx.add_path(G, [1, 2, 10])
|
| 787 |
+
nx.add_path(G, [1, 3, 10])
|
| 788 |
+
pytest.raises(nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[1])
|
| 789 |
+
pytest.raises(nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[2])
|
| 790 |
+
pytest.raises(
|
| 791 |
+
nx.NetworkXNoPath, _bidirectional_dijkstra, G, 1, 2, ignore_nodes=[1, 2]
|
| 792 |
+
)
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_threshold.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Threshold Graphs
|
| 3 |
+
================
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
import networkx as nx
|
| 9 |
+
import networkx.algorithms.threshold as nxt
|
| 10 |
+
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
|
| 11 |
+
|
| 12 |
+
cnlti = nx.convert_node_labels_to_integers
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestGeneratorThreshold:
|
| 16 |
+
def test_threshold_sequence_graph_test(self):
|
| 17 |
+
G = nx.star_graph(10)
|
| 18 |
+
assert nxt.is_threshold_graph(G)
|
| 19 |
+
assert nxt.is_threshold_sequence([d for n, d in G.degree()])
|
| 20 |
+
|
| 21 |
+
G = nx.complete_graph(10)
|
| 22 |
+
assert nxt.is_threshold_graph(G)
|
| 23 |
+
assert nxt.is_threshold_sequence([d for n, d in G.degree()])
|
| 24 |
+
|
| 25 |
+
deg = [3, 2, 2, 1, 1, 1]
|
| 26 |
+
assert not nxt.is_threshold_sequence(deg)
|
| 27 |
+
|
| 28 |
+
deg = [3, 2, 2, 1]
|
| 29 |
+
assert nxt.is_threshold_sequence(deg)
|
| 30 |
+
|
| 31 |
+
G = nx.generators.havel_hakimi_graph(deg)
|
| 32 |
+
assert nxt.is_threshold_graph(G)
|
| 33 |
+
|
| 34 |
+
def test_creation_sequences(self):
|
| 35 |
+
deg = [3, 2, 2, 1]
|
| 36 |
+
G = nx.generators.havel_hakimi_graph(deg)
|
| 37 |
+
|
| 38 |
+
with pytest.raises(ValueError):
|
| 39 |
+
nxt.creation_sequence(deg, with_labels=True, compact=True)
|
| 40 |
+
|
| 41 |
+
cs0 = nxt.creation_sequence(deg)
|
| 42 |
+
H0 = nxt.threshold_graph(cs0)
|
| 43 |
+
assert "".join(cs0) == "ddid"
|
| 44 |
+
|
| 45 |
+
cs1 = nxt.creation_sequence(deg, with_labels=True)
|
| 46 |
+
H1 = nxt.threshold_graph(cs1)
|
| 47 |
+
assert cs1 == [(1, "d"), (2, "d"), (3, "i"), (0, "d")]
|
| 48 |
+
|
| 49 |
+
cs2 = nxt.creation_sequence(deg, compact=True)
|
| 50 |
+
H2 = nxt.threshold_graph(cs2)
|
| 51 |
+
assert cs2 == [2, 1, 1]
|
| 52 |
+
assert "".join(nxt.uncompact(cs2)) == "ddid"
|
| 53 |
+
assert graph_could_be_isomorphic(H0, G)
|
| 54 |
+
assert graph_could_be_isomorphic(H0, H1)
|
| 55 |
+
assert graph_could_be_isomorphic(H0, H2)
|
| 56 |
+
|
| 57 |
+
def test_make_compact(self):
|
| 58 |
+
assert nxt.make_compact(["d", "d", "d", "i", "d", "d"]) == [3, 1, 2]
|
| 59 |
+
assert nxt.make_compact([3, 1, 2]) == [3, 1, 2]
|
| 60 |
+
assert pytest.raises(TypeError, nxt.make_compact, [3.0, 1.0, 2.0])
|
| 61 |
+
|
| 62 |
+
def test_uncompact(self):
|
| 63 |
+
assert nxt.uncompact([3, 1, 2]) == ["d", "d", "d", "i", "d", "d"]
|
| 64 |
+
assert nxt.uncompact(["d", "d", "i", "d"]) == ["d", "d", "i", "d"]
|
| 65 |
+
assert nxt.uncompact(
|
| 66 |
+
nxt.uncompact([(1, "d"), (2, "d"), (3, "i"), (0, "d")])
|
| 67 |
+
) == nxt.uncompact([(1, "d"), (2, "d"), (3, "i"), (0, "d")])
|
| 68 |
+
assert pytest.raises(TypeError, nxt.uncompact, [3.0, 1.0, 2.0])
|
| 69 |
+
|
| 70 |
+
def test_creation_sequence_to_weights(self):
|
| 71 |
+
assert nxt.creation_sequence_to_weights([3, 1, 2]) == [
|
| 72 |
+
0.5,
|
| 73 |
+
0.5,
|
| 74 |
+
0.5,
|
| 75 |
+
0.25,
|
| 76 |
+
0.75,
|
| 77 |
+
0.75,
|
| 78 |
+
]
|
| 79 |
+
assert pytest.raises(
|
| 80 |
+
TypeError, nxt.creation_sequence_to_weights, [3.0, 1.0, 2.0]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
def test_weights_to_creation_sequence(self):
|
| 84 |
+
deg = [3, 2, 2, 1]
|
| 85 |
+
with pytest.raises(ValueError):
|
| 86 |
+
nxt.weights_to_creation_sequence(deg, with_labels=True, compact=True)
|
| 87 |
+
assert nxt.weights_to_creation_sequence(deg, with_labels=True) == [
|
| 88 |
+
(3, "d"),
|
| 89 |
+
(1, "d"),
|
| 90 |
+
(2, "d"),
|
| 91 |
+
(0, "d"),
|
| 92 |
+
]
|
| 93 |
+
assert nxt.weights_to_creation_sequence(deg, compact=True) == [4]
|
| 94 |
+
|
| 95 |
+
def test_find_alternating_4_cycle(self):
|
| 96 |
+
G = nx.Graph()
|
| 97 |
+
G.add_edge(1, 2)
|
| 98 |
+
assert not nxt.find_alternating_4_cycle(G)
|
| 99 |
+
|
| 100 |
+
def test_shortest_path(self):
|
| 101 |
+
deg = [3, 2, 2, 1]
|
| 102 |
+
G = nx.generators.havel_hakimi_graph(deg)
|
| 103 |
+
cs1 = nxt.creation_sequence(deg, with_labels=True)
|
| 104 |
+
for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3), (3, 1), (1, 2), (2, 3)]:
|
| 105 |
+
assert nxt.shortest_path(cs1, n, m) == nx.shortest_path(G, n, m)
|
| 106 |
+
|
| 107 |
+
spl = nxt.shortest_path_length(cs1, 3)
|
| 108 |
+
spl2 = nxt.shortest_path_length([t for v, t in cs1], 2)
|
| 109 |
+
assert spl == spl2
|
| 110 |
+
|
| 111 |
+
spld = {}
|
| 112 |
+
for j, pl in enumerate(spl):
|
| 113 |
+
n = cs1[j][0]
|
| 114 |
+
spld[n] = pl
|
| 115 |
+
assert spld == nx.single_source_shortest_path_length(G, 3)
|
| 116 |
+
|
| 117 |
+
assert nxt.shortest_path(["d", "d", "d", "i", "d", "d"], 1, 2) == [1, 2]
|
| 118 |
+
assert nxt.shortest_path([3, 1, 2], 1, 2) == [1, 2]
|
| 119 |
+
assert pytest.raises(TypeError, nxt.shortest_path, [3.0, 1.0, 2.0], 1, 2)
|
| 120 |
+
assert pytest.raises(ValueError, nxt.shortest_path, [3, 1, 2], "a", 2)
|
| 121 |
+
assert pytest.raises(ValueError, nxt.shortest_path, [3, 1, 2], 1, "b")
|
| 122 |
+
assert nxt.shortest_path([3, 1, 2], 1, 1) == [1]
|
| 123 |
+
|
| 124 |
+
def test_shortest_path_length(self):
|
| 125 |
+
assert nxt.shortest_path_length([3, 1, 2], 1) == [1, 0, 1, 2, 1, 1]
|
| 126 |
+
assert nxt.shortest_path_length(["d", "d", "d", "i", "d", "d"], 1) == [
|
| 127 |
+
1,
|
| 128 |
+
0,
|
| 129 |
+
1,
|
| 130 |
+
2,
|
| 131 |
+
1,
|
| 132 |
+
1,
|
| 133 |
+
]
|
| 134 |
+
assert nxt.shortest_path_length(("d", "d", "d", "i", "d", "d"), 1) == [
|
| 135 |
+
1,
|
| 136 |
+
0,
|
| 137 |
+
1,
|
| 138 |
+
2,
|
| 139 |
+
1,
|
| 140 |
+
1,
|
| 141 |
+
]
|
| 142 |
+
assert pytest.raises(TypeError, nxt.shortest_path, [3.0, 1.0, 2.0], 1)
|
| 143 |
+
|
| 144 |
+
def test_random_threshold_sequence(self):
|
| 145 |
+
assert len(nxt.random_threshold_sequence(10, 0.5)) == 10
|
| 146 |
+
assert nxt.random_threshold_sequence(10, 0.5, seed=42) == [
|
| 147 |
+
"d",
|
| 148 |
+
"i",
|
| 149 |
+
"d",
|
| 150 |
+
"d",
|
| 151 |
+
"d",
|
| 152 |
+
"i",
|
| 153 |
+
"i",
|
| 154 |
+
"i",
|
| 155 |
+
"d",
|
| 156 |
+
"d",
|
| 157 |
+
]
|
| 158 |
+
assert pytest.raises(ValueError, nxt.random_threshold_sequence, 10, 1.5)
|
| 159 |
+
|
| 160 |
+
def test_right_d_threshold_sequence(self):
|
| 161 |
+
assert nxt.right_d_threshold_sequence(3, 2) == ["d", "i", "d"]
|
| 162 |
+
assert pytest.raises(ValueError, nxt.right_d_threshold_sequence, 2, 3)
|
| 163 |
+
|
| 164 |
+
def test_left_d_threshold_sequence(self):
|
| 165 |
+
assert nxt.left_d_threshold_sequence(3, 2) == ["d", "i", "d"]
|
| 166 |
+
assert pytest.raises(ValueError, nxt.left_d_threshold_sequence, 2, 3)
|
| 167 |
+
|
| 168 |
+
def test_weights_thresholds(self):
|
| 169 |
+
wseq = [3, 4, 3, 3, 5, 6, 5, 4, 5, 6]
|
| 170 |
+
cs = nxt.weights_to_creation_sequence(wseq, threshold=10)
|
| 171 |
+
wseq = nxt.creation_sequence_to_weights(cs)
|
| 172 |
+
cs2 = nxt.weights_to_creation_sequence(wseq)
|
| 173 |
+
assert cs == cs2
|
| 174 |
+
|
| 175 |
+
wseq = nxt.creation_sequence_to_weights(nxt.uncompact([3, 1, 2, 3, 3, 2, 3]))
|
| 176 |
+
assert wseq == [
|
| 177 |
+
s * 0.125 for s in [4, 4, 4, 3, 5, 5, 2, 2, 2, 6, 6, 6, 1, 1, 7, 7, 7]
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
wseq = nxt.creation_sequence_to_weights([3, 1, 2, 3, 3, 2, 3])
|
| 181 |
+
assert wseq == [
|
| 182 |
+
s * 0.125 for s in [4, 4, 4, 3, 5, 5, 2, 2, 2, 6, 6, 6, 1, 1, 7, 7, 7]
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
wseq = nxt.creation_sequence_to_weights(list(enumerate("ddidiiidididi")))
|
| 186 |
+
assert wseq == [s * 0.1 for s in [5, 5, 4, 6, 3, 3, 3, 7, 2, 8, 1, 9, 0]]
|
| 187 |
+
|
| 188 |
+
wseq = nxt.creation_sequence_to_weights("ddidiiidididi")
|
| 189 |
+
assert wseq == [s * 0.1 for s in [5, 5, 4, 6, 3, 3, 3, 7, 2, 8, 1, 9, 0]]
|
| 190 |
+
|
| 191 |
+
wseq = nxt.creation_sequence_to_weights("ddidiiidididid")
|
| 192 |
+
ws = [s / 12 for s in [6, 6, 5, 7, 4, 4, 4, 8, 3, 9, 2, 10, 1, 11]]
|
| 193 |
+
assert sum(abs(c - d) for c, d in zip(wseq, ws)) < 1e-14
|
| 194 |
+
|
| 195 |
+
def test_finding_routines(self):
|
| 196 |
+
G = nx.Graph({1: [2], 2: [3], 3: [4], 4: [5], 5: [6]})
|
| 197 |
+
G.add_edge(2, 4)
|
| 198 |
+
G.add_edge(2, 5)
|
| 199 |
+
G.add_edge(2, 7)
|
| 200 |
+
G.add_edge(3, 6)
|
| 201 |
+
G.add_edge(4, 6)
|
| 202 |
+
|
| 203 |
+
# Alternating 4 cycle
|
| 204 |
+
assert nxt.find_alternating_4_cycle(G) == [1, 2, 3, 6]
|
| 205 |
+
|
| 206 |
+
# Threshold graph
|
| 207 |
+
TG = nxt.find_threshold_graph(G)
|
| 208 |
+
assert nxt.is_threshold_graph(TG)
|
| 209 |
+
assert sorted(TG.nodes()) == [1, 2, 3, 4, 5, 7]
|
| 210 |
+
|
| 211 |
+
cs = nxt.creation_sequence(dict(TG.degree()), with_labels=True)
|
| 212 |
+
assert nxt.find_creation_sequence(G) == cs
|
| 213 |
+
|
| 214 |
+
def test_fast_versions_properties_threshold_graphs(self):
|
| 215 |
+
cs = "ddiiddid"
|
| 216 |
+
G = nxt.threshold_graph(cs)
|
| 217 |
+
assert nxt.density("ddiiddid") == nx.density(G)
|
| 218 |
+
assert sorted(nxt.degree_sequence(cs)) == sorted(d for n, d in G.degree())
|
| 219 |
+
|
| 220 |
+
ts = nxt.triangle_sequence(cs)
|
| 221 |
+
assert ts == list(nx.triangles(G).values())
|
| 222 |
+
assert sum(ts) // 3 == nxt.triangles(cs)
|
| 223 |
+
|
| 224 |
+
c1 = nxt.cluster_sequence(cs)
|
| 225 |
+
c2 = list(nx.clustering(G).values())
|
| 226 |
+
assert sum(abs(c - d) for c, d in zip(c1, c2)) == pytest.approx(0, abs=1e-7)
|
| 227 |
+
|
| 228 |
+
b1 = nx.betweenness_centrality(G).values()
|
| 229 |
+
b2 = nxt.betweenness_sequence(cs)
|
| 230 |
+
assert sum(abs(c - d) for c, d in zip(b1, b2)) < 1e-7
|
| 231 |
+
|
| 232 |
+
assert nxt.eigenvalues(cs) == [0, 1, 3, 3, 5, 7, 7, 8]
|
| 233 |
+
|
| 234 |
+
# Degree Correlation
|
| 235 |
+
assert abs(nxt.degree_correlation(cs) + 0.593038821954) < 1e-12
|
| 236 |
+
assert nxt.degree_correlation("diiiddi") == -0.8
|
| 237 |
+
assert nxt.degree_correlation("did") == -1.0
|
| 238 |
+
assert nxt.degree_correlation("ddd") == 1.0
|
| 239 |
+
assert nxt.eigenvalues("dddiii") == [0, 0, 0, 0, 3, 3]
|
| 240 |
+
assert nxt.eigenvalues("dddiiid") == [0, 1, 1, 1, 4, 4, 7]
|
| 241 |
+
|
| 242 |
+
def test_tg_creation_routines(self):
|
| 243 |
+
s = nxt.left_d_threshold_sequence(5, 7)
|
| 244 |
+
s = nxt.right_d_threshold_sequence(5, 7)
|
| 245 |
+
s1 = nxt.swap_d(s, 1.0, 1.0)
|
| 246 |
+
s1 = nxt.swap_d(s, 1.0, 1.0, seed=1)
|
| 247 |
+
|
| 248 |
+
def test_eigenvectors(self):
|
| 249 |
+
np = pytest.importorskip("numpy")
|
| 250 |
+
eigenval = np.linalg.eigvals
|
| 251 |
+
pytest.importorskip("scipy")
|
| 252 |
+
|
| 253 |
+
cs = "ddiiddid"
|
| 254 |
+
G = nxt.threshold_graph(cs)
|
| 255 |
+
(tgeval, tgevec) = nxt.eigenvectors(cs)
|
| 256 |
+
np.testing.assert_allclose([np.dot(lv, lv) for lv in tgevec], 1.0, rtol=1e-9)
|
| 257 |
+
lapl = nx.laplacian_matrix(G)
|
| 258 |
+
|
| 259 |
+
def test_create_using(self):
|
| 260 |
+
cs = "ddiiddid"
|
| 261 |
+
G = nxt.threshold_graph(cs)
|
| 262 |
+
assert pytest.raises(
|
| 263 |
+
nx.exception.NetworkXError,
|
| 264 |
+
nxt.threshold_graph,
|
| 265 |
+
cs,
|
| 266 |
+
create_using=nx.DiGraph(),
|
| 267 |
+
)
|
| 268 |
+
MG = nxt.threshold_graph(cs, create_using=nx.MultiGraph())
|
| 269 |
+
assert sorted(MG.edges()) == sorted(G.edges())
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_time_dependent.py
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit testing for time dependent algorithms."""
|
| 2 |
+
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
|
| 9 |
+
_delta = timedelta(days=5 * 365)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestCdIndex:
|
| 13 |
+
"""Unit testing for the cd index function."""
|
| 14 |
+
|
| 15 |
+
def test_common_graph(self):
|
| 16 |
+
G = nx.DiGraph()
|
| 17 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 18 |
+
G.add_edge(4, 2)
|
| 19 |
+
G.add_edge(4, 0)
|
| 20 |
+
G.add_edge(4, 1)
|
| 21 |
+
G.add_edge(4, 3)
|
| 22 |
+
G.add_edge(5, 2)
|
| 23 |
+
G.add_edge(6, 2)
|
| 24 |
+
G.add_edge(6, 4)
|
| 25 |
+
G.add_edge(7, 4)
|
| 26 |
+
G.add_edge(8, 4)
|
| 27 |
+
G.add_edge(9, 4)
|
| 28 |
+
G.add_edge(9, 1)
|
| 29 |
+
G.add_edge(9, 3)
|
| 30 |
+
G.add_edge(10, 4)
|
| 31 |
+
|
| 32 |
+
node_attrs = {
|
| 33 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 34 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 35 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 36 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 37 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 38 |
+
5: {"time": datetime(1997, 1, 1)},
|
| 39 |
+
6: {"time": datetime(1998, 1, 1)},
|
| 40 |
+
7: {"time": datetime(1999, 1, 1)},
|
| 41 |
+
8: {"time": datetime(1999, 1, 1)},
|
| 42 |
+
9: {"time": datetime(1998, 1, 1)},
|
| 43 |
+
10: {"time": datetime(1997, 4, 1)},
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
nx.set_node_attributes(G, node_attrs)
|
| 47 |
+
|
| 48 |
+
assert nx.cd_index(G, 4, time_delta=_delta) == 0.17
|
| 49 |
+
|
| 50 |
+
def test_common_graph_with_given_attributes(self):
|
| 51 |
+
G = nx.DiGraph()
|
| 52 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 53 |
+
G.add_edge(4, 2)
|
| 54 |
+
G.add_edge(4, 0)
|
| 55 |
+
G.add_edge(4, 1)
|
| 56 |
+
G.add_edge(4, 3)
|
| 57 |
+
G.add_edge(5, 2)
|
| 58 |
+
G.add_edge(6, 2)
|
| 59 |
+
G.add_edge(6, 4)
|
| 60 |
+
G.add_edge(7, 4)
|
| 61 |
+
G.add_edge(8, 4)
|
| 62 |
+
G.add_edge(9, 4)
|
| 63 |
+
G.add_edge(9, 1)
|
| 64 |
+
G.add_edge(9, 3)
|
| 65 |
+
G.add_edge(10, 4)
|
| 66 |
+
|
| 67 |
+
node_attrs = {
|
| 68 |
+
0: {"date": datetime(1992, 1, 1)},
|
| 69 |
+
1: {"date": datetime(1992, 1, 1)},
|
| 70 |
+
2: {"date": datetime(1993, 1, 1)},
|
| 71 |
+
3: {"date": datetime(1993, 1, 1)},
|
| 72 |
+
4: {"date": datetime(1995, 1, 1)},
|
| 73 |
+
5: {"date": datetime(1997, 1, 1)},
|
| 74 |
+
6: {"date": datetime(1998, 1, 1)},
|
| 75 |
+
7: {"date": datetime(1999, 1, 1)},
|
| 76 |
+
8: {"date": datetime(1999, 1, 1)},
|
| 77 |
+
9: {"date": datetime(1998, 1, 1)},
|
| 78 |
+
10: {"date": datetime(1997, 4, 1)},
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
nx.set_node_attributes(G, node_attrs)
|
| 82 |
+
|
| 83 |
+
assert nx.cd_index(G, 4, time_delta=_delta, time="date") == 0.17
|
| 84 |
+
|
| 85 |
+
def test_common_graph_with_int_attributes(self):
|
| 86 |
+
G = nx.DiGraph()
|
| 87 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 88 |
+
G.add_edge(4, 2)
|
| 89 |
+
G.add_edge(4, 0)
|
| 90 |
+
G.add_edge(4, 1)
|
| 91 |
+
G.add_edge(4, 3)
|
| 92 |
+
G.add_edge(5, 2)
|
| 93 |
+
G.add_edge(6, 2)
|
| 94 |
+
G.add_edge(6, 4)
|
| 95 |
+
G.add_edge(7, 4)
|
| 96 |
+
G.add_edge(8, 4)
|
| 97 |
+
G.add_edge(9, 4)
|
| 98 |
+
G.add_edge(9, 1)
|
| 99 |
+
G.add_edge(9, 3)
|
| 100 |
+
G.add_edge(10, 4)
|
| 101 |
+
|
| 102 |
+
node_attrs = {
|
| 103 |
+
0: {"time": 20},
|
| 104 |
+
1: {"time": 20},
|
| 105 |
+
2: {"time": 30},
|
| 106 |
+
3: {"time": 30},
|
| 107 |
+
4: {"time": 50},
|
| 108 |
+
5: {"time": 70},
|
| 109 |
+
6: {"time": 80},
|
| 110 |
+
7: {"time": 90},
|
| 111 |
+
8: {"time": 90},
|
| 112 |
+
9: {"time": 80},
|
| 113 |
+
10: {"time": 74},
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
nx.set_node_attributes(G, node_attrs)
|
| 117 |
+
|
| 118 |
+
assert nx.cd_index(G, 4, time_delta=50) == 0.17
|
| 119 |
+
|
| 120 |
+
def test_common_graph_with_float_attributes(self):
|
| 121 |
+
G = nx.DiGraph()
|
| 122 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 123 |
+
G.add_edge(4, 2)
|
| 124 |
+
G.add_edge(4, 0)
|
| 125 |
+
G.add_edge(4, 1)
|
| 126 |
+
G.add_edge(4, 3)
|
| 127 |
+
G.add_edge(5, 2)
|
| 128 |
+
G.add_edge(6, 2)
|
| 129 |
+
G.add_edge(6, 4)
|
| 130 |
+
G.add_edge(7, 4)
|
| 131 |
+
G.add_edge(8, 4)
|
| 132 |
+
G.add_edge(9, 4)
|
| 133 |
+
G.add_edge(9, 1)
|
| 134 |
+
G.add_edge(9, 3)
|
| 135 |
+
G.add_edge(10, 4)
|
| 136 |
+
|
| 137 |
+
node_attrs = {
|
| 138 |
+
0: {"time": 20.2},
|
| 139 |
+
1: {"time": 20.2},
|
| 140 |
+
2: {"time": 30.7},
|
| 141 |
+
3: {"time": 30.7},
|
| 142 |
+
4: {"time": 50.9},
|
| 143 |
+
5: {"time": 70.1},
|
| 144 |
+
6: {"time": 80.6},
|
| 145 |
+
7: {"time": 90.7},
|
| 146 |
+
8: {"time": 90.7},
|
| 147 |
+
9: {"time": 80.6},
|
| 148 |
+
10: {"time": 74.2},
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
nx.set_node_attributes(G, node_attrs)
|
| 152 |
+
|
| 153 |
+
assert nx.cd_index(G, 4, time_delta=50) == 0.17
|
| 154 |
+
|
| 155 |
+
def test_common_graph_with_weights(self):
|
| 156 |
+
G = nx.DiGraph()
|
| 157 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 158 |
+
G.add_edge(4, 2)
|
| 159 |
+
G.add_edge(4, 0)
|
| 160 |
+
G.add_edge(4, 1)
|
| 161 |
+
G.add_edge(4, 3)
|
| 162 |
+
G.add_edge(5, 2)
|
| 163 |
+
G.add_edge(6, 2)
|
| 164 |
+
G.add_edge(6, 4)
|
| 165 |
+
G.add_edge(7, 4)
|
| 166 |
+
G.add_edge(8, 4)
|
| 167 |
+
G.add_edge(9, 4)
|
| 168 |
+
G.add_edge(9, 1)
|
| 169 |
+
G.add_edge(9, 3)
|
| 170 |
+
G.add_edge(10, 4)
|
| 171 |
+
|
| 172 |
+
node_attrs = {
|
| 173 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 174 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 175 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 176 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 177 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 178 |
+
5: {"time": datetime(1997, 1, 1)},
|
| 179 |
+
6: {"time": datetime(1998, 1, 1), "weight": 5},
|
| 180 |
+
7: {"time": datetime(1999, 1, 1), "weight": 2},
|
| 181 |
+
8: {"time": datetime(1999, 1, 1), "weight": 6},
|
| 182 |
+
9: {"time": datetime(1998, 1, 1), "weight": 3},
|
| 183 |
+
10: {"time": datetime(1997, 4, 1), "weight": 10},
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
nx.set_node_attributes(G, node_attrs)
|
| 187 |
+
assert nx.cd_index(G, 4, time_delta=_delta, weight="weight") == 0.04
|
| 188 |
+
|
| 189 |
+
def test_node_with_no_predecessors(self):
|
| 190 |
+
G = nx.DiGraph()
|
| 191 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 192 |
+
G.add_edge(4, 2)
|
| 193 |
+
G.add_edge(4, 0)
|
| 194 |
+
G.add_edge(4, 3)
|
| 195 |
+
G.add_edge(5, 2)
|
| 196 |
+
G.add_edge(6, 2)
|
| 197 |
+
G.add_edge(6, 4)
|
| 198 |
+
G.add_edge(7, 4)
|
| 199 |
+
G.add_edge(8, 4)
|
| 200 |
+
G.add_edge(9, 4)
|
| 201 |
+
G.add_edge(9, 1)
|
| 202 |
+
G.add_edge(9, 3)
|
| 203 |
+
G.add_edge(10, 4)
|
| 204 |
+
|
| 205 |
+
node_attrs = {
|
| 206 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 207 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 208 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 209 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 210 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 211 |
+
5: {"time": datetime(2005, 1, 1)},
|
| 212 |
+
6: {"time": datetime(2010, 1, 1)},
|
| 213 |
+
7: {"time": datetime(2001, 1, 1)},
|
| 214 |
+
8: {"time": datetime(2020, 1, 1)},
|
| 215 |
+
9: {"time": datetime(2017, 1, 1)},
|
| 216 |
+
10: {"time": datetime(2004, 4, 1)},
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
nx.set_node_attributes(G, node_attrs)
|
| 220 |
+
assert nx.cd_index(G, 4, time_delta=_delta) == 0.0
|
| 221 |
+
|
| 222 |
+
def test_node_with_no_successors(self):
|
| 223 |
+
G = nx.DiGraph()
|
| 224 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 225 |
+
G.add_edge(8, 2)
|
| 226 |
+
G.add_edge(6, 0)
|
| 227 |
+
G.add_edge(6, 3)
|
| 228 |
+
G.add_edge(5, 2)
|
| 229 |
+
G.add_edge(6, 2)
|
| 230 |
+
G.add_edge(6, 4)
|
| 231 |
+
G.add_edge(7, 4)
|
| 232 |
+
G.add_edge(8, 4)
|
| 233 |
+
G.add_edge(9, 4)
|
| 234 |
+
G.add_edge(9, 1)
|
| 235 |
+
G.add_edge(9, 3)
|
| 236 |
+
G.add_edge(10, 4)
|
| 237 |
+
|
| 238 |
+
node_attrs = {
|
| 239 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 240 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 241 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 242 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 243 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 244 |
+
5: {"time": datetime(1997, 1, 1)},
|
| 245 |
+
6: {"time": datetime(1998, 1, 1)},
|
| 246 |
+
7: {"time": datetime(1999, 1, 1)},
|
| 247 |
+
8: {"time": datetime(1999, 1, 1)},
|
| 248 |
+
9: {"time": datetime(1998, 1, 1)},
|
| 249 |
+
10: {"time": datetime(1997, 4, 1)},
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
nx.set_node_attributes(G, node_attrs)
|
| 253 |
+
assert nx.cd_index(G, 4, time_delta=_delta) == 1.0
|
| 254 |
+
|
| 255 |
+
def test_n_equals_zero(self):
|
| 256 |
+
G = nx.DiGraph()
|
| 257 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 258 |
+
G.add_edge(4, 2)
|
| 259 |
+
G.add_edge(4, 0)
|
| 260 |
+
G.add_edge(4, 3)
|
| 261 |
+
G.add_edge(6, 4)
|
| 262 |
+
G.add_edge(7, 4)
|
| 263 |
+
G.add_edge(8, 4)
|
| 264 |
+
G.add_edge(9, 4)
|
| 265 |
+
G.add_edge(9, 1)
|
| 266 |
+
G.add_edge(10, 4)
|
| 267 |
+
|
| 268 |
+
node_attrs = {
|
| 269 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 270 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 271 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 272 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 273 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 274 |
+
5: {"time": datetime(2005, 1, 1)},
|
| 275 |
+
6: {"time": datetime(2010, 1, 1)},
|
| 276 |
+
7: {"time": datetime(2001, 1, 1)},
|
| 277 |
+
8: {"time": datetime(2020, 1, 1)},
|
| 278 |
+
9: {"time": datetime(2017, 1, 1)},
|
| 279 |
+
10: {"time": datetime(2004, 4, 1)},
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
nx.set_node_attributes(G, node_attrs)
|
| 283 |
+
|
| 284 |
+
with pytest.raises(
|
| 285 |
+
nx.NetworkXError, match="The cd index cannot be defined."
|
| 286 |
+
) as ve:
|
| 287 |
+
nx.cd_index(G, 4, time_delta=_delta)
|
| 288 |
+
|
| 289 |
+
def test_time_timedelta_compatibility(self):
|
| 290 |
+
G = nx.DiGraph()
|
| 291 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 292 |
+
G.add_edge(4, 2)
|
| 293 |
+
G.add_edge(4, 0)
|
| 294 |
+
G.add_edge(4, 3)
|
| 295 |
+
G.add_edge(6, 4)
|
| 296 |
+
G.add_edge(7, 4)
|
| 297 |
+
G.add_edge(8, 4)
|
| 298 |
+
G.add_edge(9, 4)
|
| 299 |
+
G.add_edge(9, 1)
|
| 300 |
+
G.add_edge(10, 4)
|
| 301 |
+
|
| 302 |
+
node_attrs = {
|
| 303 |
+
0: {"time": 20.2},
|
| 304 |
+
1: {"time": 20.2},
|
| 305 |
+
2: {"time": 30.7},
|
| 306 |
+
3: {"time": 30.7},
|
| 307 |
+
4: {"time": 50.9},
|
| 308 |
+
5: {"time": 70.1},
|
| 309 |
+
6: {"time": 80.6},
|
| 310 |
+
7: {"time": 90.7},
|
| 311 |
+
8: {"time": 90.7},
|
| 312 |
+
9: {"time": 80.6},
|
| 313 |
+
10: {"time": 74.2},
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
nx.set_node_attributes(G, node_attrs)
|
| 317 |
+
|
| 318 |
+
with pytest.raises(
|
| 319 |
+
nx.NetworkXError,
|
| 320 |
+
match="Addition and comparison are not supported between",
|
| 321 |
+
) as ve:
|
| 322 |
+
nx.cd_index(G, 4, time_delta=_delta)
|
| 323 |
+
|
| 324 |
+
def test_node_with_no_time(self):
|
| 325 |
+
G = nx.DiGraph()
|
| 326 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
| 327 |
+
G.add_edge(8, 2)
|
| 328 |
+
G.add_edge(6, 0)
|
| 329 |
+
G.add_edge(6, 3)
|
| 330 |
+
G.add_edge(5, 2)
|
| 331 |
+
G.add_edge(6, 2)
|
| 332 |
+
G.add_edge(6, 4)
|
| 333 |
+
G.add_edge(7, 4)
|
| 334 |
+
G.add_edge(8, 4)
|
| 335 |
+
G.add_edge(9, 4)
|
| 336 |
+
G.add_edge(9, 1)
|
| 337 |
+
G.add_edge(9, 3)
|
| 338 |
+
G.add_edge(10, 4)
|
| 339 |
+
|
| 340 |
+
node_attrs = {
|
| 341 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 342 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 343 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 344 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 345 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 346 |
+
6: {"time": datetime(1998, 1, 1)},
|
| 347 |
+
7: {"time": datetime(1999, 1, 1)},
|
| 348 |
+
8: {"time": datetime(1999, 1, 1)},
|
| 349 |
+
9: {"time": datetime(1998, 1, 1)},
|
| 350 |
+
10: {"time": datetime(1997, 4, 1)},
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
nx.set_node_attributes(G, node_attrs)
|
| 354 |
+
|
| 355 |
+
with pytest.raises(
|
| 356 |
+
nx.NetworkXError, match="Not all nodes have a 'time' attribute."
|
| 357 |
+
) as ve:
|
| 358 |
+
nx.cd_index(G, 4, time_delta=_delta)
|
| 359 |
+
|
| 360 |
+
def test_maximally_consolidating(self):
|
| 361 |
+
G = nx.DiGraph()
|
| 362 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
|
| 363 |
+
G.add_edge(5, 1)
|
| 364 |
+
G.add_edge(5, 2)
|
| 365 |
+
G.add_edge(5, 3)
|
| 366 |
+
G.add_edge(5, 4)
|
| 367 |
+
G.add_edge(6, 1)
|
| 368 |
+
G.add_edge(6, 5)
|
| 369 |
+
G.add_edge(7, 1)
|
| 370 |
+
G.add_edge(7, 5)
|
| 371 |
+
G.add_edge(8, 2)
|
| 372 |
+
G.add_edge(8, 5)
|
| 373 |
+
G.add_edge(9, 5)
|
| 374 |
+
G.add_edge(9, 3)
|
| 375 |
+
G.add_edge(10, 5)
|
| 376 |
+
G.add_edge(10, 3)
|
| 377 |
+
G.add_edge(10, 4)
|
| 378 |
+
G.add_edge(11, 5)
|
| 379 |
+
G.add_edge(11, 4)
|
| 380 |
+
|
| 381 |
+
node_attrs = {
|
| 382 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 383 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 384 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 385 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 386 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 387 |
+
5: {"time": datetime(1997, 1, 1)},
|
| 388 |
+
6: {"time": datetime(1998, 1, 1)},
|
| 389 |
+
7: {"time": datetime(1999, 1, 1)},
|
| 390 |
+
8: {"time": datetime(1999, 1, 1)},
|
| 391 |
+
9: {"time": datetime(1998, 1, 1)},
|
| 392 |
+
10: {"time": datetime(1997, 4, 1)},
|
| 393 |
+
11: {"time": datetime(1998, 5, 1)},
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
nx.set_node_attributes(G, node_attrs)
|
| 397 |
+
|
| 398 |
+
assert nx.cd_index(G, 5, time_delta=_delta) == -1
|
| 399 |
+
|
| 400 |
+
def test_maximally_destabilizing(self):
|
| 401 |
+
G = nx.DiGraph()
|
| 402 |
+
G.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
|
| 403 |
+
G.add_edge(5, 1)
|
| 404 |
+
G.add_edge(5, 2)
|
| 405 |
+
G.add_edge(5, 3)
|
| 406 |
+
G.add_edge(5, 4)
|
| 407 |
+
G.add_edge(6, 5)
|
| 408 |
+
G.add_edge(7, 5)
|
| 409 |
+
G.add_edge(8, 5)
|
| 410 |
+
G.add_edge(9, 5)
|
| 411 |
+
G.add_edge(10, 5)
|
| 412 |
+
G.add_edge(11, 5)
|
| 413 |
+
|
| 414 |
+
node_attrs = {
|
| 415 |
+
0: {"time": datetime(1992, 1, 1)},
|
| 416 |
+
1: {"time": datetime(1992, 1, 1)},
|
| 417 |
+
2: {"time": datetime(1993, 1, 1)},
|
| 418 |
+
3: {"time": datetime(1993, 1, 1)},
|
| 419 |
+
4: {"time": datetime(1995, 1, 1)},
|
| 420 |
+
5: {"time": datetime(1997, 1, 1)},
|
| 421 |
+
6: {"time": datetime(1998, 1, 1)},
|
| 422 |
+
7: {"time": datetime(1999, 1, 1)},
|
| 423 |
+
8: {"time": datetime(1999, 1, 1)},
|
| 424 |
+
9: {"time": datetime(1998, 1, 1)},
|
| 425 |
+
10: {"time": datetime(1997, 4, 1)},
|
| 426 |
+
11: {"time": datetime(1998, 5, 1)},
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
nx.set_node_attributes(G, node_attrs)
|
| 430 |
+
|
| 431 |
+
assert nx.cd_index(G, 5, time_delta=_delta) == 1
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_triads.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for the :mod:`networkx.algorithms.triads` module."""
|
| 2 |
+
|
| 3 |
+
import itertools
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
from random import sample
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
import networkx as nx
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_all_triplets_deprecated():
|
| 13 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
|
| 14 |
+
with pytest.deprecated_call():
|
| 15 |
+
nx.all_triplets(G)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_random_triad_deprecated():
|
| 19 |
+
G = nx.path_graph(3, create_using=nx.DiGraph)
|
| 20 |
+
with pytest.deprecated_call():
|
| 21 |
+
nx.random_triad(G)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_triadic_census():
|
| 25 |
+
"""Tests the triadic_census function."""
|
| 26 |
+
G = nx.DiGraph()
|
| 27 |
+
G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
|
| 28 |
+
expected = {
|
| 29 |
+
"030T": 2,
|
| 30 |
+
"120C": 1,
|
| 31 |
+
"210": 0,
|
| 32 |
+
"120U": 0,
|
| 33 |
+
"012": 9,
|
| 34 |
+
"102": 3,
|
| 35 |
+
"021U": 0,
|
| 36 |
+
"111U": 0,
|
| 37 |
+
"003": 8,
|
| 38 |
+
"030C": 0,
|
| 39 |
+
"021D": 9,
|
| 40 |
+
"201": 0,
|
| 41 |
+
"111D": 1,
|
| 42 |
+
"300": 0,
|
| 43 |
+
"120D": 0,
|
| 44 |
+
"021C": 2,
|
| 45 |
+
}
|
| 46 |
+
actual = nx.triadic_census(G)
|
| 47 |
+
assert expected == actual
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_is_triad():
|
| 51 |
+
"""Tests the is_triad function"""
|
| 52 |
+
G = nx.karate_club_graph()
|
| 53 |
+
G = G.to_directed()
|
| 54 |
+
for i in range(100):
|
| 55 |
+
nodes = sample(sorted(G.nodes()), 3)
|
| 56 |
+
G2 = G.subgraph(nodes)
|
| 57 |
+
assert nx.is_triad(G2)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_all_triplets():
|
| 61 |
+
"""Tests the all_triplets function."""
|
| 62 |
+
G = nx.DiGraph()
|
| 63 |
+
G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
|
| 64 |
+
expected = [
|
| 65 |
+
f"{i},{j},{k}"
|
| 66 |
+
for i in range(7)
|
| 67 |
+
for j in range(i + 1, 7)
|
| 68 |
+
for k in range(j + 1, 7)
|
| 69 |
+
]
|
| 70 |
+
expected = [set(x.split(",")) for x in expected]
|
| 71 |
+
actual = [set(x) for x in nx.all_triplets(G)]
|
| 72 |
+
assert all(any(s1 == s2 for s1 in expected) for s2 in actual)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_all_triads():
|
| 76 |
+
"""Tests the all_triplets function."""
|
| 77 |
+
G = nx.DiGraph()
|
| 78 |
+
G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
|
| 79 |
+
expected = [
|
| 80 |
+
f"{i},{j},{k}"
|
| 81 |
+
for i in range(7)
|
| 82 |
+
for j in range(i + 1, 7)
|
| 83 |
+
for k in range(j + 1, 7)
|
| 84 |
+
]
|
| 85 |
+
expected = [G.subgraph(x.split(",")) for x in expected]
|
| 86 |
+
actual = list(nx.all_triads(G))
|
| 87 |
+
assert all(any(nx.is_isomorphic(G1, G2) for G1 in expected) for G2 in actual)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_triad_type():
|
| 91 |
+
"""Tests the triad_type function."""
|
| 92 |
+
# 0 edges (1 type)
|
| 93 |
+
G = nx.DiGraph({0: [], 1: [], 2: []})
|
| 94 |
+
assert nx.triad_type(G) == "003"
|
| 95 |
+
# 1 edge (1 type)
|
| 96 |
+
G = nx.DiGraph({0: [1], 1: [], 2: []})
|
| 97 |
+
assert nx.triad_type(G) == "012"
|
| 98 |
+
# 2 edges (4 types)
|
| 99 |
+
G = nx.DiGraph([(0, 1), (0, 2)])
|
| 100 |
+
assert nx.triad_type(G) == "021D"
|
| 101 |
+
G = nx.DiGraph({0: [1], 1: [0], 2: []})
|
| 102 |
+
assert nx.triad_type(G) == "102"
|
| 103 |
+
G = nx.DiGraph([(0, 1), (2, 1)])
|
| 104 |
+
assert nx.triad_type(G) == "021U"
|
| 105 |
+
G = nx.DiGraph([(0, 1), (1, 2)])
|
| 106 |
+
assert nx.triad_type(G) == "021C"
|
| 107 |
+
# 3 edges (4 types)
|
| 108 |
+
G = nx.DiGraph([(0, 1), (1, 0), (2, 1)])
|
| 109 |
+
assert nx.triad_type(G) == "111D"
|
| 110 |
+
G = nx.DiGraph([(0, 1), (1, 0), (1, 2)])
|
| 111 |
+
assert nx.triad_type(G) == "111U"
|
| 112 |
+
G = nx.DiGraph([(0, 1), (1, 2), (0, 2)])
|
| 113 |
+
assert nx.triad_type(G) == "030T"
|
| 114 |
+
G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
|
| 115 |
+
assert nx.triad_type(G) == "030C"
|
| 116 |
+
# 4 edges (4 types)
|
| 117 |
+
G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (0, 2)])
|
| 118 |
+
assert nx.triad_type(G) == "201"
|
| 119 |
+
G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (2, 1)])
|
| 120 |
+
assert nx.triad_type(G) == "120D"
|
| 121 |
+
G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (1, 2)])
|
| 122 |
+
assert nx.triad_type(G) == "120U"
|
| 123 |
+
G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (2, 1)])
|
| 124 |
+
assert nx.triad_type(G) == "120C"
|
| 125 |
+
# 5 edges (1 type)
|
| 126 |
+
G = nx.DiGraph([(0, 1), (1, 0), (2, 1), (1, 2), (0, 2)])
|
| 127 |
+
assert nx.triad_type(G) == "210"
|
| 128 |
+
# 6 edges (1 type)
|
| 129 |
+
G = nx.DiGraph([(0, 1), (1, 0), (1, 2), (2, 1), (0, 2), (2, 0)])
|
| 130 |
+
assert nx.triad_type(G) == "300"
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def test_triads_by_type():
|
| 134 |
+
"""Tests the all_triplets function."""
|
| 135 |
+
G = nx.DiGraph()
|
| 136 |
+
G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
|
| 137 |
+
all_triads = nx.all_triads(G)
|
| 138 |
+
expected = defaultdict(list)
|
| 139 |
+
for triad in all_triads:
|
| 140 |
+
name = nx.triad_type(triad)
|
| 141 |
+
expected[name].append(triad)
|
| 142 |
+
actual = nx.triads_by_type(G)
|
| 143 |
+
assert set(actual.keys()) == set(expected.keys())
|
| 144 |
+
for tri_type, actual_Gs in actual.items():
|
| 145 |
+
expected_Gs = expected[tri_type]
|
| 146 |
+
for a in actual_Gs:
|
| 147 |
+
assert any(nx.is_isomorphic(a, e) for e in expected_Gs)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def test_random_triad():
|
| 151 |
+
"""Tests the random_triad function"""
|
| 152 |
+
G = nx.karate_club_graph()
|
| 153 |
+
G = G.to_directed()
|
| 154 |
+
for i in range(100):
|
| 155 |
+
assert nx.is_triad(nx.random_triad(G))
|
| 156 |
+
|
| 157 |
+
G = nx.DiGraph()
|
| 158 |
+
msg = "at least 3 nodes to form a triad"
|
| 159 |
+
with pytest.raises(nx.NetworkXError, match=msg):
|
| 160 |
+
nx.random_triad(G)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def test_triadic_census_short_path_nodelist():
|
| 164 |
+
G = nx.path_graph("abc", create_using=nx.DiGraph)
|
| 165 |
+
expected = {"021C": 1}
|
| 166 |
+
for nl in ["a", "b", "c", "ab", "ac", "bc", "abc"]:
|
| 167 |
+
triad_census = nx.triadic_census(G, nodelist=nl)
|
| 168 |
+
assert expected == {typ: cnt for typ, cnt in triad_census.items() if cnt > 0}
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def test_triadic_census_correct_nodelist_values():
|
| 172 |
+
G = nx.path_graph(5, create_using=nx.DiGraph)
|
| 173 |
+
msg = r"nodelist includes duplicate nodes or nodes not in G"
|
| 174 |
+
with pytest.raises(ValueError, match=msg):
|
| 175 |
+
nx.triadic_census(G, [1, 2, 2, 3])
|
| 176 |
+
with pytest.raises(ValueError, match=msg):
|
| 177 |
+
nx.triadic_census(G, [1, 2, "a", 3])
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def test_triadic_census_tiny_graphs():
|
| 181 |
+
tc = nx.triadic_census(nx.empty_graph(0, create_using=nx.DiGraph))
|
| 182 |
+
assert {} == {typ: cnt for typ, cnt in tc.items() if cnt > 0}
|
| 183 |
+
tc = nx.triadic_census(nx.empty_graph(1, create_using=nx.DiGraph))
|
| 184 |
+
assert {} == {typ: cnt for typ, cnt in tc.items() if cnt > 0}
|
| 185 |
+
tc = nx.triadic_census(nx.empty_graph(2, create_using=nx.DiGraph))
|
| 186 |
+
assert {} == {typ: cnt for typ, cnt in tc.items() if cnt > 0}
|
| 187 |
+
tc = nx.triadic_census(nx.DiGraph([(1, 2)]))
|
| 188 |
+
assert {} == {typ: cnt for typ, cnt in tc.items() if cnt > 0}
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def test_triadic_census_selfloops():
|
| 192 |
+
GG = nx.path_graph("abc", create_using=nx.DiGraph)
|
| 193 |
+
expected = {"021C": 1}
|
| 194 |
+
for n in GG:
|
| 195 |
+
G = GG.copy()
|
| 196 |
+
G.add_edge(n, n)
|
| 197 |
+
tc = nx.triadic_census(G)
|
| 198 |
+
assert expected == {typ: cnt for typ, cnt in tc.items() if cnt > 0}
|
| 199 |
+
|
| 200 |
+
GG = nx.path_graph("abcde", create_using=nx.DiGraph)
|
| 201 |
+
tbt = nx.triads_by_type(GG)
|
| 202 |
+
for n in GG:
|
| 203 |
+
GG.add_edge(n, n)
|
| 204 |
+
tc = nx.triadic_census(GG)
|
| 205 |
+
assert tc == {tt: len(tbt[tt]) for tt in tc}
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def test_triadic_census_four_path():
|
| 209 |
+
G = nx.path_graph("abcd", create_using=nx.DiGraph)
|
| 210 |
+
expected = {"012": 2, "021C": 2}
|
| 211 |
+
triad_census = nx.triadic_census(G)
|
| 212 |
+
assert expected == {typ: cnt for typ, cnt in triad_census.items() if cnt > 0}
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def test_triadic_census_four_path_nodelist():
|
| 216 |
+
G = nx.path_graph("abcd", create_using=nx.DiGraph)
|
| 217 |
+
expected_end = {"012": 2, "021C": 1}
|
| 218 |
+
expected_mid = {"012": 1, "021C": 2}
|
| 219 |
+
a_triad_census = nx.triadic_census(G, nodelist=["a"])
|
| 220 |
+
assert expected_end == {typ: cnt for typ, cnt in a_triad_census.items() if cnt > 0}
|
| 221 |
+
b_triad_census = nx.triadic_census(G, nodelist=["b"])
|
| 222 |
+
assert expected_mid == {typ: cnt for typ, cnt in b_triad_census.items() if cnt > 0}
|
| 223 |
+
c_triad_census = nx.triadic_census(G, nodelist=["c"])
|
| 224 |
+
assert expected_mid == {typ: cnt for typ, cnt in c_triad_census.items() if cnt > 0}
|
| 225 |
+
d_triad_census = nx.triadic_census(G, nodelist=["d"])
|
| 226 |
+
assert expected_end == {typ: cnt for typ, cnt in d_triad_census.items() if cnt > 0}
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def test_triadic_census_nodelist():
|
| 230 |
+
"""Tests the triadic_census function."""
|
| 231 |
+
G = nx.DiGraph()
|
| 232 |
+
G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
|
| 233 |
+
expected = {
|
| 234 |
+
"030T": 2,
|
| 235 |
+
"120C": 1,
|
| 236 |
+
"210": 0,
|
| 237 |
+
"120U": 0,
|
| 238 |
+
"012": 9,
|
| 239 |
+
"102": 3,
|
| 240 |
+
"021U": 0,
|
| 241 |
+
"111U": 0,
|
| 242 |
+
"003": 8,
|
| 243 |
+
"030C": 0,
|
| 244 |
+
"021D": 9,
|
| 245 |
+
"201": 0,
|
| 246 |
+
"111D": 1,
|
| 247 |
+
"300": 0,
|
| 248 |
+
"120D": 0,
|
| 249 |
+
"021C": 2,
|
| 250 |
+
}
|
| 251 |
+
actual = {k: 0 for k in expected}
|
| 252 |
+
for node in G.nodes():
|
| 253 |
+
node_triad_census = nx.triadic_census(G, nodelist=[node])
|
| 254 |
+
for triad_key in expected:
|
| 255 |
+
actual[triad_key] += node_triad_census[triad_key]
|
| 256 |
+
# Divide all counts by 3
|
| 257 |
+
for k, v in actual.items():
|
| 258 |
+
actual[k] //= 3
|
| 259 |
+
assert expected == actual
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
@pytest.mark.parametrize("N", [5, 10])
|
| 263 |
+
def test_triadic_census_on_random_graph(N):
|
| 264 |
+
G = nx.binomial_graph(N, 0.3, directed=True, seed=42)
|
| 265 |
+
tc1 = nx.triadic_census(G)
|
| 266 |
+
tbt = nx.triads_by_type(G)
|
| 267 |
+
tc2 = {tt: len(tbt[tt]) for tt in tc1}
|
| 268 |
+
assert tc1 == tc2
|
| 269 |
+
|
| 270 |
+
for n in G:
|
| 271 |
+
tc1 = nx.triadic_census(G, nodelist={n})
|
| 272 |
+
tc2 = {tt: sum(1 for t in tbt.get(tt, []) if n in t) for tt in tc1}
|
| 273 |
+
assert tc1 == tc2
|
| 274 |
+
|
| 275 |
+
for ns in itertools.combinations(G, 2):
|
| 276 |
+
ns = set(ns)
|
| 277 |
+
tc1 = nx.triadic_census(G, nodelist=ns)
|
| 278 |
+
tc2 = {
|
| 279 |
+
tt: sum(1 for t in tbt.get(tt, []) if any(n in ns for n in t)) for tt in tc1
|
| 280 |
+
}
|
| 281 |
+
assert tc1 == tc2
|
| 282 |
+
|
| 283 |
+
for ns in itertools.combinations(G, 3):
|
| 284 |
+
ns = set(ns)
|
| 285 |
+
tc1 = nx.triadic_census(G, nodelist=ns)
|
| 286 |
+
tc2 = {
|
| 287 |
+
tt: sum(1 for t in tbt.get(tt, []) if any(n in ns for n in t)) for tt in tc1
|
| 288 |
+
}
|
| 289 |
+
assert tc1 == tc2
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_vitality.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class TestClosenessVitality:
|
| 5 |
+
def test_unweighted(self):
|
| 6 |
+
G = nx.cycle_graph(3)
|
| 7 |
+
vitality = nx.closeness_vitality(G)
|
| 8 |
+
assert vitality == {0: 2, 1: 2, 2: 2}
|
| 9 |
+
|
| 10 |
+
def test_weighted(self):
|
| 11 |
+
G = nx.Graph()
|
| 12 |
+
nx.add_cycle(G, [0, 1, 2], weight=2)
|
| 13 |
+
vitality = nx.closeness_vitality(G, weight="weight")
|
| 14 |
+
assert vitality == {0: 4, 1: 4, 2: 4}
|
| 15 |
+
|
| 16 |
+
def test_unweighted_digraph(self):
|
| 17 |
+
G = nx.DiGraph(nx.cycle_graph(3))
|
| 18 |
+
vitality = nx.closeness_vitality(G)
|
| 19 |
+
assert vitality == {0: 4, 1: 4, 2: 4}
|
| 20 |
+
|
| 21 |
+
def test_weighted_digraph(self):
|
| 22 |
+
G = nx.DiGraph()
|
| 23 |
+
nx.add_cycle(G, [0, 1, 2], weight=2)
|
| 24 |
+
nx.add_cycle(G, [2, 1, 0], weight=2)
|
| 25 |
+
vitality = nx.closeness_vitality(G, weight="weight")
|
| 26 |
+
assert vitality == {0: 8, 1: 8, 2: 8}
|
| 27 |
+
|
| 28 |
+
def test_weighted_multidigraph(self):
|
| 29 |
+
G = nx.MultiDiGraph()
|
| 30 |
+
nx.add_cycle(G, [0, 1, 2], weight=2)
|
| 31 |
+
nx.add_cycle(G, [2, 1, 0], weight=2)
|
| 32 |
+
vitality = nx.closeness_vitality(G, weight="weight")
|
| 33 |
+
assert vitality == {0: 8, 1: 8, 2: 8}
|
| 34 |
+
|
| 35 |
+
def test_disconnecting_graph(self):
|
| 36 |
+
"""Tests that the closeness vitality of a node whose removal
|
| 37 |
+
disconnects the graph is negative infinity.
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
G = nx.path_graph(3)
|
| 41 |
+
assert nx.closeness_vitality(G, node=1) == -float("inf")
|
valley/lib/python3.10/site-packages/networkx/algorithms/tests/test_voronoi.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
from networkx.utils import pairwise
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class TestVoronoiCells:
|
| 6 |
+
"""Unit tests for the Voronoi cells function."""
|
| 7 |
+
|
| 8 |
+
def test_isolates(self):
|
| 9 |
+
"""Tests that a graph with isolated nodes has all isolates in
|
| 10 |
+
one block of the partition.
|
| 11 |
+
|
| 12 |
+
"""
|
| 13 |
+
G = nx.empty_graph(5)
|
| 14 |
+
cells = nx.voronoi_cells(G, {0, 2, 4})
|
| 15 |
+
expected = {0: {0}, 2: {2}, 4: {4}, "unreachable": {1, 3}}
|
| 16 |
+
assert expected == cells
|
| 17 |
+
|
| 18 |
+
def test_undirected_unweighted(self):
|
| 19 |
+
G = nx.cycle_graph(6)
|
| 20 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 21 |
+
expected = {0: {0, 1, 5}, 3: {2, 3, 4}}
|
| 22 |
+
assert expected == cells
|
| 23 |
+
|
| 24 |
+
def test_directed_unweighted(self):
|
| 25 |
+
# This is the singly-linked directed cycle graph on six nodes.
|
| 26 |
+
G = nx.DiGraph(pairwise(range(6), cyclic=True))
|
| 27 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 28 |
+
expected = {0: {0, 1, 2}, 3: {3, 4, 5}}
|
| 29 |
+
assert expected == cells
|
| 30 |
+
|
| 31 |
+
def test_directed_inward(self):
|
| 32 |
+
"""Tests that reversing the graph gives the "inward" Voronoi
|
| 33 |
+
partition.
|
| 34 |
+
|
| 35 |
+
"""
|
| 36 |
+
# This is the singly-linked reverse directed cycle graph on six nodes.
|
| 37 |
+
G = nx.DiGraph(pairwise(range(6), cyclic=True))
|
| 38 |
+
G = G.reverse(copy=False)
|
| 39 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 40 |
+
expected = {0: {0, 4, 5}, 3: {1, 2, 3}}
|
| 41 |
+
assert expected == cells
|
| 42 |
+
|
| 43 |
+
def test_undirected_weighted(self):
|
| 44 |
+
edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1)]
|
| 45 |
+
G = nx.Graph()
|
| 46 |
+
G.add_weighted_edges_from(edges)
|
| 47 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 48 |
+
expected = {0: {0}, 3: {1, 2, 3}}
|
| 49 |
+
assert expected == cells
|
| 50 |
+
|
| 51 |
+
def test_directed_weighted(self):
|
| 52 |
+
edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1), (3, 2, 1), (2, 1, 1)]
|
| 53 |
+
G = nx.DiGraph()
|
| 54 |
+
G.add_weighted_edges_from(edges)
|
| 55 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 56 |
+
expected = {0: {0}, 3: {1, 2, 3}}
|
| 57 |
+
assert expected == cells
|
| 58 |
+
|
| 59 |
+
def test_multigraph_unweighted(self):
|
| 60 |
+
"""Tests that the Voronoi cells for a multigraph are the same as
|
| 61 |
+
for a simple graph.
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
edges = [(0, 1), (1, 2), (2, 3)]
|
| 65 |
+
G = nx.MultiGraph(2 * edges)
|
| 66 |
+
H = nx.Graph(G)
|
| 67 |
+
G_cells = nx.voronoi_cells(G, {0, 3})
|
| 68 |
+
H_cells = nx.voronoi_cells(H, {0, 3})
|
| 69 |
+
assert G_cells == H_cells
|
| 70 |
+
|
| 71 |
+
def test_multidigraph_unweighted(self):
|
| 72 |
+
# This is the twice-singly-linked directed cycle graph on six nodes.
|
| 73 |
+
edges = list(pairwise(range(6), cyclic=True))
|
| 74 |
+
G = nx.MultiDiGraph(2 * edges)
|
| 75 |
+
H = nx.DiGraph(G)
|
| 76 |
+
G_cells = nx.voronoi_cells(G, {0, 3})
|
| 77 |
+
H_cells = nx.voronoi_cells(H, {0, 3})
|
| 78 |
+
assert G_cells == H_cells
|
| 79 |
+
|
| 80 |
+
def test_multigraph_weighted(self):
|
| 81 |
+
edges = [(0, 1, 10), (0, 1, 10), (1, 2, 1), (1, 2, 100), (2, 3, 1), (2, 3, 100)]
|
| 82 |
+
G = nx.MultiGraph()
|
| 83 |
+
G.add_weighted_edges_from(edges)
|
| 84 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 85 |
+
expected = {0: {0}, 3: {1, 2, 3}}
|
| 86 |
+
assert expected == cells
|
| 87 |
+
|
| 88 |
+
def test_multidigraph_weighted(self):
|
| 89 |
+
edges = [
|
| 90 |
+
(0, 1, 10),
|
| 91 |
+
(0, 1, 10),
|
| 92 |
+
(1, 2, 1),
|
| 93 |
+
(2, 3, 1),
|
| 94 |
+
(3, 2, 10),
|
| 95 |
+
(3, 2, 1),
|
| 96 |
+
(2, 1, 10),
|
| 97 |
+
(2, 1, 1),
|
| 98 |
+
]
|
| 99 |
+
G = nx.MultiDiGraph()
|
| 100 |
+
G.add_weighted_edges_from(edges)
|
| 101 |
+
cells = nx.voronoi_cells(G, {0, 3})
|
| 102 |
+
expected = {0: {0}, 3: {1, 2, 3}}
|
| 103 |
+
assert expected == cells
|
wemm/lib/python3.10/lib2to3/Grammar.txt
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Grammar for 2to3. This grammar supports Python 2.x and 3.x.
|
| 2 |
+
|
| 3 |
+
# NOTE WELL: You should also follow all the steps listed at
|
| 4 |
+
# https://devguide.python.org/grammar/
|
| 5 |
+
|
| 6 |
+
# Start symbols for the grammar:
|
| 7 |
+
# file_input is a module or sequence of commands read from an input file;
|
| 8 |
+
# single_input is a single interactive statement;
|
| 9 |
+
# eval_input is the input for the eval() and input() functions.
|
| 10 |
+
# NB: compound_stmt in single_input is followed by extra NEWLINE!
|
| 11 |
+
file_input: (NEWLINE | stmt)* ENDMARKER
|
| 12 |
+
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
|
| 13 |
+
eval_input: testlist NEWLINE* ENDMARKER
|
| 14 |
+
|
| 15 |
+
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
|
| 16 |
+
decorators: decorator+
|
| 17 |
+
decorated: decorators (classdef | funcdef | async_funcdef)
|
| 18 |
+
async_funcdef: ASYNC funcdef
|
| 19 |
+
funcdef: 'def' NAME parameters ['->' test] ':' suite
|
| 20 |
+
parameters: '(' [typedargslist] ')'
|
| 21 |
+
|
| 22 |
+
# The following definition for typedarglist is equivalent to this set of rules:
|
| 23 |
+
#
|
| 24 |
+
# arguments = argument (',' argument)*
|
| 25 |
+
# argument = tfpdef ['=' test]
|
| 26 |
+
# kwargs = '**' tname [',']
|
| 27 |
+
# args = '*' [tname]
|
| 28 |
+
# kwonly_kwargs = (',' argument)* [',' [kwargs]]
|
| 29 |
+
# args_kwonly_kwargs = args kwonly_kwargs | kwargs
|
| 30 |
+
# poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
|
| 31 |
+
# typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
|
| 32 |
+
# typedarglist = arguments ',' '/' [',' [typedargslist_no_posonly]])|(typedargslist_no_posonly)"
|
| 33 |
+
#
|
| 34 |
+
# It needs to be fully expanded to allow our LL(1) parser to work on it.
|
| 35 |
+
|
| 36 |
+
typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [
|
| 37 |
+
',' [((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])*
|
| 38 |
+
[',' ['**' tname [',']]] | '**' tname [','])
|
| 39 |
+
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])]
|
| 40 |
+
] | ((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])*
|
| 41 |
+
[',' ['**' tname [',']]] | '**' tname [','])
|
| 42 |
+
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
|
| 43 |
+
|
| 44 |
+
tname: NAME [':' test]
|
| 45 |
+
tfpdef: tname | '(' tfplist ')'
|
| 46 |
+
tfplist: tfpdef (',' tfpdef)* [',']
|
| 47 |
+
|
| 48 |
+
# The following definition for varargslist is equivalent to this set of rules:
|
| 49 |
+
#
|
| 50 |
+
# arguments = argument (',' argument )*
|
| 51 |
+
# argument = vfpdef ['=' test]
|
| 52 |
+
# kwargs = '**' vname [',']
|
| 53 |
+
# args = '*' [vname]
|
| 54 |
+
# kwonly_kwargs = (',' argument )* [',' [kwargs]]
|
| 55 |
+
# args_kwonly_kwargs = args kwonly_kwargs | kwargs
|
| 56 |
+
# poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
|
| 57 |
+
# vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
|
| 58 |
+
# varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly)
|
| 59 |
+
#
|
| 60 |
+
# It needs to be fully expanded to allow our LL(1) parser to work on it.
|
| 61 |
+
|
| 62 |
+
varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [
|
| 63 |
+
((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])*
|
| 64 |
+
[',' ['**' vname [',']]] | '**' vname [','])
|
| 65 |
+
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
|
| 66 |
+
]] | ((vfpdef ['=' test] ',')*
|
| 67 |
+
('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]]| '**' vname [','])
|
| 68 |
+
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
|
| 69 |
+
|
| 70 |
+
vname: NAME
|
| 71 |
+
vfpdef: vname | '(' vfplist ')'
|
| 72 |
+
vfplist: vfpdef (',' vfpdef)* [',']
|
| 73 |
+
|
| 74 |
+
stmt: simple_stmt | compound_stmt
|
| 75 |
+
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
|
| 76 |
+
small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
|
| 77 |
+
import_stmt | global_stmt | exec_stmt | assert_stmt)
|
| 78 |
+
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
|
| 79 |
+
('=' (yield_expr|testlist_star_expr))*)
|
| 80 |
+
annassign: ':' test ['=' test]
|
| 81 |
+
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
|
| 82 |
+
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
|
| 83 |
+
'<<=' | '>>=' | '**=' | '//=')
|
| 84 |
+
# For normal and annotated assignments, additional restrictions enforced by the interpreter
|
| 85 |
+
print_stmt: 'print' ( [ test (',' test)* [','] ] |
|
| 86 |
+
'>>' test [ (',' test)+ [','] ] )
|
| 87 |
+
del_stmt: 'del' exprlist
|
| 88 |
+
pass_stmt: 'pass'
|
| 89 |
+
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
|
| 90 |
+
break_stmt: 'break'
|
| 91 |
+
continue_stmt: 'continue'
|
| 92 |
+
return_stmt: 'return' [testlist_star_expr]
|
| 93 |
+
yield_stmt: yield_expr
|
| 94 |
+
raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]
|
| 95 |
+
import_stmt: import_name | import_from
|
| 96 |
+
import_name: 'import' dotted_as_names
|
| 97 |
+
import_from: ('from' ('.'* dotted_name | '.'+)
|
| 98 |
+
'import' ('*' | '(' import_as_names ')' | import_as_names))
|
| 99 |
+
import_as_name: NAME ['as' NAME]
|
| 100 |
+
dotted_as_name: dotted_name ['as' NAME]
|
| 101 |
+
import_as_names: import_as_name (',' import_as_name)* [',']
|
| 102 |
+
dotted_as_names: dotted_as_name (',' dotted_as_name)*
|
| 103 |
+
dotted_name: NAME ('.' NAME)*
|
| 104 |
+
global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*
|
| 105 |
+
exec_stmt: 'exec' expr ['in' test [',' test]]
|
| 106 |
+
assert_stmt: 'assert' test [',' test]
|
| 107 |
+
|
| 108 |
+
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
|
| 109 |
+
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
|
| 110 |
+
if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
|
| 111 |
+
while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
|
| 112 |
+
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
|
| 113 |
+
try_stmt: ('try' ':' suite
|
| 114 |
+
((except_clause ':' suite)+
|
| 115 |
+
['else' ':' suite]
|
| 116 |
+
['finally' ':' suite] |
|
| 117 |
+
'finally' ':' suite))
|
| 118 |
+
with_stmt: 'with' with_item (',' with_item)* ':' suite
|
| 119 |
+
with_item: test ['as' expr]
|
| 120 |
+
with_var: 'as' expr
|
| 121 |
+
# NB compile.c makes sure that the default except clause is last
|
| 122 |
+
except_clause: 'except' [test [(',' | 'as') test]]
|
| 123 |
+
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
|
| 124 |
+
|
| 125 |
+
# Backward compatibility cruft to support:
|
| 126 |
+
# [ x for x in lambda: True, lambda: False if x() ]
|
| 127 |
+
# even while also allowing:
|
| 128 |
+
# lambda x: 5 if x else 2
|
| 129 |
+
# (But not a mix of the two)
|
| 130 |
+
testlist_safe: old_test [(',' old_test)+ [',']]
|
| 131 |
+
old_test: or_test | old_lambdef
|
| 132 |
+
old_lambdef: 'lambda' [varargslist] ':' old_test
|
| 133 |
+
|
| 134 |
+
namedexpr_test: test [':=' test]
|
| 135 |
+
test: or_test ['if' or_test 'else' test] | lambdef
|
| 136 |
+
or_test: and_test ('or' and_test)*
|
| 137 |
+
and_test: not_test ('and' not_test)*
|
| 138 |
+
not_test: 'not' not_test | comparison
|
| 139 |
+
comparison: expr (comp_op expr)*
|
| 140 |
+
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
|
| 141 |
+
star_expr: '*' expr
|
| 142 |
+
expr: xor_expr ('|' xor_expr)*
|
| 143 |
+
xor_expr: and_expr ('^' and_expr)*
|
| 144 |
+
and_expr: shift_expr ('&' shift_expr)*
|
| 145 |
+
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
|
| 146 |
+
arith_expr: term (('+'|'-') term)*
|
| 147 |
+
term: factor (('*'|'@'|'/'|'%'|'//') factor)*
|
| 148 |
+
factor: ('+'|'-'|'~') factor | power
|
| 149 |
+
power: [AWAIT] atom trailer* ['**' factor]
|
| 150 |
+
atom: ('(' [yield_expr|testlist_gexp] ')' |
|
| 151 |
+
'[' [listmaker] ']' |
|
| 152 |
+
'{' [dictsetmaker] '}' |
|
| 153 |
+
'`' testlist1 '`' |
|
| 154 |
+
NAME | NUMBER | STRING+ | '.' '.' '.')
|
| 155 |
+
listmaker: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
|
| 156 |
+
testlist_gexp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
|
| 157 |
+
lambdef: 'lambda' [varargslist] ':' test
|
| 158 |
+
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
| 159 |
+
subscriptlist: subscript (',' subscript)* [',']
|
| 160 |
+
subscript: test | [test] ':' [test] [sliceop]
|
| 161 |
+
sliceop: ':' [test]
|
| 162 |
+
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
|
| 163 |
+
testlist: test (',' test)* [',']
|
| 164 |
+
dictsetmaker: ( ((test ':' test | '**' expr)
|
| 165 |
+
(comp_for | (',' (test ':' test | '**' expr))* [','])) |
|
| 166 |
+
((test | star_expr)
|
| 167 |
+
(comp_for | (',' (test | star_expr))* [','])) )
|
| 168 |
+
|
| 169 |
+
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
| 170 |
+
|
| 171 |
+
arglist: argument (',' argument)* [',']
|
| 172 |
+
|
| 173 |
+
# "test '=' test" is really "keyword '=' test", but we have no such token.
|
| 174 |
+
# These need to be in a single rule to avoid grammar that is ambiguous
|
| 175 |
+
# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
|
| 176 |
+
# we explicitly match '*' here, too, to give it proper precedence.
|
| 177 |
+
# Illegal combinations and orderings are blocked in ast.c:
|
| 178 |
+
# multiple (test comp_for) arguments are blocked; keyword unpackings
|
| 179 |
+
# that precede iterable unpackings are blocked; etc.
|
| 180 |
+
argument: ( test [comp_for] |
|
| 181 |
+
test ':=' test |
|
| 182 |
+
test '=' test |
|
| 183 |
+
'**' test |
|
| 184 |
+
'*' test )
|
| 185 |
+
|
| 186 |
+
comp_iter: comp_for | comp_if
|
| 187 |
+
comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
|
| 188 |
+
comp_if: 'if' old_test [comp_iter]
|
| 189 |
+
|
| 190 |
+
testlist1: test (',' test)*
|
| 191 |
+
|
| 192 |
+
# not used in grammar, but may appear in "node" passed from Parser to Compiler
|
| 193 |
+
encoding_decl: NAME
|
| 194 |
+
|
| 195 |
+
yield_expr: 'yield' [yield_arg]
|
| 196 |
+
yield_arg: 'from' test | testlist_star_expr
|
wemm/lib/python3.10/lib2to3/__main__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from .main import main
|
| 3 |
+
|
| 4 |
+
sys.exit(main("lib2to3.fixes"))
|
wemm/lib/python3.10/lib2to3/btm_utils.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"Utility functions used by the btm_matcher module"
|
| 2 |
+
|
| 3 |
+
from . import pytree
|
| 4 |
+
from .pgen2 import grammar, token
|
| 5 |
+
from .pygram import pattern_symbols, python_symbols
|
| 6 |
+
|
| 7 |
+
syms = pattern_symbols
|
| 8 |
+
pysyms = python_symbols
|
| 9 |
+
tokens = grammar.opmap
|
| 10 |
+
token_labels = token
|
| 11 |
+
|
| 12 |
+
TYPE_ANY = -1
|
| 13 |
+
TYPE_ALTERNATIVES = -2
|
| 14 |
+
TYPE_GROUP = -3
|
| 15 |
+
|
| 16 |
+
class MinNode(object):
|
| 17 |
+
"""This class serves as an intermediate representation of the
|
| 18 |
+
pattern tree during the conversion to sets of leaf-to-root
|
| 19 |
+
subpatterns"""
|
| 20 |
+
|
| 21 |
+
def __init__(self, type=None, name=None):
|
| 22 |
+
self.type = type
|
| 23 |
+
self.name = name
|
| 24 |
+
self.children = []
|
| 25 |
+
self.leaf = False
|
| 26 |
+
self.parent = None
|
| 27 |
+
self.alternatives = []
|
| 28 |
+
self.group = []
|
| 29 |
+
|
| 30 |
+
def __repr__(self):
|
| 31 |
+
return str(self.type) + ' ' + str(self.name)
|
| 32 |
+
|
| 33 |
+
def leaf_to_root(self):
|
| 34 |
+
"""Internal method. Returns a characteristic path of the
|
| 35 |
+
pattern tree. This method must be run for all leaves until the
|
| 36 |
+
linear subpatterns are merged into a single"""
|
| 37 |
+
node = self
|
| 38 |
+
subp = []
|
| 39 |
+
while node:
|
| 40 |
+
if node.type == TYPE_ALTERNATIVES:
|
| 41 |
+
node.alternatives.append(subp)
|
| 42 |
+
if len(node.alternatives) == len(node.children):
|
| 43 |
+
#last alternative
|
| 44 |
+
subp = [tuple(node.alternatives)]
|
| 45 |
+
node.alternatives = []
|
| 46 |
+
node = node.parent
|
| 47 |
+
continue
|
| 48 |
+
else:
|
| 49 |
+
node = node.parent
|
| 50 |
+
subp = None
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
if node.type == TYPE_GROUP:
|
| 54 |
+
node.group.append(subp)
|
| 55 |
+
#probably should check the number of leaves
|
| 56 |
+
if len(node.group) == len(node.children):
|
| 57 |
+
subp = get_characteristic_subpattern(node.group)
|
| 58 |
+
node.group = []
|
| 59 |
+
node = node.parent
|
| 60 |
+
continue
|
| 61 |
+
else:
|
| 62 |
+
node = node.parent
|
| 63 |
+
subp = None
|
| 64 |
+
break
|
| 65 |
+
|
| 66 |
+
if node.type == token_labels.NAME and node.name:
|
| 67 |
+
#in case of type=name, use the name instead
|
| 68 |
+
subp.append(node.name)
|
| 69 |
+
else:
|
| 70 |
+
subp.append(node.type)
|
| 71 |
+
|
| 72 |
+
node = node.parent
|
| 73 |
+
return subp
|
| 74 |
+
|
| 75 |
+
def get_linear_subpattern(self):
|
| 76 |
+
"""Drives the leaf_to_root method. The reason that
|
| 77 |
+
leaf_to_root must be run multiple times is because we need to
|
| 78 |
+
reject 'group' matches; for example the alternative form
|
| 79 |
+
(a | b c) creates a group [b c] that needs to be matched. Since
|
| 80 |
+
matching multiple linear patterns overcomes the automaton's
|
| 81 |
+
capabilities, leaf_to_root merges each group into a single
|
| 82 |
+
choice based on 'characteristic'ity,
|
| 83 |
+
|
| 84 |
+
i.e. (a|b c) -> (a|b) if b more characteristic than c
|
| 85 |
+
|
| 86 |
+
Returns: The most 'characteristic'(as defined by
|
| 87 |
+
get_characteristic_subpattern) path for the compiled pattern
|
| 88 |
+
tree.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
for l in self.leaves():
|
| 92 |
+
subp = l.leaf_to_root()
|
| 93 |
+
if subp:
|
| 94 |
+
return subp
|
| 95 |
+
|
| 96 |
+
def leaves(self):
|
| 97 |
+
"Generator that returns the leaves of the tree"
|
| 98 |
+
for child in self.children:
|
| 99 |
+
yield from child.leaves()
|
| 100 |
+
if not self.children:
|
| 101 |
+
yield self
|
| 102 |
+
|
| 103 |
+
def reduce_tree(node, parent=None):
|
| 104 |
+
"""
|
| 105 |
+
Internal function. Reduces a compiled pattern tree to an
|
| 106 |
+
intermediate representation suitable for feeding the
|
| 107 |
+
automaton. This also trims off any optional pattern elements(like
|
| 108 |
+
[a], a*).
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
new_node = None
|
| 112 |
+
#switch on the node type
|
| 113 |
+
if node.type == syms.Matcher:
|
| 114 |
+
#skip
|
| 115 |
+
node = node.children[0]
|
| 116 |
+
|
| 117 |
+
if node.type == syms.Alternatives :
|
| 118 |
+
#2 cases
|
| 119 |
+
if len(node.children) <= 2:
|
| 120 |
+
#just a single 'Alternative', skip this node
|
| 121 |
+
new_node = reduce_tree(node.children[0], parent)
|
| 122 |
+
else:
|
| 123 |
+
#real alternatives
|
| 124 |
+
new_node = MinNode(type=TYPE_ALTERNATIVES)
|
| 125 |
+
#skip odd children('|' tokens)
|
| 126 |
+
for child in node.children:
|
| 127 |
+
if node.children.index(child)%2:
|
| 128 |
+
continue
|
| 129 |
+
reduced = reduce_tree(child, new_node)
|
| 130 |
+
if reduced is not None:
|
| 131 |
+
new_node.children.append(reduced)
|
| 132 |
+
elif node.type == syms.Alternative:
|
| 133 |
+
if len(node.children) > 1:
|
| 134 |
+
|
| 135 |
+
new_node = MinNode(type=TYPE_GROUP)
|
| 136 |
+
for child in node.children:
|
| 137 |
+
reduced = reduce_tree(child, new_node)
|
| 138 |
+
if reduced:
|
| 139 |
+
new_node.children.append(reduced)
|
| 140 |
+
if not new_node.children:
|
| 141 |
+
# delete the group if all of the children were reduced to None
|
| 142 |
+
new_node = None
|
| 143 |
+
|
| 144 |
+
else:
|
| 145 |
+
new_node = reduce_tree(node.children[0], parent)
|
| 146 |
+
|
| 147 |
+
elif node.type == syms.Unit:
|
| 148 |
+
if (isinstance(node.children[0], pytree.Leaf) and
|
| 149 |
+
node.children[0].value == '('):
|
| 150 |
+
#skip parentheses
|
| 151 |
+
return reduce_tree(node.children[1], parent)
|
| 152 |
+
if ((isinstance(node.children[0], pytree.Leaf) and
|
| 153 |
+
node.children[0].value == '[')
|
| 154 |
+
or
|
| 155 |
+
(len(node.children)>1 and
|
| 156 |
+
hasattr(node.children[1], "value") and
|
| 157 |
+
node.children[1].value == '[')):
|
| 158 |
+
#skip whole unit if its optional
|
| 159 |
+
return None
|
| 160 |
+
|
| 161 |
+
leaf = True
|
| 162 |
+
details_node = None
|
| 163 |
+
alternatives_node = None
|
| 164 |
+
has_repeater = False
|
| 165 |
+
repeater_node = None
|
| 166 |
+
has_variable_name = False
|
| 167 |
+
|
| 168 |
+
for child in node.children:
|
| 169 |
+
if child.type == syms.Details:
|
| 170 |
+
leaf = False
|
| 171 |
+
details_node = child
|
| 172 |
+
elif child.type == syms.Repeater:
|
| 173 |
+
has_repeater = True
|
| 174 |
+
repeater_node = child
|
| 175 |
+
elif child.type == syms.Alternatives:
|
| 176 |
+
alternatives_node = child
|
| 177 |
+
if hasattr(child, 'value') and child.value == '=': # variable name
|
| 178 |
+
has_variable_name = True
|
| 179 |
+
|
| 180 |
+
#skip variable name
|
| 181 |
+
if has_variable_name:
|
| 182 |
+
#skip variable name, '='
|
| 183 |
+
name_leaf = node.children[2]
|
| 184 |
+
if hasattr(name_leaf, 'value') and name_leaf.value == '(':
|
| 185 |
+
# skip parenthesis
|
| 186 |
+
name_leaf = node.children[3]
|
| 187 |
+
else:
|
| 188 |
+
name_leaf = node.children[0]
|
| 189 |
+
|
| 190 |
+
#set node type
|
| 191 |
+
if name_leaf.type == token_labels.NAME:
|
| 192 |
+
#(python) non-name or wildcard
|
| 193 |
+
if name_leaf.value == 'any':
|
| 194 |
+
new_node = MinNode(type=TYPE_ANY)
|
| 195 |
+
else:
|
| 196 |
+
if hasattr(token_labels, name_leaf.value):
|
| 197 |
+
new_node = MinNode(type=getattr(token_labels, name_leaf.value))
|
| 198 |
+
else:
|
| 199 |
+
new_node = MinNode(type=getattr(pysyms, name_leaf.value))
|
| 200 |
+
|
| 201 |
+
elif name_leaf.type == token_labels.STRING:
|
| 202 |
+
#(python) name or character; remove the apostrophes from
|
| 203 |
+
#the string value
|
| 204 |
+
name = name_leaf.value.strip("'")
|
| 205 |
+
if name in tokens:
|
| 206 |
+
new_node = MinNode(type=tokens[name])
|
| 207 |
+
else:
|
| 208 |
+
new_node = MinNode(type=token_labels.NAME, name=name)
|
| 209 |
+
elif name_leaf.type == syms.Alternatives:
|
| 210 |
+
new_node = reduce_tree(alternatives_node, parent)
|
| 211 |
+
|
| 212 |
+
#handle repeaters
|
| 213 |
+
if has_repeater:
|
| 214 |
+
if repeater_node.children[0].value == '*':
|
| 215 |
+
#reduce to None
|
| 216 |
+
new_node = None
|
| 217 |
+
elif repeater_node.children[0].value == '+':
|
| 218 |
+
#reduce to a single occurrence i.e. do nothing
|
| 219 |
+
pass
|
| 220 |
+
else:
|
| 221 |
+
#TODO: handle {min, max} repeaters
|
| 222 |
+
raise NotImplementedError
|
| 223 |
+
pass
|
| 224 |
+
|
| 225 |
+
#add children
|
| 226 |
+
if details_node and new_node is not None:
|
| 227 |
+
for child in details_node.children[1:-1]:
|
| 228 |
+
#skip '<', '>' markers
|
| 229 |
+
reduced = reduce_tree(child, new_node)
|
| 230 |
+
if reduced is not None:
|
| 231 |
+
new_node.children.append(reduced)
|
| 232 |
+
if new_node:
|
| 233 |
+
new_node.parent = parent
|
| 234 |
+
return new_node
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def get_characteristic_subpattern(subpatterns):
|
| 238 |
+
"""Picks the most characteristic from a list of linear patterns
|
| 239 |
+
Current order used is:
|
| 240 |
+
names > common_names > common_chars
|
| 241 |
+
"""
|
| 242 |
+
if not isinstance(subpatterns, list):
|
| 243 |
+
return subpatterns
|
| 244 |
+
if len(subpatterns)==1:
|
| 245 |
+
return subpatterns[0]
|
| 246 |
+
|
| 247 |
+
# first pick out the ones containing variable names
|
| 248 |
+
subpatterns_with_names = []
|
| 249 |
+
subpatterns_with_common_names = []
|
| 250 |
+
common_names = ['in', 'for', 'if' , 'not', 'None']
|
| 251 |
+
subpatterns_with_common_chars = []
|
| 252 |
+
common_chars = "[]().,:"
|
| 253 |
+
for subpattern in subpatterns:
|
| 254 |
+
if any(rec_test(subpattern, lambda x: type(x) is str)):
|
| 255 |
+
if any(rec_test(subpattern,
|
| 256 |
+
lambda x: isinstance(x, str) and x in common_chars)):
|
| 257 |
+
subpatterns_with_common_chars.append(subpattern)
|
| 258 |
+
elif any(rec_test(subpattern,
|
| 259 |
+
lambda x: isinstance(x, str) and x in common_names)):
|
| 260 |
+
subpatterns_with_common_names.append(subpattern)
|
| 261 |
+
|
| 262 |
+
else:
|
| 263 |
+
subpatterns_with_names.append(subpattern)
|
| 264 |
+
|
| 265 |
+
if subpatterns_with_names:
|
| 266 |
+
subpatterns = subpatterns_with_names
|
| 267 |
+
elif subpatterns_with_common_names:
|
| 268 |
+
subpatterns = subpatterns_with_common_names
|
| 269 |
+
elif subpatterns_with_common_chars:
|
| 270 |
+
subpatterns = subpatterns_with_common_chars
|
| 271 |
+
# of the remaining subpatterns pick out the longest one
|
| 272 |
+
return max(subpatterns, key=len)
|
| 273 |
+
|
| 274 |
+
def rec_test(sequence, test_func):
|
| 275 |
+
"""Tests test_func on all items of sequence and items of included
|
| 276 |
+
sub-iterables"""
|
| 277 |
+
for x in sequence:
|
| 278 |
+
if isinstance(x, (list, tuple)):
|
| 279 |
+
yield from rec_test(x, test_func)
|
| 280 |
+
else:
|
| 281 |
+
yield test_func(x)
|
wemm/lib/python3.10/lib2to3/fixer_base.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2006 Google, Inc. All Rights Reserved.
|
| 2 |
+
# Licensed to PSF under a Contributor Agreement.
|
| 3 |
+
|
| 4 |
+
"""Base class for fixers (optional, but recommended)."""
|
| 5 |
+
|
| 6 |
+
# Python imports
|
| 7 |
+
import itertools
|
| 8 |
+
|
| 9 |
+
# Local imports
|
| 10 |
+
from .patcomp import PatternCompiler
|
| 11 |
+
from . import pygram
|
| 12 |
+
from .fixer_util import does_tree_import
|
| 13 |
+
|
| 14 |
+
class BaseFix(object):
|
| 15 |
+
|
| 16 |
+
"""Optional base class for fixers.
|
| 17 |
+
|
| 18 |
+
The subclass name must be FixFooBar where FooBar is the result of
|
| 19 |
+
removing underscores and capitalizing the words of the fix name.
|
| 20 |
+
For example, the class name for a fixer named 'has_key' should be
|
| 21 |
+
FixHasKey.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
PATTERN = None # Most subclasses should override with a string literal
|
| 25 |
+
pattern = None # Compiled pattern, set by compile_pattern()
|
| 26 |
+
pattern_tree = None # Tree representation of the pattern
|
| 27 |
+
options = None # Options object passed to initializer
|
| 28 |
+
filename = None # The filename (set by set_filename)
|
| 29 |
+
numbers = itertools.count(1) # For new_name()
|
| 30 |
+
used_names = set() # A set of all used NAMEs
|
| 31 |
+
order = "post" # Does the fixer prefer pre- or post-order traversal
|
| 32 |
+
explicit = False # Is this ignored by refactor.py -f all?
|
| 33 |
+
run_order = 5 # Fixers will be sorted by run order before execution
|
| 34 |
+
# Lower numbers will be run first.
|
| 35 |
+
_accept_type = None # [Advanced and not public] This tells RefactoringTool
|
| 36 |
+
# which node type to accept when there's not a pattern.
|
| 37 |
+
|
| 38 |
+
keep_line_order = False # For the bottom matcher: match with the
|
| 39 |
+
# original line order
|
| 40 |
+
BM_compatible = False # Compatibility with the bottom matching
|
| 41 |
+
# module; every fixer should set this
|
| 42 |
+
# manually
|
| 43 |
+
|
| 44 |
+
# Shortcut for access to Python grammar symbols
|
| 45 |
+
syms = pygram.python_symbols
|
| 46 |
+
|
| 47 |
+
def __init__(self, options, log):
|
| 48 |
+
"""Initializer. Subclass may override.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
options: a dict containing the options passed to RefactoringTool
|
| 52 |
+
that could be used to customize the fixer through the command line.
|
| 53 |
+
log: a list to append warnings and other messages to.
|
| 54 |
+
"""
|
| 55 |
+
self.options = options
|
| 56 |
+
self.log = log
|
| 57 |
+
self.compile_pattern()
|
| 58 |
+
|
| 59 |
+
def compile_pattern(self):
|
| 60 |
+
"""Compiles self.PATTERN into self.pattern.
|
| 61 |
+
|
| 62 |
+
Subclass may override if it doesn't want to use
|
| 63 |
+
self.{pattern,PATTERN} in .match().
|
| 64 |
+
"""
|
| 65 |
+
if self.PATTERN is not None:
|
| 66 |
+
PC = PatternCompiler()
|
| 67 |
+
self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN,
|
| 68 |
+
with_tree=True)
|
| 69 |
+
|
| 70 |
+
def set_filename(self, filename):
|
| 71 |
+
"""Set the filename.
|
| 72 |
+
|
| 73 |
+
The main refactoring tool should call this.
|
| 74 |
+
"""
|
| 75 |
+
self.filename = filename
|
| 76 |
+
|
| 77 |
+
def match(self, node):
|
| 78 |
+
"""Returns match for a given parse tree node.
|
| 79 |
+
|
| 80 |
+
Should return a true or false object (not necessarily a bool).
|
| 81 |
+
It may return a non-empty dict of matching sub-nodes as
|
| 82 |
+
returned by a matching pattern.
|
| 83 |
+
|
| 84 |
+
Subclass may override.
|
| 85 |
+
"""
|
| 86 |
+
results = {"node": node}
|
| 87 |
+
return self.pattern.match(node, results) and results
|
| 88 |
+
|
| 89 |
+
def transform(self, node, results):
|
| 90 |
+
"""Returns the transformation for a given parse tree node.
|
| 91 |
+
|
| 92 |
+
Args:
|
| 93 |
+
node: the root of the parse tree that matched the fixer.
|
| 94 |
+
results: a dict mapping symbolic names to part of the match.
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
None, or a node that is a modified copy of the
|
| 98 |
+
argument node. The node argument may also be modified in-place to
|
| 99 |
+
effect the same change.
|
| 100 |
+
|
| 101 |
+
Subclass *must* override.
|
| 102 |
+
"""
|
| 103 |
+
raise NotImplementedError()
|
| 104 |
+
|
| 105 |
+
def new_name(self, template="xxx_todo_changeme"):
|
| 106 |
+
"""Return a string suitable for use as an identifier
|
| 107 |
+
|
| 108 |
+
The new name is guaranteed not to conflict with other identifiers.
|
| 109 |
+
"""
|
| 110 |
+
name = template
|
| 111 |
+
while name in self.used_names:
|
| 112 |
+
name = template + str(next(self.numbers))
|
| 113 |
+
self.used_names.add(name)
|
| 114 |
+
return name
|
| 115 |
+
|
| 116 |
+
def log_message(self, message):
|
| 117 |
+
if self.first_log:
|
| 118 |
+
self.first_log = False
|
| 119 |
+
self.log.append("### In file %s ###" % self.filename)
|
| 120 |
+
self.log.append(message)
|
| 121 |
+
|
| 122 |
+
def cannot_convert(self, node, reason=None):
|
| 123 |
+
"""Warn the user that a given chunk of code is not valid Python 3,
|
| 124 |
+
but that it cannot be converted automatically.
|
| 125 |
+
|
| 126 |
+
First argument is the top-level node for the code in question.
|
| 127 |
+
Optional second argument is why it can't be converted.
|
| 128 |
+
"""
|
| 129 |
+
lineno = node.get_lineno()
|
| 130 |
+
for_output = node.clone()
|
| 131 |
+
for_output.prefix = ""
|
| 132 |
+
msg = "Line %d: could not convert: %s"
|
| 133 |
+
self.log_message(msg % (lineno, for_output))
|
| 134 |
+
if reason:
|
| 135 |
+
self.log_message(reason)
|
| 136 |
+
|
| 137 |
+
def warning(self, node, reason):
|
| 138 |
+
"""Used for warning the user about possible uncertainty in the
|
| 139 |
+
translation.
|
| 140 |
+
|
| 141 |
+
First argument is the top-level node for the code in question.
|
| 142 |
+
Optional second argument is why it can't be converted.
|
| 143 |
+
"""
|
| 144 |
+
lineno = node.get_lineno()
|
| 145 |
+
self.log_message("Line %d: %s" % (lineno, reason))
|
| 146 |
+
|
| 147 |
+
def start_tree(self, tree, filename):
|
| 148 |
+
"""Some fixers need to maintain tree-wide state.
|
| 149 |
+
This method is called once, at the start of tree fix-up.
|
| 150 |
+
|
| 151 |
+
tree - the root node of the tree to be processed.
|
| 152 |
+
filename - the name of the file the tree came from.
|
| 153 |
+
"""
|
| 154 |
+
self.used_names = tree.used_names
|
| 155 |
+
self.set_filename(filename)
|
| 156 |
+
self.numbers = itertools.count(1)
|
| 157 |
+
self.first_log = True
|
| 158 |
+
|
| 159 |
+
def finish_tree(self, tree, filename):
|
| 160 |
+
"""Some fixers need to maintain tree-wide state.
|
| 161 |
+
This method is called once, at the conclusion of tree fix-up.
|
| 162 |
+
|
| 163 |
+
tree - the root node of the tree to be processed.
|
| 164 |
+
filename - the name of the file the tree came from.
|
| 165 |
+
"""
|
| 166 |
+
pass
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
class ConditionalFix(BaseFix):
|
| 170 |
+
""" Base class for fixers which not execute if an import is found. """
|
| 171 |
+
|
| 172 |
+
# This is the name of the import which, if found, will cause the test to be skipped
|
| 173 |
+
skip_on = None
|
| 174 |
+
|
| 175 |
+
def start_tree(self, *args):
|
| 176 |
+
super(ConditionalFix, self).start_tree(*args)
|
| 177 |
+
self._should_skip = None
|
| 178 |
+
|
| 179 |
+
def should_skip(self, node):
|
| 180 |
+
if self._should_skip is not None:
|
| 181 |
+
return self._should_skip
|
| 182 |
+
pkg = self.skip_on.split(".")
|
| 183 |
+
name = pkg[-1]
|
| 184 |
+
pkg = ".".join(pkg[:-1])
|
| 185 |
+
self._should_skip = does_tree_import(pkg, name, node)
|
| 186 |
+
return self._should_skip
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (392 Bytes). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_buffer.cpython-310.pyc
ADDED
|
Binary file (1.06 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_dict.cpython-310.pyc
ADDED
|
Binary file (3.28 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_exitfunc.cpython-310.pyc
ADDED
|
Binary file (2.56 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_filter.cpython-310.pyc
ADDED
|
Binary file (2.69 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_future.cpython-310.pyc
ADDED
|
Binary file (1.04 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_has_key.cpython-310.pyc
ADDED
|
Binary file (2.87 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_imports.cpython-310.pyc
ADDED
|
Binary file (4.88 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_imports2.cpython-310.pyc
ADDED
|
Binary file (539 Bytes). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_input.cpython-310.pyc
ADDED
|
Binary file (1.2 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_itertools.cpython-310.pyc
ADDED
|
Binary file (1.8 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_itertools_imports.cpython-310.pyc
ADDED
|
Binary file (1.56 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_metaclass.cpython-310.pyc
ADDED
|
Binary file (5.4 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_ne.cpython-310.pyc
ADDED
|
Binary file (804 Bytes). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_next.cpython-310.pyc
ADDED
|
Binary file (3.33 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_nonzero.cpython-310.pyc
ADDED
|
Binary file (1.18 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_operator.cpython-310.pyc
ADDED
|
Binary file (4.41 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_raw_input.cpython-310.pyc
ADDED
|
Binary file (1.05 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_reduce.cpython-310.pyc
ADDED
|
Binary file (1.12 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_renames.cpython-310.pyc
ADDED
|
Binary file (2.28 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_repr.cpython-310.pyc
ADDED
|
Binary file (1.1 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_set_literal.cpython-310.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_standarderror.cpython-310.pyc
ADDED
|
Binary file (972 Bytes). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_types.cpython-310.pyc
ADDED
|
Binary file (2.2 kB). View file
|
|
|
wemm/lib/python3.10/lib2to3/fixes/__pycache__/fix_unicode.cpython-310.pyc
ADDED
|
Binary file (1.81 kB). View file
|
|
|