### comparison env/lib/python3.9/site-packages/networkx/algorithms/components/tests/test_weakly_connected.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2 import networkx as nx
3 from networkx import NetworkXNotImplemented
4
5
6 class TestWeaklyConnected:
7 @classmethod
8 def setup_class(cls):
9 cls.gc = []
10 G = nx.DiGraph()
12 [
13 (1, 2),
14 (2, 3),
15 (2, 8),
16 (3, 4),
17 (3, 7),
18 (4, 5),
19 (5, 3),
20 (5, 6),
21 (7, 4),
22 (7, 6),
23 (8, 1),
24 (8, 7),
25 ]
26 )
27 C = [[3, 4, 5, 7], [1, 2, 8], [6]]
28 cls.gc.append((G, C))
29
30 G = nx.DiGraph()
31 G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
32 C = [[2, 3, 4], [1]]
33 cls.gc.append((G, C))
34
35 G = nx.DiGraph()
36 G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
37 C = [[1, 2, 3]]
38 cls.gc.append((G, C))
39
40 # Eppstein's tests
41 G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
42 C = [[0], [1], [2], [3], [4], [5], [6]]
43 cls.gc.append((G, C))
44
45 G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
46 C = [[0, 1, 2], [3, 4]]
47 cls.gc.append((G, C))
48
49 def test_weakly_connected_components(self):
50 for G, C in self.gc:
51 U = G.to_undirected()
52 w = {frozenset(g) for g in nx.weakly_connected_components(G)}
53 c = {frozenset(g) for g in nx.connected_components(U)}
54 assert w == c
55
56 def test_number_weakly_connected_components(self):
57 for G, C in self.gc:
58 U = G.to_undirected()
59 w = nx.number_weakly_connected_components(G)
60 c = nx.number_connected_components(U)
61 assert w == c
62
63 def test_is_weakly_connected(self):
64 for G, C in self.gc:
65 U = G.to_undirected()
66 assert nx.is_weakly_connected(G) == nx.is_connected(U)
67
68 def test_null_graph(self):
69 G = nx.DiGraph()
70 assert list(nx.weakly_connected_components(G)) == []
71 assert nx.number_weakly_connected_components(G) == 0
72 pytest.raises(nx.NetworkXPointlessConcept, nx.is_weakly_connected, G)
73
74 def test_connected_raise(self):
75 G = nx.Graph()
76 pytest.raises(NetworkXNotImplemented, nx.weakly_connected_components, G)
77 pytest.raises(NetworkXNotImplemented, nx.number_weakly_connected_components, G)
78 pytest.raises(NetworkXNotImplemented, nx.is_weakly_connected, G)