File: test_community.py

package info (click to toggle)
python-networkx 1.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,052 kB
  • ctags: 3,986
  • sloc: python: 52,132; makefile: 176
file content (119 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download | duplicates (3)
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
118
119
import networkx as nx
from nose.tools import *

def test_random_partition_graph():
    G = nx.random_partition_graph([3,3,3],1,0)
    C = G.graph['partition'] 
    assert_equal(C,[set([0,1,2]), set([3,4,5]), set([6,7,8])])
    assert_equal(len(G),9)
    assert_equal(len(G.edges()),9)

    G = nx.random_partition_graph([3,3,3],0,1)
    C = G.graph['partition'] 
    assert_equal(C,[set([0,1,2]), set([3,4,5]), set([6,7,8])])
    assert_equal(len(G),9)
    assert_equal(len(G.edges()),27)

    G = nx.random_partition_graph([3,3,3],1,0,directed=True)
    C = G.graph['partition'] 
    assert_equal(C,[set([0,1,2]), set([3,4,5]), set([6,7,8])])
    assert_equal(len(G),9)
    assert_equal(len(G.edges()),18)

    G = nx.random_partition_graph([3,3,3],0,1,directed=True)
    C = G.graph['partition'] 
    assert_equal(C,[set([0,1,2]), set([3,4,5]), set([6,7,8])])
    assert_equal(len(G),9)
    assert_equal(len(G.edges()),54)

    G = nx.random_partition_graph([1,2,3,4,5], 0.5, 0.1)
    C = G.graph['partition'] 
    assert_equal(C,[set([0]), set([1,2]), set([3,4,5]),
                    set([6,7,8,9]), set([10,11,12,13,14])])
    assert_equal(len(G),15)

    assert_raises(nx.NetworkXError, nx.random_partition_graph,[1,2,3],1.1,0.1)
    assert_raises(nx.NetworkXError, nx.random_partition_graph,[1,2,3],-0.1,0.1)
    assert_raises(nx.NetworkXError, nx.random_partition_graph,[1,2,3],0.1,1.1)
    assert_raises(nx.NetworkXError, nx.random_partition_graph,[1,2,3],0.1,-0.1)

def test_planted_partition_graph():
    G = nx.planted_partition_graph(4,3,1,0)
    C = G.graph['partition'] 
    assert_equal(len(C),4)
    assert_equal(len(G),12)
    assert_equal(len(G.edges()),12)

    G = nx.planted_partition_graph(4,3,0,1)
    C = G.graph['partition'] 
    assert_equal(len(C),4)
    assert_equal(len(G),12)
    assert_equal(len(G.edges()),54)

    G = nx.planted_partition_graph(10,4,.5,.1,seed=42)
    C = G.graph['partition']
    assert_equal(len(C),10)
    assert_equal(len(G),40)
    assert_equal(len(G.edges()),108)

    G = nx.planted_partition_graph(4,3,1,0,directed=True)
    C = G.graph['partition'] 
    assert_equal(len(C),4)
    assert_equal(len(G),12)
    assert_equal(len(G.edges()),24)

    G = nx.planted_partition_graph(4,3,0,1,directed=True)
    C = G.graph['partition'] 
    assert_equal(len(C),4)
    assert_equal(len(G),12)
    assert_equal(len(G.edges()),108)

    G = nx.planted_partition_graph(10,4,.5,.1,seed=42,directed=True)
    C = G.graph['partition'] 
    assert_equal(len(C),10)
    assert_equal(len(G),40)
    assert_equal(len(G.edges()),218)

    assert_raises(nx.NetworkXError, nx.planted_partition_graph, 3, 3, 1.1, 0.1)
    assert_raises(nx.NetworkXError, nx.planted_partition_graph, 3, 3,-0.1, 0.1)
    assert_raises(nx.NetworkXError, nx.planted_partition_graph, 3, 3, 0.1, 1.1)
    assert_raises(nx.NetworkXError, nx.planted_partition_graph, 3, 3, 0.1,-0.1)
    
def test_relaxed_caveman_graph():
    G = nx.relaxed_caveman_graph(4,3,0)
    assert_equal(len(G),12)
    assert_equal(len(G.nodes()),12)
    
    G = nx.relaxed_caveman_graph(4,3,1)
    assert_equal(len(G),12)
    assert_equal(len(G.nodes()),12)

    G = nx.relaxed_caveman_graph(4,3,0.5)
    assert_equal(len(G),12)
    assert_equal(len(G.edges()),12)

def test_connected_caveman_graph():
    G = nx.connected_caveman_graph(4,3)
    assert_equal(len(G),12)
    assert_equal(len(G.nodes()),12)
    
    G = nx.connected_caveman_graph(1,5)
    K5 = nx.complete_graph(5)
    K5.remove_edge(3,4)
    assert_true(nx.is_isomorphic(G,K5))

def test_caveman_graph():
    G = nx.caveman_graph(4,3)
    assert_equal(len(G),12)
    assert_equal(len(G.nodes()),12)
    
    G = nx.caveman_graph(1,5)
    K5 = nx.complete_graph(5)
    assert_true(nx.is_isomorphic(G,K5))

def test_gaussian_random_partition_graph():
    G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01)
    assert_equal(len(G),100)
    assert_raises(nx.NetworkXError,
                  nx.gaussian_random_partition_graph, 100, 101, 10, 1, 0)