comparison env/lib/python3.9/site-packages/networkx/algorithms/components/tests/test_connected.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2 import networkx as nx
3 from networkx import convert_node_labels_to_integers as cnlti
4 from networkx import NetworkXNotImplemented
5
6
7 class TestConnected:
8 @classmethod
9 def setup_class(cls):
10 G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
11 G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
12 G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
13 cls.G = nx.union(G1, G2)
14 cls.G = nx.union(cls.G, G3)
15 cls.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
16 cls.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)
17
18 cls.gc = []
19 G = nx.DiGraph()
20 G.add_edges_from(
21 [
22 (1, 2),
23 (2, 3),
24 (2, 8),
25 (3, 4),
26 (3, 7),
27 (4, 5),
28 (5, 3),
29 (5, 6),
30 (7, 4),
31 (7, 6),
32 (8, 1),
33 (8, 7),
34 ]
35 )
36 C = [[3, 4, 5, 7], [1, 2, 8], [6]]
37 cls.gc.append((G, C))
38
39 G = nx.DiGraph()
40 G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
41 C = [[2, 3, 4], [1]]
42 cls.gc.append((G, C))
43
44 G = nx.DiGraph()
45 G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
46 C = [[1, 2, 3]]
47 cls.gc.append((G, C))
48
49 # Eppstein's tests
50 G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
51 C = [[0], [1], [2], [3], [4], [5], [6]]
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 = [[0, 1, 2], [3, 4]]
56 cls.gc.append((G, C))
57
58 G = nx.DiGraph()
59 C = []
60 cls.gc.append((G, C))
61
62 def test_connected_components(self):
63 cc = nx.connected_components
64 G = self.G
65 C = {
66 frozenset([0, 1, 2, 3]),
67 frozenset([4, 5, 6, 7, 8, 9]),
68 frozenset([10, 11, 12, 13, 14]),
69 }
70 assert {frozenset(g) for g in cc(G)} == C
71
72 def test_number_connected_components(self):
73 ncc = nx.number_connected_components
74 assert ncc(self.G) == 3
75
76 def test_number_connected_components2(self):
77 ncc = nx.number_connected_components
78 assert ncc(self.grid) == 1
79
80 def test_connected_components2(self):
81 cc = nx.connected_components
82 G = self.grid
83 C = {frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])}
84 assert {frozenset(g) for g in cc(G)} == C
85
86 def test_node_connected_components(self):
87 ncc = nx.node_connected_component
88 G = self.grid
89 C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
90 assert ncc(G, 1) == C
91
92 def test_is_connected(self):
93 assert nx.is_connected(self.grid)
94 G = nx.Graph()
95 G.add_nodes_from([1, 2])
96 assert not nx.is_connected(G)
97
98 def test_connected_raise(self):
99 pytest.raises(NetworkXNotImplemented, nx.connected_components, self.DG)
100 pytest.raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
101 pytest.raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
102 pytest.raises(NetworkXNotImplemented, nx.is_connected, self.DG)
103 pytest.raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
104
105 def test_connected_mutability(self):
106 G = self.grid
107 seen = set()
108 for component in nx.connected_components(G):
109 assert len(seen & component) == 0
110 seen.update(component)
111 component.clear()