author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.algorithms.triads` module."""
2
3 import networkx as nx
4 from collections import defaultdict
5 from random import sample
6
7
10 G = nx.DiGraph()
11 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
12 expected = {
13 "030T": 2,
14 "120C": 1,
15 "210": 0,
16 "120U": 0,
17 "012": 9,
18 "102": 3,
19 "021U": 0,
20 "111U": 0,
21 "003": 8,
22 "030C": 0,
23 "021D": 9,
24 "201": 0,
25 "111D": 1,
26 "300": 0,
27 "120D": 0,
28 "021C": 2,
29 }
31 assert expected == actual
32
33
36 G = nx.karate_club_graph()
37 G = G.to_directed()
38 for i in range(100):
39 nodes = sample(G.nodes(), 3)
40 G2 = G.subgraph(nodes)
42
43
44 def test_all_triplets():
45 """Tests the all_triplets function."""
46 G = nx.DiGraph()
47 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
48 expected = [
49 f"{i},{j},{k}"
50 for i in range(7)
51 for j in range(i + 1, 7)
52 for k in range(j + 1, 7)
53 ]
54 expected = [set(x.split(",")) for x in expected]
55 actual = list(set(x) for x in nx.all_triplets(G))
56 assert all([any([s1 == s2 for s1 in expected]) for s2 in actual])
57
58
60 """Tests the all_triplets function."""
61 G = nx.DiGraph()
62 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
63 expected = [
64 f"{i},{j},{k}"
65 for i in range(7)
66 for j in range(i + 1, 7)
67 for k in range(j + 1, 7)
68 ]
69 expected = [G.subgraph(x.split(",")) for x in expected]
71 assert all(any([nx.is_isomorphic(G1, G2) for G1 in expected]) for G2 in actual)
72
73
76 # 0 edges (1 type)
77 G = nx.DiGraph({0: [], 1: [], 2: []})
79 # 1 edge (1 type)
80 G = nx.DiGraph({0: [1], 1: [], 2: []})
82 # 2 edges (4 types)
83 G = nx.DiGraph([(0, 1), (0, 2)])
85 G = nx.DiGraph({0: [1], 1: [0], 2: []})
87 G = nx.DiGraph([(0, 1), (2, 1)])
89 G = nx.DiGraph([(0, 1), (1, 2)])
91 # 3 edges (4 types)
92 G = nx.DiGraph([(0, 1), (1, 0), (2, 1)])
94 G = nx.DiGraph([(0, 1), (1, 0), (1, 2)])
96 G = nx.DiGraph([(0, 1), (1, 2), (0, 2)])
98 G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
100 # 4 edges (4 types)
101 G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (0, 2)])
103 G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (2, 1)])
105 G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (1, 2)])
107 G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (2, 1)])
109 # 5 edges (1 type)
110 G = nx.DiGraph([(0, 1), (1, 0), (2, 1), (1, 2), (0, 2)])
112 # 6 edges (1 type)
113 G = nx.DiGraph([(0, 1), (1, 0), (1, 2), (2, 1), (0, 2), (2, 0)])
115
116
118 """Tests the all_triplets function."""
119 G = nx.DiGraph()
120 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"])
122 expected = defaultdict(list)
127 assert set(actual.keys()) == set(expected.keys())
128 for tri_type, actual_Gs in actual.items():
129 expected_Gs = expected[tri_type]
130 for a in actual_Gs:
131 assert any(nx.is_isomorphic(a, e) for e in expected_Gs)
132
133
136 G = nx.karate_club_graph()
137 G = G.to_directed()
138 for i in range(100):