Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / generators / tests / test_nonisomorphic_trees.py @ 5cef0f13

History | View | Annotate | Download (2.38 KB)

1
#!/usr/bin/env python
2
"""
3
====================
4
Generators - Non Isomorphic Trees
5
====================
6

7
Unit tests for WROM algorithm generator in generators/nonisomorphic_trees.py
8
"""
9
from nose.tools import *
10
from networkx import *
11
from networkx.testing import *
12

    
13

    
14
class TestGeneratorNonIsomorphicTrees():
15

    
16
    def test_tree_structure(self):
17
        # test for tree structure for nx.nonisomorphic_trees()
18
        def f(x): return list(nx.nonisomorphic_trees(x))
19
        for i in f(6):
20
            assert_true(nx.is_tree(i))
21
        for i in f(8):
22
            assert_true(nx.is_tree(i))
23

    
24
    def test_nonisomorphism(self):
25
        # test for nonisomorphism of trees for nx.nonisomorphic_trees()
26
        def f(x): return list(nx.nonisomorphic_trees(x))
27
        trees = f(6)
28
        for i in range(len(trees)):
29
            for j in range(i + 1, len(trees)):
30
                assert_false(nx.is_isomorphic(trees[i], trees[j]))
31
        trees = f(8)
32
        for i in range(len(trees)):
33
            for j in range(i + 1, len(trees)):
34
                assert_false(nx.is_isomorphic(trees[i], trees[j]))
35

    
36
    def test_number_of_nonisomorphic_trees(self):
37
        # http://oeis.org/A000055
38
        assert_equal(nx.number_of_nonisomorphic_trees(2), 1)
39
        assert_equal(nx.number_of_nonisomorphic_trees(3), 1)
40
        assert_equal(nx.number_of_nonisomorphic_trees(4), 2)
41
        assert_equal(nx.number_of_nonisomorphic_trees(5), 3)
42
        assert_equal(nx.number_of_nonisomorphic_trees(6), 6)
43
        assert_equal(nx.number_of_nonisomorphic_trees(7), 11)
44
        assert_equal(nx.number_of_nonisomorphic_trees(8), 23)
45

    
46
    def test_nonisomorphic_trees(self):
47
        def f(x): return list(nx.nonisomorphic_trees(x))
48
        assert_edges_equal(f(3)[0].edges(), [(0, 1), (0, 2)])
49
        assert_edges_equal(f(4)[0].edges(), [(0, 1), (0, 3), (1, 2)])
50
        assert_edges_equal(f(4)[1].edges(), [(0, 1), (0, 2), (0, 3)])
51

    
52
    def test_nonisomorphic_trees_matrix(self):
53
        trees_2 = [[[0, 1], [1, 0]]]
54
        assert_equal(list(nx.nonisomorphic_trees(2, create="matrix")), trees_2)
55
        trees_3 = [[[0, 1, 1], [1, 0, 0], [1, 0, 0]]]
56
        assert_equal(list(nx.nonisomorphic_trees(3, create="matrix")), trees_3)
57
        trees_4 = [[[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]],
58
                   [[0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]]
59
        assert_equal(list(nx.nonisomorphic_trees(4, create="matrix")), trees_4)