summaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/setoolsgui/networkx/algorithms/bipartite/tests/test_basic.py
blob: 5b22e9a789c7621c85d1bbe42205ba7abcc236a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
from nose.tools import *
from nose import SkipTest
from nose.plugins.attrib import attr
import networkx as nx
from networkx.algorithms import bipartite
class TestBipartiteBasic:

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


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

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


    def test_bipartite_directed(self):
        G = nx.bipartite_random_graph(10, 10, 0.1, directed=True)
        assert_true(bipartite.is_bipartite(G))

    def test_bipartite_sets(self):
        G=nx.path_graph(4)
        X,Y=bipartite.sets(G)
        assert_equal(X,set([0,2]))
        assert_equal(Y,set([1,3]))

    def test_is_bipartite_node_set(self):
        G=nx.path_graph(4)
        assert_true(bipartite.is_bipartite_node_set(G,[0,2]))
        assert_true(bipartite.is_bipartite_node_set(G,[1,3]))
        assert_false(bipartite.is_bipartite_node_set(G,[1,2]))
        G.add_path([10,20])
        assert_true(bipartite.is_bipartite_node_set(G,[0,2,10]))
        assert_true(bipartite.is_bipartite_node_set(G,[0,2,20]))
        assert_true(bipartite.is_bipartite_node_set(G,[1,3,10]))
        assert_true(bipartite.is_bipartite_node_set(G,[1,3,20]))

    def test_bipartite_density(self):
        G=nx.path_graph(5)
        X,Y=bipartite.sets(G)
        density=float(len(G.edges()))/(len(X)*len(Y))
        assert_equal(bipartite.density(G,X),density)
        D = nx.DiGraph(G.edges())
        assert_equal(bipartite.density(D,X),density/2.0)
        assert_equal(bipartite.density(nx.Graph(),{}),0.0)

    def test_bipartite_degrees(self):
        G=nx.path_graph(5)
        X=set([1,3])
        Y=set([0,2,4])
        u,d=bipartite.degrees(G,Y)
        assert_equal(u,{1:2,3:2})
        assert_equal(d,{0:1,2:2,4:1})

    def test_bipartite_weighted_degrees(self):
        G=nx.path_graph(5)
        G.add_edge(0,1,weight=0.1,other=0.2)
        X=set([1,3])
        Y=set([0,2,4])
        u,d=bipartite.degrees(G,Y,weight='weight')
        assert_equal(u,{1:1.1,3:2})
        assert_equal(d,{0:0.1,2:2,4:1})
        u,d=bipartite.degrees(G,Y,weight='other')
        assert_equal(u,{1:1.2,3:2})
        assert_equal(d,{0:0.2,2:2,4:1})


    @attr('numpy')
    def test_biadjacency_matrix_weight(self):
        try:
            import numpy
        except ImportError:
            raise SkipTest('numpy not available.')
        G=nx.path_graph(5)
        G.add_edge(0,1,weight=2,other=4)
        X=[1,3]
        Y=[0,2,4]
        M = bipartite.biadjacency_matrix(G,X,weight='weight')
        assert_equal(M[0,0], 2)
        M = bipartite.biadjacency_matrix(G, X, weight='other')
        assert_equal(M[0,0], 4)

    @attr('numpy')
    def test_biadjacency_matrix(self):
        try:
            import numpy
        except ImportError:
            raise SkipTest('numpy not available.')
        tops = [2,5,10]
        bots = [5,10,15]
        for i in range(len(tops)):
            G = nx.bipartite_random_graph(tops[i], bots[i], 0.2)
            top = [n for n,d in G.nodes(data=True) if d['bipartite']==0]
            M = bipartite.biadjacency_matrix(G, top)
            assert_equal(M.shape[0],tops[i])
            assert_equal(M.shape[1],bots[i])

    @attr('numpy')
    def test_biadjacency_matrix_order(self):
        try:
            import numpy
        except ImportError:
            raise SkipTest('numpy not available.')
        G=nx.path_graph(5)
        G.add_edge(0,1,weight=2)
        X=[3,1]
        Y=[4,2,0]
        M = bipartite.biadjacency_matrix(G,X,Y,weight='weight')
        assert_equal(M[1,2], 2)