Add files using upload-large-folder tool
Browse files- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/__init__.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/astar.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/dense.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/generic.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/unweighted.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/weighted.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_generic.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_unweighted.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_weighted.cpython-310.pyc +0 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_asteroidal.py +23 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_boundary.py +154 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_bridges.py +144 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_broadcasting.py +81 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_chains.py +140 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_chordal.py +129 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_clique.py +291 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_cluster.py +549 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_communicability.py +80 -0
- pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_core.py +266 -0
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (466 Bytes). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/astar.cpython-310.pyc
ADDED
|
Binary file (7.42 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/dense.cpython-310.pyc
ADDED
|
Binary file (7.72 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/generic.cpython-310.pyc
ADDED
|
Binary file (21.6 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/unweighted.cpython-310.pyc
ADDED
|
Binary file (13.1 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/__pycache__/weighted.cpython-310.pyc
ADDED
|
Binary file (74.6 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (197 Bytes). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_generic.cpython-310.pyc
ADDED
|
Binary file (15.5 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_unweighted.cpython-310.pyc
ADDED
|
Binary file (7.03 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/shortest_paths/tests/__pycache__/test_weighted.cpython-310.pyc
ADDED
|
Binary file (30 kB). View file
|
|
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_asteroidal.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_is_at_free():
|
| 5 |
+
is_at_free = nx.asteroidal.is_at_free
|
| 6 |
+
|
| 7 |
+
cycle = nx.cycle_graph(6)
|
| 8 |
+
assert not is_at_free(cycle)
|
| 9 |
+
|
| 10 |
+
path = nx.path_graph(6)
|
| 11 |
+
assert is_at_free(path)
|
| 12 |
+
|
| 13 |
+
small_graph = nx.complete_graph(2)
|
| 14 |
+
assert is_at_free(small_graph)
|
| 15 |
+
|
| 16 |
+
petersen = nx.petersen_graph()
|
| 17 |
+
assert not is_at_free(petersen)
|
| 18 |
+
|
| 19 |
+
clique = nx.complete_graph(6)
|
| 20 |
+
assert is_at_free(clique)
|
| 21 |
+
|
| 22 |
+
line_clique = nx.line_graph(clique)
|
| 23 |
+
assert not is_at_free(line_clique)
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_boundary.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the :mod:`networkx.algorithms.boundary` module."""
|
| 2 |
+
|
| 3 |
+
from itertools import combinations
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
| 9 |
+
from networkx.utils import edges_equal
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestNodeBoundary:
|
| 13 |
+
"""Unit tests for the :func:`~networkx.node_boundary` function."""
|
| 14 |
+
|
| 15 |
+
def test_null_graph(self):
|
| 16 |
+
"""Tests that the null graph has empty node boundaries."""
|
| 17 |
+
null = nx.null_graph()
|
| 18 |
+
assert nx.node_boundary(null, []) == set()
|
| 19 |
+
assert nx.node_boundary(null, [], []) == set()
|
| 20 |
+
assert nx.node_boundary(null, [1, 2, 3]) == set()
|
| 21 |
+
assert nx.node_boundary(null, [1, 2, 3], [4, 5, 6]) == set()
|
| 22 |
+
assert nx.node_boundary(null, [1, 2, 3], [3, 4, 5]) == set()
|
| 23 |
+
|
| 24 |
+
def test_path_graph(self):
|
| 25 |
+
P10 = cnlti(nx.path_graph(10), first_label=1)
|
| 26 |
+
assert nx.node_boundary(P10, []) == set()
|
| 27 |
+
assert nx.node_boundary(P10, [], []) == set()
|
| 28 |
+
assert nx.node_boundary(P10, [1, 2, 3]) == {4}
|
| 29 |
+
assert nx.node_boundary(P10, [4, 5, 6]) == {3, 7}
|
| 30 |
+
assert nx.node_boundary(P10, [3, 4, 5, 6, 7]) == {2, 8}
|
| 31 |
+
assert nx.node_boundary(P10, [8, 9, 10]) == {7}
|
| 32 |
+
assert nx.node_boundary(P10, [4, 5, 6], [9, 10]) == set()
|
| 33 |
+
|
| 34 |
+
def test_complete_graph(self):
|
| 35 |
+
K10 = cnlti(nx.complete_graph(10), first_label=1)
|
| 36 |
+
assert nx.node_boundary(K10, []) == set()
|
| 37 |
+
assert nx.node_boundary(K10, [], []) == set()
|
| 38 |
+
assert nx.node_boundary(K10, [1, 2, 3]) == {4, 5, 6, 7, 8, 9, 10}
|
| 39 |
+
assert nx.node_boundary(K10, [4, 5, 6]) == {1, 2, 3, 7, 8, 9, 10}
|
| 40 |
+
assert nx.node_boundary(K10, [3, 4, 5, 6, 7]) == {1, 2, 8, 9, 10}
|
| 41 |
+
assert nx.node_boundary(K10, [4, 5, 6], []) == set()
|
| 42 |
+
assert nx.node_boundary(K10, K10) == set()
|
| 43 |
+
assert nx.node_boundary(K10, [1, 2, 3], [3, 4, 5]) == {4, 5}
|
| 44 |
+
|
| 45 |
+
def test_petersen(self):
|
| 46 |
+
"""Check boundaries in the petersen graph
|
| 47 |
+
|
| 48 |
+
cheeger(G,k)=min(|bdy(S)|/|S| for |S|=k, 0<k<=|V(G)|/2)
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
def cheeger(G, k):
|
| 53 |
+
return min(len(nx.node_boundary(G, nn)) / k for nn in combinations(G, k))
|
| 54 |
+
|
| 55 |
+
P = nx.petersen_graph()
|
| 56 |
+
assert cheeger(P, 1) == pytest.approx(3.00, abs=1e-2)
|
| 57 |
+
assert cheeger(P, 2) == pytest.approx(2.00, abs=1e-2)
|
| 58 |
+
assert cheeger(P, 3) == pytest.approx(1.67, abs=1e-2)
|
| 59 |
+
assert cheeger(P, 4) == pytest.approx(1.00, abs=1e-2)
|
| 60 |
+
assert cheeger(P, 5) == pytest.approx(0.80, abs=1e-2)
|
| 61 |
+
|
| 62 |
+
def test_directed(self):
|
| 63 |
+
"""Tests the node boundary of a directed graph."""
|
| 64 |
+
G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)])
|
| 65 |
+
S = {0, 1}
|
| 66 |
+
boundary = nx.node_boundary(G, S)
|
| 67 |
+
expected = {2}
|
| 68 |
+
assert boundary == expected
|
| 69 |
+
|
| 70 |
+
def test_multigraph(self):
|
| 71 |
+
"""Tests the node boundary of a multigraph."""
|
| 72 |
+
G = nx.MultiGraph(list(nx.cycle_graph(5).edges()) * 2)
|
| 73 |
+
S = {0, 1}
|
| 74 |
+
boundary = nx.node_boundary(G, S)
|
| 75 |
+
expected = {2, 4}
|
| 76 |
+
assert boundary == expected
|
| 77 |
+
|
| 78 |
+
def test_multidigraph(self):
|
| 79 |
+
"""Tests the edge boundary of a multidigraph."""
|
| 80 |
+
edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
|
| 81 |
+
G = nx.MultiDiGraph(edges * 2)
|
| 82 |
+
S = {0, 1}
|
| 83 |
+
boundary = nx.node_boundary(G, S)
|
| 84 |
+
expected = {2}
|
| 85 |
+
assert boundary == expected
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class TestEdgeBoundary:
|
| 89 |
+
"""Unit tests for the :func:`~networkx.edge_boundary` function."""
|
| 90 |
+
|
| 91 |
+
def test_null_graph(self):
|
| 92 |
+
null = nx.null_graph()
|
| 93 |
+
assert list(nx.edge_boundary(null, [])) == []
|
| 94 |
+
assert list(nx.edge_boundary(null, [], [])) == []
|
| 95 |
+
assert list(nx.edge_boundary(null, [1, 2, 3])) == []
|
| 96 |
+
assert list(nx.edge_boundary(null, [1, 2, 3], [4, 5, 6])) == []
|
| 97 |
+
assert list(nx.edge_boundary(null, [1, 2, 3], [3, 4, 5])) == []
|
| 98 |
+
|
| 99 |
+
def test_path_graph(self):
|
| 100 |
+
P10 = cnlti(nx.path_graph(10), first_label=1)
|
| 101 |
+
assert list(nx.edge_boundary(P10, [])) == []
|
| 102 |
+
assert list(nx.edge_boundary(P10, [], [])) == []
|
| 103 |
+
assert list(nx.edge_boundary(P10, [1, 2, 3])) == [(3, 4)]
|
| 104 |
+
assert sorted(nx.edge_boundary(P10, [4, 5, 6])) == [(4, 3), (6, 7)]
|
| 105 |
+
assert sorted(nx.edge_boundary(P10, [3, 4, 5, 6, 7])) == [(3, 2), (7, 8)]
|
| 106 |
+
assert list(nx.edge_boundary(P10, [8, 9, 10])) == [(8, 7)]
|
| 107 |
+
assert sorted(nx.edge_boundary(P10, [4, 5, 6], [9, 10])) == []
|
| 108 |
+
assert list(nx.edge_boundary(P10, [1, 2, 3], [3, 4, 5])) == [(2, 3), (3, 4)]
|
| 109 |
+
|
| 110 |
+
def test_complete_graph(self):
|
| 111 |
+
K10 = cnlti(nx.complete_graph(10), first_label=1)
|
| 112 |
+
|
| 113 |
+
def ilen(iterable):
|
| 114 |
+
return sum(1 for i in iterable)
|
| 115 |
+
|
| 116 |
+
assert list(nx.edge_boundary(K10, [])) == []
|
| 117 |
+
assert list(nx.edge_boundary(K10, [], [])) == []
|
| 118 |
+
assert ilen(nx.edge_boundary(K10, [1, 2, 3])) == 21
|
| 119 |
+
assert ilen(nx.edge_boundary(K10, [4, 5, 6, 7])) == 24
|
| 120 |
+
assert ilen(nx.edge_boundary(K10, [3, 4, 5, 6, 7])) == 25
|
| 121 |
+
assert ilen(nx.edge_boundary(K10, [8, 9, 10])) == 21
|
| 122 |
+
assert edges_equal(
|
| 123 |
+
nx.edge_boundary(K10, [4, 5, 6], [9, 10]),
|
| 124 |
+
[(4, 9), (4, 10), (5, 9), (5, 10), (6, 9), (6, 10)],
|
| 125 |
+
)
|
| 126 |
+
assert edges_equal(
|
| 127 |
+
nx.edge_boundary(K10, [1, 2, 3], [3, 4, 5]),
|
| 128 |
+
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5)],
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
def test_directed(self):
|
| 132 |
+
"""Tests the edge boundary of a directed graph."""
|
| 133 |
+
G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)])
|
| 134 |
+
S = {0, 1}
|
| 135 |
+
boundary = list(nx.edge_boundary(G, S))
|
| 136 |
+
expected = [(1, 2)]
|
| 137 |
+
assert boundary == expected
|
| 138 |
+
|
| 139 |
+
def test_multigraph(self):
|
| 140 |
+
"""Tests the edge boundary of a multigraph."""
|
| 141 |
+
G = nx.MultiGraph(list(nx.cycle_graph(5).edges()) * 2)
|
| 142 |
+
S = {0, 1}
|
| 143 |
+
boundary = list(nx.edge_boundary(G, S))
|
| 144 |
+
expected = [(0, 4), (0, 4), (1, 2), (1, 2)]
|
| 145 |
+
assert boundary == expected
|
| 146 |
+
|
| 147 |
+
def test_multidigraph(self):
|
| 148 |
+
"""Tests the edge boundary of a multidigraph."""
|
| 149 |
+
edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
|
| 150 |
+
G = nx.MultiDiGraph(edges * 2)
|
| 151 |
+
S = {0, 1}
|
| 152 |
+
boundary = list(nx.edge_boundary(G, S))
|
| 153 |
+
expected = [(1, 2), (1, 2)]
|
| 154 |
+
assert boundary == expected
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_bridges.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for bridge-finding algorithms."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestBridges:
|
| 9 |
+
"""Unit tests for the bridge-finding function."""
|
| 10 |
+
|
| 11 |
+
def test_single_bridge(self):
|
| 12 |
+
edges = [
|
| 13 |
+
# DFS tree edges.
|
| 14 |
+
(1, 2),
|
| 15 |
+
(2, 3),
|
| 16 |
+
(3, 4),
|
| 17 |
+
(3, 5),
|
| 18 |
+
(5, 6),
|
| 19 |
+
(6, 7),
|
| 20 |
+
(7, 8),
|
| 21 |
+
(5, 9),
|
| 22 |
+
(9, 10),
|
| 23 |
+
# Nontree edges.
|
| 24 |
+
(1, 3),
|
| 25 |
+
(1, 4),
|
| 26 |
+
(2, 5),
|
| 27 |
+
(5, 10),
|
| 28 |
+
(6, 8),
|
| 29 |
+
]
|
| 30 |
+
G = nx.Graph(edges)
|
| 31 |
+
source = 1
|
| 32 |
+
bridges = list(nx.bridges(G, source))
|
| 33 |
+
assert bridges == [(5, 6)]
|
| 34 |
+
|
| 35 |
+
def test_barbell_graph(self):
|
| 36 |
+
# The (3, 0) barbell graph has two triangles joined by a single edge.
|
| 37 |
+
G = nx.barbell_graph(3, 0)
|
| 38 |
+
source = 0
|
| 39 |
+
bridges = list(nx.bridges(G, source))
|
| 40 |
+
assert bridges == [(2, 3)]
|
| 41 |
+
|
| 42 |
+
def test_multiedge_bridge(self):
|
| 43 |
+
edges = [
|
| 44 |
+
(0, 1),
|
| 45 |
+
(0, 2),
|
| 46 |
+
(1, 2),
|
| 47 |
+
(1, 2),
|
| 48 |
+
(2, 3),
|
| 49 |
+
(3, 4),
|
| 50 |
+
(3, 4),
|
| 51 |
+
]
|
| 52 |
+
G = nx.MultiGraph(edges)
|
| 53 |
+
assert list(nx.bridges(G)) == [(2, 3)]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class TestHasBridges:
|
| 57 |
+
"""Unit tests for the has bridges function."""
|
| 58 |
+
|
| 59 |
+
def test_single_bridge(self):
|
| 60 |
+
edges = [
|
| 61 |
+
# DFS tree edges.
|
| 62 |
+
(1, 2),
|
| 63 |
+
(2, 3),
|
| 64 |
+
(3, 4),
|
| 65 |
+
(3, 5),
|
| 66 |
+
(5, 6), # The only bridge edge
|
| 67 |
+
(6, 7),
|
| 68 |
+
(7, 8),
|
| 69 |
+
(5, 9),
|
| 70 |
+
(9, 10),
|
| 71 |
+
# Nontree edges.
|
| 72 |
+
(1, 3),
|
| 73 |
+
(1, 4),
|
| 74 |
+
(2, 5),
|
| 75 |
+
(5, 10),
|
| 76 |
+
(6, 8),
|
| 77 |
+
]
|
| 78 |
+
G = nx.Graph(edges)
|
| 79 |
+
assert nx.has_bridges(G) # Default root
|
| 80 |
+
assert nx.has_bridges(G, root=1) # arbitrary root in G
|
| 81 |
+
|
| 82 |
+
def test_has_bridges_raises_root_not_in_G(self):
|
| 83 |
+
G = nx.Graph()
|
| 84 |
+
G.add_nodes_from([1, 2, 3])
|
| 85 |
+
with pytest.raises(nx.NodeNotFound):
|
| 86 |
+
nx.has_bridges(G, root=6)
|
| 87 |
+
|
| 88 |
+
def test_multiedge_bridge(self):
|
| 89 |
+
edges = [
|
| 90 |
+
(0, 1),
|
| 91 |
+
(0, 2),
|
| 92 |
+
(1, 2),
|
| 93 |
+
(1, 2),
|
| 94 |
+
(2, 3),
|
| 95 |
+
(3, 4),
|
| 96 |
+
(3, 4),
|
| 97 |
+
]
|
| 98 |
+
G = nx.MultiGraph(edges)
|
| 99 |
+
assert nx.has_bridges(G)
|
| 100 |
+
# Make every edge a multiedge
|
| 101 |
+
G.add_edges_from([(0, 1), (0, 2), (2, 3)])
|
| 102 |
+
assert not nx.has_bridges(G)
|
| 103 |
+
|
| 104 |
+
def test_bridges_multiple_components(self):
|
| 105 |
+
G = nx.Graph()
|
| 106 |
+
nx.add_path(G, [0, 1, 2]) # One connected component
|
| 107 |
+
nx.add_path(G, [4, 5, 6]) # Another connected component
|
| 108 |
+
assert list(nx.bridges(G, root=4)) == [(4, 5), (5, 6)]
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class TestLocalBridges:
|
| 112 |
+
"""Unit tests for the local_bridge function."""
|
| 113 |
+
|
| 114 |
+
@classmethod
|
| 115 |
+
def setup_class(cls):
|
| 116 |
+
cls.BB = nx.barbell_graph(4, 0)
|
| 117 |
+
cls.square = nx.cycle_graph(4)
|
| 118 |
+
cls.tri = nx.cycle_graph(3)
|
| 119 |
+
|
| 120 |
+
def test_nospan(self):
|
| 121 |
+
expected = {(3, 4), (4, 3)}
|
| 122 |
+
assert next(nx.local_bridges(self.BB, with_span=False)) in expected
|
| 123 |
+
assert set(nx.local_bridges(self.square, with_span=False)) == self.square.edges
|
| 124 |
+
assert list(nx.local_bridges(self.tri, with_span=False)) == []
|
| 125 |
+
|
| 126 |
+
def test_no_weight(self):
|
| 127 |
+
inf = float("inf")
|
| 128 |
+
expected = {(3, 4, inf), (4, 3, inf)}
|
| 129 |
+
assert next(nx.local_bridges(self.BB)) in expected
|
| 130 |
+
expected = {(u, v, 3) for u, v in self.square.edges}
|
| 131 |
+
assert set(nx.local_bridges(self.square)) == expected
|
| 132 |
+
assert list(nx.local_bridges(self.tri)) == []
|
| 133 |
+
|
| 134 |
+
def test_weight(self):
|
| 135 |
+
inf = float("inf")
|
| 136 |
+
G = self.square.copy()
|
| 137 |
+
|
| 138 |
+
G.edges[1, 2]["weight"] = 2
|
| 139 |
+
expected = {(u, v, 5 - wt) for u, v, wt in G.edges(data="weight", default=1)}
|
| 140 |
+
assert set(nx.local_bridges(G, weight="weight")) == expected
|
| 141 |
+
|
| 142 |
+
expected = {(u, v, 6) for u, v in G.edges}
|
| 143 |
+
lb = nx.local_bridges(G, weight=lambda u, v, d: 2)
|
| 144 |
+
assert set(lb) == expected
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_broadcasting.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for the broadcasting module."""
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import networkx as nx
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_example_tree_broadcast():
|
| 8 |
+
"""
|
| 9 |
+
Test the BROADCAST algorithm on the example in the paper titled: "Information Dissemination in Trees"
|
| 10 |
+
"""
|
| 11 |
+
edge_list = [
|
| 12 |
+
(0, 1),
|
| 13 |
+
(1, 2),
|
| 14 |
+
(2, 7),
|
| 15 |
+
(3, 4),
|
| 16 |
+
(5, 4),
|
| 17 |
+
(4, 7),
|
| 18 |
+
(6, 7),
|
| 19 |
+
(7, 9),
|
| 20 |
+
(8, 9),
|
| 21 |
+
(9, 13),
|
| 22 |
+
(13, 14),
|
| 23 |
+
(14, 15),
|
| 24 |
+
(14, 16),
|
| 25 |
+
(14, 17),
|
| 26 |
+
(13, 11),
|
| 27 |
+
(11, 10),
|
| 28 |
+
(11, 12),
|
| 29 |
+
(13, 18),
|
| 30 |
+
(18, 19),
|
| 31 |
+
(18, 20),
|
| 32 |
+
]
|
| 33 |
+
G = nx.Graph(edge_list)
|
| 34 |
+
b_T, b_C = nx.tree_broadcast_center(G)
|
| 35 |
+
assert b_T == 6
|
| 36 |
+
assert b_C == {13, 9}
|
| 37 |
+
# test broadcast time from specific vertex
|
| 38 |
+
assert nx.tree_broadcast_time(G, 17) == 8
|
| 39 |
+
assert nx.tree_broadcast_time(G, 3) == 9
|
| 40 |
+
# test broadcast time of entire tree
|
| 41 |
+
assert nx.tree_broadcast_time(G) == 10
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_path_broadcast():
|
| 45 |
+
for i in range(2, 12):
|
| 46 |
+
G = nx.path_graph(i)
|
| 47 |
+
b_T, b_C = nx.tree_broadcast_center(G)
|
| 48 |
+
assert b_T == math.ceil(i / 2)
|
| 49 |
+
assert b_C == {
|
| 50 |
+
math.ceil(i / 2),
|
| 51 |
+
math.floor(i / 2),
|
| 52 |
+
math.ceil(i / 2 - 1),
|
| 53 |
+
math.floor(i / 2 - 1),
|
| 54 |
+
}
|
| 55 |
+
assert nx.tree_broadcast_time(G) == i - 1
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_empty_graph_broadcast():
|
| 59 |
+
H = nx.empty_graph(1)
|
| 60 |
+
b_T, b_C = nx.tree_broadcast_center(H)
|
| 61 |
+
assert b_T == 0
|
| 62 |
+
assert b_C == {0}
|
| 63 |
+
assert nx.tree_broadcast_time(H) == 0
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_star_broadcast():
|
| 67 |
+
for i in range(4, 12):
|
| 68 |
+
G = nx.star_graph(i)
|
| 69 |
+
b_T, b_C = nx.tree_broadcast_center(G)
|
| 70 |
+
assert b_T == i
|
| 71 |
+
assert b_C == set(G.nodes())
|
| 72 |
+
assert nx.tree_broadcast_time(G) == b_T
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_binomial_tree_broadcast():
|
| 76 |
+
for i in range(2, 8):
|
| 77 |
+
G = nx.binomial_tree(i)
|
| 78 |
+
b_T, b_C = nx.tree_broadcast_center(G)
|
| 79 |
+
assert b_T == i
|
| 80 |
+
assert b_C == {0, 2 ** (i - 1)}
|
| 81 |
+
assert nx.tree_broadcast_time(G) == 2 * i - 1
|
pythonProject/.venv/Lib/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)
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_chordal.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestMCS:
|
| 7 |
+
@classmethod
|
| 8 |
+
def setup_class(cls):
|
| 9 |
+
# simple graph
|
| 10 |
+
connected_chordal_G = nx.Graph()
|
| 11 |
+
connected_chordal_G.add_edges_from(
|
| 12 |
+
[
|
| 13 |
+
(1, 2),
|
| 14 |
+
(1, 3),
|
| 15 |
+
(2, 3),
|
| 16 |
+
(2, 4),
|
| 17 |
+
(3, 4),
|
| 18 |
+
(3, 5),
|
| 19 |
+
(3, 6),
|
| 20 |
+
(4, 5),
|
| 21 |
+
(4, 6),
|
| 22 |
+
(5, 6),
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
cls.connected_chordal_G = connected_chordal_G
|
| 26 |
+
|
| 27 |
+
chordal_G = nx.Graph()
|
| 28 |
+
chordal_G.add_edges_from(
|
| 29 |
+
[
|
| 30 |
+
(1, 2),
|
| 31 |
+
(1, 3),
|
| 32 |
+
(2, 3),
|
| 33 |
+
(2, 4),
|
| 34 |
+
(3, 4),
|
| 35 |
+
(3, 5),
|
| 36 |
+
(3, 6),
|
| 37 |
+
(4, 5),
|
| 38 |
+
(4, 6),
|
| 39 |
+
(5, 6),
|
| 40 |
+
(7, 8),
|
| 41 |
+
]
|
| 42 |
+
)
|
| 43 |
+
chordal_G.add_node(9)
|
| 44 |
+
cls.chordal_G = chordal_G
|
| 45 |
+
|
| 46 |
+
non_chordal_G = nx.Graph()
|
| 47 |
+
non_chordal_G.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5), (3, 4), (3, 5)])
|
| 48 |
+
cls.non_chordal_G = non_chordal_G
|
| 49 |
+
|
| 50 |
+
self_loop_G = nx.Graph()
|
| 51 |
+
self_loop_G.add_edges_from([(1, 1)])
|
| 52 |
+
cls.self_loop_G = self_loop_G
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize("G", (nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()))
|
| 55 |
+
def test_is_chordal_not_implemented(self, G):
|
| 56 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 57 |
+
nx.is_chordal(G)
|
| 58 |
+
|
| 59 |
+
def test_is_chordal(self):
|
| 60 |
+
assert not nx.is_chordal(self.non_chordal_G)
|
| 61 |
+
assert nx.is_chordal(self.chordal_G)
|
| 62 |
+
assert nx.is_chordal(self.connected_chordal_G)
|
| 63 |
+
assert nx.is_chordal(nx.Graph())
|
| 64 |
+
assert nx.is_chordal(nx.complete_graph(3))
|
| 65 |
+
assert nx.is_chordal(nx.cycle_graph(3))
|
| 66 |
+
assert not nx.is_chordal(nx.cycle_graph(5))
|
| 67 |
+
assert nx.is_chordal(self.self_loop_G)
|
| 68 |
+
|
| 69 |
+
def test_induced_nodes(self):
|
| 70 |
+
G = nx.generators.classic.path_graph(10)
|
| 71 |
+
Induced_nodes = nx.find_induced_nodes(G, 1, 9, 2)
|
| 72 |
+
assert Induced_nodes == {1, 2, 3, 4, 5, 6, 7, 8, 9}
|
| 73 |
+
pytest.raises(
|
| 74 |
+
nx.NetworkXTreewidthBoundExceeded, nx.find_induced_nodes, G, 1, 9, 1
|
| 75 |
+
)
|
| 76 |
+
Induced_nodes = nx.find_induced_nodes(self.chordal_G, 1, 6)
|
| 77 |
+
assert Induced_nodes == {1, 2, 4, 6}
|
| 78 |
+
pytest.raises(nx.NetworkXError, nx.find_induced_nodes, self.non_chordal_G, 1, 5)
|
| 79 |
+
|
| 80 |
+
def test_graph_treewidth(self):
|
| 81 |
+
with pytest.raises(nx.NetworkXError, match="Input graph is not chordal"):
|
| 82 |
+
nx.chordal_graph_treewidth(self.non_chordal_G)
|
| 83 |
+
|
| 84 |
+
def test_chordal_find_cliques(self):
|
| 85 |
+
cliques = {
|
| 86 |
+
frozenset([9]),
|
| 87 |
+
frozenset([7, 8]),
|
| 88 |
+
frozenset([1, 2, 3]),
|
| 89 |
+
frozenset([2, 3, 4]),
|
| 90 |
+
frozenset([3, 4, 5, 6]),
|
| 91 |
+
}
|
| 92 |
+
assert set(nx.chordal_graph_cliques(self.chordal_G)) == cliques
|
| 93 |
+
with pytest.raises(nx.NetworkXError, match="Input graph is not chordal"):
|
| 94 |
+
set(nx.chordal_graph_cliques(self.non_chordal_G))
|
| 95 |
+
with pytest.raises(nx.NetworkXError, match="Input graph is not chordal"):
|
| 96 |
+
set(nx.chordal_graph_cliques(self.self_loop_G))
|
| 97 |
+
|
| 98 |
+
def test_chordal_find_cliques_path(self):
|
| 99 |
+
G = nx.path_graph(10)
|
| 100 |
+
cliqueset = nx.chordal_graph_cliques(G)
|
| 101 |
+
for u, v in G.edges():
|
| 102 |
+
assert frozenset([u, v]) in cliqueset or frozenset([v, u]) in cliqueset
|
| 103 |
+
|
| 104 |
+
def test_chordal_find_cliquesCC(self):
|
| 105 |
+
cliques = {frozenset([1, 2, 3]), frozenset([2, 3, 4]), frozenset([3, 4, 5, 6])}
|
| 106 |
+
cgc = nx.chordal_graph_cliques
|
| 107 |
+
assert set(cgc(self.connected_chordal_G)) == cliques
|
| 108 |
+
|
| 109 |
+
def test_complete_to_chordal_graph(self):
|
| 110 |
+
fgrg = nx.fast_gnp_random_graph
|
| 111 |
+
test_graphs = [
|
| 112 |
+
nx.barbell_graph(6, 2),
|
| 113 |
+
nx.cycle_graph(15),
|
| 114 |
+
nx.wheel_graph(20),
|
| 115 |
+
nx.grid_graph([10, 4]),
|
| 116 |
+
nx.ladder_graph(15),
|
| 117 |
+
nx.star_graph(5),
|
| 118 |
+
nx.bull_graph(),
|
| 119 |
+
fgrg(20, 0.3, seed=1),
|
| 120 |
+
]
|
| 121 |
+
for G in test_graphs:
|
| 122 |
+
H, a = nx.complete_to_chordal_graph(G)
|
| 123 |
+
assert nx.is_chordal(H)
|
| 124 |
+
assert len(a) == H.number_of_nodes()
|
| 125 |
+
if nx.is_chordal(G):
|
| 126 |
+
assert G.number_of_edges() == H.number_of_edges()
|
| 127 |
+
assert set(a.values()) == {0}
|
| 128 |
+
else:
|
| 129 |
+
assert len(set(a.values())) == H.number_of_nodes()
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_clique.py
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx import convert_node_labels_to_integers as cnlti
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestCliques:
|
| 8 |
+
def setup_method(self):
|
| 9 |
+
z = [3, 4, 3, 4, 2, 4, 2, 1, 1, 1, 1]
|
| 10 |
+
self.G = cnlti(nx.generators.havel_hakimi_graph(z), first_label=1)
|
| 11 |
+
self.cl = list(nx.find_cliques(self.G))
|
| 12 |
+
H = nx.complete_graph(6)
|
| 13 |
+
H = nx.relabel_nodes(H, {i: i + 1 for i in range(6)})
|
| 14 |
+
H.remove_edges_from([(2, 6), (2, 5), (2, 4), (1, 3), (5, 3)])
|
| 15 |
+
self.H = H
|
| 16 |
+
|
| 17 |
+
def test_find_cliques1(self):
|
| 18 |
+
cl = list(nx.find_cliques(self.G))
|
| 19 |
+
rcl = nx.find_cliques_recursive(self.G)
|
| 20 |
+
expected = [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
|
| 21 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, rcl))
|
| 22 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
| 23 |
+
|
| 24 |
+
def test_selfloops(self):
|
| 25 |
+
self.G.add_edge(1, 1)
|
| 26 |
+
cl = list(nx.find_cliques(self.G))
|
| 27 |
+
rcl = list(nx.find_cliques_recursive(self.G))
|
| 28 |
+
assert set(map(frozenset, cl)) == set(map(frozenset, rcl))
|
| 29 |
+
answer = [{2, 6, 1, 3}, {2, 6, 4}, {5, 4, 7}, {8, 9}, {10, 11}]
|
| 30 |
+
assert len(answer) == len(cl)
|
| 31 |
+
assert all(set(c) in answer for c in cl)
|
| 32 |
+
|
| 33 |
+
def test_find_cliques2(self):
|
| 34 |
+
hcl = list(nx.find_cliques(self.H))
|
| 35 |
+
assert sorted(map(sorted, hcl)) == [[1, 2], [1, 4, 5, 6], [2, 3], [3, 4, 6]]
|
| 36 |
+
|
| 37 |
+
def test_find_cliques3(self):
|
| 38 |
+
# all cliques are [[2, 6, 1, 3], [2, 6, 4], [5, 4, 7], [8, 9], [10, 11]]
|
| 39 |
+
|
| 40 |
+
cl = list(nx.find_cliques(self.G, [2]))
|
| 41 |
+
rcl = nx.find_cliques_recursive(self.G, [2])
|
| 42 |
+
expected = [[2, 6, 1, 3], [2, 6, 4]]
|
| 43 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
| 44 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
| 45 |
+
|
| 46 |
+
cl = list(nx.find_cliques(self.G, [2, 3]))
|
| 47 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 3])
|
| 48 |
+
expected = [[2, 6, 1, 3]]
|
| 49 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
| 50 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
| 51 |
+
|
| 52 |
+
cl = list(nx.find_cliques(self.G, [2, 6, 4]))
|
| 53 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 6, 4])
|
| 54 |
+
expected = [[2, 6, 4]]
|
| 55 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
| 56 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
| 57 |
+
|
| 58 |
+
cl = list(nx.find_cliques(self.G, [2, 6, 4]))
|
| 59 |
+
rcl = nx.find_cliques_recursive(self.G, [2, 6, 4])
|
| 60 |
+
expected = [[2, 6, 4]]
|
| 61 |
+
assert sorted(map(sorted, rcl)) == sorted(map(sorted, expected))
|
| 62 |
+
assert sorted(map(sorted, cl)) == sorted(map(sorted, expected))
|
| 63 |
+
|
| 64 |
+
with pytest.raises(ValueError):
|
| 65 |
+
list(nx.find_cliques(self.G, [2, 6, 4, 1]))
|
| 66 |
+
|
| 67 |
+
with pytest.raises(ValueError):
|
| 68 |
+
list(nx.find_cliques_recursive(self.G, [2, 6, 4, 1]))
|
| 69 |
+
|
| 70 |
+
def test_number_of_cliques(self):
|
| 71 |
+
G = self.G
|
| 72 |
+
assert nx.number_of_cliques(G, 1) == 1
|
| 73 |
+
assert list(nx.number_of_cliques(G, [1]).values()) == [1]
|
| 74 |
+
assert list(nx.number_of_cliques(G, [1, 2]).values()) == [1, 2]
|
| 75 |
+
assert nx.number_of_cliques(G, [1, 2]) == {1: 1, 2: 2}
|
| 76 |
+
assert nx.number_of_cliques(G, 2) == 2
|
| 77 |
+
assert nx.number_of_cliques(G) == {
|
| 78 |
+
1: 1,
|
| 79 |
+
2: 2,
|
| 80 |
+
3: 1,
|
| 81 |
+
4: 2,
|
| 82 |
+
5: 1,
|
| 83 |
+
6: 2,
|
| 84 |
+
7: 1,
|
| 85 |
+
8: 1,
|
| 86 |
+
9: 1,
|
| 87 |
+
10: 1,
|
| 88 |
+
11: 1,
|
| 89 |
+
}
|
| 90 |
+
assert nx.number_of_cliques(G, nodes=list(G)) == {
|
| 91 |
+
1: 1,
|
| 92 |
+
2: 2,
|
| 93 |
+
3: 1,
|
| 94 |
+
4: 2,
|
| 95 |
+
5: 1,
|
| 96 |
+
6: 2,
|
| 97 |
+
7: 1,
|
| 98 |
+
8: 1,
|
| 99 |
+
9: 1,
|
| 100 |
+
10: 1,
|
| 101 |
+
11: 1,
|
| 102 |
+
}
|
| 103 |
+
assert nx.number_of_cliques(G, nodes=[2, 3, 4]) == {2: 2, 3: 1, 4: 2}
|
| 104 |
+
assert nx.number_of_cliques(G, cliques=self.cl) == {
|
| 105 |
+
1: 1,
|
| 106 |
+
2: 2,
|
| 107 |
+
3: 1,
|
| 108 |
+
4: 2,
|
| 109 |
+
5: 1,
|
| 110 |
+
6: 2,
|
| 111 |
+
7: 1,
|
| 112 |
+
8: 1,
|
| 113 |
+
9: 1,
|
| 114 |
+
10: 1,
|
| 115 |
+
11: 1,
|
| 116 |
+
}
|
| 117 |
+
assert nx.number_of_cliques(G, list(G), cliques=self.cl) == {
|
| 118 |
+
1: 1,
|
| 119 |
+
2: 2,
|
| 120 |
+
3: 1,
|
| 121 |
+
4: 2,
|
| 122 |
+
5: 1,
|
| 123 |
+
6: 2,
|
| 124 |
+
7: 1,
|
| 125 |
+
8: 1,
|
| 126 |
+
9: 1,
|
| 127 |
+
10: 1,
|
| 128 |
+
11: 1,
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
def test_node_clique_number(self):
|
| 132 |
+
G = self.G
|
| 133 |
+
assert nx.node_clique_number(G, 1) == 4
|
| 134 |
+
assert list(nx.node_clique_number(G, [1]).values()) == [4]
|
| 135 |
+
assert list(nx.node_clique_number(G, [1, 2]).values()) == [4, 4]
|
| 136 |
+
assert nx.node_clique_number(G, [1, 2]) == {1: 4, 2: 4}
|
| 137 |
+
assert nx.node_clique_number(G, 1) == 4
|
| 138 |
+
assert nx.node_clique_number(G) == {
|
| 139 |
+
1: 4,
|
| 140 |
+
2: 4,
|
| 141 |
+
3: 4,
|
| 142 |
+
4: 3,
|
| 143 |
+
5: 3,
|
| 144 |
+
6: 4,
|
| 145 |
+
7: 3,
|
| 146 |
+
8: 2,
|
| 147 |
+
9: 2,
|
| 148 |
+
10: 2,
|
| 149 |
+
11: 2,
|
| 150 |
+
}
|
| 151 |
+
assert nx.node_clique_number(G, cliques=self.cl) == {
|
| 152 |
+
1: 4,
|
| 153 |
+
2: 4,
|
| 154 |
+
3: 4,
|
| 155 |
+
4: 3,
|
| 156 |
+
5: 3,
|
| 157 |
+
6: 4,
|
| 158 |
+
7: 3,
|
| 159 |
+
8: 2,
|
| 160 |
+
9: 2,
|
| 161 |
+
10: 2,
|
| 162 |
+
11: 2,
|
| 163 |
+
}
|
| 164 |
+
assert nx.node_clique_number(G, [1, 2], cliques=self.cl) == {1: 4, 2: 4}
|
| 165 |
+
assert nx.node_clique_number(G, 1, cliques=self.cl) == 4
|
| 166 |
+
|
| 167 |
+
def test_make_clique_bipartite(self):
|
| 168 |
+
G = self.G
|
| 169 |
+
B = nx.make_clique_bipartite(G)
|
| 170 |
+
assert sorted(B) == [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
| 171 |
+
# Project onto the nodes of the original graph.
|
| 172 |
+
H = nx.projected_graph(B, range(1, 12))
|
| 173 |
+
assert H.adj == G.adj
|
| 174 |
+
# Project onto the nodes representing the cliques.
|
| 175 |
+
H1 = nx.projected_graph(B, range(-5, 0))
|
| 176 |
+
# Relabel the negative numbers as positive ones.
|
| 177 |
+
H1 = nx.relabel_nodes(H1, {-v: v for v in range(1, 6)})
|
| 178 |
+
assert sorted(H1) == [1, 2, 3, 4, 5]
|
| 179 |
+
|
| 180 |
+
def test_make_max_clique_graph(self):
|
| 181 |
+
"""Tests that the maximal clique graph is the same as the bipartite
|
| 182 |
+
clique graph after being projected onto the nodes representing the
|
| 183 |
+
cliques.
|
| 184 |
+
|
| 185 |
+
"""
|
| 186 |
+
G = self.G
|
| 187 |
+
B = nx.make_clique_bipartite(G)
|
| 188 |
+
# Project onto the nodes representing the cliques.
|
| 189 |
+
H1 = nx.projected_graph(B, range(-5, 0))
|
| 190 |
+
# Relabel the negative numbers as nonnegative ones, starting at
|
| 191 |
+
# 0.
|
| 192 |
+
H1 = nx.relabel_nodes(H1, {-v: v - 1 for v in range(1, 6)})
|
| 193 |
+
H2 = nx.make_max_clique_graph(G)
|
| 194 |
+
assert H1.adj == H2.adj
|
| 195 |
+
|
| 196 |
+
def test_directed(self):
|
| 197 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 198 |
+
next(nx.find_cliques(nx.DiGraph()))
|
| 199 |
+
|
| 200 |
+
def test_find_cliques_trivial(self):
|
| 201 |
+
G = nx.Graph()
|
| 202 |
+
assert sorted(nx.find_cliques(G)) == []
|
| 203 |
+
assert sorted(nx.find_cliques_recursive(G)) == []
|
| 204 |
+
|
| 205 |
+
def test_make_max_clique_graph_create_using(self):
|
| 206 |
+
G = nx.Graph([(1, 2), (3, 1), (4, 1), (5, 6)])
|
| 207 |
+
E = nx.Graph([(0, 1), (0, 2), (1, 2)])
|
| 208 |
+
E.add_node(3)
|
| 209 |
+
assert nx.is_isomorphic(nx.make_max_clique_graph(G, create_using=nx.Graph), E)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
class TestEnumerateAllCliques:
|
| 213 |
+
def test_paper_figure_4(self):
|
| 214 |
+
# Same graph as given in Fig. 4 of paper enumerate_all_cliques is
|
| 215 |
+
# based on.
|
| 216 |
+
# http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1559964&isnumber=33129
|
| 217 |
+
G = nx.Graph()
|
| 218 |
+
edges_fig_4 = [
|
| 219 |
+
("a", "b"),
|
| 220 |
+
("a", "c"),
|
| 221 |
+
("a", "d"),
|
| 222 |
+
("a", "e"),
|
| 223 |
+
("b", "c"),
|
| 224 |
+
("b", "d"),
|
| 225 |
+
("b", "e"),
|
| 226 |
+
("c", "d"),
|
| 227 |
+
("c", "e"),
|
| 228 |
+
("d", "e"),
|
| 229 |
+
("f", "b"),
|
| 230 |
+
("f", "c"),
|
| 231 |
+
("f", "g"),
|
| 232 |
+
("g", "f"),
|
| 233 |
+
("g", "c"),
|
| 234 |
+
("g", "d"),
|
| 235 |
+
("g", "e"),
|
| 236 |
+
]
|
| 237 |
+
G.add_edges_from(edges_fig_4)
|
| 238 |
+
|
| 239 |
+
cliques = list(nx.enumerate_all_cliques(G))
|
| 240 |
+
clique_sizes = list(map(len, cliques))
|
| 241 |
+
assert sorted(clique_sizes) == clique_sizes
|
| 242 |
+
|
| 243 |
+
expected_cliques = [
|
| 244 |
+
["a"],
|
| 245 |
+
["b"],
|
| 246 |
+
["c"],
|
| 247 |
+
["d"],
|
| 248 |
+
["e"],
|
| 249 |
+
["f"],
|
| 250 |
+
["g"],
|
| 251 |
+
["a", "b"],
|
| 252 |
+
["a", "b", "d"],
|
| 253 |
+
["a", "b", "d", "e"],
|
| 254 |
+
["a", "b", "e"],
|
| 255 |
+
["a", "c"],
|
| 256 |
+
["a", "c", "d"],
|
| 257 |
+
["a", "c", "d", "e"],
|
| 258 |
+
["a", "c", "e"],
|
| 259 |
+
["a", "d"],
|
| 260 |
+
["a", "d", "e"],
|
| 261 |
+
["a", "e"],
|
| 262 |
+
["b", "c"],
|
| 263 |
+
["b", "c", "d"],
|
| 264 |
+
["b", "c", "d", "e"],
|
| 265 |
+
["b", "c", "e"],
|
| 266 |
+
["b", "c", "f"],
|
| 267 |
+
["b", "d"],
|
| 268 |
+
["b", "d", "e"],
|
| 269 |
+
["b", "e"],
|
| 270 |
+
["b", "f"],
|
| 271 |
+
["c", "d"],
|
| 272 |
+
["c", "d", "e"],
|
| 273 |
+
["c", "d", "e", "g"],
|
| 274 |
+
["c", "d", "g"],
|
| 275 |
+
["c", "e"],
|
| 276 |
+
["c", "e", "g"],
|
| 277 |
+
["c", "f"],
|
| 278 |
+
["c", "f", "g"],
|
| 279 |
+
["c", "g"],
|
| 280 |
+
["d", "e"],
|
| 281 |
+
["d", "e", "g"],
|
| 282 |
+
["d", "g"],
|
| 283 |
+
["e", "g"],
|
| 284 |
+
["f", "g"],
|
| 285 |
+
["a", "b", "c"],
|
| 286 |
+
["a", "b", "c", "d"],
|
| 287 |
+
["a", "b", "c", "d", "e"],
|
| 288 |
+
["a", "b", "c", "e"],
|
| 289 |
+
]
|
| 290 |
+
|
| 291 |
+
assert sorted(map(sorted, cliques)) == sorted(map(sorted, expected_cliques))
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_cluster.py
ADDED
|
@@ -0,0 +1,549 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestTriangles:
|
| 7 |
+
def test_empty(self):
|
| 8 |
+
G = nx.Graph()
|
| 9 |
+
assert list(nx.triangles(G).values()) == []
|
| 10 |
+
|
| 11 |
+
def test_path(self):
|
| 12 |
+
G = nx.path_graph(10)
|
| 13 |
+
assert list(nx.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
| 14 |
+
assert nx.triangles(G) == {
|
| 15 |
+
0: 0,
|
| 16 |
+
1: 0,
|
| 17 |
+
2: 0,
|
| 18 |
+
3: 0,
|
| 19 |
+
4: 0,
|
| 20 |
+
5: 0,
|
| 21 |
+
6: 0,
|
| 22 |
+
7: 0,
|
| 23 |
+
8: 0,
|
| 24 |
+
9: 0,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def test_cubical(self):
|
| 28 |
+
G = nx.cubical_graph()
|
| 29 |
+
assert list(nx.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0]
|
| 30 |
+
assert nx.triangles(G, 1) == 0
|
| 31 |
+
assert list(nx.triangles(G, [1, 2]).values()) == [0, 0]
|
| 32 |
+
assert nx.triangles(G, 1) == 0
|
| 33 |
+
assert nx.triangles(G, [1, 2]) == {1: 0, 2: 0}
|
| 34 |
+
|
| 35 |
+
def test_k5(self):
|
| 36 |
+
G = nx.complete_graph(5)
|
| 37 |
+
assert list(nx.triangles(G).values()) == [6, 6, 6, 6, 6]
|
| 38 |
+
assert sum(nx.triangles(G).values()) / 3 == 10
|
| 39 |
+
assert nx.triangles(G, 1) == 6
|
| 40 |
+
G.remove_edge(1, 2)
|
| 41 |
+
assert list(nx.triangles(G).values()) == [5, 3, 3, 5, 5]
|
| 42 |
+
assert nx.triangles(G, 1) == 3
|
| 43 |
+
G.add_edge(3, 3) # ignore self-edges
|
| 44 |
+
assert list(nx.triangles(G).values()) == [5, 3, 3, 5, 5]
|
| 45 |
+
assert nx.triangles(G, 3) == 5
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class TestDirectedClustering:
|
| 49 |
+
def test_clustering(self):
|
| 50 |
+
G = nx.DiGraph()
|
| 51 |
+
assert list(nx.clustering(G).values()) == []
|
| 52 |
+
assert nx.clustering(G) == {}
|
| 53 |
+
|
| 54 |
+
def test_path(self):
|
| 55 |
+
G = nx.path_graph(10, create_using=nx.DiGraph())
|
| 56 |
+
assert list(nx.clustering(G).values()) == [
|
| 57 |
+
0,
|
| 58 |
+
0,
|
| 59 |
+
0,
|
| 60 |
+
0,
|
| 61 |
+
0,
|
| 62 |
+
0,
|
| 63 |
+
0,
|
| 64 |
+
0,
|
| 65 |
+
0,
|
| 66 |
+
0,
|
| 67 |
+
]
|
| 68 |
+
assert nx.clustering(G) == {
|
| 69 |
+
0: 0,
|
| 70 |
+
1: 0,
|
| 71 |
+
2: 0,
|
| 72 |
+
3: 0,
|
| 73 |
+
4: 0,
|
| 74 |
+
5: 0,
|
| 75 |
+
6: 0,
|
| 76 |
+
7: 0,
|
| 77 |
+
8: 0,
|
| 78 |
+
9: 0,
|
| 79 |
+
}
|
| 80 |
+
assert nx.clustering(G, 0) == 0
|
| 81 |
+
|
| 82 |
+
def test_k5(self):
|
| 83 |
+
G = nx.complete_graph(5, create_using=nx.DiGraph())
|
| 84 |
+
assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1]
|
| 85 |
+
assert nx.average_clustering(G) == 1
|
| 86 |
+
G.remove_edge(1, 2)
|
| 87 |
+
assert list(nx.clustering(G).values()) == [
|
| 88 |
+
11 / 12,
|
| 89 |
+
1,
|
| 90 |
+
1,
|
| 91 |
+
11 / 12,
|
| 92 |
+
11 / 12,
|
| 93 |
+
]
|
| 94 |
+
assert nx.clustering(G, [1, 4]) == {1: 1, 4: 11 / 12}
|
| 95 |
+
G.remove_edge(2, 1)
|
| 96 |
+
assert list(nx.clustering(G).values()) == [
|
| 97 |
+
5 / 6,
|
| 98 |
+
1,
|
| 99 |
+
1,
|
| 100 |
+
5 / 6,
|
| 101 |
+
5 / 6,
|
| 102 |
+
]
|
| 103 |
+
assert nx.clustering(G, [1, 4]) == {1: 1, 4: 0.83333333333333337}
|
| 104 |
+
assert nx.clustering(G, 4) == 5 / 6
|
| 105 |
+
|
| 106 |
+
def test_triangle_and_edge(self):
|
| 107 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 108 |
+
G.add_edge(0, 4)
|
| 109 |
+
assert nx.clustering(G)[0] == 1 / 6
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class TestDirectedWeightedClustering:
|
| 113 |
+
@classmethod
|
| 114 |
+
def setup_class(cls):
|
| 115 |
+
global np
|
| 116 |
+
np = pytest.importorskip("numpy")
|
| 117 |
+
|
| 118 |
+
def test_clustering(self):
|
| 119 |
+
G = nx.DiGraph()
|
| 120 |
+
assert list(nx.clustering(G, weight="weight").values()) == []
|
| 121 |
+
assert nx.clustering(G) == {}
|
| 122 |
+
|
| 123 |
+
def test_path(self):
|
| 124 |
+
G = nx.path_graph(10, create_using=nx.DiGraph())
|
| 125 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 126 |
+
0,
|
| 127 |
+
0,
|
| 128 |
+
0,
|
| 129 |
+
0,
|
| 130 |
+
0,
|
| 131 |
+
0,
|
| 132 |
+
0,
|
| 133 |
+
0,
|
| 134 |
+
0,
|
| 135 |
+
0,
|
| 136 |
+
]
|
| 137 |
+
assert nx.clustering(G, weight="weight") == {
|
| 138 |
+
0: 0,
|
| 139 |
+
1: 0,
|
| 140 |
+
2: 0,
|
| 141 |
+
3: 0,
|
| 142 |
+
4: 0,
|
| 143 |
+
5: 0,
|
| 144 |
+
6: 0,
|
| 145 |
+
7: 0,
|
| 146 |
+
8: 0,
|
| 147 |
+
9: 0,
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
def test_k5(self):
|
| 151 |
+
G = nx.complete_graph(5, create_using=nx.DiGraph())
|
| 152 |
+
assert list(nx.clustering(G, weight="weight").values()) == [1, 1, 1, 1, 1]
|
| 153 |
+
assert nx.average_clustering(G, weight="weight") == 1
|
| 154 |
+
G.remove_edge(1, 2)
|
| 155 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 156 |
+
11 / 12,
|
| 157 |
+
1,
|
| 158 |
+
1,
|
| 159 |
+
11 / 12,
|
| 160 |
+
11 / 12,
|
| 161 |
+
]
|
| 162 |
+
assert nx.clustering(G, [1, 4], weight="weight") == {1: 1, 4: 11 / 12}
|
| 163 |
+
G.remove_edge(2, 1)
|
| 164 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 165 |
+
5 / 6,
|
| 166 |
+
1,
|
| 167 |
+
1,
|
| 168 |
+
5 / 6,
|
| 169 |
+
5 / 6,
|
| 170 |
+
]
|
| 171 |
+
assert nx.clustering(G, [1, 4], weight="weight") == {
|
| 172 |
+
1: 1,
|
| 173 |
+
4: 0.83333333333333337,
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
def test_triangle_and_edge(self):
|
| 177 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 178 |
+
G.add_edge(0, 4, weight=2)
|
| 179 |
+
assert nx.clustering(G)[0] == 1 / 6
|
| 180 |
+
# Relaxed comparisons to allow graphblas-algorithms to pass tests
|
| 181 |
+
np.testing.assert_allclose(nx.clustering(G, weight="weight")[0], 1 / 12)
|
| 182 |
+
np.testing.assert_allclose(nx.clustering(G, 0, weight="weight"), 1 / 12)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
class TestWeightedClustering:
|
| 186 |
+
@classmethod
|
| 187 |
+
def setup_class(cls):
|
| 188 |
+
global np
|
| 189 |
+
np = pytest.importorskip("numpy")
|
| 190 |
+
|
| 191 |
+
def test_clustering(self):
|
| 192 |
+
G = nx.Graph()
|
| 193 |
+
assert list(nx.clustering(G, weight="weight").values()) == []
|
| 194 |
+
assert nx.clustering(G) == {}
|
| 195 |
+
|
| 196 |
+
def test_path(self):
|
| 197 |
+
G = nx.path_graph(10)
|
| 198 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 199 |
+
0,
|
| 200 |
+
0,
|
| 201 |
+
0,
|
| 202 |
+
0,
|
| 203 |
+
0,
|
| 204 |
+
0,
|
| 205 |
+
0,
|
| 206 |
+
0,
|
| 207 |
+
0,
|
| 208 |
+
0,
|
| 209 |
+
]
|
| 210 |
+
assert nx.clustering(G, weight="weight") == {
|
| 211 |
+
0: 0,
|
| 212 |
+
1: 0,
|
| 213 |
+
2: 0,
|
| 214 |
+
3: 0,
|
| 215 |
+
4: 0,
|
| 216 |
+
5: 0,
|
| 217 |
+
6: 0,
|
| 218 |
+
7: 0,
|
| 219 |
+
8: 0,
|
| 220 |
+
9: 0,
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
def test_cubical(self):
|
| 224 |
+
G = nx.cubical_graph()
|
| 225 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 226 |
+
0,
|
| 227 |
+
0,
|
| 228 |
+
0,
|
| 229 |
+
0,
|
| 230 |
+
0,
|
| 231 |
+
0,
|
| 232 |
+
0,
|
| 233 |
+
0,
|
| 234 |
+
]
|
| 235 |
+
assert nx.clustering(G, 1) == 0
|
| 236 |
+
assert list(nx.clustering(G, [1, 2], weight="weight").values()) == [0, 0]
|
| 237 |
+
assert nx.clustering(G, 1, weight="weight") == 0
|
| 238 |
+
assert nx.clustering(G, [1, 2], weight="weight") == {1: 0, 2: 0}
|
| 239 |
+
|
| 240 |
+
def test_k5(self):
|
| 241 |
+
G = nx.complete_graph(5)
|
| 242 |
+
assert list(nx.clustering(G, weight="weight").values()) == [1, 1, 1, 1, 1]
|
| 243 |
+
assert nx.average_clustering(G, weight="weight") == 1
|
| 244 |
+
G.remove_edge(1, 2)
|
| 245 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 246 |
+
5 / 6,
|
| 247 |
+
1,
|
| 248 |
+
1,
|
| 249 |
+
5 / 6,
|
| 250 |
+
5 / 6,
|
| 251 |
+
]
|
| 252 |
+
assert nx.clustering(G, [1, 4], weight="weight") == {
|
| 253 |
+
1: 1,
|
| 254 |
+
4: 0.83333333333333337,
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
def test_triangle_and_edge(self):
|
| 258 |
+
G = nx.cycle_graph(3)
|
| 259 |
+
G.add_edge(0, 4, weight=2)
|
| 260 |
+
assert nx.clustering(G)[0] == 1 / 3
|
| 261 |
+
np.testing.assert_allclose(nx.clustering(G, weight="weight")[0], 1 / 6)
|
| 262 |
+
np.testing.assert_allclose(nx.clustering(G, 0, weight="weight"), 1 / 6)
|
| 263 |
+
|
| 264 |
+
def test_triangle_and_signed_edge(self):
|
| 265 |
+
G = nx.cycle_graph(3)
|
| 266 |
+
G.add_edge(0, 1, weight=-1)
|
| 267 |
+
G.add_edge(3, 0, weight=0)
|
| 268 |
+
assert nx.clustering(G)[0] == 1 / 3
|
| 269 |
+
assert nx.clustering(G, weight="weight")[0] == -1 / 3
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
class TestClustering:
|
| 273 |
+
@classmethod
|
| 274 |
+
def setup_class(cls):
|
| 275 |
+
pytest.importorskip("numpy")
|
| 276 |
+
|
| 277 |
+
def test_clustering(self):
|
| 278 |
+
G = nx.Graph()
|
| 279 |
+
assert list(nx.clustering(G).values()) == []
|
| 280 |
+
assert nx.clustering(G) == {}
|
| 281 |
+
|
| 282 |
+
def test_path(self):
|
| 283 |
+
G = nx.path_graph(10)
|
| 284 |
+
assert list(nx.clustering(G).values()) == [
|
| 285 |
+
0,
|
| 286 |
+
0,
|
| 287 |
+
0,
|
| 288 |
+
0,
|
| 289 |
+
0,
|
| 290 |
+
0,
|
| 291 |
+
0,
|
| 292 |
+
0,
|
| 293 |
+
0,
|
| 294 |
+
0,
|
| 295 |
+
]
|
| 296 |
+
assert nx.clustering(G) == {
|
| 297 |
+
0: 0,
|
| 298 |
+
1: 0,
|
| 299 |
+
2: 0,
|
| 300 |
+
3: 0,
|
| 301 |
+
4: 0,
|
| 302 |
+
5: 0,
|
| 303 |
+
6: 0,
|
| 304 |
+
7: 0,
|
| 305 |
+
8: 0,
|
| 306 |
+
9: 0,
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
def test_cubical(self):
|
| 310 |
+
G = nx.cubical_graph()
|
| 311 |
+
assert list(nx.clustering(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0]
|
| 312 |
+
assert nx.clustering(G, 1) == 0
|
| 313 |
+
assert list(nx.clustering(G, [1, 2]).values()) == [0, 0]
|
| 314 |
+
assert nx.clustering(G, 1) == 0
|
| 315 |
+
assert nx.clustering(G, [1, 2]) == {1: 0, 2: 0}
|
| 316 |
+
|
| 317 |
+
def test_k5(self):
|
| 318 |
+
G = nx.complete_graph(5)
|
| 319 |
+
assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1]
|
| 320 |
+
assert nx.average_clustering(G) == 1
|
| 321 |
+
G.remove_edge(1, 2)
|
| 322 |
+
assert list(nx.clustering(G).values()) == [
|
| 323 |
+
5 / 6,
|
| 324 |
+
1,
|
| 325 |
+
1,
|
| 326 |
+
5 / 6,
|
| 327 |
+
5 / 6,
|
| 328 |
+
]
|
| 329 |
+
assert nx.clustering(G, [1, 4]) == {1: 1, 4: 0.83333333333333337}
|
| 330 |
+
|
| 331 |
+
def test_k5_signed(self):
|
| 332 |
+
G = nx.complete_graph(5)
|
| 333 |
+
assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1]
|
| 334 |
+
assert nx.average_clustering(G) == 1
|
| 335 |
+
G.remove_edge(1, 2)
|
| 336 |
+
G.add_edge(0, 1, weight=-1)
|
| 337 |
+
assert list(nx.clustering(G, weight="weight").values()) == [
|
| 338 |
+
1 / 6,
|
| 339 |
+
-1 / 3,
|
| 340 |
+
1,
|
| 341 |
+
3 / 6,
|
| 342 |
+
3 / 6,
|
| 343 |
+
]
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class TestTransitivity:
|
| 347 |
+
def test_transitivity(self):
|
| 348 |
+
G = nx.Graph()
|
| 349 |
+
assert nx.transitivity(G) == 0
|
| 350 |
+
|
| 351 |
+
def test_path(self):
|
| 352 |
+
G = nx.path_graph(10)
|
| 353 |
+
assert nx.transitivity(G) == 0
|
| 354 |
+
|
| 355 |
+
def test_cubical(self):
|
| 356 |
+
G = nx.cubical_graph()
|
| 357 |
+
assert nx.transitivity(G) == 0
|
| 358 |
+
|
| 359 |
+
def test_k5(self):
|
| 360 |
+
G = nx.complete_graph(5)
|
| 361 |
+
assert nx.transitivity(G) == 1
|
| 362 |
+
G.remove_edge(1, 2)
|
| 363 |
+
assert nx.transitivity(G) == 0.875
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
class TestSquareClustering:
|
| 367 |
+
def test_clustering(self):
|
| 368 |
+
G = nx.Graph()
|
| 369 |
+
assert list(nx.square_clustering(G).values()) == []
|
| 370 |
+
assert nx.square_clustering(G) == {}
|
| 371 |
+
|
| 372 |
+
def test_path(self):
|
| 373 |
+
G = nx.path_graph(10)
|
| 374 |
+
assert list(nx.square_clustering(G).values()) == [
|
| 375 |
+
0,
|
| 376 |
+
0,
|
| 377 |
+
0,
|
| 378 |
+
0,
|
| 379 |
+
0,
|
| 380 |
+
0,
|
| 381 |
+
0,
|
| 382 |
+
0,
|
| 383 |
+
0,
|
| 384 |
+
0,
|
| 385 |
+
]
|
| 386 |
+
assert nx.square_clustering(G) == {
|
| 387 |
+
0: 0,
|
| 388 |
+
1: 0,
|
| 389 |
+
2: 0,
|
| 390 |
+
3: 0,
|
| 391 |
+
4: 0,
|
| 392 |
+
5: 0,
|
| 393 |
+
6: 0,
|
| 394 |
+
7: 0,
|
| 395 |
+
8: 0,
|
| 396 |
+
9: 0,
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
def test_cubical(self):
|
| 400 |
+
G = nx.cubical_graph()
|
| 401 |
+
assert list(nx.square_clustering(G).values()) == [
|
| 402 |
+
1 / 3,
|
| 403 |
+
1 / 3,
|
| 404 |
+
1 / 3,
|
| 405 |
+
1 / 3,
|
| 406 |
+
1 / 3,
|
| 407 |
+
1 / 3,
|
| 408 |
+
1 / 3,
|
| 409 |
+
1 / 3,
|
| 410 |
+
]
|
| 411 |
+
assert list(nx.square_clustering(G, [1, 2]).values()) == [1 / 3, 1 / 3]
|
| 412 |
+
assert nx.square_clustering(G, [1])[1] == 1 / 3
|
| 413 |
+
assert nx.square_clustering(G, 1) == 1 / 3
|
| 414 |
+
assert nx.square_clustering(G, [1, 2]) == {1: 1 / 3, 2: 1 / 3}
|
| 415 |
+
|
| 416 |
+
def test_k5(self):
|
| 417 |
+
G = nx.complete_graph(5)
|
| 418 |
+
assert list(nx.square_clustering(G).values()) == [1, 1, 1, 1, 1]
|
| 419 |
+
|
| 420 |
+
def test_bipartite_k5(self):
|
| 421 |
+
G = nx.complete_bipartite_graph(5, 5)
|
| 422 |
+
assert list(nx.square_clustering(G).values()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
|
| 423 |
+
|
| 424 |
+
def test_lind_square_clustering(self):
|
| 425 |
+
"""Test C4 for figure 1 Lind et al (2005)"""
|
| 426 |
+
G = nx.Graph(
|
| 427 |
+
[
|
| 428 |
+
(1, 2),
|
| 429 |
+
(1, 3),
|
| 430 |
+
(1, 6),
|
| 431 |
+
(1, 7),
|
| 432 |
+
(2, 4),
|
| 433 |
+
(2, 5),
|
| 434 |
+
(3, 4),
|
| 435 |
+
(3, 5),
|
| 436 |
+
(6, 7),
|
| 437 |
+
(7, 8),
|
| 438 |
+
(6, 8),
|
| 439 |
+
(7, 9),
|
| 440 |
+
(7, 10),
|
| 441 |
+
(6, 11),
|
| 442 |
+
(6, 12),
|
| 443 |
+
(2, 13),
|
| 444 |
+
(2, 14),
|
| 445 |
+
(3, 15),
|
| 446 |
+
(3, 16),
|
| 447 |
+
]
|
| 448 |
+
)
|
| 449 |
+
G1 = G.subgraph([1, 2, 3, 4, 5, 13, 14, 15, 16])
|
| 450 |
+
G2 = G.subgraph([1, 6, 7, 8, 9, 10, 11, 12])
|
| 451 |
+
assert nx.square_clustering(G, [1])[1] == 3 / 43
|
| 452 |
+
assert nx.square_clustering(G1, [1])[1] == 2 / 6
|
| 453 |
+
assert nx.square_clustering(G2, [1])[1] == 1 / 5
|
| 454 |
+
|
| 455 |
+
def test_peng_square_clustering(self):
|
| 456 |
+
"""Test eq2 for figure 1 Peng et al (2008)"""
|
| 457 |
+
G = nx.Graph([(1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (3, 6)])
|
| 458 |
+
assert nx.square_clustering(G, [1])[1] == 1 / 3
|
| 459 |
+
|
| 460 |
+
def test_self_loops_square_clustering(self):
|
| 461 |
+
G = nx.path_graph(5)
|
| 462 |
+
assert nx.square_clustering(G) == {0: 0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0}
|
| 463 |
+
G.add_edges_from([(0, 0), (1, 1), (2, 2)])
|
| 464 |
+
assert nx.square_clustering(G) == {0: 1, 1: 0.5, 2: 0.2, 3: 0.0, 4: 0}
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
class TestAverageClustering:
|
| 468 |
+
@classmethod
|
| 469 |
+
def setup_class(cls):
|
| 470 |
+
pytest.importorskip("numpy")
|
| 471 |
+
|
| 472 |
+
def test_empty(self):
|
| 473 |
+
G = nx.Graph()
|
| 474 |
+
with pytest.raises(ZeroDivisionError):
|
| 475 |
+
nx.average_clustering(G)
|
| 476 |
+
|
| 477 |
+
def test_average_clustering(self):
|
| 478 |
+
G = nx.cycle_graph(3)
|
| 479 |
+
G.add_edge(2, 3)
|
| 480 |
+
assert nx.average_clustering(G) == (1 + 1 + 1 / 3) / 4
|
| 481 |
+
assert nx.average_clustering(G, count_zeros=True) == (1 + 1 + 1 / 3) / 4
|
| 482 |
+
assert nx.average_clustering(G, count_zeros=False) == (1 + 1 + 1 / 3) / 3
|
| 483 |
+
assert nx.average_clustering(G, [1, 2, 3]) == (1 + 1 / 3) / 3
|
| 484 |
+
assert nx.average_clustering(G, [1, 2, 3], count_zeros=True) == (1 + 1 / 3) / 3
|
| 485 |
+
assert nx.average_clustering(G, [1, 2, 3], count_zeros=False) == (1 + 1 / 3) / 2
|
| 486 |
+
|
| 487 |
+
def test_average_clustering_signed(self):
|
| 488 |
+
G = nx.cycle_graph(3)
|
| 489 |
+
G.add_edge(2, 3)
|
| 490 |
+
G.add_edge(0, 1, weight=-1)
|
| 491 |
+
assert nx.average_clustering(G, weight="weight") == (-1 - 1 - 1 / 3) / 4
|
| 492 |
+
assert (
|
| 493 |
+
nx.average_clustering(G, weight="weight", count_zeros=True)
|
| 494 |
+
== (-1 - 1 - 1 / 3) / 4
|
| 495 |
+
)
|
| 496 |
+
assert (
|
| 497 |
+
nx.average_clustering(G, weight="weight", count_zeros=False)
|
| 498 |
+
== (-1 - 1 - 1 / 3) / 3
|
| 499 |
+
)
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
class TestDirectedAverageClustering:
|
| 503 |
+
@classmethod
|
| 504 |
+
def setup_class(cls):
|
| 505 |
+
pytest.importorskip("numpy")
|
| 506 |
+
|
| 507 |
+
def test_empty(self):
|
| 508 |
+
G = nx.DiGraph()
|
| 509 |
+
with pytest.raises(ZeroDivisionError):
|
| 510 |
+
nx.average_clustering(G)
|
| 511 |
+
|
| 512 |
+
def test_average_clustering(self):
|
| 513 |
+
G = nx.cycle_graph(3, create_using=nx.DiGraph())
|
| 514 |
+
G.add_edge(2, 3)
|
| 515 |
+
assert nx.average_clustering(G) == (1 + 1 + 1 / 3) / 8
|
| 516 |
+
assert nx.average_clustering(G, count_zeros=True) == (1 + 1 + 1 / 3) / 8
|
| 517 |
+
assert nx.average_clustering(G, count_zeros=False) == (1 + 1 + 1 / 3) / 6
|
| 518 |
+
assert nx.average_clustering(G, [1, 2, 3]) == (1 + 1 / 3) / 6
|
| 519 |
+
assert nx.average_clustering(G, [1, 2, 3], count_zeros=True) == (1 + 1 / 3) / 6
|
| 520 |
+
assert nx.average_clustering(G, [1, 2, 3], count_zeros=False) == (1 + 1 / 3) / 4
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
class TestGeneralizedDegree:
|
| 524 |
+
def test_generalized_degree(self):
|
| 525 |
+
G = nx.Graph()
|
| 526 |
+
assert nx.generalized_degree(G) == {}
|
| 527 |
+
|
| 528 |
+
def test_path(self):
|
| 529 |
+
G = nx.path_graph(5)
|
| 530 |
+
assert nx.generalized_degree(G, 0) == {0: 1}
|
| 531 |
+
assert nx.generalized_degree(G, 1) == {0: 2}
|
| 532 |
+
|
| 533 |
+
def test_cubical(self):
|
| 534 |
+
G = nx.cubical_graph()
|
| 535 |
+
assert nx.generalized_degree(G, 0) == {0: 3}
|
| 536 |
+
|
| 537 |
+
def test_k5(self):
|
| 538 |
+
G = nx.complete_graph(5)
|
| 539 |
+
assert nx.generalized_degree(G, 0) == {3: 4}
|
| 540 |
+
G.remove_edge(0, 1)
|
| 541 |
+
assert nx.generalized_degree(G, 0) == {2: 3}
|
| 542 |
+
assert nx.generalized_degree(G, [1, 2]) == {1: {2: 3}, 2: {2: 2, 3: 2}}
|
| 543 |
+
assert nx.generalized_degree(G) == {
|
| 544 |
+
0: {2: 3},
|
| 545 |
+
1: {2: 3},
|
| 546 |
+
2: {2: 2, 3: 2},
|
| 547 |
+
3: {2: 2, 3: 2},
|
| 548 |
+
4: {2: 2, 3: 2},
|
| 549 |
+
}
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_communicability.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import defaultdict
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
pytest.importorskip("numpy")
|
| 6 |
+
pytest.importorskip("scipy")
|
| 7 |
+
|
| 8 |
+
import networkx as nx
|
| 9 |
+
from networkx.algorithms.communicability_alg import communicability, communicability_exp
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestCommunicability:
|
| 13 |
+
def test_communicability(self):
|
| 14 |
+
answer = {
|
| 15 |
+
0: {0: 1.5430806348152435, 1: 1.1752011936438012},
|
| 16 |
+
1: {0: 1.1752011936438012, 1: 1.5430806348152435},
|
| 17 |
+
}
|
| 18 |
+
# answer={(0, 0): 1.5430806348152435,
|
| 19 |
+
# (0, 1): 1.1752011936438012,
|
| 20 |
+
# (1, 0): 1.1752011936438012,
|
| 21 |
+
# (1, 1): 1.5430806348152435}
|
| 22 |
+
|
| 23 |
+
result = communicability(nx.path_graph(2))
|
| 24 |
+
for k1, val in result.items():
|
| 25 |
+
for k2 in val:
|
| 26 |
+
assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
|
| 27 |
+
|
| 28 |
+
def test_communicability2(self):
|
| 29 |
+
answer_orig = {
|
| 30 |
+
("1", "1"): 1.6445956054135658,
|
| 31 |
+
("1", "Albert"): 0.7430186221096251,
|
| 32 |
+
("1", "Aric"): 0.7430186221096251,
|
| 33 |
+
("1", "Dan"): 1.6208126320442937,
|
| 34 |
+
("1", "Franck"): 0.42639707170035257,
|
| 35 |
+
("Albert", "1"): 0.7430186221096251,
|
| 36 |
+
("Albert", "Albert"): 2.4368257358712189,
|
| 37 |
+
("Albert", "Aric"): 1.4368257358712191,
|
| 38 |
+
("Albert", "Dan"): 2.0472097037446453,
|
| 39 |
+
("Albert", "Franck"): 1.8340111678944691,
|
| 40 |
+
("Aric", "1"): 0.7430186221096251,
|
| 41 |
+
("Aric", "Albert"): 1.4368257358712191,
|
| 42 |
+
("Aric", "Aric"): 2.4368257358712193,
|
| 43 |
+
("Aric", "Dan"): 2.0472097037446457,
|
| 44 |
+
("Aric", "Franck"): 1.8340111678944691,
|
| 45 |
+
("Dan", "1"): 1.6208126320442937,
|
| 46 |
+
("Dan", "Albert"): 2.0472097037446453,
|
| 47 |
+
("Dan", "Aric"): 2.0472097037446457,
|
| 48 |
+
("Dan", "Dan"): 3.1306328496328168,
|
| 49 |
+
("Dan", "Franck"): 1.4860372442192515,
|
| 50 |
+
("Franck", "1"): 0.42639707170035257,
|
| 51 |
+
("Franck", "Albert"): 1.8340111678944691,
|
| 52 |
+
("Franck", "Aric"): 1.8340111678944691,
|
| 53 |
+
("Franck", "Dan"): 1.4860372442192515,
|
| 54 |
+
("Franck", "Franck"): 2.3876142275231915,
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
answer = defaultdict(dict)
|
| 58 |
+
for (k1, k2), v in answer_orig.items():
|
| 59 |
+
answer[k1][k2] = v
|
| 60 |
+
|
| 61 |
+
G1 = nx.Graph(
|
| 62 |
+
[
|
| 63 |
+
("Franck", "Aric"),
|
| 64 |
+
("Aric", "Dan"),
|
| 65 |
+
("Dan", "Albert"),
|
| 66 |
+
("Albert", "Franck"),
|
| 67 |
+
("Dan", "1"),
|
| 68 |
+
("Franck", "Albert"),
|
| 69 |
+
]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
result = communicability(G1)
|
| 73 |
+
for k1, val in result.items():
|
| 74 |
+
for k2 in val:
|
| 75 |
+
assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
|
| 76 |
+
|
| 77 |
+
result = communicability_exp(G1)
|
| 78 |
+
for k1, val in result.items():
|
| 79 |
+
for k2 in val:
|
| 80 |
+
assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
|
pythonProject/.venv/Lib/site-packages/networkx/algorithms/tests/test_core.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils import nodes_equal
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestCore:
|
| 8 |
+
@classmethod
|
| 9 |
+
def setup_class(cls):
|
| 10 |
+
# G is the example graph in Figure 1 from Batagelj and
|
| 11 |
+
# Zaversnik's paper titled An O(m) Algorithm for Cores
|
| 12 |
+
# Decomposition of Networks, 2003,
|
| 13 |
+
# http://arXiv.org/abs/cs/0310049. With nodes labeled as
|
| 14 |
+
# shown, the 3-core is given by nodes 1-8, the 2-core by nodes
|
| 15 |
+
# 9-16, the 1-core by nodes 17-20 and node 21 is in the
|
| 16 |
+
# 0-core.
|
| 17 |
+
t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
|
| 18 |
+
t2 = nx.convert_node_labels_to_integers(t1, 5)
|
| 19 |
+
G = nx.union(t1, t2)
|
| 20 |
+
G.add_edges_from(
|
| 21 |
+
[
|
| 22 |
+
(3, 7),
|
| 23 |
+
(2, 11),
|
| 24 |
+
(11, 5),
|
| 25 |
+
(11, 12),
|
| 26 |
+
(5, 12),
|
| 27 |
+
(12, 19),
|
| 28 |
+
(12, 18),
|
| 29 |
+
(3, 9),
|
| 30 |
+
(7, 9),
|
| 31 |
+
(7, 10),
|
| 32 |
+
(9, 10),
|
| 33 |
+
(9, 20),
|
| 34 |
+
(17, 13),
|
| 35 |
+
(13, 14),
|
| 36 |
+
(14, 15),
|
| 37 |
+
(15, 16),
|
| 38 |
+
(16, 13),
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
G.add_node(21)
|
| 42 |
+
cls.G = G
|
| 43 |
+
|
| 44 |
+
# Create the graph H resulting from the degree sequence
|
| 45 |
+
# [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.
|
| 46 |
+
|
| 47 |
+
degseq = [0, 1, 2, 2, 2, 2, 3]
|
| 48 |
+
H = nx.havel_hakimi_graph(degseq)
|
| 49 |
+
mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
|
| 50 |
+
cls.H = nx.relabel_nodes(H, mapping)
|
| 51 |
+
|
| 52 |
+
def test_trivial(self):
|
| 53 |
+
"""Empty graph"""
|
| 54 |
+
G = nx.Graph()
|
| 55 |
+
assert nx.core_number(G) == {}
|
| 56 |
+
|
| 57 |
+
def test_core_number(self):
|
| 58 |
+
core = nx.core_number(self.G)
|
| 59 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)]
|
| 60 |
+
assert nodes_equal(nodes_by_core[0], [21])
|
| 61 |
+
assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20])
|
| 62 |
+
assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16])
|
| 63 |
+
assert nodes_equal(nodes_by_core[3], [1, 2, 3, 4, 5, 6, 7, 8])
|
| 64 |
+
|
| 65 |
+
def test_core_number2(self):
|
| 66 |
+
core = nx.core_number(self.H)
|
| 67 |
+
nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(3)]
|
| 68 |
+
assert nodes_equal(nodes_by_core[0], [0])
|
| 69 |
+
assert nodes_equal(nodes_by_core[1], [1, 3])
|
| 70 |
+
assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6])
|
| 71 |
+
|
| 72 |
+
def test_core_number_multigraph(self):
|
| 73 |
+
G = nx.complete_graph(3)
|
| 74 |
+
G = nx.MultiGraph(G)
|
| 75 |
+
G.add_edge(1, 2)
|
| 76 |
+
with pytest.raises(
|
| 77 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 78 |
+
):
|
| 79 |
+
nx.core_number(G)
|
| 80 |
+
|
| 81 |
+
def test_core_number_self_loop(self):
|
| 82 |
+
G = nx.cycle_graph(3)
|
| 83 |
+
G.add_edge(0, 0)
|
| 84 |
+
with pytest.raises(
|
| 85 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
| 86 |
+
):
|
| 87 |
+
nx.core_number(G)
|
| 88 |
+
|
| 89 |
+
def test_directed_core_number(self):
|
| 90 |
+
"""core number had a bug for directed graphs found in issue #1959"""
|
| 91 |
+
# small example where too timid edge removal can make cn[2] = 3
|
| 92 |
+
G = nx.DiGraph()
|
| 93 |
+
edges = [(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)]
|
| 94 |
+
G.add_edges_from(edges)
|
| 95 |
+
assert nx.core_number(G) == {1: 2, 2: 2, 3: 2, 4: 2}
|
| 96 |
+
# small example where too aggressive edge removal can make cn[2] = 2
|
| 97 |
+
more_edges = [(1, 5), (3, 5), (4, 5), (3, 6), (4, 6), (5, 6)]
|
| 98 |
+
G.add_edges_from(more_edges)
|
| 99 |
+
assert nx.core_number(G) == {1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3}
|
| 100 |
+
|
| 101 |
+
def test_main_core(self):
|
| 102 |
+
main_core_subgraph = nx.k_core(self.H)
|
| 103 |
+
assert sorted(main_core_subgraph.nodes()) == [2, 4, 5, 6]
|
| 104 |
+
|
| 105 |
+
def test_k_core(self):
|
| 106 |
+
# k=0
|
| 107 |
+
k_core_subgraph = nx.k_core(self.H, k=0)
|
| 108 |
+
assert sorted(k_core_subgraph.nodes()) == sorted(self.H.nodes())
|
| 109 |
+
# k=1
|
| 110 |
+
k_core_subgraph = nx.k_core(self.H, k=1)
|
| 111 |
+
assert sorted(k_core_subgraph.nodes()) == [1, 2, 3, 4, 5, 6]
|
| 112 |
+
# k = 2
|
| 113 |
+
k_core_subgraph = nx.k_core(self.H, k=2)
|
| 114 |
+
assert sorted(k_core_subgraph.nodes()) == [2, 4, 5, 6]
|
| 115 |
+
|
| 116 |
+
def test_k_core_multigraph(self):
|
| 117 |
+
core_number = nx.core_number(self.H)
|
| 118 |
+
H = nx.MultiGraph(self.H)
|
| 119 |
+
with pytest.deprecated_call():
|
| 120 |
+
nx.k_core(H, k=0, core_number=core_number)
|
| 121 |
+
|
| 122 |
+
def test_main_crust(self):
|
| 123 |
+
main_crust_subgraph = nx.k_crust(self.H)
|
| 124 |
+
assert sorted(main_crust_subgraph.nodes()) == [0, 1, 3]
|
| 125 |
+
|
| 126 |
+
def test_k_crust(self):
|
| 127 |
+
# k = 0
|
| 128 |
+
k_crust_subgraph = nx.k_crust(self.H, k=2)
|
| 129 |
+
assert sorted(k_crust_subgraph.nodes()) == sorted(self.H.nodes())
|
| 130 |
+
# k=1
|
| 131 |
+
k_crust_subgraph = nx.k_crust(self.H, k=1)
|
| 132 |
+
assert sorted(k_crust_subgraph.nodes()) == [0, 1, 3]
|
| 133 |
+
# k=2
|
| 134 |
+
k_crust_subgraph = nx.k_crust(self.H, k=0)
|
| 135 |
+
assert sorted(k_crust_subgraph.nodes()) == [0]
|
| 136 |
+
|
| 137 |
+
def test_k_crust_multigraph(self):
|
| 138 |
+
core_number = nx.core_number(self.H)
|
| 139 |
+
H = nx.MultiGraph(self.H)
|
| 140 |
+
with pytest.deprecated_call():
|
| 141 |
+
nx.k_crust(H, k=0, core_number=core_number)
|
| 142 |
+
|
| 143 |
+
def test_main_shell(self):
|
| 144 |
+
main_shell_subgraph = nx.k_shell(self.H)
|
| 145 |
+
assert sorted(main_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
| 146 |
+
|
| 147 |
+
def test_k_shell(self):
|
| 148 |
+
# k=0
|
| 149 |
+
k_shell_subgraph = nx.k_shell(self.H, k=2)
|
| 150 |
+
assert sorted(k_shell_subgraph.nodes()) == [2, 4, 5, 6]
|
| 151 |
+
# k=1
|
| 152 |
+
k_shell_subgraph = nx.k_shell(self.H, k=1)
|
| 153 |
+
assert sorted(k_shell_subgraph.nodes()) == [1, 3]
|
| 154 |
+
# k=2
|
| 155 |
+
k_shell_subgraph = nx.k_shell(self.H, k=0)
|
| 156 |
+
assert sorted(k_shell_subgraph.nodes()) == [0]
|
| 157 |
+
|
| 158 |
+
def test_k_shell_multigraph(self):
|
| 159 |
+
core_number = nx.core_number(self.H)
|
| 160 |
+
H = nx.MultiGraph(self.H)
|
| 161 |
+
with pytest.deprecated_call():
|
| 162 |
+
nx.k_shell(H, k=0, core_number=core_number)
|
| 163 |
+
|
| 164 |
+
def test_k_corona(self):
|
| 165 |
+
# k=0
|
| 166 |
+
k_corona_subgraph = nx.k_corona(self.H, k=2)
|
| 167 |
+
assert sorted(k_corona_subgraph.nodes()) == [2, 4, 5, 6]
|
| 168 |
+
# k=1
|
| 169 |
+
k_corona_subgraph = nx.k_corona(self.H, k=1)
|
| 170 |
+
assert sorted(k_corona_subgraph.nodes()) == [1]
|
| 171 |
+
# k=2
|
| 172 |
+
k_corona_subgraph = nx.k_corona(self.H, k=0)
|
| 173 |
+
assert sorted(k_corona_subgraph.nodes()) == [0]
|
| 174 |
+
|
| 175 |
+
def test_k_corona_multigraph(self):
|
| 176 |
+
core_number = nx.core_number(self.H)
|
| 177 |
+
H = nx.MultiGraph(self.H)
|
| 178 |
+
with pytest.deprecated_call():
|
| 179 |
+
nx.k_corona(H, k=0, core_number=core_number)
|
| 180 |
+
|
| 181 |
+
def test_k_truss(self):
|
| 182 |
+
# k=-1
|
| 183 |
+
k_truss_subgraph = nx.k_truss(self.G, -1)
|
| 184 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 185 |
+
# k=0
|
| 186 |
+
k_truss_subgraph = nx.k_truss(self.G, 0)
|
| 187 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 188 |
+
# k=1
|
| 189 |
+
k_truss_subgraph = nx.k_truss(self.G, 1)
|
| 190 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 191 |
+
# k=2
|
| 192 |
+
k_truss_subgraph = nx.k_truss(self.G, 2)
|
| 193 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21))
|
| 194 |
+
# k=3
|
| 195 |
+
k_truss_subgraph = nx.k_truss(self.G, 3)
|
| 196 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 13))
|
| 197 |
+
|
| 198 |
+
k_truss_subgraph = nx.k_truss(self.G, 4)
|
| 199 |
+
assert sorted(k_truss_subgraph.nodes()) == list(range(1, 9))
|
| 200 |
+
|
| 201 |
+
k_truss_subgraph = nx.k_truss(self.G, 5)
|
| 202 |
+
assert sorted(k_truss_subgraph.nodes()) == []
|
| 203 |
+
|
| 204 |
+
def test_k_truss_digraph(self):
|
| 205 |
+
G = nx.complete_graph(3)
|
| 206 |
+
G = nx.DiGraph(G)
|
| 207 |
+
G.add_edge(2, 1)
|
| 208 |
+
with pytest.raises(
|
| 209 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
| 210 |
+
):
|
| 211 |
+
nx.k_truss(G, k=1)
|
| 212 |
+
|
| 213 |
+
def test_k_truss_multigraph(self):
|
| 214 |
+
G = nx.complete_graph(3)
|
| 215 |
+
G = nx.MultiGraph(G)
|
| 216 |
+
G.add_edge(1, 2)
|
| 217 |
+
with pytest.raises(
|
| 218 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 219 |
+
):
|
| 220 |
+
nx.k_truss(G, k=1)
|
| 221 |
+
|
| 222 |
+
def test_k_truss_self_loop(self):
|
| 223 |
+
G = nx.cycle_graph(3)
|
| 224 |
+
G.add_edge(0, 0)
|
| 225 |
+
with pytest.raises(
|
| 226 |
+
nx.NetworkXNotImplemented, match="Input graph has self loops"
|
| 227 |
+
):
|
| 228 |
+
nx.k_truss(G, k=1)
|
| 229 |
+
|
| 230 |
+
def test_onion_layers(self):
|
| 231 |
+
layers = nx.onion_layers(self.G)
|
| 232 |
+
nodes_by_layer = [
|
| 233 |
+
sorted(n for n in layers if layers[n] == val) for val in range(1, 7)
|
| 234 |
+
]
|
| 235 |
+
assert nodes_equal(nodes_by_layer[0], [21])
|
| 236 |
+
assert nodes_equal(nodes_by_layer[1], [17, 18, 19, 20])
|
| 237 |
+
assert nodes_equal(nodes_by_layer[2], [10, 12, 13, 14, 15, 16])
|
| 238 |
+
assert nodes_equal(nodes_by_layer[3], [9, 11])
|
| 239 |
+
assert nodes_equal(nodes_by_layer[4], [1, 2, 4, 5, 6, 8])
|
| 240 |
+
assert nodes_equal(nodes_by_layer[5], [3, 7])
|
| 241 |
+
|
| 242 |
+
def test_onion_digraph(self):
|
| 243 |
+
G = nx.complete_graph(3)
|
| 244 |
+
G = nx.DiGraph(G)
|
| 245 |
+
G.add_edge(2, 1)
|
| 246 |
+
with pytest.raises(
|
| 247 |
+
nx.NetworkXNotImplemented, match="not implemented for directed type"
|
| 248 |
+
):
|
| 249 |
+
nx.onion_layers(G)
|
| 250 |
+
|
| 251 |
+
def test_onion_multigraph(self):
|
| 252 |
+
G = nx.complete_graph(3)
|
| 253 |
+
G = nx.MultiGraph(G)
|
| 254 |
+
G.add_edge(1, 2)
|
| 255 |
+
with pytest.raises(
|
| 256 |
+
nx.NetworkXNotImplemented, match="not implemented for multigraph type"
|
| 257 |
+
):
|
| 258 |
+
nx.onion_layers(G)
|
| 259 |
+
|
| 260 |
+
def test_onion_self_loop(self):
|
| 261 |
+
G = nx.cycle_graph(3)
|
| 262 |
+
G.add_edge(0, 0)
|
| 263 |
+
with pytest.raises(
|
| 264 |
+
nx.NetworkXNotImplemented, match="Input graph contains self loops"
|
| 265 |
+
):
|
| 266 |
+
nx.onion_layers(G)
|