Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / bipartite / tests / test_basic.py @ 5cef0f13

History | View | Annotate | Download (4.6 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from nose import SkipTest
4
from nose.plugins.attrib import attr
5
import networkx as nx
6
from networkx.algorithms import bipartite
7

    
8

    
9
class TestBipartiteBasic:
10

    
11
    def test_is_bipartite(self):
12
        assert_true(bipartite.is_bipartite(nx.path_graph(4)))
13
        assert_true(bipartite.is_bipartite(nx.DiGraph([(1, 0)])))
14
        assert_false(bipartite.is_bipartite(nx.complete_graph(3)))
15

    
16
    def test_bipartite_color(self):
17
        G = nx.path_graph(4)
18
        c = bipartite.color(G)
19
        assert_equal(c, {0: 1, 1: 0, 2: 1, 3: 0})
20

    
21
    @raises(nx.NetworkXError)
22
    def test_not_bipartite_color(self):
23
        c = bipartite.color(nx.complete_graph(4))
24

    
25
    def test_bipartite_directed(self):
26
        G = bipartite.random_graph(10, 10, 0.1, directed=True)
27
        assert_true(bipartite.is_bipartite(G))
28

    
29
    def test_bipartite_sets(self):
30
        G = nx.path_graph(4)
31
        X, Y = bipartite.sets(G)
32
        assert_equal(X, {0, 2})
33
        assert_equal(Y, {1, 3})
34

    
35
    def test_bipartite_sets_directed(self):
36
        G = nx.path_graph(4)
37
        D = G.to_directed()
38
        X, Y = bipartite.sets(D)
39
        assert_equal(X, {0, 2})
40
        assert_equal(Y, {1, 3})
41

    
42
    def test_bipartite_sets_given_top_nodes(self):
43
        G = nx.path_graph(4)
44
        top_nodes = [0, 2]
45
        X, Y = bipartite.sets(G, top_nodes)
46
        assert_equal(X, {0, 2})
47
        assert_equal(Y, {1, 3})
48

    
49
    @raises(nx.AmbiguousSolution)
50
    def test_bipartite_sets_disconnected(self):
51
        G = nx.path_graph(4)
52
        G.add_edges_from([(5, 6), (6, 7)])
53
        X, Y = bipartite.sets(G)
54

    
55
    def test_is_bipartite_node_set(self):
56
        G = nx.path_graph(4)
57
        assert_true(bipartite.is_bipartite_node_set(G, [0, 2]))
58
        assert_true(bipartite.is_bipartite_node_set(G, [1, 3]))
59
        assert_false(bipartite.is_bipartite_node_set(G, [1, 2]))
60
        G.add_edge(10, 20)
61
        assert_true(bipartite.is_bipartite_node_set(G, [0, 2, 10]))
62
        assert_true(bipartite.is_bipartite_node_set(G, [0, 2, 20]))
63
        assert_true(bipartite.is_bipartite_node_set(G, [1, 3, 10]))
64
        assert_true(bipartite.is_bipartite_node_set(G, [1, 3, 20]))
65

    
66
    def test_bipartite_density(self):
67
        G = nx.path_graph(5)
68
        X, Y = bipartite.sets(G)
69
        density = float(len(list(G.edges()))) / (len(X) * len(Y))
70
        assert_equal(bipartite.density(G, X), density)
71
        D = nx.DiGraph(G.edges())
72
        assert_equal(bipartite.density(D, X), density / 2.0)
73
        assert_equal(bipartite.density(nx.Graph(), {}), 0.0)
74

    
75
    def test_bipartite_degrees(self):
76
        G = nx.path_graph(5)
77
        X = set([1, 3])
78
        Y = set([0, 2, 4])
79
        u, d = bipartite.degrees(G, Y)
80
        assert_equal(dict(u), {1: 2, 3: 2})
81
        assert_equal(dict(d), {0: 1, 2: 2, 4: 1})
82

    
83
    def test_bipartite_weighted_degrees(self):
84
        G = nx.path_graph(5)
85
        G.add_edge(0, 1, weight=0.1, other=0.2)
86
        X = set([1, 3])
87
        Y = set([0, 2, 4])
88
        u, d = bipartite.degrees(G, Y, weight='weight')
89
        assert_equal(dict(u), {1: 1.1, 3: 2})
90
        assert_equal(dict(d), {0: 0.1, 2: 2, 4: 1})
91
        u, d = bipartite.degrees(G, Y, weight='other')
92
        assert_equal(dict(u), {1: 1.2, 3: 2})
93
        assert_equal(dict(d), {0: 0.2, 2: 2, 4: 1})
94

    
95
    @attr('numpy')
96
    def test_biadjacency_matrix_weight(self):
97
        try:
98
            import scipy
99
        except ImportError:
100
            raise SkipTest('SciPy not available.')
101
        G = nx.path_graph(5)
102
        G.add_edge(0, 1, weight=2, other=4)
103
        X = [1, 3]
104
        Y = [0, 2, 4]
105
        M = bipartite.biadjacency_matrix(G, X, weight='weight')
106
        assert_equal(M[0, 0], 2)
107
        M = bipartite.biadjacency_matrix(G, X, weight='other')
108
        assert_equal(M[0, 0], 4)
109

    
110
    @attr('numpy')
111
    def test_biadjacency_matrix(self):
112
        try:
113
            import scipy
114
        except ImportError:
115
            raise SkipTest('SciPy not available.')
116
        tops = [2, 5, 10]
117
        bots = [5, 10, 15]
118
        for i in range(len(tops)):
119
            G = bipartite.random_graph(tops[i], bots[i], 0.2)
120
            top = [n for n, d in G.nodes(data=True) if d['bipartite'] == 0]
121
            M = bipartite.biadjacency_matrix(G, top)
122
            assert_equal(M.shape[0], tops[i])
123
            assert_equal(M.shape[1], bots[i])
124

    
125
    @attr('numpy')
126
    def test_biadjacency_matrix_order(self):
127
        try:
128
            import scipy
129
        except ImportError:
130
            raise SkipTest('SciPy not available.')
131
        G = nx.path_graph(5)
132
        G.add_edge(0, 1, weight=2)
133
        X = [3, 1]
134
        Y = [4, 2, 0]
135
        M = bipartite.biadjacency_matrix(G, X, Y, weight='weight')
136
        assert_equal(M[1, 2], 2)