Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/boundary.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/bridges.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/chordal.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/clique.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/core.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/dominance.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/efficiency_measures.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/euler.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/graphical.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/hybrid.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/isolate.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/link_prediction.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/moral.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/non_randomness.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/planar_drawing.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/regular.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/similarity.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/sparsifiers.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/swap.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/triads.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/vitality.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/wiener.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/__pycache__/neighbor_degree.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/__pycache__/pairs.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/base_test.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/test_connectivity.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/test_correlation.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/base_test.py +81 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/test_pairs.py +87 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/__pycache__/greedy_coloring.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/equitable_coloring.py +505 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/tests/__pycache__/test_coloring.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/__init__.py +6 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/__pycache__/__init__.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/__pycache__/strongly_connected.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/attracting.py +115 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/biconnected.py +394 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/connected.py +216 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/semiconnected.py +71 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/strongly_connected.py +351 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_connected.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_semiconnected.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_strongly_connected.cpython-310.pyc +0 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_attracting.py +70 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_semiconnected.py +55 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_strongly_connected.py +193 -0
- wemm/lib/python3.10/site-packages/networkx/algorithms/components/weakly_connected.py +197 -0
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (5.49 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/boundary.cpython-310.pyc
ADDED
|
Binary file (5.36 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/bridges.cpython-310.pyc
ADDED
|
Binary file (6.09 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/chordal.cpython-310.pyc
ADDED
|
Binary file (12.5 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/clique.cpython-310.pyc
ADDED
|
Binary file (27 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/core.cpython-310.pyc
ADDED
|
Binary file (17.7 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/dominance.cpython-310.pyc
ADDED
|
Binary file (3.96 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/efficiency_measures.cpython-310.pyc
ADDED
|
Binary file (4.85 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/euler.cpython-310.pyc
ADDED
|
Binary file (12.6 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/graphical.cpython-310.pyc
ADDED
|
Binary file (12.7 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/hybrid.cpython-310.pyc
ADDED
|
Binary file (4.59 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/isolate.cpython-310.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/link_prediction.cpython-310.pyc
ADDED
|
Binary file (23 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/moral.cpython-310.pyc
ADDED
|
Binary file (1.76 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/non_randomness.cpython-310.pyc
ADDED
|
Binary file (3.2 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/planar_drawing.cpython-310.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/regular.cpython-310.pyc
ADDED
|
Binary file (7.4 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/similarity.cpython-310.pyc
ADDED
|
Binary file (53.8 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/sparsifiers.cpython-310.pyc
ADDED
|
Binary file (6.87 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/swap.cpython-310.pyc
ADDED
|
Binary file (9.92 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/triads.cpython-310.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/vitality.cpython-310.pyc
ADDED
|
Binary file (2.52 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/__pycache__/wiener.cpython-310.pyc
ADDED
|
Binary file (7.99 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/__pycache__/neighbor_degree.cpython-310.pyc
ADDED
|
Binary file (4.69 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/__pycache__/pairs.cpython-310.pyc
ADDED
|
Binary file (3.88 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/base_test.cpython-310.pyc
ADDED
|
Binary file (2.47 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/test_connectivity.cpython-310.pyc
ADDED
|
Binary file (4.4 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/__pycache__/test_correlation.cpython-310.pyc
ADDED
|
Binary file (7.08 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/base_test.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class BaseTestAttributeMixing:
|
| 5 |
+
@classmethod
|
| 6 |
+
def setup_class(cls):
|
| 7 |
+
G = nx.Graph()
|
| 8 |
+
G.add_nodes_from([0, 1], fish="one")
|
| 9 |
+
G.add_nodes_from([2, 3], fish="two")
|
| 10 |
+
G.add_nodes_from([4], fish="red")
|
| 11 |
+
G.add_nodes_from([5], fish="blue")
|
| 12 |
+
G.add_edges_from([(0, 1), (2, 3), (0, 4), (2, 5)])
|
| 13 |
+
cls.G = G
|
| 14 |
+
|
| 15 |
+
D = nx.DiGraph()
|
| 16 |
+
D.add_nodes_from([0, 1], fish="one")
|
| 17 |
+
D.add_nodes_from([2, 3], fish="two")
|
| 18 |
+
D.add_nodes_from([4], fish="red")
|
| 19 |
+
D.add_nodes_from([5], fish="blue")
|
| 20 |
+
D.add_edges_from([(0, 1), (2, 3), (0, 4), (2, 5)])
|
| 21 |
+
cls.D = D
|
| 22 |
+
|
| 23 |
+
M = nx.MultiGraph()
|
| 24 |
+
M.add_nodes_from([0, 1], fish="one")
|
| 25 |
+
M.add_nodes_from([2, 3], fish="two")
|
| 26 |
+
M.add_nodes_from([4], fish="red")
|
| 27 |
+
M.add_nodes_from([5], fish="blue")
|
| 28 |
+
M.add_edges_from([(0, 1), (0, 1), (2, 3)])
|
| 29 |
+
cls.M = M
|
| 30 |
+
|
| 31 |
+
S = nx.Graph()
|
| 32 |
+
S.add_nodes_from([0, 1], fish="one")
|
| 33 |
+
S.add_nodes_from([2, 3], fish="two")
|
| 34 |
+
S.add_nodes_from([4], fish="red")
|
| 35 |
+
S.add_nodes_from([5], fish="blue")
|
| 36 |
+
S.add_edge(0, 0)
|
| 37 |
+
S.add_edge(2, 2)
|
| 38 |
+
cls.S = S
|
| 39 |
+
|
| 40 |
+
N = nx.Graph()
|
| 41 |
+
N.add_nodes_from([0, 1], margin=-2)
|
| 42 |
+
N.add_nodes_from([2, 3], margin=-2)
|
| 43 |
+
N.add_nodes_from([4], margin=-3)
|
| 44 |
+
N.add_nodes_from([5], margin=-4)
|
| 45 |
+
N.add_edges_from([(0, 1), (2, 3), (0, 4), (2, 5)])
|
| 46 |
+
cls.N = N
|
| 47 |
+
|
| 48 |
+
F = nx.Graph()
|
| 49 |
+
F.add_edges_from([(0, 3), (1, 3), (2, 3)], weight=0.5)
|
| 50 |
+
F.add_edge(0, 2, weight=1)
|
| 51 |
+
nx.set_node_attributes(F, dict(F.degree(weight="weight")), "margin")
|
| 52 |
+
cls.F = F
|
| 53 |
+
|
| 54 |
+
K = nx.Graph()
|
| 55 |
+
K.add_nodes_from([1, 2], margin=-1)
|
| 56 |
+
K.add_nodes_from([3], margin=1)
|
| 57 |
+
K.add_nodes_from([4], margin=2)
|
| 58 |
+
K.add_edges_from([(3, 4), (1, 2), (1, 3)])
|
| 59 |
+
cls.K = K
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class BaseTestDegreeMixing:
|
| 63 |
+
@classmethod
|
| 64 |
+
def setup_class(cls):
|
| 65 |
+
cls.P4 = nx.path_graph(4)
|
| 66 |
+
cls.D = nx.DiGraph()
|
| 67 |
+
cls.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
|
| 68 |
+
cls.D2 = nx.DiGraph()
|
| 69 |
+
cls.D2.add_edges_from([(0, 3), (1, 0), (1, 2), (2, 4), (4, 1), (4, 3), (4, 2)])
|
| 70 |
+
cls.M = nx.MultiGraph()
|
| 71 |
+
nx.add_path(cls.M, range(4))
|
| 72 |
+
cls.M.add_edge(0, 1)
|
| 73 |
+
cls.S = nx.Graph()
|
| 74 |
+
cls.S.add_edges_from([(0, 0), (1, 1)])
|
| 75 |
+
cls.W = nx.Graph()
|
| 76 |
+
cls.W.add_edges_from([(0, 3), (1, 3), (2, 3)], weight=0.5)
|
| 77 |
+
cls.W.add_edge(0, 2, weight=1)
|
| 78 |
+
S1 = nx.star_graph(4)
|
| 79 |
+
S2 = nx.star_graph(4)
|
| 80 |
+
cls.DS = nx.disjoint_union(S1, S2)
|
| 81 |
+
cls.DS.add_edge(4, 5)
|
wemm/lib/python3.10/site-packages/networkx/algorithms/assortativity/tests/test_pairs.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
from .base_test import BaseTestAttributeMixing, BaseTestDegreeMixing
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestAttributeMixingXY(BaseTestAttributeMixing):
|
| 7 |
+
def test_node_attribute_xy_undirected(self):
|
| 8 |
+
attrxy = sorted(nx.node_attribute_xy(self.G, "fish"))
|
| 9 |
+
attrxy_result = sorted(
|
| 10 |
+
[
|
| 11 |
+
("one", "one"),
|
| 12 |
+
("one", "one"),
|
| 13 |
+
("two", "two"),
|
| 14 |
+
("two", "two"),
|
| 15 |
+
("one", "red"),
|
| 16 |
+
("red", "one"),
|
| 17 |
+
("blue", "two"),
|
| 18 |
+
("two", "blue"),
|
| 19 |
+
]
|
| 20 |
+
)
|
| 21 |
+
assert attrxy == attrxy_result
|
| 22 |
+
|
| 23 |
+
def test_node_attribute_xy_undirected_nodes(self):
|
| 24 |
+
attrxy = sorted(nx.node_attribute_xy(self.G, "fish", nodes=["one", "yellow"]))
|
| 25 |
+
attrxy_result = sorted([])
|
| 26 |
+
assert attrxy == attrxy_result
|
| 27 |
+
|
| 28 |
+
def test_node_attribute_xy_directed(self):
|
| 29 |
+
attrxy = sorted(nx.node_attribute_xy(self.D, "fish"))
|
| 30 |
+
attrxy_result = sorted(
|
| 31 |
+
[("one", "one"), ("two", "two"), ("one", "red"), ("two", "blue")]
|
| 32 |
+
)
|
| 33 |
+
assert attrxy == attrxy_result
|
| 34 |
+
|
| 35 |
+
def test_node_attribute_xy_multigraph(self):
|
| 36 |
+
attrxy = sorted(nx.node_attribute_xy(self.M, "fish"))
|
| 37 |
+
attrxy_result = [
|
| 38 |
+
("one", "one"),
|
| 39 |
+
("one", "one"),
|
| 40 |
+
("one", "one"),
|
| 41 |
+
("one", "one"),
|
| 42 |
+
("two", "two"),
|
| 43 |
+
("two", "two"),
|
| 44 |
+
]
|
| 45 |
+
assert attrxy == attrxy_result
|
| 46 |
+
|
| 47 |
+
def test_node_attribute_xy_selfloop(self):
|
| 48 |
+
attrxy = sorted(nx.node_attribute_xy(self.S, "fish"))
|
| 49 |
+
attrxy_result = [("one", "one"), ("two", "two")]
|
| 50 |
+
assert attrxy == attrxy_result
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class TestDegreeMixingXY(BaseTestDegreeMixing):
|
| 54 |
+
def test_node_degree_xy_undirected(self):
|
| 55 |
+
xy = sorted(nx.node_degree_xy(self.P4))
|
| 56 |
+
xy_result = sorted([(1, 2), (2, 1), (2, 2), (2, 2), (1, 2), (2, 1)])
|
| 57 |
+
assert xy == xy_result
|
| 58 |
+
|
| 59 |
+
def test_node_degree_xy_undirected_nodes(self):
|
| 60 |
+
xy = sorted(nx.node_degree_xy(self.P4, nodes=[0, 1, -1]))
|
| 61 |
+
xy_result = sorted([(1, 2), (2, 1)])
|
| 62 |
+
assert xy == xy_result
|
| 63 |
+
|
| 64 |
+
def test_node_degree_xy_directed(self):
|
| 65 |
+
xy = sorted(nx.node_degree_xy(self.D))
|
| 66 |
+
xy_result = sorted([(2, 1), (2, 3), (1, 3), (1, 3)])
|
| 67 |
+
assert xy == xy_result
|
| 68 |
+
|
| 69 |
+
def test_node_degree_xy_multigraph(self):
|
| 70 |
+
xy = sorted(nx.node_degree_xy(self.M))
|
| 71 |
+
xy_result = sorted(
|
| 72 |
+
[(2, 3), (2, 3), (3, 2), (3, 2), (2, 3), (3, 2), (1, 2), (2, 1)]
|
| 73 |
+
)
|
| 74 |
+
assert xy == xy_result
|
| 75 |
+
|
| 76 |
+
def test_node_degree_xy_selfloop(self):
|
| 77 |
+
xy = sorted(nx.node_degree_xy(self.S))
|
| 78 |
+
xy_result = sorted([(2, 2), (2, 2)])
|
| 79 |
+
assert xy == xy_result
|
| 80 |
+
|
| 81 |
+
def test_node_degree_xy_weighted(self):
|
| 82 |
+
G = nx.Graph()
|
| 83 |
+
G.add_edge(1, 2, weight=7)
|
| 84 |
+
G.add_edge(2, 3, weight=10)
|
| 85 |
+
xy = sorted(nx.node_degree_xy(G, weight="weight"))
|
| 86 |
+
xy_result = sorted([(7, 17), (17, 10), (17, 7), (10, 17)])
|
| 87 |
+
assert xy == xy_result
|
wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (364 Bytes). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/__pycache__/greedy_coloring.cpython-310.pyc
ADDED
|
Binary file (16.6 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/equitable_coloring.py
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Equitable coloring of graphs with bounded degree.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from collections import defaultdict
|
| 6 |
+
|
| 7 |
+
import networkx as nx
|
| 8 |
+
|
| 9 |
+
__all__ = ["equitable_color"]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@nx._dispatchable
|
| 13 |
+
def is_coloring(G, coloring):
|
| 14 |
+
"""Determine if the coloring is a valid coloring for the graph G."""
|
| 15 |
+
# Verify that the coloring is valid.
|
| 16 |
+
return all(coloring[s] != coloring[d] for s, d in G.edges)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@nx._dispatchable
|
| 20 |
+
def is_equitable(G, coloring, num_colors=None):
|
| 21 |
+
"""Determines if the coloring is valid and equitable for the graph G."""
|
| 22 |
+
|
| 23 |
+
if not is_coloring(G, coloring):
|
| 24 |
+
return False
|
| 25 |
+
|
| 26 |
+
# Verify whether it is equitable.
|
| 27 |
+
color_set_size = defaultdict(int)
|
| 28 |
+
for color in coloring.values():
|
| 29 |
+
color_set_size[color] += 1
|
| 30 |
+
|
| 31 |
+
if num_colors is not None:
|
| 32 |
+
for color in range(num_colors):
|
| 33 |
+
if color not in color_set_size:
|
| 34 |
+
# These colors do not have any vertices attached to them.
|
| 35 |
+
color_set_size[color] = 0
|
| 36 |
+
|
| 37 |
+
# If there are more than 2 distinct values, the coloring cannot be equitable
|
| 38 |
+
all_set_sizes = set(color_set_size.values())
|
| 39 |
+
if len(all_set_sizes) == 0 and num_colors is None: # Was an empty graph
|
| 40 |
+
return True
|
| 41 |
+
elif len(all_set_sizes) == 1:
|
| 42 |
+
return True
|
| 43 |
+
elif len(all_set_sizes) == 2:
|
| 44 |
+
a, b = list(all_set_sizes)
|
| 45 |
+
return abs(a - b) <= 1
|
| 46 |
+
else: # len(all_set_sizes) > 2:
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def make_C_from_F(F):
|
| 51 |
+
C = defaultdict(list)
|
| 52 |
+
for node, color in F.items():
|
| 53 |
+
C[color].append(node)
|
| 54 |
+
|
| 55 |
+
return C
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def make_N_from_L_C(L, C):
|
| 59 |
+
nodes = L.keys()
|
| 60 |
+
colors = C.keys()
|
| 61 |
+
return {
|
| 62 |
+
(node, color): sum(1 for v in L[node] if v in C[color])
|
| 63 |
+
for node in nodes
|
| 64 |
+
for color in colors
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def make_H_from_C_N(C, N):
|
| 69 |
+
return {
|
| 70 |
+
(c1, c2): sum(1 for node in C[c1] if N[(node, c2)] == 0) for c1 in C for c2 in C
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def change_color(u, X, Y, N, H, F, C, L):
|
| 75 |
+
"""Change the color of 'u' from X to Y and update N, H, F, C."""
|
| 76 |
+
assert F[u] == X and X != Y
|
| 77 |
+
|
| 78 |
+
# Change the class of 'u' from X to Y
|
| 79 |
+
F[u] = Y
|
| 80 |
+
|
| 81 |
+
for k in C:
|
| 82 |
+
# 'u' witnesses an edge from k -> Y instead of from k -> X now.
|
| 83 |
+
if N[u, k] == 0:
|
| 84 |
+
H[(X, k)] -= 1
|
| 85 |
+
H[(Y, k)] += 1
|
| 86 |
+
|
| 87 |
+
for v in L[u]:
|
| 88 |
+
# 'v' has lost a neighbor in X and gained one in Y
|
| 89 |
+
N[(v, X)] -= 1
|
| 90 |
+
N[(v, Y)] += 1
|
| 91 |
+
|
| 92 |
+
if N[(v, X)] == 0:
|
| 93 |
+
# 'v' witnesses F[v] -> X
|
| 94 |
+
H[(F[v], X)] += 1
|
| 95 |
+
|
| 96 |
+
if N[(v, Y)] == 1:
|
| 97 |
+
# 'v' no longer witnesses F[v] -> Y
|
| 98 |
+
H[(F[v], Y)] -= 1
|
| 99 |
+
|
| 100 |
+
C[X].remove(u)
|
| 101 |
+
C[Y].append(u)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def move_witnesses(src_color, dst_color, N, H, F, C, T_cal, L):
|
| 105 |
+
"""Move witness along a path from src_color to dst_color."""
|
| 106 |
+
X = src_color
|
| 107 |
+
while X != dst_color:
|
| 108 |
+
Y = T_cal[X]
|
| 109 |
+
# Move _any_ witness from X to Y = T_cal[X]
|
| 110 |
+
w = next(x for x in C[X] if N[(x, Y)] == 0)
|
| 111 |
+
change_color(w, X, Y, N=N, H=H, F=F, C=C, L=L)
|
| 112 |
+
X = Y
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
@nx._dispatchable(mutates_input=True)
|
| 116 |
+
def pad_graph(G, num_colors):
|
| 117 |
+
"""Add a disconnected complete clique K_p such that the number of nodes in
|
| 118 |
+
the graph becomes a multiple of `num_colors`.
|
| 119 |
+
|
| 120 |
+
Assumes that the graph's nodes are labelled using integers.
|
| 121 |
+
|
| 122 |
+
Returns the number of nodes with each color.
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
n_ = len(G)
|
| 126 |
+
r = num_colors - 1
|
| 127 |
+
|
| 128 |
+
# Ensure that the number of nodes in G is a multiple of (r + 1)
|
| 129 |
+
s = n_ // (r + 1)
|
| 130 |
+
if n_ != s * (r + 1):
|
| 131 |
+
p = (r + 1) - n_ % (r + 1)
|
| 132 |
+
s += 1
|
| 133 |
+
|
| 134 |
+
# Complete graph K_p between (imaginary) nodes [n_, ... , n_ + p]
|
| 135 |
+
K = nx.relabel_nodes(nx.complete_graph(p), {idx: idx + n_ for idx in range(p)})
|
| 136 |
+
G.add_edges_from(K.edges)
|
| 137 |
+
|
| 138 |
+
return s
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def procedure_P(V_minus, V_plus, N, H, F, C, L, excluded_colors=None):
|
| 142 |
+
"""Procedure P as described in the paper."""
|
| 143 |
+
|
| 144 |
+
if excluded_colors is None:
|
| 145 |
+
excluded_colors = set()
|
| 146 |
+
|
| 147 |
+
A_cal = set()
|
| 148 |
+
T_cal = {}
|
| 149 |
+
R_cal = []
|
| 150 |
+
|
| 151 |
+
# BFS to determine A_cal, i.e. colors reachable from V-
|
| 152 |
+
reachable = [V_minus]
|
| 153 |
+
marked = set(reachable)
|
| 154 |
+
idx = 0
|
| 155 |
+
|
| 156 |
+
while idx < len(reachable):
|
| 157 |
+
pop = reachable[idx]
|
| 158 |
+
idx += 1
|
| 159 |
+
|
| 160 |
+
A_cal.add(pop)
|
| 161 |
+
R_cal.append(pop)
|
| 162 |
+
|
| 163 |
+
# TODO: Checking whether a color has been visited can be made faster by
|
| 164 |
+
# using a look-up table instead of testing for membership in a set by a
|
| 165 |
+
# logarithmic factor.
|
| 166 |
+
next_layer = []
|
| 167 |
+
for k in C:
|
| 168 |
+
if (
|
| 169 |
+
H[(k, pop)] > 0
|
| 170 |
+
and k not in A_cal
|
| 171 |
+
and k not in excluded_colors
|
| 172 |
+
and k not in marked
|
| 173 |
+
):
|
| 174 |
+
next_layer.append(k)
|
| 175 |
+
|
| 176 |
+
for dst in next_layer:
|
| 177 |
+
# Record that `dst` can reach `pop`
|
| 178 |
+
T_cal[dst] = pop
|
| 179 |
+
|
| 180 |
+
marked.update(next_layer)
|
| 181 |
+
reachable.extend(next_layer)
|
| 182 |
+
|
| 183 |
+
# Variables for the algorithm
|
| 184 |
+
b = len(C) - len(A_cal)
|
| 185 |
+
|
| 186 |
+
if V_plus in A_cal:
|
| 187 |
+
# Easy case: V+ is in A_cal
|
| 188 |
+
# Move one node from V+ to V- using T_cal to find the parents.
|
| 189 |
+
move_witnesses(V_plus, V_minus, N=N, H=H, F=F, C=C, T_cal=T_cal, L=L)
|
| 190 |
+
else:
|
| 191 |
+
# If there is a solo edge, we can resolve the situation by
|
| 192 |
+
# moving witnesses from B to A, making G[A] equitable and then
|
| 193 |
+
# recursively balancing G[B - w] with a different V_minus and
|
| 194 |
+
# but the same V_plus.
|
| 195 |
+
|
| 196 |
+
A_0 = set()
|
| 197 |
+
A_cal_0 = set()
|
| 198 |
+
num_terminal_sets_found = 0
|
| 199 |
+
made_equitable = False
|
| 200 |
+
|
| 201 |
+
for W_1 in R_cal[::-1]:
|
| 202 |
+
for v in C[W_1]:
|
| 203 |
+
X = None
|
| 204 |
+
|
| 205 |
+
for U in C:
|
| 206 |
+
if N[(v, U)] == 0 and U in A_cal and U != W_1:
|
| 207 |
+
X = U
|
| 208 |
+
|
| 209 |
+
# v does not witness an edge in H[A_cal]
|
| 210 |
+
if X is None:
|
| 211 |
+
continue
|
| 212 |
+
|
| 213 |
+
for U in C:
|
| 214 |
+
# Note: Departing from the paper here.
|
| 215 |
+
if N[(v, U)] >= 1 and U not in A_cal:
|
| 216 |
+
X_prime = U
|
| 217 |
+
w = v
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
# Finding the solo neighbor of w in X_prime
|
| 221 |
+
y = next(
|
| 222 |
+
node
|
| 223 |
+
for node in L[w]
|
| 224 |
+
if F[node] == X_prime and N[(node, W_1)] == 1
|
| 225 |
+
)
|
| 226 |
+
except StopIteration:
|
| 227 |
+
pass
|
| 228 |
+
else:
|
| 229 |
+
W = W_1
|
| 230 |
+
|
| 231 |
+
# Move w from W to X, now X has one extra node.
|
| 232 |
+
change_color(w, W, X, N=N, H=H, F=F, C=C, L=L)
|
| 233 |
+
|
| 234 |
+
# Move witness from X to V_minus, making the coloring
|
| 235 |
+
# equitable.
|
| 236 |
+
move_witnesses(
|
| 237 |
+
src_color=X,
|
| 238 |
+
dst_color=V_minus,
|
| 239 |
+
N=N,
|
| 240 |
+
H=H,
|
| 241 |
+
F=F,
|
| 242 |
+
C=C,
|
| 243 |
+
T_cal=T_cal,
|
| 244 |
+
L=L,
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Move y from X_prime to W, making W the correct size.
|
| 248 |
+
change_color(y, X_prime, W, N=N, H=H, F=F, C=C, L=L)
|
| 249 |
+
|
| 250 |
+
# Then call the procedure on G[B - y]
|
| 251 |
+
procedure_P(
|
| 252 |
+
V_minus=X_prime,
|
| 253 |
+
V_plus=V_plus,
|
| 254 |
+
N=N,
|
| 255 |
+
H=H,
|
| 256 |
+
C=C,
|
| 257 |
+
F=F,
|
| 258 |
+
L=L,
|
| 259 |
+
excluded_colors=excluded_colors.union(A_cal),
|
| 260 |
+
)
|
| 261 |
+
made_equitable = True
|
| 262 |
+
break
|
| 263 |
+
|
| 264 |
+
if made_equitable:
|
| 265 |
+
break
|
| 266 |
+
else:
|
| 267 |
+
# No node in W_1 was found such that
|
| 268 |
+
# it had a solo-neighbor.
|
| 269 |
+
A_cal_0.add(W_1)
|
| 270 |
+
A_0.update(C[W_1])
|
| 271 |
+
num_terminal_sets_found += 1
|
| 272 |
+
|
| 273 |
+
if num_terminal_sets_found == b:
|
| 274 |
+
# Otherwise, construct the maximal independent set and find
|
| 275 |
+
# a pair of z_1, z_2 as in Case II.
|
| 276 |
+
|
| 277 |
+
# BFS to determine B_cal': the set of colors reachable from V+
|
| 278 |
+
B_cal_prime = set()
|
| 279 |
+
T_cal_prime = {}
|
| 280 |
+
|
| 281 |
+
reachable = [V_plus]
|
| 282 |
+
marked = set(reachable)
|
| 283 |
+
idx = 0
|
| 284 |
+
while idx < len(reachable):
|
| 285 |
+
pop = reachable[idx]
|
| 286 |
+
idx += 1
|
| 287 |
+
|
| 288 |
+
B_cal_prime.add(pop)
|
| 289 |
+
|
| 290 |
+
# No need to check for excluded_colors here because
|
| 291 |
+
# they only exclude colors from A_cal
|
| 292 |
+
next_layer = [
|
| 293 |
+
k
|
| 294 |
+
for k in C
|
| 295 |
+
if H[(pop, k)] > 0 and k not in B_cal_prime and k not in marked
|
| 296 |
+
]
|
| 297 |
+
|
| 298 |
+
for dst in next_layer:
|
| 299 |
+
T_cal_prime[pop] = dst
|
| 300 |
+
|
| 301 |
+
marked.update(next_layer)
|
| 302 |
+
reachable.extend(next_layer)
|
| 303 |
+
|
| 304 |
+
# Construct the independent set of G[B']
|
| 305 |
+
I_set = set()
|
| 306 |
+
I_covered = set()
|
| 307 |
+
W_covering = {}
|
| 308 |
+
|
| 309 |
+
B_prime = [node for k in B_cal_prime for node in C[k]]
|
| 310 |
+
|
| 311 |
+
# Add the nodes in V_plus to I first.
|
| 312 |
+
for z in C[V_plus] + B_prime:
|
| 313 |
+
if z in I_covered or F[z] not in B_cal_prime:
|
| 314 |
+
continue
|
| 315 |
+
|
| 316 |
+
I_set.add(z)
|
| 317 |
+
I_covered.add(z)
|
| 318 |
+
I_covered.update(list(L[z]))
|
| 319 |
+
|
| 320 |
+
for w in L[z]:
|
| 321 |
+
if F[w] in A_cal_0 and N[(z, F[w])] == 1:
|
| 322 |
+
if w not in W_covering:
|
| 323 |
+
W_covering[w] = z
|
| 324 |
+
else:
|
| 325 |
+
# Found z1, z2 which have the same solo
|
| 326 |
+
# neighbor in some W
|
| 327 |
+
z_1 = W_covering[w]
|
| 328 |
+
# z_2 = z
|
| 329 |
+
|
| 330 |
+
Z = F[z_1]
|
| 331 |
+
W = F[w]
|
| 332 |
+
|
| 333 |
+
# shift nodes along W, V-
|
| 334 |
+
move_witnesses(
|
| 335 |
+
W, V_minus, N=N, H=H, F=F, C=C, T_cal=T_cal, L=L
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
# shift nodes along V+ to Z
|
| 339 |
+
move_witnesses(
|
| 340 |
+
V_plus,
|
| 341 |
+
Z,
|
| 342 |
+
N=N,
|
| 343 |
+
H=H,
|
| 344 |
+
F=F,
|
| 345 |
+
C=C,
|
| 346 |
+
T_cal=T_cal_prime,
|
| 347 |
+
L=L,
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
# change color of z_1 to W
|
| 351 |
+
change_color(z_1, Z, W, N=N, H=H, F=F, C=C, L=L)
|
| 352 |
+
|
| 353 |
+
# change color of w to some color in B_cal
|
| 354 |
+
W_plus = next(
|
| 355 |
+
k for k in C if N[(w, k)] == 0 and k not in A_cal
|
| 356 |
+
)
|
| 357 |
+
change_color(w, W, W_plus, N=N, H=H, F=F, C=C, L=L)
|
| 358 |
+
|
| 359 |
+
# recurse with G[B \cup W*]
|
| 360 |
+
excluded_colors.update(
|
| 361 |
+
[k for k in C if k != W and k not in B_cal_prime]
|
| 362 |
+
)
|
| 363 |
+
procedure_P(
|
| 364 |
+
V_minus=W,
|
| 365 |
+
V_plus=W_plus,
|
| 366 |
+
N=N,
|
| 367 |
+
H=H,
|
| 368 |
+
C=C,
|
| 369 |
+
F=F,
|
| 370 |
+
L=L,
|
| 371 |
+
excluded_colors=excluded_colors,
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
made_equitable = True
|
| 375 |
+
break
|
| 376 |
+
|
| 377 |
+
if made_equitable:
|
| 378 |
+
break
|
| 379 |
+
else:
|
| 380 |
+
assert False, (
|
| 381 |
+
"Must find a w which is the solo neighbor "
|
| 382 |
+
"of two vertices in B_cal_prime."
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
if made_equitable:
|
| 386 |
+
break
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
@nx._dispatchable
|
| 390 |
+
def equitable_color(G, num_colors):
|
| 391 |
+
"""Provides an equitable coloring for nodes of `G`.
|
| 392 |
+
|
| 393 |
+
Attempts to color a graph using `num_colors` colors, where no neighbors of
|
| 394 |
+
a node can have same color as the node itself and the number of nodes with
|
| 395 |
+
each color differ by at most 1. `num_colors` must be greater than the
|
| 396 |
+
maximum degree of `G`. The algorithm is described in [1]_ and has
|
| 397 |
+
complexity O(num_colors * n**2).
|
| 398 |
+
|
| 399 |
+
Parameters
|
| 400 |
+
----------
|
| 401 |
+
G : networkX graph
|
| 402 |
+
The nodes of this graph will be colored.
|
| 403 |
+
|
| 404 |
+
num_colors : number of colors to use
|
| 405 |
+
This number must be at least one more than the maximum degree of nodes
|
| 406 |
+
in the graph.
|
| 407 |
+
|
| 408 |
+
Returns
|
| 409 |
+
-------
|
| 410 |
+
A dictionary with keys representing nodes and values representing
|
| 411 |
+
corresponding coloring.
|
| 412 |
+
|
| 413 |
+
Examples
|
| 414 |
+
--------
|
| 415 |
+
>>> G = nx.cycle_graph(4)
|
| 416 |
+
>>> nx.coloring.equitable_color(G, num_colors=3) # doctest: +SKIP
|
| 417 |
+
{0: 2, 1: 1, 2: 2, 3: 0}
|
| 418 |
+
|
| 419 |
+
Raises
|
| 420 |
+
------
|
| 421 |
+
NetworkXAlgorithmError
|
| 422 |
+
If `num_colors` is not at least the maximum degree of the graph `G`
|
| 423 |
+
|
| 424 |
+
References
|
| 425 |
+
----------
|
| 426 |
+
.. [1] Kierstead, H. A., Kostochka, A. V., Mydlarz, M., & Szemerédi, E.
|
| 427 |
+
(2010). A fast algorithm for equitable coloring. Combinatorica, 30(2),
|
| 428 |
+
217-224.
|
| 429 |
+
"""
|
| 430 |
+
|
| 431 |
+
# Map nodes to integers for simplicity later.
|
| 432 |
+
nodes_to_int = {}
|
| 433 |
+
int_to_nodes = {}
|
| 434 |
+
|
| 435 |
+
for idx, node in enumerate(G.nodes):
|
| 436 |
+
nodes_to_int[node] = idx
|
| 437 |
+
int_to_nodes[idx] = node
|
| 438 |
+
|
| 439 |
+
G = nx.relabel_nodes(G, nodes_to_int, copy=True)
|
| 440 |
+
|
| 441 |
+
# Basic graph statistics and sanity check.
|
| 442 |
+
if len(G.nodes) > 0:
|
| 443 |
+
r_ = max(G.degree(node) for node in G.nodes)
|
| 444 |
+
else:
|
| 445 |
+
r_ = 0
|
| 446 |
+
|
| 447 |
+
if r_ >= num_colors:
|
| 448 |
+
raise nx.NetworkXAlgorithmError(
|
| 449 |
+
f"Graph has maximum degree {r_}, needs "
|
| 450 |
+
f"{r_ + 1} (> {num_colors}) colors for guaranteed coloring."
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
# Ensure that the number of nodes in G is a multiple of (r + 1)
|
| 454 |
+
pad_graph(G, num_colors)
|
| 455 |
+
|
| 456 |
+
# Starting the algorithm.
|
| 457 |
+
# L = {node: list(G.neighbors(node)) for node in G.nodes}
|
| 458 |
+
L_ = {node: [] for node in G.nodes}
|
| 459 |
+
|
| 460 |
+
# Arbitrary equitable allocation of colors to nodes.
|
| 461 |
+
F = {node: idx % num_colors for idx, node in enumerate(G.nodes)}
|
| 462 |
+
|
| 463 |
+
C = make_C_from_F(F)
|
| 464 |
+
|
| 465 |
+
# The neighborhood is empty initially.
|
| 466 |
+
N = make_N_from_L_C(L_, C)
|
| 467 |
+
|
| 468 |
+
# Currently all nodes witness all edges.
|
| 469 |
+
H = make_H_from_C_N(C, N)
|
| 470 |
+
|
| 471 |
+
# Start of algorithm.
|
| 472 |
+
edges_seen = set()
|
| 473 |
+
|
| 474 |
+
for u in sorted(G.nodes):
|
| 475 |
+
for v in sorted(G.neighbors(u)):
|
| 476 |
+
# Do not double count edges if (v, u) has already been seen.
|
| 477 |
+
if (v, u) in edges_seen:
|
| 478 |
+
continue
|
| 479 |
+
|
| 480 |
+
edges_seen.add((u, v))
|
| 481 |
+
|
| 482 |
+
L_[u].append(v)
|
| 483 |
+
L_[v].append(u)
|
| 484 |
+
|
| 485 |
+
N[(u, F[v])] += 1
|
| 486 |
+
N[(v, F[u])] += 1
|
| 487 |
+
|
| 488 |
+
if F[u] != F[v]:
|
| 489 |
+
# Were 'u' and 'v' witnesses for F[u] -> F[v] or F[v] -> F[u]?
|
| 490 |
+
if N[(u, F[v])] == 1:
|
| 491 |
+
H[F[u], F[v]] -= 1 # u cannot witness an edge between F[u], F[v]
|
| 492 |
+
|
| 493 |
+
if N[(v, F[u])] == 1:
|
| 494 |
+
H[F[v], F[u]] -= 1 # v cannot witness an edge between F[v], F[u]
|
| 495 |
+
|
| 496 |
+
if N[(u, F[u])] != 0:
|
| 497 |
+
# Find the first color where 'u' does not have any neighbors.
|
| 498 |
+
Y = next(k for k in C if N[(u, k)] == 0)
|
| 499 |
+
X = F[u]
|
| 500 |
+
change_color(u, X, Y, N=N, H=H, F=F, C=C, L=L_)
|
| 501 |
+
|
| 502 |
+
# Procedure P
|
| 503 |
+
procedure_P(V_minus=X, V_plus=Y, N=N, H=H, F=F, C=C, L=L_)
|
| 504 |
+
|
| 505 |
+
return {int_to_nodes[x]: F[x] for x in int_to_nodes}
|
wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/coloring/tests/__pycache__/test_coloring.cpython-310.pyc
ADDED
|
Binary file (17.9 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .connected import *
|
| 2 |
+
from .strongly_connected import *
|
| 3 |
+
from .weakly_connected import *
|
| 4 |
+
from .attracting import *
|
| 5 |
+
from .biconnected import *
|
| 6 |
+
from .semiconnected import *
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (334 Bytes). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/__pycache__/strongly_connected.cpython-310.pyc
ADDED
|
Binary file (9.35 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/attracting.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Attracting components."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils.decorators import not_implemented_for
|
| 5 |
+
|
| 6 |
+
__all__ = [
|
| 7 |
+
"number_attracting_components",
|
| 8 |
+
"attracting_components",
|
| 9 |
+
"is_attracting_component",
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@not_implemented_for("undirected")
|
| 14 |
+
@nx._dispatchable
|
| 15 |
+
def attracting_components(G):
|
| 16 |
+
"""Generates the attracting components in `G`.
|
| 17 |
+
|
| 18 |
+
An attracting component in a directed graph `G` is a strongly connected
|
| 19 |
+
component with the property that a random walker on the graph will never
|
| 20 |
+
leave the component, once it enters the component.
|
| 21 |
+
|
| 22 |
+
The nodes in attracting components can also be thought of as recurrent
|
| 23 |
+
nodes. If a random walker enters the attractor containing the node, then
|
| 24 |
+
the node will be visited infinitely often.
|
| 25 |
+
|
| 26 |
+
To obtain induced subgraphs on each component use:
|
| 27 |
+
``(G.subgraph(c).copy() for c in attracting_components(G))``
|
| 28 |
+
|
| 29 |
+
Parameters
|
| 30 |
+
----------
|
| 31 |
+
G : DiGraph, MultiDiGraph
|
| 32 |
+
The graph to be analyzed.
|
| 33 |
+
|
| 34 |
+
Returns
|
| 35 |
+
-------
|
| 36 |
+
attractors : generator of sets
|
| 37 |
+
A generator of sets of nodes, one for each attracting component of G.
|
| 38 |
+
|
| 39 |
+
Raises
|
| 40 |
+
------
|
| 41 |
+
NetworkXNotImplemented
|
| 42 |
+
If the input graph is undirected.
|
| 43 |
+
|
| 44 |
+
See Also
|
| 45 |
+
--------
|
| 46 |
+
number_attracting_components
|
| 47 |
+
is_attracting_component
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
scc = list(nx.strongly_connected_components(G))
|
| 51 |
+
cG = nx.condensation(G, scc)
|
| 52 |
+
for n in cG:
|
| 53 |
+
if cG.out_degree(n) == 0:
|
| 54 |
+
yield scc[n]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@not_implemented_for("undirected")
|
| 58 |
+
@nx._dispatchable
|
| 59 |
+
def number_attracting_components(G):
|
| 60 |
+
"""Returns the number of attracting components in `G`.
|
| 61 |
+
|
| 62 |
+
Parameters
|
| 63 |
+
----------
|
| 64 |
+
G : DiGraph, MultiDiGraph
|
| 65 |
+
The graph to be analyzed.
|
| 66 |
+
|
| 67 |
+
Returns
|
| 68 |
+
-------
|
| 69 |
+
n : int
|
| 70 |
+
The number of attracting components in G.
|
| 71 |
+
|
| 72 |
+
Raises
|
| 73 |
+
------
|
| 74 |
+
NetworkXNotImplemented
|
| 75 |
+
If the input graph is undirected.
|
| 76 |
+
|
| 77 |
+
See Also
|
| 78 |
+
--------
|
| 79 |
+
attracting_components
|
| 80 |
+
is_attracting_component
|
| 81 |
+
|
| 82 |
+
"""
|
| 83 |
+
return sum(1 for ac in attracting_components(G))
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@not_implemented_for("undirected")
|
| 87 |
+
@nx._dispatchable
|
| 88 |
+
def is_attracting_component(G):
|
| 89 |
+
"""Returns True if `G` consists of a single attracting component.
|
| 90 |
+
|
| 91 |
+
Parameters
|
| 92 |
+
----------
|
| 93 |
+
G : DiGraph, MultiDiGraph
|
| 94 |
+
The graph to be analyzed.
|
| 95 |
+
|
| 96 |
+
Returns
|
| 97 |
+
-------
|
| 98 |
+
attracting : bool
|
| 99 |
+
True if `G` has a single attracting component. Otherwise, False.
|
| 100 |
+
|
| 101 |
+
Raises
|
| 102 |
+
------
|
| 103 |
+
NetworkXNotImplemented
|
| 104 |
+
If the input graph is undirected.
|
| 105 |
+
|
| 106 |
+
See Also
|
| 107 |
+
--------
|
| 108 |
+
attracting_components
|
| 109 |
+
number_attracting_components
|
| 110 |
+
|
| 111 |
+
"""
|
| 112 |
+
ac = list(attracting_components(G))
|
| 113 |
+
if len(ac) == 1:
|
| 114 |
+
return len(ac[0]) == len(G)
|
| 115 |
+
return False
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/biconnected.py
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Biconnected components and articulation points."""
|
| 2 |
+
|
| 3 |
+
from itertools import chain
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx.utils.decorators import not_implemented_for
|
| 7 |
+
|
| 8 |
+
__all__ = [
|
| 9 |
+
"biconnected_components",
|
| 10 |
+
"biconnected_component_edges",
|
| 11 |
+
"is_biconnected",
|
| 12 |
+
"articulation_points",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@not_implemented_for("directed")
|
| 17 |
+
@nx._dispatchable
|
| 18 |
+
def is_biconnected(G):
|
| 19 |
+
"""Returns True if the graph is biconnected, False otherwise.
|
| 20 |
+
|
| 21 |
+
A graph is biconnected if, and only if, it cannot be disconnected by
|
| 22 |
+
removing only one node (and all edges incident on that node). If
|
| 23 |
+
removing a node increases the number of disconnected components
|
| 24 |
+
in the graph, that node is called an articulation point, or cut
|
| 25 |
+
vertex. A biconnected graph has no articulation points.
|
| 26 |
+
|
| 27 |
+
Parameters
|
| 28 |
+
----------
|
| 29 |
+
G : NetworkX Graph
|
| 30 |
+
An undirected graph.
|
| 31 |
+
|
| 32 |
+
Returns
|
| 33 |
+
-------
|
| 34 |
+
biconnected : bool
|
| 35 |
+
True if the graph is biconnected, False otherwise.
|
| 36 |
+
|
| 37 |
+
Raises
|
| 38 |
+
------
|
| 39 |
+
NetworkXNotImplemented
|
| 40 |
+
If the input graph is not undirected.
|
| 41 |
+
|
| 42 |
+
Examples
|
| 43 |
+
--------
|
| 44 |
+
>>> G = nx.path_graph(4)
|
| 45 |
+
>>> print(nx.is_biconnected(G))
|
| 46 |
+
False
|
| 47 |
+
>>> G.add_edge(0, 3)
|
| 48 |
+
>>> print(nx.is_biconnected(G))
|
| 49 |
+
True
|
| 50 |
+
|
| 51 |
+
See Also
|
| 52 |
+
--------
|
| 53 |
+
biconnected_components
|
| 54 |
+
articulation_points
|
| 55 |
+
biconnected_component_edges
|
| 56 |
+
is_strongly_connected
|
| 57 |
+
is_weakly_connected
|
| 58 |
+
is_connected
|
| 59 |
+
is_semiconnected
|
| 60 |
+
|
| 61 |
+
Notes
|
| 62 |
+
-----
|
| 63 |
+
The algorithm to find articulation points and biconnected
|
| 64 |
+
components is implemented using a non-recursive depth-first-search
|
| 65 |
+
(DFS) that keeps track of the highest level that back edges reach
|
| 66 |
+
in the DFS tree. A node `n` is an articulation point if, and only
|
| 67 |
+
if, there exists a subtree rooted at `n` such that there is no
|
| 68 |
+
back edge from any successor of `n` that links to a predecessor of
|
| 69 |
+
`n` in the DFS tree. By keeping track of all the edges traversed
|
| 70 |
+
by the DFS we can obtain the biconnected components because all
|
| 71 |
+
edges of a bicomponent will be traversed consecutively between
|
| 72 |
+
articulation points.
|
| 73 |
+
|
| 74 |
+
References
|
| 75 |
+
----------
|
| 76 |
+
.. [1] Hopcroft, J.; Tarjan, R. (1973).
|
| 77 |
+
"Efficient algorithms for graph manipulation".
|
| 78 |
+
Communications of the ACM 16: 372–378. doi:10.1145/362248.362272
|
| 79 |
+
|
| 80 |
+
"""
|
| 81 |
+
bccs = biconnected_components(G)
|
| 82 |
+
try:
|
| 83 |
+
bcc = next(bccs)
|
| 84 |
+
except StopIteration:
|
| 85 |
+
# No bicomponents (empty graph?)
|
| 86 |
+
return False
|
| 87 |
+
try:
|
| 88 |
+
next(bccs)
|
| 89 |
+
except StopIteration:
|
| 90 |
+
# Only one bicomponent
|
| 91 |
+
return len(bcc) == len(G)
|
| 92 |
+
else:
|
| 93 |
+
# Multiple bicomponents
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@not_implemented_for("directed")
|
| 98 |
+
@nx._dispatchable
|
| 99 |
+
def biconnected_component_edges(G):
|
| 100 |
+
"""Returns a generator of lists of edges, one list for each biconnected
|
| 101 |
+
component of the input graph.
|
| 102 |
+
|
| 103 |
+
Biconnected components are maximal subgraphs such that the removal of a
|
| 104 |
+
node (and all edges incident on that node) will not disconnect the
|
| 105 |
+
subgraph. Note that nodes may be part of more than one biconnected
|
| 106 |
+
component. Those nodes are articulation points, or cut vertices.
|
| 107 |
+
However, each edge belongs to one, and only one, biconnected component.
|
| 108 |
+
|
| 109 |
+
Notice that by convention a dyad is considered a biconnected component.
|
| 110 |
+
|
| 111 |
+
Parameters
|
| 112 |
+
----------
|
| 113 |
+
G : NetworkX Graph
|
| 114 |
+
An undirected graph.
|
| 115 |
+
|
| 116 |
+
Returns
|
| 117 |
+
-------
|
| 118 |
+
edges : generator of lists
|
| 119 |
+
Generator of lists of edges, one list for each bicomponent.
|
| 120 |
+
|
| 121 |
+
Raises
|
| 122 |
+
------
|
| 123 |
+
NetworkXNotImplemented
|
| 124 |
+
If the input graph is not undirected.
|
| 125 |
+
|
| 126 |
+
Examples
|
| 127 |
+
--------
|
| 128 |
+
>>> G = nx.barbell_graph(4, 2)
|
| 129 |
+
>>> print(nx.is_biconnected(G))
|
| 130 |
+
False
|
| 131 |
+
>>> bicomponents_edges = list(nx.biconnected_component_edges(G))
|
| 132 |
+
>>> len(bicomponents_edges)
|
| 133 |
+
5
|
| 134 |
+
>>> G.add_edge(2, 8)
|
| 135 |
+
>>> print(nx.is_biconnected(G))
|
| 136 |
+
True
|
| 137 |
+
>>> bicomponents_edges = list(nx.biconnected_component_edges(G))
|
| 138 |
+
>>> len(bicomponents_edges)
|
| 139 |
+
1
|
| 140 |
+
|
| 141 |
+
See Also
|
| 142 |
+
--------
|
| 143 |
+
is_biconnected,
|
| 144 |
+
biconnected_components,
|
| 145 |
+
articulation_points,
|
| 146 |
+
|
| 147 |
+
Notes
|
| 148 |
+
-----
|
| 149 |
+
The algorithm to find articulation points and biconnected
|
| 150 |
+
components is implemented using a non-recursive depth-first-search
|
| 151 |
+
(DFS) that keeps track of the highest level that back edges reach
|
| 152 |
+
in the DFS tree. A node `n` is an articulation point if, and only
|
| 153 |
+
if, there exists a subtree rooted at `n` such that there is no
|
| 154 |
+
back edge from any successor of `n` that links to a predecessor of
|
| 155 |
+
`n` in the DFS tree. By keeping track of all the edges traversed
|
| 156 |
+
by the DFS we can obtain the biconnected components because all
|
| 157 |
+
edges of a bicomponent will be traversed consecutively between
|
| 158 |
+
articulation points.
|
| 159 |
+
|
| 160 |
+
References
|
| 161 |
+
----------
|
| 162 |
+
.. [1] Hopcroft, J.; Tarjan, R. (1973).
|
| 163 |
+
"Efficient algorithms for graph manipulation".
|
| 164 |
+
Communications of the ACM 16: 372–378. doi:10.1145/362248.362272
|
| 165 |
+
|
| 166 |
+
"""
|
| 167 |
+
yield from _biconnected_dfs(G, components=True)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@not_implemented_for("directed")
|
| 171 |
+
@nx._dispatchable
|
| 172 |
+
def biconnected_components(G):
|
| 173 |
+
"""Returns a generator of sets of nodes, one set for each biconnected
|
| 174 |
+
component of the graph
|
| 175 |
+
|
| 176 |
+
Biconnected components are maximal subgraphs such that the removal of a
|
| 177 |
+
node (and all edges incident on that node) will not disconnect the
|
| 178 |
+
subgraph. Note that nodes may be part of more than one biconnected
|
| 179 |
+
component. Those nodes are articulation points, or cut vertices. The
|
| 180 |
+
removal of articulation points will increase the number of connected
|
| 181 |
+
components of the graph.
|
| 182 |
+
|
| 183 |
+
Notice that by convention a dyad is considered a biconnected component.
|
| 184 |
+
|
| 185 |
+
Parameters
|
| 186 |
+
----------
|
| 187 |
+
G : NetworkX Graph
|
| 188 |
+
An undirected graph.
|
| 189 |
+
|
| 190 |
+
Returns
|
| 191 |
+
-------
|
| 192 |
+
nodes : generator
|
| 193 |
+
Generator of sets of nodes, one set for each biconnected component.
|
| 194 |
+
|
| 195 |
+
Raises
|
| 196 |
+
------
|
| 197 |
+
NetworkXNotImplemented
|
| 198 |
+
If the input graph is not undirected.
|
| 199 |
+
|
| 200 |
+
Examples
|
| 201 |
+
--------
|
| 202 |
+
>>> G = nx.lollipop_graph(5, 1)
|
| 203 |
+
>>> print(nx.is_biconnected(G))
|
| 204 |
+
False
|
| 205 |
+
>>> bicomponents = list(nx.biconnected_components(G))
|
| 206 |
+
>>> len(bicomponents)
|
| 207 |
+
2
|
| 208 |
+
>>> G.add_edge(0, 5)
|
| 209 |
+
>>> print(nx.is_biconnected(G))
|
| 210 |
+
True
|
| 211 |
+
>>> bicomponents = list(nx.biconnected_components(G))
|
| 212 |
+
>>> len(bicomponents)
|
| 213 |
+
1
|
| 214 |
+
|
| 215 |
+
You can generate a sorted list of biconnected components, largest
|
| 216 |
+
first, using sort.
|
| 217 |
+
|
| 218 |
+
>>> G.remove_edge(0, 5)
|
| 219 |
+
>>> [len(c) for c in sorted(nx.biconnected_components(G), key=len, reverse=True)]
|
| 220 |
+
[5, 2]
|
| 221 |
+
|
| 222 |
+
If you only want the largest connected component, it's more
|
| 223 |
+
efficient to use max instead of sort.
|
| 224 |
+
|
| 225 |
+
>>> Gc = max(nx.biconnected_components(G), key=len)
|
| 226 |
+
|
| 227 |
+
To create the components as subgraphs use:
|
| 228 |
+
``(G.subgraph(c).copy() for c in biconnected_components(G))``
|
| 229 |
+
|
| 230 |
+
See Also
|
| 231 |
+
--------
|
| 232 |
+
is_biconnected
|
| 233 |
+
articulation_points
|
| 234 |
+
biconnected_component_edges
|
| 235 |
+
k_components : this function is a special case where k=2
|
| 236 |
+
bridge_components : similar to this function, but is defined using
|
| 237 |
+
2-edge-connectivity instead of 2-node-connectivity.
|
| 238 |
+
|
| 239 |
+
Notes
|
| 240 |
+
-----
|
| 241 |
+
The algorithm to find articulation points and biconnected
|
| 242 |
+
components is implemented using a non-recursive depth-first-search
|
| 243 |
+
(DFS) that keeps track of the highest level that back edges reach
|
| 244 |
+
in the DFS tree. A node `n` is an articulation point if, and only
|
| 245 |
+
if, there exists a subtree rooted at `n` such that there is no
|
| 246 |
+
back edge from any successor of `n` that links to a predecessor of
|
| 247 |
+
`n` in the DFS tree. By keeping track of all the edges traversed
|
| 248 |
+
by the DFS we can obtain the biconnected components because all
|
| 249 |
+
edges of a bicomponent will be traversed consecutively between
|
| 250 |
+
articulation points.
|
| 251 |
+
|
| 252 |
+
References
|
| 253 |
+
----------
|
| 254 |
+
.. [1] Hopcroft, J.; Tarjan, R. (1973).
|
| 255 |
+
"Efficient algorithms for graph manipulation".
|
| 256 |
+
Communications of the ACM 16: 372–378. doi:10.1145/362248.362272
|
| 257 |
+
|
| 258 |
+
"""
|
| 259 |
+
for comp in _biconnected_dfs(G, components=True):
|
| 260 |
+
yield set(chain.from_iterable(comp))
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
@not_implemented_for("directed")
|
| 264 |
+
@nx._dispatchable
|
| 265 |
+
def articulation_points(G):
|
| 266 |
+
"""Yield the articulation points, or cut vertices, of a graph.
|
| 267 |
+
|
| 268 |
+
An articulation point or cut vertex is any node whose removal (along with
|
| 269 |
+
all its incident edges) increases the number of connected components of
|
| 270 |
+
a graph. An undirected connected graph without articulation points is
|
| 271 |
+
biconnected. Articulation points belong to more than one biconnected
|
| 272 |
+
component of a graph.
|
| 273 |
+
|
| 274 |
+
Notice that by convention a dyad is considered a biconnected component.
|
| 275 |
+
|
| 276 |
+
Parameters
|
| 277 |
+
----------
|
| 278 |
+
G : NetworkX Graph
|
| 279 |
+
An undirected graph.
|
| 280 |
+
|
| 281 |
+
Yields
|
| 282 |
+
------
|
| 283 |
+
node
|
| 284 |
+
An articulation point in the graph.
|
| 285 |
+
|
| 286 |
+
Raises
|
| 287 |
+
------
|
| 288 |
+
NetworkXNotImplemented
|
| 289 |
+
If the input graph is not undirected.
|
| 290 |
+
|
| 291 |
+
Examples
|
| 292 |
+
--------
|
| 293 |
+
|
| 294 |
+
>>> G = nx.barbell_graph(4, 2)
|
| 295 |
+
>>> print(nx.is_biconnected(G))
|
| 296 |
+
False
|
| 297 |
+
>>> len(list(nx.articulation_points(G)))
|
| 298 |
+
4
|
| 299 |
+
>>> G.add_edge(2, 8)
|
| 300 |
+
>>> print(nx.is_biconnected(G))
|
| 301 |
+
True
|
| 302 |
+
>>> len(list(nx.articulation_points(G)))
|
| 303 |
+
0
|
| 304 |
+
|
| 305 |
+
See Also
|
| 306 |
+
--------
|
| 307 |
+
is_biconnected
|
| 308 |
+
biconnected_components
|
| 309 |
+
biconnected_component_edges
|
| 310 |
+
|
| 311 |
+
Notes
|
| 312 |
+
-----
|
| 313 |
+
The algorithm to find articulation points and biconnected
|
| 314 |
+
components is implemented using a non-recursive depth-first-search
|
| 315 |
+
(DFS) that keeps track of the highest level that back edges reach
|
| 316 |
+
in the DFS tree. A node `n` is an articulation point if, and only
|
| 317 |
+
if, there exists a subtree rooted at `n` such that there is no
|
| 318 |
+
back edge from any successor of `n` that links to a predecessor of
|
| 319 |
+
`n` in the DFS tree. By keeping track of all the edges traversed
|
| 320 |
+
by the DFS we can obtain the biconnected components because all
|
| 321 |
+
edges of a bicomponent will be traversed consecutively between
|
| 322 |
+
articulation points.
|
| 323 |
+
|
| 324 |
+
References
|
| 325 |
+
----------
|
| 326 |
+
.. [1] Hopcroft, J.; Tarjan, R. (1973).
|
| 327 |
+
"Efficient algorithms for graph manipulation".
|
| 328 |
+
Communications of the ACM 16: 372–378. doi:10.1145/362248.362272
|
| 329 |
+
|
| 330 |
+
"""
|
| 331 |
+
seen = set()
|
| 332 |
+
for articulation in _biconnected_dfs(G, components=False):
|
| 333 |
+
if articulation not in seen:
|
| 334 |
+
seen.add(articulation)
|
| 335 |
+
yield articulation
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
@not_implemented_for("directed")
|
| 339 |
+
def _biconnected_dfs(G, components=True):
|
| 340 |
+
# depth-first search algorithm to generate articulation points
|
| 341 |
+
# and biconnected components
|
| 342 |
+
visited = set()
|
| 343 |
+
for start in G:
|
| 344 |
+
if start in visited:
|
| 345 |
+
continue
|
| 346 |
+
discovery = {start: 0} # time of first discovery of node during search
|
| 347 |
+
low = {start: 0}
|
| 348 |
+
root_children = 0
|
| 349 |
+
visited.add(start)
|
| 350 |
+
edge_stack = []
|
| 351 |
+
stack = [(start, start, iter(G[start]))]
|
| 352 |
+
edge_index = {}
|
| 353 |
+
while stack:
|
| 354 |
+
grandparent, parent, children = stack[-1]
|
| 355 |
+
try:
|
| 356 |
+
child = next(children)
|
| 357 |
+
if grandparent == child:
|
| 358 |
+
continue
|
| 359 |
+
if child in visited:
|
| 360 |
+
if discovery[child] <= discovery[parent]: # back edge
|
| 361 |
+
low[parent] = min(low[parent], discovery[child])
|
| 362 |
+
if components:
|
| 363 |
+
edge_index[parent, child] = len(edge_stack)
|
| 364 |
+
edge_stack.append((parent, child))
|
| 365 |
+
else:
|
| 366 |
+
low[child] = discovery[child] = len(discovery)
|
| 367 |
+
visited.add(child)
|
| 368 |
+
stack.append((parent, child, iter(G[child])))
|
| 369 |
+
if components:
|
| 370 |
+
edge_index[parent, child] = len(edge_stack)
|
| 371 |
+
edge_stack.append((parent, child))
|
| 372 |
+
|
| 373 |
+
except StopIteration:
|
| 374 |
+
stack.pop()
|
| 375 |
+
if len(stack) > 1:
|
| 376 |
+
if low[parent] >= discovery[grandparent]:
|
| 377 |
+
if components:
|
| 378 |
+
ind = edge_index[grandparent, parent]
|
| 379 |
+
yield edge_stack[ind:]
|
| 380 |
+
del edge_stack[ind:]
|
| 381 |
+
|
| 382 |
+
else:
|
| 383 |
+
yield grandparent
|
| 384 |
+
low[grandparent] = min(low[parent], low[grandparent])
|
| 385 |
+
elif stack: # length 1 so grandparent is root
|
| 386 |
+
root_children += 1
|
| 387 |
+
if components:
|
| 388 |
+
ind = edge_index[grandparent, parent]
|
| 389 |
+
yield edge_stack[ind:]
|
| 390 |
+
del edge_stack[ind:]
|
| 391 |
+
if not components:
|
| 392 |
+
# root node is articulation point if it has more than 1 child
|
| 393 |
+
if root_children > 1:
|
| 394 |
+
yield start
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/connected.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Connected components."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils.decorators import not_implemented_for
|
| 5 |
+
|
| 6 |
+
from ...utils import arbitrary_element
|
| 7 |
+
|
| 8 |
+
__all__ = [
|
| 9 |
+
"number_connected_components",
|
| 10 |
+
"connected_components",
|
| 11 |
+
"is_connected",
|
| 12 |
+
"node_connected_component",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@not_implemented_for("directed")
|
| 17 |
+
@nx._dispatchable
|
| 18 |
+
def connected_components(G):
|
| 19 |
+
"""Generate connected components.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
----------
|
| 23 |
+
G : NetworkX graph
|
| 24 |
+
An undirected graph
|
| 25 |
+
|
| 26 |
+
Returns
|
| 27 |
+
-------
|
| 28 |
+
comp : generator of sets
|
| 29 |
+
A generator of sets of nodes, one for each component of G.
|
| 30 |
+
|
| 31 |
+
Raises
|
| 32 |
+
------
|
| 33 |
+
NetworkXNotImplemented
|
| 34 |
+
If G is directed.
|
| 35 |
+
|
| 36 |
+
Examples
|
| 37 |
+
--------
|
| 38 |
+
Generate a sorted list of connected components, largest first.
|
| 39 |
+
|
| 40 |
+
>>> G = nx.path_graph(4)
|
| 41 |
+
>>> nx.add_path(G, [10, 11, 12])
|
| 42 |
+
>>> [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]
|
| 43 |
+
[4, 3]
|
| 44 |
+
|
| 45 |
+
If you only want the largest connected component, it's more
|
| 46 |
+
efficient to use max instead of sort.
|
| 47 |
+
|
| 48 |
+
>>> largest_cc = max(nx.connected_components(G), key=len)
|
| 49 |
+
|
| 50 |
+
To create the induced subgraph of each component use:
|
| 51 |
+
|
| 52 |
+
>>> S = [G.subgraph(c).copy() for c in nx.connected_components(G)]
|
| 53 |
+
|
| 54 |
+
See Also
|
| 55 |
+
--------
|
| 56 |
+
strongly_connected_components
|
| 57 |
+
weakly_connected_components
|
| 58 |
+
|
| 59 |
+
Notes
|
| 60 |
+
-----
|
| 61 |
+
For undirected graphs only.
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
seen = set()
|
| 65 |
+
n = len(G)
|
| 66 |
+
for v in G:
|
| 67 |
+
if v not in seen:
|
| 68 |
+
c = _plain_bfs(G, n, v)
|
| 69 |
+
seen.update(c)
|
| 70 |
+
yield c
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@not_implemented_for("directed")
|
| 74 |
+
@nx._dispatchable
|
| 75 |
+
def number_connected_components(G):
|
| 76 |
+
"""Returns the number of connected components.
|
| 77 |
+
|
| 78 |
+
Parameters
|
| 79 |
+
----------
|
| 80 |
+
G : NetworkX graph
|
| 81 |
+
An undirected graph.
|
| 82 |
+
|
| 83 |
+
Returns
|
| 84 |
+
-------
|
| 85 |
+
n : integer
|
| 86 |
+
Number of connected components
|
| 87 |
+
|
| 88 |
+
Raises
|
| 89 |
+
------
|
| 90 |
+
NetworkXNotImplemented
|
| 91 |
+
If G is directed.
|
| 92 |
+
|
| 93 |
+
Examples
|
| 94 |
+
--------
|
| 95 |
+
>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
|
| 96 |
+
>>> nx.number_connected_components(G)
|
| 97 |
+
3
|
| 98 |
+
|
| 99 |
+
See Also
|
| 100 |
+
--------
|
| 101 |
+
connected_components
|
| 102 |
+
number_weakly_connected_components
|
| 103 |
+
number_strongly_connected_components
|
| 104 |
+
|
| 105 |
+
Notes
|
| 106 |
+
-----
|
| 107 |
+
For undirected graphs only.
|
| 108 |
+
|
| 109 |
+
"""
|
| 110 |
+
return sum(1 for cc in connected_components(G))
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
@not_implemented_for("directed")
|
| 114 |
+
@nx._dispatchable
|
| 115 |
+
def is_connected(G):
|
| 116 |
+
"""Returns True if the graph is connected, False otherwise.
|
| 117 |
+
|
| 118 |
+
Parameters
|
| 119 |
+
----------
|
| 120 |
+
G : NetworkX Graph
|
| 121 |
+
An undirected graph.
|
| 122 |
+
|
| 123 |
+
Returns
|
| 124 |
+
-------
|
| 125 |
+
connected : bool
|
| 126 |
+
True if the graph is connected, false otherwise.
|
| 127 |
+
|
| 128 |
+
Raises
|
| 129 |
+
------
|
| 130 |
+
NetworkXNotImplemented
|
| 131 |
+
If G is directed.
|
| 132 |
+
|
| 133 |
+
Examples
|
| 134 |
+
--------
|
| 135 |
+
>>> G = nx.path_graph(4)
|
| 136 |
+
>>> print(nx.is_connected(G))
|
| 137 |
+
True
|
| 138 |
+
|
| 139 |
+
See Also
|
| 140 |
+
--------
|
| 141 |
+
is_strongly_connected
|
| 142 |
+
is_weakly_connected
|
| 143 |
+
is_semiconnected
|
| 144 |
+
is_biconnected
|
| 145 |
+
connected_components
|
| 146 |
+
|
| 147 |
+
Notes
|
| 148 |
+
-----
|
| 149 |
+
For undirected graphs only.
|
| 150 |
+
|
| 151 |
+
"""
|
| 152 |
+
n = len(G)
|
| 153 |
+
if n == 0:
|
| 154 |
+
raise nx.NetworkXPointlessConcept(
|
| 155 |
+
"Connectivity is undefined for the null graph."
|
| 156 |
+
)
|
| 157 |
+
return sum(1 for node in _plain_bfs(G, n, arbitrary_element(G))) == len(G)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
@not_implemented_for("directed")
|
| 161 |
+
@nx._dispatchable
|
| 162 |
+
def node_connected_component(G, n):
|
| 163 |
+
"""Returns the set of nodes in the component of graph containing node n.
|
| 164 |
+
|
| 165 |
+
Parameters
|
| 166 |
+
----------
|
| 167 |
+
G : NetworkX Graph
|
| 168 |
+
An undirected graph.
|
| 169 |
+
|
| 170 |
+
n : node label
|
| 171 |
+
A node in G
|
| 172 |
+
|
| 173 |
+
Returns
|
| 174 |
+
-------
|
| 175 |
+
comp : set
|
| 176 |
+
A set of nodes in the component of G containing node n.
|
| 177 |
+
|
| 178 |
+
Raises
|
| 179 |
+
------
|
| 180 |
+
NetworkXNotImplemented
|
| 181 |
+
If G is directed.
|
| 182 |
+
|
| 183 |
+
Examples
|
| 184 |
+
--------
|
| 185 |
+
>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
|
| 186 |
+
>>> nx.node_connected_component(G, 0) # nodes of component that contains node 0
|
| 187 |
+
{0, 1, 2}
|
| 188 |
+
|
| 189 |
+
See Also
|
| 190 |
+
--------
|
| 191 |
+
connected_components
|
| 192 |
+
|
| 193 |
+
Notes
|
| 194 |
+
-----
|
| 195 |
+
For undirected graphs only.
|
| 196 |
+
|
| 197 |
+
"""
|
| 198 |
+
return _plain_bfs(G, len(G), n)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def _plain_bfs(G, n, source):
|
| 202 |
+
"""A fast BFS node generator"""
|
| 203 |
+
adj = G._adj
|
| 204 |
+
seen = {source}
|
| 205 |
+
nextlevel = [source]
|
| 206 |
+
while nextlevel:
|
| 207 |
+
thislevel = nextlevel
|
| 208 |
+
nextlevel = []
|
| 209 |
+
for v in thislevel:
|
| 210 |
+
for w in adj[v]:
|
| 211 |
+
if w not in seen:
|
| 212 |
+
seen.add(w)
|
| 213 |
+
nextlevel.append(w)
|
| 214 |
+
if len(seen) == n:
|
| 215 |
+
return seen
|
| 216 |
+
return seen
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/semiconnected.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Semiconnectedness."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils import not_implemented_for, pairwise
|
| 5 |
+
|
| 6 |
+
__all__ = ["is_semiconnected"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@not_implemented_for("undirected")
|
| 10 |
+
@nx._dispatchable
|
| 11 |
+
def is_semiconnected(G):
|
| 12 |
+
r"""Returns True if the graph is semiconnected, False otherwise.
|
| 13 |
+
|
| 14 |
+
A graph is semiconnected if and only if for any pair of nodes, either one
|
| 15 |
+
is reachable from the other, or they are mutually reachable.
|
| 16 |
+
|
| 17 |
+
This function uses a theorem that states that a DAG is semiconnected
|
| 18 |
+
if for any topological sort, for node $v_n$ in that sort, there is an
|
| 19 |
+
edge $(v_i, v_{i+1})$. That allows us to check if a non-DAG `G` is
|
| 20 |
+
semiconnected by condensing the graph: i.e. constructing a new graph `H`
|
| 21 |
+
with nodes being the strongly connected components of `G`, and edges
|
| 22 |
+
(scc_1, scc_2) if there is a edge $(v_1, v_2)$ in `G` for some
|
| 23 |
+
$v_1 \in scc_1$ and $v_2 \in scc_2$. That results in a DAG, so we compute
|
| 24 |
+
the topological sort of `H` and check if for every $n$ there is an edge
|
| 25 |
+
$(scc_n, scc_{n+1})$.
|
| 26 |
+
|
| 27 |
+
Parameters
|
| 28 |
+
----------
|
| 29 |
+
G : NetworkX graph
|
| 30 |
+
A directed graph.
|
| 31 |
+
|
| 32 |
+
Returns
|
| 33 |
+
-------
|
| 34 |
+
semiconnected : bool
|
| 35 |
+
True if the graph is semiconnected, False otherwise.
|
| 36 |
+
|
| 37 |
+
Raises
|
| 38 |
+
------
|
| 39 |
+
NetworkXNotImplemented
|
| 40 |
+
If the input graph is undirected.
|
| 41 |
+
|
| 42 |
+
NetworkXPointlessConcept
|
| 43 |
+
If the graph is empty.
|
| 44 |
+
|
| 45 |
+
Examples
|
| 46 |
+
--------
|
| 47 |
+
>>> G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 48 |
+
>>> print(nx.is_semiconnected(G))
|
| 49 |
+
True
|
| 50 |
+
>>> G = nx.DiGraph([(1, 2), (3, 2)])
|
| 51 |
+
>>> print(nx.is_semiconnected(G))
|
| 52 |
+
False
|
| 53 |
+
|
| 54 |
+
See Also
|
| 55 |
+
--------
|
| 56 |
+
is_strongly_connected
|
| 57 |
+
is_weakly_connected
|
| 58 |
+
is_connected
|
| 59 |
+
is_biconnected
|
| 60 |
+
"""
|
| 61 |
+
if len(G) == 0:
|
| 62 |
+
raise nx.NetworkXPointlessConcept(
|
| 63 |
+
"Connectivity is undefined for the null graph."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if not nx.is_weakly_connected(G):
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
+
H = nx.condensation(G)
|
| 70 |
+
|
| 71 |
+
return all(H.has_edge(u, v) for u, v in pairwise(nx.topological_sort(H)))
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/strongly_connected.py
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Strongly connected components."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils.decorators import not_implemented_for
|
| 5 |
+
|
| 6 |
+
__all__ = [
|
| 7 |
+
"number_strongly_connected_components",
|
| 8 |
+
"strongly_connected_components",
|
| 9 |
+
"is_strongly_connected",
|
| 10 |
+
"kosaraju_strongly_connected_components",
|
| 11 |
+
"condensation",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@not_implemented_for("undirected")
|
| 16 |
+
@nx._dispatchable
|
| 17 |
+
def strongly_connected_components(G):
|
| 18 |
+
"""Generate nodes in strongly connected components of graph.
|
| 19 |
+
|
| 20 |
+
Parameters
|
| 21 |
+
----------
|
| 22 |
+
G : NetworkX Graph
|
| 23 |
+
A directed graph.
|
| 24 |
+
|
| 25 |
+
Returns
|
| 26 |
+
-------
|
| 27 |
+
comp : generator of sets
|
| 28 |
+
A generator of sets of nodes, one for each strongly connected
|
| 29 |
+
component of G.
|
| 30 |
+
|
| 31 |
+
Raises
|
| 32 |
+
------
|
| 33 |
+
NetworkXNotImplemented
|
| 34 |
+
If G is undirected.
|
| 35 |
+
|
| 36 |
+
Examples
|
| 37 |
+
--------
|
| 38 |
+
Generate a sorted list of strongly connected components, largest first.
|
| 39 |
+
|
| 40 |
+
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
|
| 41 |
+
>>> nx.add_cycle(G, [10, 11, 12])
|
| 42 |
+
>>> [
|
| 43 |
+
... len(c)
|
| 44 |
+
... for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True)
|
| 45 |
+
... ]
|
| 46 |
+
[4, 3]
|
| 47 |
+
|
| 48 |
+
If you only want the largest component, it's more efficient to
|
| 49 |
+
use max instead of sort.
|
| 50 |
+
|
| 51 |
+
>>> largest = max(nx.strongly_connected_components(G), key=len)
|
| 52 |
+
|
| 53 |
+
See Also
|
| 54 |
+
--------
|
| 55 |
+
connected_components
|
| 56 |
+
weakly_connected_components
|
| 57 |
+
kosaraju_strongly_connected_components
|
| 58 |
+
|
| 59 |
+
Notes
|
| 60 |
+
-----
|
| 61 |
+
Uses Tarjan's algorithm[1]_ with Nuutila's modifications[2]_.
|
| 62 |
+
Nonrecursive version of algorithm.
|
| 63 |
+
|
| 64 |
+
References
|
| 65 |
+
----------
|
| 66 |
+
.. [1] Depth-first search and linear graph algorithms, R. Tarjan
|
| 67 |
+
SIAM Journal of Computing 1(2):146-160, (1972).
|
| 68 |
+
|
| 69 |
+
.. [2] On finding the strongly connected components in a directed graph.
|
| 70 |
+
E. Nuutila and E. Soisalon-Soinen
|
| 71 |
+
Information Processing Letters 49(1): 9-14, (1994)..
|
| 72 |
+
|
| 73 |
+
"""
|
| 74 |
+
preorder = {}
|
| 75 |
+
lowlink = {}
|
| 76 |
+
scc_found = set()
|
| 77 |
+
scc_queue = []
|
| 78 |
+
i = 0 # Preorder counter
|
| 79 |
+
neighbors = {v: iter(G[v]) for v in G}
|
| 80 |
+
for source in G:
|
| 81 |
+
if source not in scc_found:
|
| 82 |
+
queue = [source]
|
| 83 |
+
while queue:
|
| 84 |
+
v = queue[-1]
|
| 85 |
+
if v not in preorder:
|
| 86 |
+
i = i + 1
|
| 87 |
+
preorder[v] = i
|
| 88 |
+
done = True
|
| 89 |
+
for w in neighbors[v]:
|
| 90 |
+
if w not in preorder:
|
| 91 |
+
queue.append(w)
|
| 92 |
+
done = False
|
| 93 |
+
break
|
| 94 |
+
if done:
|
| 95 |
+
lowlink[v] = preorder[v]
|
| 96 |
+
for w in G[v]:
|
| 97 |
+
if w not in scc_found:
|
| 98 |
+
if preorder[w] > preorder[v]:
|
| 99 |
+
lowlink[v] = min([lowlink[v], lowlink[w]])
|
| 100 |
+
else:
|
| 101 |
+
lowlink[v] = min([lowlink[v], preorder[w]])
|
| 102 |
+
queue.pop()
|
| 103 |
+
if lowlink[v] == preorder[v]:
|
| 104 |
+
scc = {v}
|
| 105 |
+
while scc_queue and preorder[scc_queue[-1]] > preorder[v]:
|
| 106 |
+
k = scc_queue.pop()
|
| 107 |
+
scc.add(k)
|
| 108 |
+
scc_found.update(scc)
|
| 109 |
+
yield scc
|
| 110 |
+
else:
|
| 111 |
+
scc_queue.append(v)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@not_implemented_for("undirected")
|
| 115 |
+
@nx._dispatchable
|
| 116 |
+
def kosaraju_strongly_connected_components(G, source=None):
|
| 117 |
+
"""Generate nodes in strongly connected components of graph.
|
| 118 |
+
|
| 119 |
+
Parameters
|
| 120 |
+
----------
|
| 121 |
+
G : NetworkX Graph
|
| 122 |
+
A directed graph.
|
| 123 |
+
|
| 124 |
+
Returns
|
| 125 |
+
-------
|
| 126 |
+
comp : generator of sets
|
| 127 |
+
A generator of sets of nodes, one for each strongly connected
|
| 128 |
+
component of G.
|
| 129 |
+
|
| 130 |
+
Raises
|
| 131 |
+
------
|
| 132 |
+
NetworkXNotImplemented
|
| 133 |
+
If G is undirected.
|
| 134 |
+
|
| 135 |
+
Examples
|
| 136 |
+
--------
|
| 137 |
+
Generate a sorted list of strongly connected components, largest first.
|
| 138 |
+
|
| 139 |
+
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
|
| 140 |
+
>>> nx.add_cycle(G, [10, 11, 12])
|
| 141 |
+
>>> [
|
| 142 |
+
... len(c)
|
| 143 |
+
... for c in sorted(
|
| 144 |
+
... nx.kosaraju_strongly_connected_components(G), key=len, reverse=True
|
| 145 |
+
... )
|
| 146 |
+
... ]
|
| 147 |
+
[4, 3]
|
| 148 |
+
|
| 149 |
+
If you only want the largest component, it's more efficient to
|
| 150 |
+
use max instead of sort.
|
| 151 |
+
|
| 152 |
+
>>> largest = max(nx.kosaraju_strongly_connected_components(G), key=len)
|
| 153 |
+
|
| 154 |
+
See Also
|
| 155 |
+
--------
|
| 156 |
+
strongly_connected_components
|
| 157 |
+
|
| 158 |
+
Notes
|
| 159 |
+
-----
|
| 160 |
+
Uses Kosaraju's algorithm.
|
| 161 |
+
|
| 162 |
+
"""
|
| 163 |
+
post = list(nx.dfs_postorder_nodes(G.reverse(copy=False), source=source))
|
| 164 |
+
|
| 165 |
+
seen = set()
|
| 166 |
+
while post:
|
| 167 |
+
r = post.pop()
|
| 168 |
+
if r in seen:
|
| 169 |
+
continue
|
| 170 |
+
c = nx.dfs_preorder_nodes(G, r)
|
| 171 |
+
new = {v for v in c if v not in seen}
|
| 172 |
+
seen.update(new)
|
| 173 |
+
yield new
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@not_implemented_for("undirected")
|
| 177 |
+
@nx._dispatchable
|
| 178 |
+
def number_strongly_connected_components(G):
|
| 179 |
+
"""Returns number of strongly connected components in graph.
|
| 180 |
+
|
| 181 |
+
Parameters
|
| 182 |
+
----------
|
| 183 |
+
G : NetworkX graph
|
| 184 |
+
A directed graph.
|
| 185 |
+
|
| 186 |
+
Returns
|
| 187 |
+
-------
|
| 188 |
+
n : integer
|
| 189 |
+
Number of strongly connected components
|
| 190 |
+
|
| 191 |
+
Raises
|
| 192 |
+
------
|
| 193 |
+
NetworkXNotImplemented
|
| 194 |
+
If G is undirected.
|
| 195 |
+
|
| 196 |
+
Examples
|
| 197 |
+
--------
|
| 198 |
+
>>> G = nx.DiGraph(
|
| 199 |
+
... [(0, 1), (1, 2), (2, 0), (2, 3), (4, 5), (3, 4), (5, 6), (6, 3), (6, 7)]
|
| 200 |
+
... )
|
| 201 |
+
>>> nx.number_strongly_connected_components(G)
|
| 202 |
+
3
|
| 203 |
+
|
| 204 |
+
See Also
|
| 205 |
+
--------
|
| 206 |
+
strongly_connected_components
|
| 207 |
+
number_connected_components
|
| 208 |
+
number_weakly_connected_components
|
| 209 |
+
|
| 210 |
+
Notes
|
| 211 |
+
-----
|
| 212 |
+
For directed graphs only.
|
| 213 |
+
"""
|
| 214 |
+
return sum(1 for scc in strongly_connected_components(G))
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
@not_implemented_for("undirected")
|
| 218 |
+
@nx._dispatchable
|
| 219 |
+
def is_strongly_connected(G):
|
| 220 |
+
"""Test directed graph for strong connectivity.
|
| 221 |
+
|
| 222 |
+
A directed graph is strongly connected if and only if every vertex in
|
| 223 |
+
the graph is reachable from every other vertex.
|
| 224 |
+
|
| 225 |
+
Parameters
|
| 226 |
+
----------
|
| 227 |
+
G : NetworkX Graph
|
| 228 |
+
A directed graph.
|
| 229 |
+
|
| 230 |
+
Returns
|
| 231 |
+
-------
|
| 232 |
+
connected : bool
|
| 233 |
+
True if the graph is strongly connected, False otherwise.
|
| 234 |
+
|
| 235 |
+
Examples
|
| 236 |
+
--------
|
| 237 |
+
>>> G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 2)])
|
| 238 |
+
>>> nx.is_strongly_connected(G)
|
| 239 |
+
True
|
| 240 |
+
>>> G.remove_edge(2, 3)
|
| 241 |
+
>>> nx.is_strongly_connected(G)
|
| 242 |
+
False
|
| 243 |
+
|
| 244 |
+
Raises
|
| 245 |
+
------
|
| 246 |
+
NetworkXNotImplemented
|
| 247 |
+
If G is undirected.
|
| 248 |
+
|
| 249 |
+
See Also
|
| 250 |
+
--------
|
| 251 |
+
is_weakly_connected
|
| 252 |
+
is_semiconnected
|
| 253 |
+
is_connected
|
| 254 |
+
is_biconnected
|
| 255 |
+
strongly_connected_components
|
| 256 |
+
|
| 257 |
+
Notes
|
| 258 |
+
-----
|
| 259 |
+
For directed graphs only.
|
| 260 |
+
"""
|
| 261 |
+
if len(G) == 0:
|
| 262 |
+
raise nx.NetworkXPointlessConcept(
|
| 263 |
+
"""Connectivity is undefined for the null graph."""
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
return len(next(strongly_connected_components(G))) == len(G)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
@not_implemented_for("undirected")
|
| 270 |
+
@nx._dispatchable(returns_graph=True)
|
| 271 |
+
def condensation(G, scc=None):
|
| 272 |
+
"""Returns the condensation of G.
|
| 273 |
+
|
| 274 |
+
The condensation of G is the graph with each of the strongly connected
|
| 275 |
+
components contracted into a single node.
|
| 276 |
+
|
| 277 |
+
Parameters
|
| 278 |
+
----------
|
| 279 |
+
G : NetworkX DiGraph
|
| 280 |
+
A directed graph.
|
| 281 |
+
|
| 282 |
+
scc: list or generator (optional, default=None)
|
| 283 |
+
Strongly connected components. If provided, the elements in
|
| 284 |
+
`scc` must partition the nodes in `G`. If not provided, it will be
|
| 285 |
+
calculated as scc=nx.strongly_connected_components(G).
|
| 286 |
+
|
| 287 |
+
Returns
|
| 288 |
+
-------
|
| 289 |
+
C : NetworkX DiGraph
|
| 290 |
+
The condensation graph C of G. The node labels are integers
|
| 291 |
+
corresponding to the index of the component in the list of
|
| 292 |
+
strongly connected components of G. C has a graph attribute named
|
| 293 |
+
'mapping' with a dictionary mapping the original nodes to the
|
| 294 |
+
nodes in C to which they belong. Each node in C also has a node
|
| 295 |
+
attribute 'members' with the set of original nodes in G that
|
| 296 |
+
form the SCC that the node in C represents.
|
| 297 |
+
|
| 298 |
+
Raises
|
| 299 |
+
------
|
| 300 |
+
NetworkXNotImplemented
|
| 301 |
+
If G is undirected.
|
| 302 |
+
|
| 303 |
+
Examples
|
| 304 |
+
--------
|
| 305 |
+
Contracting two sets of strongly connected nodes into two distinct SCC
|
| 306 |
+
using the barbell graph.
|
| 307 |
+
|
| 308 |
+
>>> G = nx.barbell_graph(4, 0)
|
| 309 |
+
>>> G.remove_edge(3, 4)
|
| 310 |
+
>>> G = nx.DiGraph(G)
|
| 311 |
+
>>> H = nx.condensation(G)
|
| 312 |
+
>>> H.nodes.data()
|
| 313 |
+
NodeDataView({0: {'members': {0, 1, 2, 3}}, 1: {'members': {4, 5, 6, 7}}})
|
| 314 |
+
>>> H.graph["mapping"]
|
| 315 |
+
{0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1}
|
| 316 |
+
|
| 317 |
+
Contracting a complete graph into one single SCC.
|
| 318 |
+
|
| 319 |
+
>>> G = nx.complete_graph(7, create_using=nx.DiGraph)
|
| 320 |
+
>>> H = nx.condensation(G)
|
| 321 |
+
>>> H.nodes
|
| 322 |
+
NodeView((0,))
|
| 323 |
+
>>> H.nodes.data()
|
| 324 |
+
NodeDataView({0: {'members': {0, 1, 2, 3, 4, 5, 6}}})
|
| 325 |
+
|
| 326 |
+
Notes
|
| 327 |
+
-----
|
| 328 |
+
After contracting all strongly connected components to a single node,
|
| 329 |
+
the resulting graph is a directed acyclic graph.
|
| 330 |
+
|
| 331 |
+
"""
|
| 332 |
+
if scc is None:
|
| 333 |
+
scc = nx.strongly_connected_components(G)
|
| 334 |
+
mapping = {}
|
| 335 |
+
members = {}
|
| 336 |
+
C = nx.DiGraph()
|
| 337 |
+
# Add mapping dict as graph attribute
|
| 338 |
+
C.graph["mapping"] = mapping
|
| 339 |
+
if len(G) == 0:
|
| 340 |
+
return C
|
| 341 |
+
for i, component in enumerate(scc):
|
| 342 |
+
members[i] = component
|
| 343 |
+
mapping.update((n, i) for n in component)
|
| 344 |
+
number_of_components = i + 1
|
| 345 |
+
C.add_nodes_from(range(number_of_components))
|
| 346 |
+
C.add_edges_from(
|
| 347 |
+
(mapping[u], mapping[v]) for u, v in G.edges() if mapping[u] != mapping[v]
|
| 348 |
+
)
|
| 349 |
+
# Add a list of members (ie original nodes) to each node (ie scc) in C.
|
| 350 |
+
nx.set_node_attributes(C, members, "members")
|
| 351 |
+
return C
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_connected.cpython-310.pyc
ADDED
|
Binary file (5.24 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_semiconnected.cpython-310.pyc
ADDED
|
Binary file (2.99 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/__pycache__/test_strongly_connected.cpython-310.pyc
ADDED
|
Binary file (6.58 kB). View file
|
|
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_attracting.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx import NetworkXNotImplemented
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestAttractingComponents:
|
| 8 |
+
@classmethod
|
| 9 |
+
def setup_class(cls):
|
| 10 |
+
cls.G1 = nx.DiGraph()
|
| 11 |
+
cls.G1.add_edges_from(
|
| 12 |
+
[
|
| 13 |
+
(5, 11),
|
| 14 |
+
(11, 2),
|
| 15 |
+
(11, 9),
|
| 16 |
+
(11, 10),
|
| 17 |
+
(7, 11),
|
| 18 |
+
(7, 8),
|
| 19 |
+
(8, 9),
|
| 20 |
+
(3, 8),
|
| 21 |
+
(3, 10),
|
| 22 |
+
]
|
| 23 |
+
)
|
| 24 |
+
cls.G2 = nx.DiGraph()
|
| 25 |
+
cls.G2.add_edges_from([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1)])
|
| 26 |
+
|
| 27 |
+
cls.G3 = nx.DiGraph()
|
| 28 |
+
cls.G3.add_edges_from([(0, 1), (1, 2), (2, 1), (0, 3), (3, 4), (4, 3)])
|
| 29 |
+
|
| 30 |
+
cls.G4 = nx.DiGraph()
|
| 31 |
+
|
| 32 |
+
def test_attracting_components(self):
|
| 33 |
+
ac = list(nx.attracting_components(self.G1))
|
| 34 |
+
assert {2} in ac
|
| 35 |
+
assert {9} in ac
|
| 36 |
+
assert {10} in ac
|
| 37 |
+
|
| 38 |
+
ac = list(nx.attracting_components(self.G2))
|
| 39 |
+
ac = [tuple(sorted(x)) for x in ac]
|
| 40 |
+
assert ac == [(1, 2)]
|
| 41 |
+
|
| 42 |
+
ac = list(nx.attracting_components(self.G3))
|
| 43 |
+
ac = [tuple(sorted(x)) for x in ac]
|
| 44 |
+
assert (1, 2) in ac
|
| 45 |
+
assert (3, 4) in ac
|
| 46 |
+
assert len(ac) == 2
|
| 47 |
+
|
| 48 |
+
ac = list(nx.attracting_components(self.G4))
|
| 49 |
+
assert ac == []
|
| 50 |
+
|
| 51 |
+
def test_number_attacting_components(self):
|
| 52 |
+
assert nx.number_attracting_components(self.G1) == 3
|
| 53 |
+
assert nx.number_attracting_components(self.G2) == 1
|
| 54 |
+
assert nx.number_attracting_components(self.G3) == 2
|
| 55 |
+
assert nx.number_attracting_components(self.G4) == 0
|
| 56 |
+
|
| 57 |
+
def test_is_attracting_component(self):
|
| 58 |
+
assert not nx.is_attracting_component(self.G1)
|
| 59 |
+
assert not nx.is_attracting_component(self.G2)
|
| 60 |
+
assert not nx.is_attracting_component(self.G3)
|
| 61 |
+
g2 = self.G3.subgraph([1, 2])
|
| 62 |
+
assert nx.is_attracting_component(g2)
|
| 63 |
+
assert not nx.is_attracting_component(self.G4)
|
| 64 |
+
|
| 65 |
+
def test_connected_raise(self):
|
| 66 |
+
G = nx.Graph()
|
| 67 |
+
with pytest.raises(NetworkXNotImplemented):
|
| 68 |
+
next(nx.attracting_components(G))
|
| 69 |
+
pytest.raises(NetworkXNotImplemented, nx.number_attracting_components, G)
|
| 70 |
+
pytest.raises(NetworkXNotImplemented, nx.is_attracting_component, G)
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_semiconnected.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from itertools import chain
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestIsSemiconnected:
|
| 9 |
+
def test_undirected(self):
|
| 10 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.Graph())
|
| 11 |
+
pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.MultiGraph())
|
| 12 |
+
|
| 13 |
+
def test_empty(self):
|
| 14 |
+
pytest.raises(nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.DiGraph())
|
| 15 |
+
pytest.raises(
|
| 16 |
+
nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.MultiDiGraph()
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def test_single_node_graph(self):
|
| 20 |
+
G = nx.DiGraph()
|
| 21 |
+
G.add_node(0)
|
| 22 |
+
assert nx.is_semiconnected(G)
|
| 23 |
+
|
| 24 |
+
def test_path(self):
|
| 25 |
+
G = nx.path_graph(100, create_using=nx.DiGraph())
|
| 26 |
+
assert nx.is_semiconnected(G)
|
| 27 |
+
G.add_edge(100, 99)
|
| 28 |
+
assert not nx.is_semiconnected(G)
|
| 29 |
+
|
| 30 |
+
def test_cycle(self):
|
| 31 |
+
G = nx.cycle_graph(100, create_using=nx.DiGraph())
|
| 32 |
+
assert nx.is_semiconnected(G)
|
| 33 |
+
G = nx.path_graph(100, create_using=nx.DiGraph())
|
| 34 |
+
G.add_edge(0, 99)
|
| 35 |
+
assert nx.is_semiconnected(G)
|
| 36 |
+
|
| 37 |
+
def test_tree(self):
|
| 38 |
+
G = nx.DiGraph()
|
| 39 |
+
G.add_edges_from(
|
| 40 |
+
chain.from_iterable([(i, 2 * i + 1), (i, 2 * i + 2)] for i in range(100))
|
| 41 |
+
)
|
| 42 |
+
assert not nx.is_semiconnected(G)
|
| 43 |
+
|
| 44 |
+
def test_dumbbell(self):
|
| 45 |
+
G = nx.cycle_graph(100, create_using=nx.DiGraph())
|
| 46 |
+
G.add_edges_from((i + 100, (i + 1) % 100 + 100) for i in range(100))
|
| 47 |
+
assert not nx.is_semiconnected(G) # G is disconnected.
|
| 48 |
+
G.add_edge(100, 99)
|
| 49 |
+
assert nx.is_semiconnected(G)
|
| 50 |
+
|
| 51 |
+
def test_alternating_path(self):
|
| 52 |
+
G = nx.DiGraph(
|
| 53 |
+
chain.from_iterable([(i, i - 1), (i, i + 1)] for i in range(0, 100, 2))
|
| 54 |
+
)
|
| 55 |
+
assert not nx.is_semiconnected(G)
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/tests/test_strongly_connected.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx import NetworkXNotImplemented
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestStronglyConnected:
|
| 8 |
+
@classmethod
|
| 9 |
+
def setup_class(cls):
|
| 10 |
+
cls.gc = []
|
| 11 |
+
G = nx.DiGraph()
|
| 12 |
+
G.add_edges_from(
|
| 13 |
+
[
|
| 14 |
+
(1, 2),
|
| 15 |
+
(2, 3),
|
| 16 |
+
(2, 8),
|
| 17 |
+
(3, 4),
|
| 18 |
+
(3, 7),
|
| 19 |
+
(4, 5),
|
| 20 |
+
(5, 3),
|
| 21 |
+
(5, 6),
|
| 22 |
+
(7, 4),
|
| 23 |
+
(7, 6),
|
| 24 |
+
(8, 1),
|
| 25 |
+
(8, 7),
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
C = {frozenset([3, 4, 5, 7]), frozenset([1, 2, 8]), frozenset([6])}
|
| 29 |
+
cls.gc.append((G, C))
|
| 30 |
+
|
| 31 |
+
G = nx.DiGraph()
|
| 32 |
+
G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
|
| 33 |
+
C = {frozenset([2, 3, 4]), frozenset([1])}
|
| 34 |
+
cls.gc.append((G, C))
|
| 35 |
+
|
| 36 |
+
G = nx.DiGraph()
|
| 37 |
+
G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
|
| 38 |
+
C = {frozenset([1, 2, 3])}
|
| 39 |
+
cls.gc.append((G, C))
|
| 40 |
+
|
| 41 |
+
# Eppstein's tests
|
| 42 |
+
G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
|
| 43 |
+
C = {
|
| 44 |
+
frozenset([0]),
|
| 45 |
+
frozenset([1]),
|
| 46 |
+
frozenset([2]),
|
| 47 |
+
frozenset([3]),
|
| 48 |
+
frozenset([4]),
|
| 49 |
+
frozenset([5]),
|
| 50 |
+
frozenset([6]),
|
| 51 |
+
}
|
| 52 |
+
cls.gc.append((G, C))
|
| 53 |
+
|
| 54 |
+
G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
|
| 55 |
+
C = {frozenset([0, 1, 2]), frozenset([3, 4])}
|
| 56 |
+
cls.gc.append((G, C))
|
| 57 |
+
|
| 58 |
+
def test_tarjan(self):
|
| 59 |
+
scc = nx.strongly_connected_components
|
| 60 |
+
for G, C in self.gc:
|
| 61 |
+
assert {frozenset(g) for g in scc(G)} == C
|
| 62 |
+
|
| 63 |
+
def test_kosaraju(self):
|
| 64 |
+
scc = nx.kosaraju_strongly_connected_components
|
| 65 |
+
for G, C in self.gc:
|
| 66 |
+
assert {frozenset(g) for g in scc(G)} == C
|
| 67 |
+
|
| 68 |
+
def test_number_strongly_connected_components(self):
|
| 69 |
+
ncc = nx.number_strongly_connected_components
|
| 70 |
+
for G, C in self.gc:
|
| 71 |
+
assert ncc(G) == len(C)
|
| 72 |
+
|
| 73 |
+
def test_is_strongly_connected(self):
|
| 74 |
+
for G, C in self.gc:
|
| 75 |
+
if len(C) == 1:
|
| 76 |
+
assert nx.is_strongly_connected(G)
|
| 77 |
+
else:
|
| 78 |
+
assert not nx.is_strongly_connected(G)
|
| 79 |
+
|
| 80 |
+
def test_contract_scc1(self):
|
| 81 |
+
G = nx.DiGraph()
|
| 82 |
+
G.add_edges_from(
|
| 83 |
+
[
|
| 84 |
+
(1, 2),
|
| 85 |
+
(2, 3),
|
| 86 |
+
(2, 11),
|
| 87 |
+
(2, 12),
|
| 88 |
+
(3, 4),
|
| 89 |
+
(4, 3),
|
| 90 |
+
(4, 5),
|
| 91 |
+
(5, 6),
|
| 92 |
+
(6, 5),
|
| 93 |
+
(6, 7),
|
| 94 |
+
(7, 8),
|
| 95 |
+
(7, 9),
|
| 96 |
+
(7, 10),
|
| 97 |
+
(8, 9),
|
| 98 |
+
(9, 7),
|
| 99 |
+
(10, 6),
|
| 100 |
+
(11, 2),
|
| 101 |
+
(11, 4),
|
| 102 |
+
(11, 6),
|
| 103 |
+
(12, 6),
|
| 104 |
+
(12, 11),
|
| 105 |
+
]
|
| 106 |
+
)
|
| 107 |
+
scc = list(nx.strongly_connected_components(G))
|
| 108 |
+
cG = nx.condensation(G, scc)
|
| 109 |
+
# DAG
|
| 110 |
+
assert nx.is_directed_acyclic_graph(cG)
|
| 111 |
+
# nodes
|
| 112 |
+
assert sorted(cG.nodes()) == [0, 1, 2, 3]
|
| 113 |
+
# edges
|
| 114 |
+
mapping = {}
|
| 115 |
+
for i, component in enumerate(scc):
|
| 116 |
+
for n in component:
|
| 117 |
+
mapping[n] = i
|
| 118 |
+
edge = (mapping[2], mapping[3])
|
| 119 |
+
assert cG.has_edge(*edge)
|
| 120 |
+
edge = (mapping[2], mapping[5])
|
| 121 |
+
assert cG.has_edge(*edge)
|
| 122 |
+
edge = (mapping[3], mapping[5])
|
| 123 |
+
assert cG.has_edge(*edge)
|
| 124 |
+
|
| 125 |
+
def test_contract_scc_isolate(self):
|
| 126 |
+
# Bug found and fixed in [1687].
|
| 127 |
+
G = nx.DiGraph()
|
| 128 |
+
G.add_edge(1, 2)
|
| 129 |
+
G.add_edge(2, 1)
|
| 130 |
+
scc = list(nx.strongly_connected_components(G))
|
| 131 |
+
cG = nx.condensation(G, scc)
|
| 132 |
+
assert list(cG.nodes()) == [0]
|
| 133 |
+
assert list(cG.edges()) == []
|
| 134 |
+
|
| 135 |
+
def test_contract_scc_edge(self):
|
| 136 |
+
G = nx.DiGraph()
|
| 137 |
+
G.add_edge(1, 2)
|
| 138 |
+
G.add_edge(2, 1)
|
| 139 |
+
G.add_edge(2, 3)
|
| 140 |
+
G.add_edge(3, 4)
|
| 141 |
+
G.add_edge(4, 3)
|
| 142 |
+
scc = list(nx.strongly_connected_components(G))
|
| 143 |
+
cG = nx.condensation(G, scc)
|
| 144 |
+
assert sorted(cG.nodes()) == [0, 1]
|
| 145 |
+
if 1 in scc[0]:
|
| 146 |
+
edge = (0, 1)
|
| 147 |
+
else:
|
| 148 |
+
edge = (1, 0)
|
| 149 |
+
assert list(cG.edges()) == [edge]
|
| 150 |
+
|
| 151 |
+
def test_condensation_mapping_and_members(self):
|
| 152 |
+
G, C = self.gc[1]
|
| 153 |
+
C = sorted(C, key=len, reverse=True)
|
| 154 |
+
cG = nx.condensation(G)
|
| 155 |
+
mapping = cG.graph["mapping"]
|
| 156 |
+
assert all(n in G for n in mapping)
|
| 157 |
+
assert all(0 == cN for n, cN in mapping.items() if n in C[0])
|
| 158 |
+
assert all(1 == cN for n, cN in mapping.items() if n in C[1])
|
| 159 |
+
for n, d in cG.nodes(data=True):
|
| 160 |
+
assert set(C[n]) == cG.nodes[n]["members"]
|
| 161 |
+
|
| 162 |
+
def test_null_graph(self):
|
| 163 |
+
G = nx.DiGraph()
|
| 164 |
+
assert list(nx.strongly_connected_components(G)) == []
|
| 165 |
+
assert list(nx.kosaraju_strongly_connected_components(G)) == []
|
| 166 |
+
assert len(nx.condensation(G)) == 0
|
| 167 |
+
pytest.raises(
|
| 168 |
+
nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph()
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
def test_connected_raise(self):
|
| 172 |
+
G = nx.Graph()
|
| 173 |
+
with pytest.raises(NetworkXNotImplemented):
|
| 174 |
+
next(nx.strongly_connected_components(G))
|
| 175 |
+
with pytest.raises(NetworkXNotImplemented):
|
| 176 |
+
next(nx.kosaraju_strongly_connected_components(G))
|
| 177 |
+
pytest.raises(NetworkXNotImplemented, nx.is_strongly_connected, G)
|
| 178 |
+
pytest.raises(NetworkXNotImplemented, nx.condensation, G)
|
| 179 |
+
|
| 180 |
+
strong_cc_methods = (
|
| 181 |
+
nx.strongly_connected_components,
|
| 182 |
+
nx.kosaraju_strongly_connected_components,
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
@pytest.mark.parametrize("get_components", strong_cc_methods)
|
| 186 |
+
def test_connected_mutability(self, get_components):
|
| 187 |
+
DG = nx.path_graph(5, create_using=nx.DiGraph)
|
| 188 |
+
G = nx.disjoint_union(DG, DG)
|
| 189 |
+
seen = set()
|
| 190 |
+
for component in get_components(G):
|
| 191 |
+
assert len(seen & component) == 0
|
| 192 |
+
seen.update(component)
|
| 193 |
+
component.clear()
|
wemm/lib/python3.10/site-packages/networkx/algorithms/components/weakly_connected.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Weakly connected components."""
|
| 2 |
+
|
| 3 |
+
import networkx as nx
|
| 4 |
+
from networkx.utils.decorators import not_implemented_for
|
| 5 |
+
|
| 6 |
+
__all__ = [
|
| 7 |
+
"number_weakly_connected_components",
|
| 8 |
+
"weakly_connected_components",
|
| 9 |
+
"is_weakly_connected",
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@not_implemented_for("undirected")
|
| 14 |
+
@nx._dispatchable
|
| 15 |
+
def weakly_connected_components(G):
|
| 16 |
+
"""Generate weakly connected components of G.
|
| 17 |
+
|
| 18 |
+
Parameters
|
| 19 |
+
----------
|
| 20 |
+
G : NetworkX graph
|
| 21 |
+
A directed graph
|
| 22 |
+
|
| 23 |
+
Returns
|
| 24 |
+
-------
|
| 25 |
+
comp : generator of sets
|
| 26 |
+
A generator of sets of nodes, one for each weakly connected
|
| 27 |
+
component of G.
|
| 28 |
+
|
| 29 |
+
Raises
|
| 30 |
+
------
|
| 31 |
+
NetworkXNotImplemented
|
| 32 |
+
If G is undirected.
|
| 33 |
+
|
| 34 |
+
Examples
|
| 35 |
+
--------
|
| 36 |
+
Generate a sorted list of weakly connected components, largest first.
|
| 37 |
+
|
| 38 |
+
>>> G = nx.path_graph(4, create_using=nx.DiGraph())
|
| 39 |
+
>>> nx.add_path(G, [10, 11, 12])
|
| 40 |
+
>>> [
|
| 41 |
+
... len(c)
|
| 42 |
+
... for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)
|
| 43 |
+
... ]
|
| 44 |
+
[4, 3]
|
| 45 |
+
|
| 46 |
+
If you only want the largest component, it's more efficient to
|
| 47 |
+
use max instead of sort:
|
| 48 |
+
|
| 49 |
+
>>> largest_cc = max(nx.weakly_connected_components(G), key=len)
|
| 50 |
+
|
| 51 |
+
See Also
|
| 52 |
+
--------
|
| 53 |
+
connected_components
|
| 54 |
+
strongly_connected_components
|
| 55 |
+
|
| 56 |
+
Notes
|
| 57 |
+
-----
|
| 58 |
+
For directed graphs only.
|
| 59 |
+
|
| 60 |
+
"""
|
| 61 |
+
seen = set()
|
| 62 |
+
n = len(G) # must be outside the loop to avoid performance hit with graph views
|
| 63 |
+
for v in G:
|
| 64 |
+
if v not in seen:
|
| 65 |
+
c = set(_plain_bfs(G, n, v))
|
| 66 |
+
seen.update(c)
|
| 67 |
+
yield c
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@not_implemented_for("undirected")
|
| 71 |
+
@nx._dispatchable
|
| 72 |
+
def number_weakly_connected_components(G):
|
| 73 |
+
"""Returns the number of weakly connected components in G.
|
| 74 |
+
|
| 75 |
+
Parameters
|
| 76 |
+
----------
|
| 77 |
+
G : NetworkX graph
|
| 78 |
+
A directed graph.
|
| 79 |
+
|
| 80 |
+
Returns
|
| 81 |
+
-------
|
| 82 |
+
n : integer
|
| 83 |
+
Number of weakly connected components
|
| 84 |
+
|
| 85 |
+
Raises
|
| 86 |
+
------
|
| 87 |
+
NetworkXNotImplemented
|
| 88 |
+
If G is undirected.
|
| 89 |
+
|
| 90 |
+
Examples
|
| 91 |
+
--------
|
| 92 |
+
>>> G = nx.DiGraph([(0, 1), (2, 1), (3, 4)])
|
| 93 |
+
>>> nx.number_weakly_connected_components(G)
|
| 94 |
+
2
|
| 95 |
+
|
| 96 |
+
See Also
|
| 97 |
+
--------
|
| 98 |
+
weakly_connected_components
|
| 99 |
+
number_connected_components
|
| 100 |
+
number_strongly_connected_components
|
| 101 |
+
|
| 102 |
+
Notes
|
| 103 |
+
-----
|
| 104 |
+
For directed graphs only.
|
| 105 |
+
|
| 106 |
+
"""
|
| 107 |
+
return sum(1 for wcc in weakly_connected_components(G))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
@not_implemented_for("undirected")
|
| 111 |
+
@nx._dispatchable
|
| 112 |
+
def is_weakly_connected(G):
|
| 113 |
+
"""Test directed graph for weak connectivity.
|
| 114 |
+
|
| 115 |
+
A directed graph is weakly connected if and only if the graph
|
| 116 |
+
is connected when the direction of the edge between nodes is ignored.
|
| 117 |
+
|
| 118 |
+
Note that if a graph is strongly connected (i.e. the graph is connected
|
| 119 |
+
even when we account for directionality), it is by definition weakly
|
| 120 |
+
connected as well.
|
| 121 |
+
|
| 122 |
+
Parameters
|
| 123 |
+
----------
|
| 124 |
+
G : NetworkX Graph
|
| 125 |
+
A directed graph.
|
| 126 |
+
|
| 127 |
+
Returns
|
| 128 |
+
-------
|
| 129 |
+
connected : bool
|
| 130 |
+
True if the graph is weakly connected, False otherwise.
|
| 131 |
+
|
| 132 |
+
Raises
|
| 133 |
+
------
|
| 134 |
+
NetworkXNotImplemented
|
| 135 |
+
If G is undirected.
|
| 136 |
+
|
| 137 |
+
Examples
|
| 138 |
+
--------
|
| 139 |
+
>>> G = nx.DiGraph([(0, 1), (2, 1)])
|
| 140 |
+
>>> G.add_node(3)
|
| 141 |
+
>>> nx.is_weakly_connected(G) # node 3 is not connected to the graph
|
| 142 |
+
False
|
| 143 |
+
>>> G.add_edge(2, 3)
|
| 144 |
+
>>> nx.is_weakly_connected(G)
|
| 145 |
+
True
|
| 146 |
+
|
| 147 |
+
See Also
|
| 148 |
+
--------
|
| 149 |
+
is_strongly_connected
|
| 150 |
+
is_semiconnected
|
| 151 |
+
is_connected
|
| 152 |
+
is_biconnected
|
| 153 |
+
weakly_connected_components
|
| 154 |
+
|
| 155 |
+
Notes
|
| 156 |
+
-----
|
| 157 |
+
For directed graphs only.
|
| 158 |
+
|
| 159 |
+
"""
|
| 160 |
+
if len(G) == 0:
|
| 161 |
+
raise nx.NetworkXPointlessConcept(
|
| 162 |
+
"""Connectivity is undefined for the null graph."""
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
return len(next(weakly_connected_components(G))) == len(G)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def _plain_bfs(G, n, source):
|
| 169 |
+
"""A fast BFS node generator
|
| 170 |
+
|
| 171 |
+
The direction of the edge between nodes is ignored.
|
| 172 |
+
|
| 173 |
+
For directed graphs only.
|
| 174 |
+
|
| 175 |
+
"""
|
| 176 |
+
Gsucc = G._succ
|
| 177 |
+
Gpred = G._pred
|
| 178 |
+
seen = {source}
|
| 179 |
+
nextlevel = [source]
|
| 180 |
+
|
| 181 |
+
yield source
|
| 182 |
+
while nextlevel:
|
| 183 |
+
thislevel = nextlevel
|
| 184 |
+
nextlevel = []
|
| 185 |
+
for v in thislevel:
|
| 186 |
+
for w in Gsucc[v]:
|
| 187 |
+
if w not in seen:
|
| 188 |
+
seen.add(w)
|
| 189 |
+
nextlevel.append(w)
|
| 190 |
+
yield w
|
| 191 |
+
for w in Gpred[v]:
|
| 192 |
+
if w not in seen:
|
| 193 |
+
seen.add(w)
|
| 194 |
+
nextlevel.append(w)
|
| 195 |
+
yield w
|
| 196 |
+
if len(seen) == n:
|
| 197 |
+
return
|