igraph 0.3.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'igraph'
3
+
4
+ class TestGraph < Test::Unit::TestCase
5
+ def test_modularity
6
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'])
7
+ assert_in_delta 0.357, g.modularity([['A','B','C'],['D','E','F']]), 0.001
8
+ end
9
+ def test_spinglass
10
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'])
11
+ groups,mod,temp = g.community_spinglass([],25,false,1,0.01,0.99,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0)
12
+ assert_in_delta 0.25, mod, 0.15
13
+ assert_in_delta 0.200, temp, 0.100
14
+ assert_equal [['A','B','C','D','E','F']], groups
15
+ commun,coh,adh = g.community_spinglass_single([],'A',25,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0)
16
+ assert_in_delta 1.25, coh, 0.001
17
+ assert_in_delta(-2.5, adh, 0.100)
18
+ assert_equal ['A','B','C'], commun
19
+ end
20
+ def test_eigen
21
+
22
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false)
23
+ groups,merges = g.community_leading_eigenvector(6)
24
+ assert_equal [['A','B','C'],['D','E','F']], groups
25
+ assert_equal [[0,1]], merges.to_a
26
+ groups,merges = g.community_leading_eigenvector_naive(6)
27
+ assert_equal [['A','B','C'],['D','E','F']], groups
28
+ assert_equal [[0,1]], merges.to_a
29
+
30
+ groups,split,eigenvec,eigenval =
31
+ g.community_leading_eigenvector_step([['A','B','C','D','E','F']],0)
32
+ assert_equal [['A','B','C'],['D','E','F']], groups
33
+ assert split
34
+ assert_in_delta 0.433, eigenvec[0], 0.001
35
+ assert_in_delta 2.100, eigenval, 0.001
36
+
37
+ end
38
+
39
+ def test_random_walk
40
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false)
41
+ merges,modularity = g.community_walktrap([],10)
42
+ groups = g.community_to_membership(merges,4)
43
+ assert_equal [['A','B','C'],['D','E','F']], groups.sort
44
+ assert_in_delta 0.19, modularity[3], 0.1
45
+ end
46
+
47
+ def test_comm_edge_betweenness
48
+
49
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false)
50
+ merges,result,edge_betw,bridges = g.community_edge_betweenness(false)
51
+ groups = g.community_to_membership(merges,4)
52
+ assert_equal [['A','B','C'],['D','E','F']], groups.sort
53
+ assert_equal 3, result[0]
54
+ assert_equal 9, edge_betw[0]
55
+ assert_equal 7, bridges[0]
56
+
57
+ merges,bridges = g.community_eb_get_merges(result)
58
+ groups = g.community_to_membership(merges,4)
59
+ assert_equal [['A','B','C'],['D','E','F']], groups.sort
60
+ assert_equal 7, bridges[0]
61
+
62
+ end
63
+
64
+ def test_fastgreedy
65
+ g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false)
66
+ merges,mod = g.community_fastgreedy
67
+ groups = g.community_to_membership(merges,4)
68
+ assert_equal [['A','B','C'],['D','E','F']], groups.sort
69
+ assert_in_delta 0.19, mod[3], 0.1
70
+ end
71
+
72
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'igraph'
3
+
4
+ class TestGraph < Test::Unit::TestCase
5
+ def test_connecitivity
6
+ g = IGraph.new(['A','B','B','C','C','D'],true)
7
+ assert_equal 1, g.st_edge_connectivity('A','B')
8
+ assert_equal 0, g.edge_connectivity
9
+ assert_equal 1, g.st_vertex_connectivity('A','C',IGraph::VCONN_NEI_ERROR)
10
+ assert_equal 0, g.vertex_connectivity
11
+ end
12
+ def test_disjoint
13
+ g = IGraph.new(['A','B','B','C','C','D','A','E','E','D'],true)
14
+ assert_equal 2, g.edge_disjoint_paths('A','D')
15
+ assert_equal 2, g.vertex_disjoint_paths('A','D')
16
+ end
17
+ def test_adhesion
18
+ g = IGraph.new(['A','B','B','C','C','D'],true)
19
+ assert_equal 0, g.adhesion
20
+ assert_equal 0, g.cohesion
21
+ end
22
+ end
@@ -6,7 +6,7 @@ class TestGraph < Test::Unit::TestCase
6
6
  def test_edgelist_read
7
7
  g = nil
8
8
  assert_nothing_raised{
9
- g = IGraph.read_graph_edgelist(StringIO.new("0 1 2 3"),true)
9
+ g = IGraph::FileRead.read_graph_edgelist(StringIO.new("0 1 2 3"),true)
10
10
  }
11
11
  assert_instance_of IGraph, g
12
12
  assert_equal 4, g.vcount
@@ -21,9 +21,96 @@ class TestGraph < Test::Unit::TestCase
21
21
  assert_equal "0 1\n2 3\n", s.read
22
22
  end
23
23
 
24
+ def test_ncol_read
25
+ g = nil
26
+ assert_nothing_raised{
27
+ g = IGraph::FileRead.read_graph_ncol(StringIO.new("0 1\n2 3\n"),[],
28
+ false,false,false)
29
+ }
30
+ assert_instance_of IGraph, g
31
+ assert_equal 4, g.vcount
32
+ assert g.are_connected?(0,1)
33
+
34
+ assert_nothing_raised{
35
+ g = IGraph::FileRead.read_graph_ncol(StringIO.new("A B\nC D\n"),[],
36
+ true,false,false)
37
+ }
38
+ assert_instance_of IGraph, g
39
+ assert_equal 4, g.vcount
40
+ assert g.are_connected?('A','B')
41
+
42
+ assert_nothing_raised{
43
+ g = IGraph::FileRead.read_graph_ncol(StringIO.new("A B 1\nC D 2\n"),[],
44
+ true,true,false)
45
+ }
46
+ assert_instance_of IGraph, g
47
+ assert_equal 4, g.vcount
48
+ assert g.are_connected?('A','B')
49
+ assert_equal 1, g['A','B']
50
+ end
51
+
52
+ def test_ncol_write
53
+ g = IGraph.new(["A","B","C","D"],true,[1,2])
54
+ s = StringIO.new("")
55
+ str = g.write_graph_ncol(s,true,true)
56
+ s.rewind
57
+ assert_equal "A B 1.0\nC D 2.0\n", s.read
58
+ end
59
+
60
+ def test_lgl_read
61
+ g = nil
62
+ assert_nothing_raised{
63
+ g = IGraph::FileRead.read_graph_lgl(StringIO.new("#A\nB\n#C\nD\n"),
64
+ false,false)
65
+ }
66
+ assert_instance_of IGraph, g
67
+ assert_equal 4, g.vcount
68
+ assert g.are_connected?(0,1)
69
+
70
+ assert_nothing_raised{
71
+ g = IGraph::FileRead.read_graph_lgl(StringIO.new("#A\nB 1\n#C\nD 1\n"),
72
+ true,true)
73
+ }
74
+ assert_instance_of IGraph, g
75
+ assert_equal 4, g.vcount
76
+ assert g.are_connected?('A','B')
77
+ assert_equal 1, g['A','B']
78
+ end
79
+
80
+ def test_lgl_write
81
+ g = IGraph.new(["A","B","C","D"],true,[1,2])
82
+ s = StringIO.new("")
83
+ str = g.write_graph_lgl(s,true,true,false)
84
+ s.rewind
85
+ assert_equal "# A\nB 1.0\n# C\nD 2.0\n", s.read
86
+ end
87
+
88
+ def test_dimacs_read
89
+ g = nil
90
+ assert_nothing_raised{
91
+ s = StringIO.new("c com\np min 4 2\nn 1 s\nn 2 t\na 1 2 1\na 3 4 2\n")
92
+ g = IGraph::FileRead.read_graph_dimacs(s,
93
+ false)
94
+ }
95
+ assert_instance_of IGraph, g
96
+ assert_equal 4, g.vcount
97
+ assert g.are_connected?(0,1)
98
+ assert_equal 0, g.attributes['source']
99
+ assert_equal 1, g.attributes['target']
100
+ assert_equal [1,2], g.attributes['capacity']
101
+ end
102
+
103
+ def test_dimacs_write
104
+ g = IGraph.new(["A","B","C","D"],true,[1,2])
105
+ s = StringIO.new("")
106
+ str = g.write_graph_dimacs(s,0,1,[1,2])
107
+ s.rewind
108
+ assert_equal "c created by igraph\np max 4 2\nn 1 s\nn 2 t\na 1 2 1\na 3 4 2\n", s.read
109
+ end
110
+
24
111
  def test_graphml_read
25
112
  g = nil
26
- g = IGraph.read_graph_graphml(StringIO.new(Graphml),0)
113
+ g = IGraph::FileRead.read_graph_graphml(StringIO.new(Graphml),0)
27
114
  assert_instance_of IGraph, g
28
115
  assert_equal '2006-11-12', g.attributes['date']
29
116
  h = g.dup
@@ -46,9 +133,31 @@ class TestGraph < Test::Unit::TestCase
46
133
  assert_equal Graphml_out, s.read
47
134
  end
48
135
 
136
+ def test_gml_read
137
+ g = IGraph::FileRead.read_graph_gml(StringIO.new(Gml))
138
+ assert_instance_of IGraph, g
139
+ end
140
+
141
+ def test_gml_write
142
+ g = IGraph.new([{'id'=>0,'name'=>'a','type' => 4.0},
143
+ {'id'=>1,'name'=>'b','type' => 5},
144
+ {'id'=>2,'type' => 6},
145
+ {'id'=>3,'name'=>'d'}],
146
+ true,
147
+ [{'eid'=>'e1'},
148
+ {'eid'=>'e2'}])
149
+ g.attributes['date'] = 'Friday'
150
+ s = StringIO.new("")
151
+ str = g.write_graph_gml(s)
152
+ s.rewind
153
+ s = s.read
154
+ s = s.split(/\n/)[1..-1].join("\n")
155
+ assert_equal Gml_out, s
156
+ end
157
+
49
158
  def test_pajek_read_write
50
159
  g = nil
51
- g = IGraph.read_graph_pajek(StringIO.new(Pajek),0)
160
+ g = IGraph::FileRead.read_graph_pajek(StringIO.new(Pajek),0)
52
161
  assert_instance_of IGraph, g
53
162
  assert_equal 4, g.vcount
54
163
  assert_equal 1, g[4,1]['weight']
@@ -158,4 +267,85 @@ awing.org/xmlns/1.0/graphml.xsd">
158
267
  2 3 2
159
268
  }
160
269
 
270
+ Gml = %q{graph [
271
+ comment "This is a sample graph"
272
+ directed 1
273
+ id 42
274
+ label "Hello, I am a graph"
275
+ node [
276
+ id 1
277
+ label "Node 1"
278
+ thisIsASampleAttribute 42
279
+ ]
280
+ node [
281
+ id 2
282
+ label "node 2"
283
+ thisIsASampleAttribute 43
284
+ ]
285
+ node [
286
+ id 3
287
+ label "node 3"
288
+ thisIsASampleAttribute 44
289
+ ]
290
+ edge [
291
+ source 1
292
+ target 2
293
+ label "Edge from node 1 to node 2"
294
+ ]
295
+ edge [
296
+ source 2
297
+ target 3
298
+ label "Edge from node 2 to node 3"
299
+ ]
300
+ edge [
301
+ source 3
302
+ target 1
303
+ label "Edge from node 3 to node 1"
304
+ ]
305
+ ]
306
+ }
307
+
308
+ Gml_out = %q{Version 1
309
+ graph
310
+ [
311
+ directed 0
312
+ date "Friday"
313
+ node
314
+ [
315
+ id 0
316
+ name "a"
317
+ type 4
318
+ ]
319
+ node
320
+ [
321
+ id 1
322
+ name "b"
323
+ type 5
324
+ ]
325
+ node
326
+ [
327
+ id 2
328
+ name ""
329
+ type 6
330
+ ]
331
+ node
332
+ [
333
+ id 3
334
+ name "d"
335
+ type nan
336
+ ]
337
+ edge
338
+ [
339
+ source 0
340
+ target 1
341
+ eid "e1"
342
+ ]
343
+ edge
344
+ [
345
+ source 2
346
+ target 3
347
+ eid "e2"
348
+ ]
349
+ ]}
350
+
161
351
  end
@@ -0,0 +1,61 @@
1
+ require 'test/unit'
2
+ require 'igraph'
3
+
4
+ class TestGraph < Test::Unit::TestCase
5
+ def test_adjacency
6
+ m = IGraphMatrix.new([0,1,1,0],[1,0,0,0],[1,0,0,1],[0,0,1,0])
7
+
8
+ g = IGraph::Generate.adjacency(m,IGraph::ADJ_MAX)
9
+ assert_equal 4, g.vcount
10
+ assert_equal 3, g.ecount
11
+ end
12
+
13
+ def test_star
14
+ g = IGraph::Generate.star(10,IGraph::STAR_UNDIRECTED,0)
15
+ assert_equal 10, g.vcount
16
+ assert_equal 9, g.ecount
17
+ end
18
+
19
+ def test_lattice
20
+ g = IGraph::Generate.lattice([2,2],false,false,false)
21
+ assert_equal 4, g.vcount
22
+ assert_equal 4, g.ecount
23
+ end
24
+
25
+ def test_ring
26
+ g = IGraph::Generate.ring(10,false,false,false)
27
+ assert_equal 10, g.vcount
28
+ assert_equal 9, g.ecount
29
+ end
30
+
31
+ def test_tree
32
+ g = IGraph::Generate.tree(13,3,IGraph::TREE_UNDIRECTED)
33
+ assert_equal 13, g.vcount
34
+ assert_equal 12, g.ecount
35
+ end
36
+
37
+ def test_full
38
+ g = IGraph::Generate.full(10,false,false)
39
+ assert_equal 10, g.vcount
40
+ assert_equal 45, g.ecount
41
+ end
42
+
43
+ def test_atlas
44
+ g = IGraph::Generate.atlas(10)
45
+ assert_equal 4, g.vcount
46
+ assert_equal 2, g.ecount
47
+ end
48
+
49
+ def test_extended_chordal_ring
50
+ g = IGraph::Generate.extended_chordal_ring(3,IGraphMatrix.new([1,2,3],[1,2,3],[1,2,3]))
51
+ assert_equal 3, g.vcount
52
+ assert_equal 6, g.ecount
53
+ end
54
+
55
+ def test_connect_neighborhood
56
+ g = IGraph.new([1,2,1,3,3,4],false)
57
+ g.connect_neighborhood(2,IGraph::ALL)
58
+ assert g.are_connected?(2,3)
59
+ end
60
+
61
+ end
@@ -3,24 +3,98 @@ require 'igraph'
3
3
 
4
4
  class TestGraph < Test::Unit::TestCase
5
5
  def test_grg
6
- g = IGraph.grg_game(10,0.1,false)
6
+ g = IGraph::GenerateRandom.grg_game(10,0.1,false)
7
7
  assert_equal 10, g.vertices.size
8
8
  end
9
9
  def test_barabasi
10
- g = IGraph.barabasi_game(10,3,false,false)
10
+ g = IGraph::GenerateRandom.barabasi_game(10,3,false,false)
11
11
  assert_equal 10, g.vertices.size
12
12
  end
13
13
  def test_nonlinear_barabasi
14
- g = IGraph.nonlinear_barabasi_game(10,1.9,3,false,0.1,false)
14
+ g = IGraph::GenerateRandom.nonlinear_barabasi_game(10,1.9,3,false,0.1,false)
15
15
  assert_equal 10, g.vertices.size
16
16
  end
17
17
  def test_erdos_renyi
18
- g = IGraph.erdos_renyi_game(IGraph::ERDOS_RENYI_GNP,10,0.5,false,false)
19
- assert_equal 10, g.vertices.size
20
- g = IGraph.erdos_renyi_game(IGraph::ERDOS_RENYI_GNM,10,0.5,false,false)
21
- assert_equal 10, g.vertices.size
18
+ g = IGraph::GenerateRandom.erdos_renyi_game(IGraph::ERDOS_RENYI_GNP,10,0.5,false,false)
19
+ assert_instance_of IGraph, g
20
+ g = IGraph::GenerateRandom.erdos_renyi_game(IGraph::ERDOS_RENYI_GNM,10,0.5,false,false)
21
+ assert_instance_of IGraph, g
22
22
  end
23
23
  def test_watts_strogatz
24
-
24
+ g = IGraph::GenerateRandom.watts_strogatz_game(10,1,2,0.6)
25
+ assert_instance_of IGraph, g
25
26
  end
27
+ def test_degree_sequence_game
28
+ g = IGraph::GenerateRandom.degree_sequence_game([1,2,3],[1,2,3])
29
+ assert_instance_of IGraph, g
30
+ end
31
+ def test_growing_random_game
32
+ assert_instance_of IGraph, IGraph::GenerateRandom.growing_random_game(10,2,true,true)
33
+ end
34
+ def test_callaway_traits_game
35
+ assert_instance_of IGraph,
36
+ IGraph::GenerateRandom.callaway_traits_game(30,4,2,[0.25,0.25,0.25,0.25],
37
+ IGraphMatrix.new([0,0.5,0.25,0.25],
38
+ [0.5,0,0.25,0.25],
39
+ [0.5,0.25,0,0.25],
40
+ [0.5,0.25,0.25,0]),true)
41
+ end
42
+ def test_establishment_game
43
+ assert_instance_of IGraph,
44
+ IGraph::GenerateRandom.establishment_game(30,4,2,[0.25,0.25,0.25,0.25],
45
+ IGraphMatrix.new([0,0.5,0.25,0.25],
46
+ [0.5,0,0.25,0.25],
47
+ [0.5,0.25,0,0.25],
48
+ [0.5,0.25,0.25,0]),true)
49
+ end
50
+ def test_preference_game
51
+ assert_instance_of IGraph,
52
+ IGraph::GenerateRandom.preference_game(30,4,[0.25,0.25,0.25,0.25],
53
+ IGraphMatrix.new([0,0.5,0.25,0.25],
54
+ [0.5,0,0.25,0.25],
55
+ [0.5,0.25,0,0.25],
56
+ [0.5,0.25,0.25,0]),true,false)
57
+ end
58
+ def test_asymmetric_preference_game
59
+ assert_instance_of IGraph,
60
+ IGraph::GenerateRandom.asymmetric_preference_game(30,4,
61
+ IGraphMatrix.new([0,0.5,0.25,0.25],
62
+ [0.5,0,0.25,0.25],
63
+ [0.5,0.25,0,0.25],
64
+ [0.5,0.25,0.25,0]),
65
+ IGraphMatrix.new([0,0.5,0.25,0.25],
66
+ [0.5,0,0.25,0.25],
67
+ [0.5,0.25,0,0.25],
68
+ [0.5,0.25,0.25,0]),
69
+ false)
70
+ end
71
+ def test_recent_degree_game
72
+ assert_instance_of IGraph,
73
+ IGraph::GenerateRandom.recent_degree_game(30,2,4,5,false,0.1,true)
74
+ end
75
+ def test_barabasi_aging_game
76
+ assert_instance_of IGraph,
77
+ IGraph::GenerateRandom.barabasi_aging_game(30,2,true,0.9,-0.5,3,0.1,0.1,2,2,true)
78
+ end
79
+ def test_recent_degree_aging_game
80
+ assert_instance_of IGraph,
81
+ IGraph::GenerateRandom.recent_degree_aging_game(30,2,true,0.9,-0.5,3,4,0.1,true)
82
+ end
83
+ def test_cited_type_game
84
+ assert_instance_of IGraph,
85
+ IGraph::GenerateRandom.cited_type_game(10,(0..9).to_a,
86
+ Array.new(5,0.5)+Array.new(5,0.2),
87
+ 2,true)
88
+ end
89
+ def test_citing_cited_type_Game
90
+ # assert_instance_of IGraph,
91
+ # IGraph::GenerateRandom.citing_cited_type_game(4,(0..3).to_a,
92
+ # IGraphMatrix.new([0,0.5,0.25,0.25],
93
+ # [0.5,0,0.25,0.25],
94
+ # [0.5,0.25,0,0.25],
95
+ # [0.5,0.25,0.25,0]),
96
+ # 1,
97
+ # true)
98
+ end
99
+
26
100
  end