Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.54 KB)

1
# -*- coding: utf-8 -*-
2
from nose import SkipTest
3
from nose.tools import *
4
import networkx as nx
5
from networkx.algorithms.bipartite import spectral_bipartivity as sb
6

    
7
# Examples from Figure 1
8
# E. Estrada and J. A. Rodríguez-Velázquez, "Spectral measures of
9
# bipartivity in complex networks", PhysRev E 72, 046105 (2005)
10

    
11

    
12
class TestSpectralBipartivity(object):
13
    @classmethod
14
    def setupClass(cls):
15
        global scipy
16
        global assert_equal
17
        global assert_almost_equal
18
        try:
19
            import scipy.linalg
20
        except ImportError:
21
            raise SkipTest('SciPy not available.')
22

    
23
    def test_star_like(self):
24
        # star-like
25

    
26
        G = nx.star_graph(2)
27
        G.add_edge(1, 2)
28
        assert_almost_equal(sb(G), 0.843, places=3)
29

    
30
        G = nx.star_graph(3)
31
        G.add_edge(1, 2)
32
        assert_almost_equal(sb(G), 0.871, places=3)
33

    
34
        G = nx.star_graph(4)
35
        G.add_edge(1, 2)
36
        assert_almost_equal(sb(G), 0.890, places=3)
37

    
38
    def k23_like(self):
39
        # K2,3-like
40
        G = nx.complete_bipartite_graph(2, 3)
41
        G.add_edge(0, 1)
42
        assert_almost_equal(sb(G), 0.769, places=3)
43

    
44
        G = nx.complete_bipartite_graph(2, 3)
45
        G.add_edge(2, 4)
46
        assert_almost_equal(sb(G), 0.829, places=3)
47

    
48
        G = nx.complete_bipartite_graph(2, 3)
49
        G.add_edge(2, 4)
50
        G.add_edge(3, 4)
51
        assert_almost_equal(sb(G), 0.731, places=3)
52

    
53
        G = nx.complete_bipartite_graph(2, 3)
54
        G.add_edge(0, 1)
55
        G.add_edge(2, 4)
56
        assert_almost_equal(sb(G), 0.692, places=3)
57

    
58
        G = nx.complete_bipartite_graph(2, 3)
59
        G.add_edge(2, 4)
60
        G.add_edge(3, 4)
61
        G.add_edge(0, 1)
62
        assert_almost_equal(sb(G), 0.645, places=3)
63

    
64
        G = nx.complete_bipartite_graph(2, 3)
65
        G.add_edge(2, 4)
66
        G.add_edge(3, 4)
67
        G.add_edge(2, 3)
68
        assert_almost_equal(sb(G), 0.645, places=3)
69

    
70
        G = nx.complete_bipartite_graph(2, 3)
71
        G.add_edge(2, 4)
72
        G.add_edge(3, 4)
73
        G.add_edge(2, 3)
74
        G.add_edge(0, 1)
75
        assert_almost_equal(sb(G), 0.597, places=3)
76

    
77
    def test_single_nodes(self):
78

    
79
        # single nodes
80
        G = nx.complete_bipartite_graph(2, 3)
81
        G.add_edge(2, 4)
82
        sbn = sb(G, nodes=[1, 2])
83
        assert_almost_equal(sbn[1], 0.85, places=2)
84
        assert_almost_equal(sbn[2], 0.77, places=2)
85

    
86
        G = nx.complete_bipartite_graph(2, 3)
87
        G.add_edge(0, 1)
88
        sbn = sb(G, nodes=[1, 2])
89
        assert_almost_equal(sbn[1], 0.73, places=2)
90
        assert_almost_equal(sbn[2], 0.82, places=2)