comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_nonisomorphic_trees.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 """
2 ====================
3 Generators - Non Isomorphic Trees
4 ====================
5
6 Unit tests for WROM algorithm generator in generators/nonisomorphic_trees.py
7 """
8 import networkx as nx
9 from networkx.testing import assert_edges_equal
10
11
12 class TestGeneratorNonIsomorphicTrees:
13 def test_tree_structure(self):
14 # test for tree structure for nx.nonisomorphic_trees()
15 def f(x):
16 return list(nx.nonisomorphic_trees(x))
17
18 for i in f(6):
19 assert nx.is_tree(i)
20 for i in f(8):
21 assert nx.is_tree(i)
22
23 def test_nonisomorphism(self):
24 # test for nonisomorphism of trees for nx.nonisomorphic_trees()
25 def f(x):
26 return list(nx.nonisomorphic_trees(x))
27
28 trees = f(6)
29 for i in range(len(trees)):
30 for j in range(i + 1, len(trees)):
31 assert not nx.is_isomorphic(trees[i], trees[j])
32 trees = f(8)
33 for i in range(len(trees)):
34 for j in range(i + 1, len(trees)):
35 assert not nx.is_isomorphic(trees[i], trees[j])
36
37 def test_number_of_nonisomorphic_trees(self):
38 # http://oeis.org/A000055
39 assert nx.number_of_nonisomorphic_trees(2) == 1
40 assert nx.number_of_nonisomorphic_trees(3) == 1
41 assert nx.number_of_nonisomorphic_trees(4) == 2
42 assert nx.number_of_nonisomorphic_trees(5) == 3
43 assert nx.number_of_nonisomorphic_trees(6) == 6
44 assert nx.number_of_nonisomorphic_trees(7) == 11
45 assert nx.number_of_nonisomorphic_trees(8) == 23
46
47 def test_nonisomorphic_trees(self):
48 def f(x):
49 return list(nx.nonisomorphic_trees(x))
50
51 assert_edges_equal(f(3)[0].edges(), [(0, 1), (0, 2)])
52 assert_edges_equal(f(4)[0].edges(), [(0, 1), (0, 3), (1, 2)])
53 assert_edges_equal(f(4)[1].edges(), [(0, 1), (0, 2), (0, 3)])
54
55 def test_nonisomorphic_trees_matrix(self):
56 trees_2 = [[[0, 1], [1, 0]]]
57 assert list(nx.nonisomorphic_trees(2, create="matrix")) == trees_2
58 trees_3 = [[[0, 1, 1], [1, 0, 0], [1, 0, 0]]]
59 assert list(nx.nonisomorphic_trees(3, create="matrix")) == trees_3
60 trees_4 = [
61 [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]],
62 [[0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]],
63 ]
64 assert list(nx.nonisomorphic_trees(4, create="matrix")) == trees_4