Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.76 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 TestWeaklyConnected:
8

    
9
    def setUp(self):
10
        self.gc = []
11
        G = nx.DiGraph()
12
        G.add_edges_from([(1, 2), (2, 3), (2, 8), (3, 4), (3, 7), (4, 5),
13
                          (5, 3), (5, 6), (7, 4), (7, 6), (8, 1), (8, 7)])
14
        C = [[3, 4, 5, 7], [1, 2, 8], [6]]
15
        self.gc.append((G, C))
16

    
17
        G = nx.DiGraph()
18
        G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
19
        C = [[2, 3, 4], [1]]
20
        self.gc.append((G, C))
21

    
22
        G = nx.DiGraph()
23
        G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
24
        C = [[1, 2, 3]]
25
        self.gc.append((G, C))
26

    
27
        # Eppstein's tests
28
        G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
29
        C = [[0], [1], [2], [3], [4], [5], [6]]
30
        self.gc.append((G, C))
31

    
32
        G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
33
        C = [[0, 1, 2], [3, 4]]
34
        self.gc.append((G, C))
35

    
36
    def test_weakly_connected_components(self):
37
        for G, C in self.gc:
38
            U = G.to_undirected()
39
            w = {frozenset(g) for g in nx.weakly_connected_components(G)}
40
            c = {frozenset(g) for g in nx.connected_components(U)}
41
            assert_equal(w, c)
42

    
43
    def test_number_weakly_connected_components(self):
44
        for G, C in self.gc:
45
            U = G.to_undirected()
46
            w = nx.number_weakly_connected_components(G)
47
            c = nx.number_connected_components(U)
48
            assert_equal(w, c)
49

    
50
    # deprecated
51
    def test_weakly_connected_component_subgraphs(self):
52
        wcc = nx.weakly_connected_component_subgraphs
53
        cc = nx.connected_component_subgraphs
54
        for G, C in self.gc:
55
            U = G.to_undirected()
56
            w = {frozenset(g) for g in wcc(G)}
57
            c = {frozenset(g) for g in cc(U)}
58
            assert_equal(w, c)
59

    
60
    def test_is_weakly_connected(self):
61
        for G, C in self.gc:
62
            U = G.to_undirected()
63
            assert_equal(nx.is_weakly_connected(G), nx.is_connected(U))
64

    
65
    def test_null_graph(self):
66
        G = nx.DiGraph()
67
        assert_equal(list(nx.weakly_connected_components(G)), [])
68
        assert_equal(nx.number_weakly_connected_components(G), 0)
69
        assert_raises(nx.NetworkXPointlessConcept, nx.is_weakly_connected, G)
70

    
71
    def test_connected_raise(self):
72
        G = nx.Graph()
73
        assert_raises(NetworkXNotImplemented, nx.weakly_connected_components, G)
74
        assert_raises(NetworkXNotImplemented, nx.number_weakly_connected_components, G)
75
        assert_raises(NetworkXNotImplemented, nx.is_weakly_connected, G)
76
        # deprecated
77
        assert_raises(NetworkXNotImplemented, nx.weakly_connected_component_subgraphs, G)