igraph 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,11 @@
1
+ = 0.3.2 2007-09-03
2
+
3
+ * All 'structural property' functions now complete.
4
+
5
+ = 0.3.1 2007-08-31
6
+
7
+ * New functions added
8
+
1
9
  = 0.3 2007-08-16
2
10
 
3
11
  * Over-hauled attribute code
@@ -19,6 +19,7 @@ ext/cIGraph_kcores.c
19
19
  ext/cIGraph_layout.c
20
20
  ext/cIGraph_matrix.c
21
21
  ext/cIGraph_operators.c
22
+ ext/cIGraph_other_ops.c
22
23
  ext/cIGraph_selectors.c
23
24
  ext/cIGraph_shortest_paths.c
24
25
  ext/cIGraph_spanning.c
@@ -43,6 +44,7 @@ test/tc_file_read_write.rb
43
44
  test/tc_iterators.rb
44
45
  test/tc_layout.rb
45
46
  test/tc_matrix.rb
47
+ test/tc_other_ops.rb
46
48
  test/tc_selectors.rb
47
49
  test/tc_shortest_paths.rb
48
50
  test/tc_spanning.rb
@@ -184,7 +184,7 @@ void Init_igraph(){
184
184
 
185
185
  rb_include_module(cIGraph, rb_mEnumerable);
186
186
 
187
- rb_define_const(cIGraph, "VERSION", rb_str_new2("0.3.1"));
187
+ rb_define_const(cIGraph, "VERSION", rb_str_new2("0.3.2"));
188
188
 
189
189
  rb_define_const(cIGraph, "EDGEORDER_ID", INT2NUM(1));
190
190
  rb_define_const(cIGraph, "EDGEORDER_FROM", INT2NUM(2));
@@ -203,6 +203,10 @@ void Init_igraph(){
203
203
  rb_define_const(cIGraph, "EACH", INT2NUM(0));
204
204
  rb_define_const(cIGraph, "COLLAPSE", INT2NUM(1));
205
205
 
206
+ rb_define_const(cIGraph, "GET_ADJACENCY_UPPER", INT2NUM(0));
207
+ rb_define_const(cIGraph, "GET_ADJACENCY_LOWER", INT2NUM(1));
208
+ rb_define_const(cIGraph, "GET_ADJACENCY_BOTH", INT2NUM(2));
209
+
206
210
  rb_define_method(cIGraph, "[]", cIGraph_get_edge_attr, 2); /* in cIGraph_attribute_handler.c */
207
211
  rb_define_method(cIGraph, "[]=", cIGraph_set_edge_attr, 3); /* in cIGraph_attribute_handler.c */
208
212
  rb_define_alias (cIGraph, "get_edge_attr", "[]");
@@ -285,6 +289,13 @@ void Init_igraph(){
285
289
 
286
290
  rb_define_method(cIGraph, "coreness", cIGraph_coreness, 1); /* in cIGraph_kcores.c */
287
291
 
292
+ rb_define_method(cIGraph, "density", cIGraph_density, 1); /* in cIGraph_other_ops.c */
293
+ rb_define_method(cIGraph, "simplify", cIGraph_simplify, 2); /* in cIGraph_other_ops.c */
294
+ rb_define_method(cIGraph, "reciprocity", cIGraph_reciprocity, 1); /* in cIGraph_other_ops.c */
295
+ rb_define_method(cIGraph, "bibcoupling", cIGraph_bibcoupling, 1); /* in cIGraph_other_ops.c */
296
+ rb_define_method(cIGraph, "cocitation", cIGraph_cocitation, 1); /* in cIGraph_other_ops.c */
297
+ rb_define_method(cIGraph, "get_adjacency", cIGraph_get_adjacency, 1); /* in cIGraph_other_ops.c */
298
+
288
299
  rb_define_method(cIGraph, "topological_sorting", cIGraph_topological_sorting, 1); /* in cIGraph_topological_sort.c */
289
300
 
290
301
  rb_define_singleton_method(cIGraph, "read_graph_edgelist", cIGraph_read_graph_edgelist, 2); /* in cIGraph_file.c */
@@ -122,6 +122,14 @@ VALUE cIGraph_coreness(VALUE self, VALUE mode);
122
122
  //Topological sorting
123
123
  VALUE cIGraph_topological_sorting(VALUE self, VALUE mode);
124
124
 
125
+ //Other operations
126
+ VALUE cIGraph_density (VALUE self, VALUE loops);
127
+ VALUE cIGraph_simplify (VALUE self, VALUE mult, VALUE loops);
128
+ VALUE cIGraph_reciprocity (VALUE self, VALUE loops);
129
+ VALUE cIGraph_bibcoupling (VALUE self, VALUE vs);
130
+ VALUE cIGraph_cocitation (VALUE self, VALUE vs);
131
+ VALUE cIGraph_get_adjacency(VALUE self, VALUE mode);
132
+
125
133
  //File handling
126
134
  VALUE cIGraph_read_graph_edgelist (VALUE self, VALUE file, VALUE mode);
127
135
  VALUE cIGraph_write_graph_edgelist(VALUE self, VALUE file);
@@ -0,0 +1,249 @@
1
+ #include "igraph.h"
2
+ #include "ruby.h"
3
+ #include "cIGraph.h"
4
+
5
+ /* call-seq:
6
+ * graph.density(loops) -> Float
7
+ *
8
+ * Calculate the density of a graph.
9
+ *
10
+ * The density of a graph is simply the ratio number of edges and the number
11
+ * of possible edges. Note that density is ill-defined for graphs with
12
+ * multiple and/or loop edges, so consider calling IGraph#simplify() on the
13
+ * graph if you know that it contains multiple or loop edges.
14
+ *
15
+ */
16
+
17
+ VALUE cIGraph_density(VALUE self, VALUE loops){
18
+
19
+ igraph_t *graph;
20
+ igraph_bool_t l = 0;
21
+ igraph_real_t r;
22
+
23
+ if(loops == Qtrue)
24
+ l = 1;
25
+
26
+ Data_Get_Struct(self, igraph_t, graph);
27
+
28
+ igraph_density(graph,&r,l);
29
+
30
+ return rb_float_new(r);
31
+
32
+ }
33
+
34
+ /* call-seq:
35
+ * graph.simplify(multiple,loops) -> nil
36
+ *
37
+ * Removes loop and/or multiple edges from the graph.
38
+ * multiple: Logical, if true, multiple edges will be removed. loops: Logical,
39
+ * if true, loops (self edges) will be removed.
40
+ *
41
+ */
42
+
43
+ VALUE cIGraph_simplify(VALUE self, VALUE mult, VALUE loops){
44
+
45
+ igraph_t *graph;
46
+ igraph_bool_t l = 0;
47
+ igraph_bool_t m = 0;
48
+
49
+ if(loops == Qtrue)
50
+ l = 1;
51
+ if(mult == Qtrue)
52
+ m = 1;
53
+
54
+ Data_Get_Struct(self, igraph_t, graph);
55
+
56
+ igraph_simplify(graph,m,l);
57
+
58
+ return Qnil;
59
+
60
+ }
61
+
62
+ /* call-seq:
63
+ * graph.reciprocity(loops) -> Float
64
+ *
65
+ * Calculates the reciprocity of a directed graph.
66
+ *
67
+ * A vertex pair (A, B) is said to be reciprocal if there are edges between
68
+ * them in both directions. The reciprocity of a directed graph is the
69
+ * proportion of all possible (A, B) pairs which are reciprocal, provided
70
+ * there is at least one edge between A and B. The reciprocity of an empty
71
+ * graph is undefined (results in an error code). Undirected graphs always
72
+ * have a reciprocity of 1.0 unless they are empty.
73
+ *
74
+ */
75
+
76
+ VALUE cIGraph_reciprocity(VALUE self, VALUE loops){
77
+
78
+ igraph_t *graph;
79
+ igraph_bool_t l = 0;
80
+ igraph_real_t r;
81
+
82
+ if(loops == Qtrue)
83
+ l = 1;
84
+
85
+ Data_Get_Struct(self, igraph_t, graph);
86
+
87
+ igraph_reciprocity(graph,&r,l);
88
+
89
+ return rb_float_new(r);
90
+
91
+ }
92
+
93
+ /* call-seq:
94
+ * graph.bibcoupling(varray) -> Array
95
+ *
96
+ * Bibliographic coupling.
97
+ *
98
+ * The bibliographic coupling of two vertices is the number of other
99
+ * vertices they both cite. The
100
+ * bibliographic coupling score for each given vertex and all other
101
+ * vertices in the graph will be calculated.
102
+ *
103
+ */
104
+ VALUE cIGraph_bibcoupling(VALUE self, VALUE vs){
105
+
106
+ igraph_t *graph;
107
+ igraph_vs_t vids;
108
+ igraph_vector_t vidv;
109
+ igraph_matrix_t res;
110
+ int i;
111
+ int j;
112
+ VALUE row;
113
+ VALUE path_length;
114
+ VALUE matrix = rb_ary_new();
115
+ int n_row;
116
+ int n_col;
117
+
118
+ Data_Get_Struct(self, igraph_t, graph);
119
+
120
+ n_row = NUM2INT(rb_funcall(vs,rb_intern("length"),0));
121
+ n_col = igraph_vcount(graph);
122
+
123
+ //matrix to hold the results of the calculations
124
+ igraph_matrix_init(&res,n_row,n_col);
125
+
126
+ //Convert an array of vertices to a vector of vertex ids
127
+ igraph_vector_init_int(&vidv,0);
128
+ cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
129
+ //create vertex selector from the vecotr of ids
130
+ igraph_vs_vector(&vids,&vidv);
131
+
132
+ igraph_bibcoupling(graph,&res,vids);
133
+
134
+ for(i=0; i<igraph_matrix_nrow(&res); i++){
135
+ row = rb_ary_new();
136
+ rb_ary_push(matrix,row);
137
+ for(j=0; j<igraph_matrix_ncol(&res); j++){
138
+ path_length = INT2NUM(MATRIX(res,i,j));
139
+ rb_ary_push(row,path_length);
140
+ }
141
+ }
142
+
143
+ igraph_vector_destroy(&vidv);
144
+ igraph_matrix_destroy(&res);
145
+ igraph_vs_destroy(&vids);
146
+
147
+ return matrix;
148
+
149
+ }
150
+
151
+ /* call-seq:
152
+ * graph.cocitation(varray) -> Array
153
+ *
154
+ * Cocitation coupling.
155
+ *
156
+ * Two vertices are cocited if there is another vertex citing both of them.
157
+ * igraph_cocitation() simply counts how many types two vertices are cocited.
158
+ * The cocitation score for each given vertex and all other vertices in the
159
+ * graph will be calculated.
160
+ *
161
+ */
162
+ VALUE cIGraph_cocitation(VALUE self, VALUE vs){
163
+
164
+ igraph_t *graph;
165
+ igraph_vs_t vids;
166
+ igraph_vector_t vidv;
167
+ igraph_matrix_t res;
168
+ int i;
169
+ int j;
170
+ VALUE row;
171
+ VALUE path_length;
172
+ VALUE matrix = rb_ary_new();
173
+ int n_row;
174
+ int n_col;
175
+
176
+ Data_Get_Struct(self, igraph_t, graph);
177
+
178
+ n_row = NUM2INT(rb_funcall(vs,rb_intern("length"),0));
179
+ n_col = igraph_vcount(graph);
180
+
181
+ //matrix to hold the results of the calculations
182
+ igraph_matrix_init(&res,n_row,n_col);
183
+
184
+ //Convert an array of vertices to a vector of vertex ids
185
+ igraph_vector_init_int(&vidv,0);
186
+ cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
187
+ //create vertex selector from the vecotr of ids
188
+ igraph_vs_vector(&vids,&vidv);
189
+
190
+ igraph_cocitation(graph,&res,vids);
191
+
192
+ for(i=0; i<igraph_matrix_nrow(&res); i++){
193
+ row = rb_ary_new();
194
+ rb_ary_push(matrix,row);
195
+ for(j=0; j<igraph_matrix_ncol(&res); j++){
196
+ path_length = INT2NUM(MATRIX(res,i,j));
197
+ rb_ary_push(row,path_length);
198
+ }
199
+ }
200
+
201
+ igraph_vector_destroy(&vidv);
202
+ igraph_matrix_destroy(&res);
203
+ igraph_vs_destroy(&vids);
204
+
205
+ return matrix;
206
+
207
+ }
208
+
209
+ /* call-seq:
210
+ * graph.get_adjacency(type) -> Array
211
+ *
212
+ * Returns the adjacency matrix of a graph
213
+ *
214
+ */
215
+ VALUE cIGraph_get_adjacency(VALUE self, VALUE mode){
216
+
217
+ igraph_t *graph;
218
+ igraph_get_adjacency_t pmode = NUM2INT(mode);
219
+ igraph_matrix_t res;
220
+ int i;
221
+ int j;
222
+ VALUE row;
223
+ VALUE path_length;
224
+ VALUE matrix = rb_ary_new();
225
+ int n;
226
+
227
+ Data_Get_Struct(self, igraph_t, graph);
228
+
229
+ n = igraph_vcount(graph);
230
+
231
+ //matrix to hold the results of the calculations
232
+ igraph_matrix_init(&res,n,n);
233
+
234
+ igraph_get_adjacency(graph,&res,pmode);
235
+
236
+ for(i=0; i<igraph_matrix_nrow(&res); i++){
237
+ row = rb_ary_new();
238
+ rb_ary_push(matrix,row);
239
+ for(j=0; j<igraph_matrix_ncol(&res); j++){
240
+ path_length = INT2NUM(MATRIX(res,i,j));
241
+ rb_ary_push(row,path_length);
242
+ }
243
+ }
244
+
245
+ igraph_matrix_destroy(&res);
246
+
247
+ return matrix;
248
+
249
+ }
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'igraph'
3
+
4
+ class TestGraph < Test::Unit::TestCase
5
+ def test_density
6
+ g = IGraph.new(['A','B','C','D'],true)
7
+ assert_equal 0.125, g.density(true)
8
+ end
9
+ def test_simplify
10
+ g = IGraph.new(['A','B','A','B','C','D','A','A'],true)
11
+ assert_equal 4, g.ecount
12
+ g.simplify(true,false)
13
+ assert_equal 3, g.ecount
14
+ g.simplify(true,true)
15
+ assert_equal 2, g.ecount
16
+ end
17
+ def test_reciprocity
18
+ g = IGraph.new(['A','B','C','D','B','A'],true)
19
+ assert_equal 0.5, g.reciprocity(true)
20
+ end
21
+ def test_bibcoupling
22
+ g = IGraph.new(['A','B','C','D','D','B'],true)
23
+ assert_equal [[0,0,0,1]], g.bibcoupling(['A'])
24
+ end
25
+ def test_cocitation
26
+ g = IGraph.new(['A','B','C','D','A','D'],true)
27
+ assert_equal [[0,0,0,1]], g.cocitation(['B'])
28
+ end
29
+ def test_get_adjacency
30
+ g = IGraph.new(['A','B','C','D'],true)
31
+ assert_equal [[0,1,0,0],[0,0,0,0],[0,0,0,1],[0,0,0,0]], g.get_adjacency(1)
32
+ end
33
+ end
34
+
@@ -18,6 +18,7 @@ require 'tc_error_handling'
18
18
  require 'tc_file_read_write'
19
19
  require 'tc_layout'
20
20
  require 'tc_matrix'
21
+ require 'tc_other_ops'
21
22
  require 'tc_shortest_paths'
22
23
  require 'tc_spanning'
23
24
  require 'tc_spectral'
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: igraph
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-08-31 00:00:00 +09:00
6
+ version: 0.3.2
7
+ date: 2007-09-03 00:00:00 +09:00
8
8
  summary: IGraph is a Ruby extension for interfacing with the C igraph library (http://cneurocvs.rmki.kfki.hu/igraph/). igraph is a library for creating and manipulating graphs with a particular emphasis on network analysis functions.
9
9
  require_paths:
10
10
  - test
@@ -50,6 +50,7 @@ files:
50
50
  - ext/cIGraph_layout.c
51
51
  - ext/cIGraph_matrix.c
52
52
  - ext/cIGraph_operators.c
53
+ - ext/cIGraph_other_ops.c
53
54
  - ext/cIGraph_selectors.c
54
55
  - ext/cIGraph_shortest_paths.c
55
56
  - ext/cIGraph_spanning.c
@@ -74,6 +75,7 @@ files:
74
75
  - test/tc_iterators.rb
75
76
  - test/tc_layout.rb
76
77
  - test/tc_matrix.rb
78
+ - test/tc_other_ops.rb
77
79
  - test/tc_selectors.rb
78
80
  - test/tc_shortest_paths.rb
79
81
  - test/tc_spanning.rb