Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / components / tests / test_attracting.py @ 5cef0f13

History | View | Annotate | Download (2.23 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
import networkx as nx
4
from networkx import NetworkXNotImplemented
5

    
6

    
7
class TestAttractingComponents(object):
8
    def setUp(self):
9
        self.G1 = nx.DiGraph()
10
        self.G1.add_edges_from([(5, 11), (11, 2), (11, 9), (11, 10),
11
                                (7, 11), (7, 8), (8, 9), (3, 8), (3, 10)])
12
        self.G2 = nx.DiGraph()
13
        self.G2.add_edges_from([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1)])
14

    
15
        self.G3 = nx.DiGraph()
16
        self.G3.add_edges_from([(0, 1), (1, 2), (2, 1), (0, 3), (3, 4), (4, 3)])
17

    
18
        self.G4 = nx.DiGraph()
19

    
20
    def test_attracting_components(self):
21
        ac = list(nx.attracting_components(self.G1))
22
        assert_true({2} in ac)
23
        assert_true({9} in ac)
24
        assert_true({10} in ac)
25

    
26
        ac = list(nx.attracting_components(self.G2))
27
        ac = [tuple(sorted(x)) for x in ac]
28
        assert_true(ac == [(1, 2)])
29

    
30
        ac = list(nx.attracting_components(self.G3))
31
        ac = [tuple(sorted(x)) for x in ac]
32
        assert_true((1, 2) in ac)
33
        assert_true((3, 4) in ac)
34
        assert_equal(len(ac), 2)
35

    
36
        ac = list(nx.attracting_components(self.G4))
37
        assert_equal(ac, [])
38

    
39
    def test_number_attacting_components(self):
40
        assert_equal(nx.number_attracting_components(self.G1), 3)
41
        assert_equal(nx.number_attracting_components(self.G2), 1)
42
        assert_equal(nx.number_attracting_components(self.G3), 2)
43
        assert_equal(nx.number_attracting_components(self.G4), 0)
44

    
45
    def test_is_attracting_component(self):
46
        assert_false(nx.is_attracting_component(self.G1))
47
        assert_false(nx.is_attracting_component(self.G2))
48
        assert_false(nx.is_attracting_component(self.G3))
49
        g2 = self.G3.subgraph([1, 2])
50
        assert_true(nx.is_attracting_component(g2))
51
        assert_false(nx.is_attracting_component(self.G4))
52

    
53
    def test_connected_raise(self):
54
        G = nx.Graph()
55
        assert_raises(NetworkXNotImplemented, nx.attracting_components, G)
56
        assert_raises(NetworkXNotImplemented, nx.number_attracting_components, G)
57
        assert_raises(NetworkXNotImplemented, nx.is_attracting_component, G)
58
        # deprecated
59
        assert_raises(NetworkXNotImplemented, nx.attracting_component_subgraphs, G)