Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.63 KB)

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

    
7

    
8
class TestConnected:
9

    
10
    def setUp(self):
11
        G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
12
        G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
13
        G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
14
        self.G = nx.union(G1, G2)
15
        self.G = nx.union(self.G, G3)
16
        self.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
17
        self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)
18

    
19
        self.gc = []
20
        G = nx.DiGraph()
21
        G.add_edges_from([(1, 2), (2, 3), (2, 8), (3, 4), (3, 7), (4, 5),
22
                          (5, 3), (5, 6), (7, 4), (7, 6), (8, 1), (8, 7)])
23
        C = [[3, 4, 5, 7], [1, 2, 8], [6]]
24
        self.gc.append((G, C))
25

    
26
        G = nx.DiGraph()
27
        G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
28
        C = [[2, 3, 4], [1]]
29
        self.gc.append((G, C))
30

    
31
        G = nx.DiGraph()
32
        G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
33
        C = [[1, 2, 3]]
34
        self.gc.append((G, C))
35

    
36
        # Eppstein's tests
37
        G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
38
        C = [[0], [1], [2], [3], [4], [5], [6]]
39
        self.gc.append((G, C))
40

    
41
        G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
42
        C = [[0, 1, 2], [3, 4]]
43
        self.gc.append((G, C))
44

    
45
        G = nx.DiGraph()
46
        C = []
47
        self.gc.append((G, C))
48

    
49
    def test_connected_components(self):
50
        cc = nx.connected_components
51
        G = self.G
52
        C = {
53
            frozenset([0, 1, 2, 3]),
54
            frozenset([4, 5, 6, 7, 8, 9]),
55
            frozenset([10, 11, 12, 13, 14])
56
        }
57
        assert_equal({frozenset(g) for g in cc(G)}, C)
58

    
59
    def test_number_connected_components(self):
60
        ncc = nx.number_connected_components
61
        assert_equal(ncc(self.G), 3)
62

    
63
    def test_number_connected_components2(self):
64
        ncc = nx.number_connected_components
65
        assert_equal(ncc(self.grid), 1)
66

    
67
    def test_connected_components2(self):
68
        cc = nx.connected_components
69
        G = self.grid
70
        C = {frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])}
71
        assert_equal({frozenset(g) for g in cc(G)}, C)
72

    
73
    def test_node_connected_components(self):
74
        ncc = nx.node_connected_component
75
        G = self.grid
76
        C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
77
        assert_equal(ncc(G, 1), C)
78

    
79
    # deprecated
80
    def test_connected_component_subgraphs(self):
81
        wcc = nx.weakly_connected_component_subgraphs
82
        cc = nx.connected_component_subgraphs
83
        for G, C in self.gc:
84
            U = G.to_undirected()
85
            w = {frozenset(g) for g in wcc(G)}
86
            c = {frozenset(g) for g in cc(U)}
87
            assert_equal(w, c)
88

    
89
    def test_is_connected(self):
90
        assert_true(nx.is_connected(self.grid))
91
        G = nx.Graph()
92
        G.add_nodes_from([1, 2])
93
        assert_false(nx.is_connected(G))
94

    
95
    def test_connected_raise(self):
96
        assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
97
        assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
98
        assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
99
        assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
100
        assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
101
        # deprecated
102
        assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG)