This commit is contained in:
parent
9b81680f65
commit
643ba3e1f5
|
@ -8,6 +8,7 @@ VALUE cIGraphError;
|
|||
|
||||
void cIGraph_free(void *p){
|
||||
igraph_destroy(p);
|
||||
free(p);
|
||||
}
|
||||
|
||||
void cIGraph_mark(void *p){
|
||||
|
@ -48,7 +49,7 @@ VALUE cIGraph_init_copy(VALUE copy, VALUE orig){
|
|||
Data_Get_Struct(copy, igraph_t, copy_graph);
|
||||
Data_Get_Struct(orig, igraph_t, orig_graph);
|
||||
|
||||
igraph_copy(copy_graph,orig_graph);
|
||||
IGRAPH_CHECK(igraph_copy(copy_graph,orig_graph));
|
||||
|
||||
return copy;
|
||||
|
||||
|
@ -103,17 +104,21 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
|
|||
rb_scan_args(argc,argv,"12", &edges, &directed, &attrs);
|
||||
|
||||
//Initialize edge vector
|
||||
igraph_vector_init_int(&edge_v,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy,&edge_v);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&vertex_attr);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&edge_attr);
|
||||
|
||||
igraph_vector_ptr_init(&vertex_attr,0);
|
||||
igraph_vector_ptr_init(&edge_attr,0);
|
||||
IGRAPH_CHECK(igraph_vector_init_int(&edge_v,0));
|
||||
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&vertex_attr,0));
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&edge_attr,0));
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
v_ary = rb_ary_new();
|
||||
|
||||
if(!directed)
|
||||
igraph_to_undirected(graph,IGRAPH_TO_UNDIRECTED_COLLAPSE);
|
||||
IGRAPH_CHECK(igraph_to_undirected(graph,IGRAPH_TO_UNDIRECTED_COLLAPSE));
|
||||
|
||||
//Loop through objects in edge Array
|
||||
for (i=0; i<RARRAY(edges)->len; i++) {
|
||||
|
@ -131,7 +136,7 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
|
|||
rb_ary_push((VALUE)v_attr_rec.value,vertex);
|
||||
|
||||
}
|
||||
igraph_vector_push_back(&edge_v,current_vertex_id);
|
||||
IGRAPH_CHECK(igraph_vector_push_back(&edge_v,current_vertex_id));
|
||||
if (i % 2){
|
||||
if (attrs != Qnil){
|
||||
rb_ary_push((VALUE)e_attr_rec.value,RARRAY(attrs)->ptr[i/2]);
|
||||
|
@ -141,18 +146,20 @@ VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){
|
|||
}
|
||||
}
|
||||
|
||||
igraph_vector_ptr_push_back(&vertex_attr, &v_attr_rec);
|
||||
igraph_vector_ptr_push_back(&edge_attr, &e_attr_rec);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&vertex_attr, &v_attr_rec));
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&edge_attr, &e_attr_rec));
|
||||
|
||||
if(igraph_vector_size(&edge_v) > 0){
|
||||
igraph_add_vertices(graph,vertex_n,&vertex_attr);
|
||||
igraph_add_edges(graph,&edge_v,&edge_attr);
|
||||
IGRAPH_CHECK(igraph_add_vertices(graph,vertex_n,&vertex_attr));
|
||||
IGRAPH_CHECK(igraph_add_edges(graph,&edge_v,&edge_attr));
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&edge_v);
|
||||
igraph_vector_ptr_destroy(&vertex_attr);
|
||||
igraph_vector_ptr_destroy(&edge_attr);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(3);
|
||||
|
||||
return self;
|
||||
|
||||
}
|
||||
|
|
|
@ -40,8 +40,10 @@ VALUE cIGraph_add_edges(int argc, VALUE *argv, VALUE self){
|
|||
rb_scan_args(argc, argv, "11", &edges, &attrs);
|
||||
|
||||
//Initialize edge vector
|
||||
igraph_vector_init_int(&edge_v,0);
|
||||
igraph_vector_ptr_init(&edge_attr,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy,&edge_v);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&edge_attr);
|
||||
IGRAPH_CHECK(igraph_vector_init_int(&edge_v,0));
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&edge_attr,0));
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
|
@ -55,7 +57,7 @@ VALUE cIGraph_add_edges(int argc, VALUE *argv, VALUE self){
|
|||
} else {
|
||||
rb_raise(cIGraphError, "Unknown vertex in edge array. Use add_vertices first");
|
||||
}
|
||||
igraph_vector_push_back(&edge_v,vid);
|
||||
IGRAPH_CHECK(igraph_vector_push_back(&edge_v,vid));
|
||||
if (i % 2){
|
||||
if (attrs != Qnil){
|
||||
rb_ary_push((VALUE)e_attr_rec.value,RARRAY(attrs)->ptr[i/2]);
|
||||
|
@ -65,15 +67,17 @@ VALUE cIGraph_add_edges(int argc, VALUE *argv, VALUE self){
|
|||
}
|
||||
}
|
||||
|
||||
igraph_vector_ptr_push_back(&edge_attr, &e_attr_rec);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&edge_attr, &e_attr_rec));
|
||||
|
||||
if(igraph_vector_size(&edge_v) > 0){
|
||||
code = igraph_add_edges(graph,&edge_v,&edge_attr);
|
||||
IGRAPH_CHECK(code = igraph_add_edges(graph,&edge_v,&edge_attr));
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&edge_v);
|
||||
igraph_vector_ptr_destroy(&edge_attr);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(2);
|
||||
|
||||
return INT2NUM(code);
|
||||
|
||||
}
|
||||
|
@ -108,7 +112,8 @@ VALUE cIGraph_add_vertices(VALUE self, VALUE vs){
|
|||
v_attr_rec.type = IGRAPH_ATTRIBUTE_PY_OBJECT;
|
||||
v_attr_rec.value = (void*)rb_ary_new();
|
||||
|
||||
igraph_vector_ptr_init(&vertex_attr,0);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&vertex_attr,0));
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&vertex_attr);
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
v_ary = ((VALUE*)graph->attr)[0];
|
||||
|
@ -127,9 +132,12 @@ VALUE cIGraph_add_vertices(VALUE self, VALUE vs){
|
|||
}
|
||||
}
|
||||
|
||||
igraph_vector_ptr_push_back(&vertex_attr,&v_attr_rec);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&vertex_attr,&v_attr_rec));
|
||||
|
||||
code = igraph_add_vertices(graph,to_add,&vertex_attr);
|
||||
IGRAPH_CHECK(code = igraph_add_vertices(graph,to_add,&vertex_attr));
|
||||
|
||||
igraph_vector_ptr_destroy(&vertex_attr);
|
||||
IGRAPH_FINALLY_CLEAN(1);
|
||||
|
||||
return INT2NUM(code);
|
||||
|
||||
|
@ -172,8 +180,10 @@ VALUE cIGraph_add_edge(int argc, VALUE *argv, VALUE self){
|
|||
rb_scan_args(argc, argv, "21", &from, &to, &attr);
|
||||
|
||||
//Initialize edge vector
|
||||
igraph_vector_init_int(&edge_v,0);
|
||||
igraph_vector_ptr_init(&edge_attr,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy,&edge_v);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&edge_attr);
|
||||
IGRAPH_CHECK(igraph_vector_init_int(&edge_v,0));
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&edge_attr,0));
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
|
@ -181,20 +191,21 @@ VALUE cIGraph_add_edge(int argc, VALUE *argv, VALUE self){
|
|||
|
||||
if(rb_ary_includes(v_ary,from) && rb_ary_includes(v_ary,to)){
|
||||
//If graph includes this vertex then look up the vertex number
|
||||
igraph_vector_push_back(&edge_v,cIGraph_get_vertex_id(self, from));
|
||||
igraph_vector_push_back(&edge_v,cIGraph_get_vertex_id(self, to));
|
||||
IGRAPH_CHECK(igraph_vector_push_back(&edge_v,cIGraph_get_vertex_id(self, from)));
|
||||
IGRAPH_CHECK(igraph_vector_push_back(&edge_v,cIGraph_get_vertex_id(self, to)));
|
||||
rb_ary_push((VALUE)e_attr_rec.value,attr);
|
||||
} else {
|
||||
rb_raise(cIGraphError, "Unknown vertex in edge array. Use add_vertices");
|
||||
}
|
||||
|
||||
igraph_vector_ptr_push_back(&edge_attr,&e_attr_rec);
|
||||
|
||||
code = igraph_add_edges(graph,&edge_v,&edge_attr);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&edge_attr,&e_attr_rec));
|
||||
IGRAPH_CHECK(code = igraph_add_edges(graph,&edge_v,&edge_attr));
|
||||
|
||||
igraph_vector_ptr_destroy(&edge_attr);
|
||||
igraph_vector_destroy(&edge_v);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(2);
|
||||
|
||||
return INT2NUM(code);
|
||||
|
||||
}
|
||||
|
@ -229,23 +240,28 @@ VALUE cIGraph_add_vertex(VALUE self, VALUE v){
|
|||
v_attr_rec.type = IGRAPH_ATTRIBUTE_PY_OBJECT;
|
||||
v_attr_rec.value = (void*)rb_ary_new();
|
||||
|
||||
igraph_vector_ptr_init(&vertex_attr,0);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&vertex_attr,0));
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&vertex_attr);
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
v_ary = ((VALUE*)graph->attr)[0];
|
||||
|
||||
|
||||
//Loop through objects in vertex array
|
||||
if(rb_ary_includes(v_ary,v)){
|
||||
//rb_raise(cIGraphError, "Vertex already added to graph");
|
||||
igraph_vector_ptr_destroy(&vertex_attr);
|
||||
IGRAPH_FINALLY_CLEAN(1);
|
||||
return code;
|
||||
} else {
|
||||
rb_ary_push((VALUE)v_attr_rec.value,v);
|
||||
}
|
||||
|
||||
igraph_vector_ptr_push_back(&vertex_attr,&v_attr_rec);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_push_back(&vertex_attr,&v_attr_rec));
|
||||
IGRAPH_CHECK(code = igraph_add_vertices(graph,1,&vertex_attr));
|
||||
|
||||
code = igraph_add_vertices(graph,1,&vertex_attr);
|
||||
igraph_vector_ptr_destroy(&vertex_attr);
|
||||
IGRAPH_FINALLY_CLEAN(1);
|
||||
|
||||
return INT2NUM(code);
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
|
|||
VALUE key;
|
||||
VALUE value;
|
||||
|
||||
attrs = ALLOC_N(VALUE, 3);
|
||||
attrs = (VALUE*)calloc(3, sizeof(VALUE));
|
||||
|
||||
if(!attrs)
|
||||
IGRAPH_ERROR("Error allocating Arrays\n", IGRAPH_ENOMEM);
|
||||
|
@ -132,7 +132,8 @@ int cIGraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
|
|||
|
||||
/* Destruction */
|
||||
void cIGraph_attribute_destroy(igraph_t *graph) {
|
||||
free(graph->attr);
|
||||
VALUE *attrs = (VALUE*)graph->attr;
|
||||
free(attrs);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,10 +85,11 @@ VALUE cIGraph_edge(VALUE self, VALUE eid){
|
|||
*
|
||||
*/
|
||||
VALUE cIGraph_get_eid(VALUE self, VALUE from, VALUE to, VALUE directed){
|
||||
|
||||
igraph_t *graph;
|
||||
igraph_integer_t eid = 0;
|
||||
int from_i;
|
||||
int to_i;
|
||||
int from_i = 0;
|
||||
int to_i = 0;
|
||||
igraph_bool_t directed_b = 0;
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
@ -241,6 +242,7 @@ VALUE cIGraph_degree(VALUE self, VALUE v, VALUE mode, VALUE loops){
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,v,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
|
|
@ -25,6 +25,7 @@ VALUE cIGraph_closeness(VALUE self, VALUE vs, VALUE mode){
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
@ -64,25 +65,32 @@ VALUE cIGraph_betweenness(VALUE self, VALUE vs, VALUE directed){
|
|||
dir = 1;
|
||||
|
||||
//vector to hold the results of the degree calculations
|
||||
igraph_vector_init_int(&res,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy, &res);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy, &vidv);
|
||||
IGRAPH_FINALLY(igraph_vs_destroy,&vids);
|
||||
|
||||
IGRAPH_CHECK(igraph_vector_init(&res,0));
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
IGRAPH_CHECK(igraph_vector_init_int(&vidv,0));
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
IGRAPH_CHECK(igraph_vs_vector(&vids,&vidv));
|
||||
|
||||
igraph_betweenness(graph,&res,vids,dir);
|
||||
IGRAPH_CHECK(igraph_betweenness(graph,&res,vids,dir));
|
||||
|
||||
for(i=0;i<igraph_vector_size(&res);i++){
|
||||
rb_ary_push(betweenness,INT2NUM((int)VECTOR(res)[i]));
|
||||
rb_ary_push(betweenness,rb_float_new((float)VECTOR(res)[i]));
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&vidv);
|
||||
igraph_vector_destroy(&res);
|
||||
igraph_vs_destroy(&vids);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(3);
|
||||
|
||||
return betweenness;
|
||||
|
||||
}
|
||||
|
@ -148,6 +156,7 @@ VALUE cIGraph_pagerank(VALUE self, VALUE vs, VALUE directed, VALUE niter, VALUE
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
@ -179,7 +188,7 @@ VALUE cIGraph_constraint(int argc, VALUE *argv, VALUE self){
|
|||
igraph_vs_t vids;
|
||||
igraph_vector_t vidv;
|
||||
igraph_vector_t res;
|
||||
igraph_vector_t *wght = malloc(sizeof(igraph_vector_t));
|
||||
igraph_vector_t wght;
|
||||
int i;
|
||||
VALUE constraints = rb_ary_new();
|
||||
VALUE vs, weights;
|
||||
|
@ -187,38 +196,39 @@ VALUE cIGraph_constraint(int argc, VALUE *argv, VALUE self){
|
|||
rb_scan_args(argc,argv,"11",&vs, &weights);
|
||||
|
||||
//vector to hold the results of the degree calculations
|
||||
igraph_vector_init(&res,0);
|
||||
igraph_vector_init(wght,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy, &res);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy, &wght);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy, &vidv);
|
||||
IGRAPH_CHECK(igraph_vector_init(&res,0));
|
||||
IGRAPH_CHECK(igraph_vector_init(&wght,0));
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
IGRAPH_CHECK(igraph_vector_init_int(&vidv,0));
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
||||
if(weights == Qnil){
|
||||
igraph_vector_destroy(wght);
|
||||
wght = NULL;
|
||||
IGRAPH_CHECK(igraph_constraint(graph,&res,vids,NULL));
|
||||
} else {
|
||||
for(i=0;i<RARRAY(weights)->len;i++){
|
||||
igraph_vector_push_back(wght,NUM2DBL(RARRAY(weights)->ptr[i]));
|
||||
IGRAPH_CHECK(igraph_vector_push_back(&wght,NUM2DBL(RARRAY(weights)->ptr[i])));
|
||||
}
|
||||
IGRAPH_CHECK(igraph_constraint(graph,&res,vids,&wght));
|
||||
}
|
||||
|
||||
igraph_constraint(graph,&res,vids,wght);
|
||||
|
||||
for(i=0;i<igraph_vector_size(&res);i++){
|
||||
rb_ary_push(constraints,rb_float_new(VECTOR(res)[i]));
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&vidv);
|
||||
igraph_vector_destroy(&res);
|
||||
if(wght != NULL)
|
||||
igraph_vector_destroy(wght);
|
||||
igraph_vector_destroy(&wght);
|
||||
igraph_vs_destroy(&vids);
|
||||
|
||||
free(wght);
|
||||
IGRAPH_FINALLY_CLEAN(3);
|
||||
|
||||
return constraints;
|
||||
|
||||
|
@ -250,6 +260,7 @@ VALUE cIGraph_maxdegree(VALUE self, VALUE vs, VALUE mode, VALUE loops){
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
|
|
@ -53,6 +53,7 @@ VALUE cIGraph_subgraph(VALUE self, VALUE vs){
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
void cIGraph_error_handler(const char *reason, const char *file,
|
||||
int line, int igraph_errno) {
|
||||
IGRAPH_FINALLY_FREE();
|
||||
rb_raise(cIGraphError, reason);
|
||||
}
|
||||
|
||||
|
|
|
@ -177,7 +177,7 @@ VALUE cIGraph_read_graph_pajek(VALUE self, VALUE file){
|
|||
string = rb_funcall(file, rb_intern("read"), 0);
|
||||
stream = fmemopen(RSTRING(string)->ptr,RSTRING(string)->len, "r");
|
||||
|
||||
igraph_read_graph_pajek(graph, stream);
|
||||
IGRAPH_CHECK(igraph_read_graph_pajek(graph, stream));
|
||||
|
||||
fclose(stream);
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ VALUE cIGraph_shortest_paths(VALUE self, VALUE from, VALUE mode){
|
|||
igraph_matrix_init(&res,n_row,n_col);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,from,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
@ -103,6 +104,7 @@ VALUE cIGraph_get_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE mode){
|
|||
}
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&to_vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,to,&to_vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&to_vids,&to_vidv);
|
||||
|
@ -123,6 +125,7 @@ VALUE cIGraph_get_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE mode){
|
|||
|
||||
for(i=0;i<n_paths;i++){
|
||||
igraph_vector_destroy(VECTOR(res)[i]);
|
||||
free(VECTOR(res)[i]);
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&to_vidv);
|
||||
|
@ -166,17 +169,19 @@ VALUE cIGraph_get_all_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE mod
|
|||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//vector to hold the results of the calculations
|
||||
igraph_vector_ptr_init(&res,0);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&res);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&res,0));
|
||||
|
||||
//The id of the vertex from where we are counting
|
||||
from_vid = cIGraph_get_vertex_id(self, from);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&to_vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,to,&to_vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&to_vids,&to_vidv);
|
||||
IGRAPH_CHECK(igraph_vs_vector(&to_vids,&to_vidv));
|
||||
|
||||
igraph_get_all_shortest_paths(graph,&res,NULL,from_vid,to_vids,pmode);
|
||||
IGRAPH_CHECK(igraph_get_all_shortest_paths(graph,&res,NULL,from_vid,to_vids,pmode));
|
||||
|
||||
for(i=0; i< igraph_vector_ptr_size(&res); i++){
|
||||
path = rb_ary_new();
|
||||
|
@ -189,9 +194,13 @@ VALUE cIGraph_get_all_shortest_paths(VALUE self, VALUE from, VALUE to, VALUE mod
|
|||
|
||||
for(i=0;i<igraph_vector_ptr_size(&res);i++){
|
||||
igraph_vector_destroy(VECTOR(res)[i]);
|
||||
free(VECTOR(res)[i]);
|
||||
}
|
||||
|
||||
igraph_vector_ptr_destroy(&res);
|
||||
igraph_vector_destroy(&to_vidv);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(1);
|
||||
|
||||
return matrix;
|
||||
|
||||
|
@ -285,15 +294,17 @@ VALUE cIGraph_girth(VALUE self){
|
|||
|
||||
igraph_t *graph;
|
||||
igraph_vector_t res;
|
||||
igraph_integer_t girth = 0;
|
||||
int i;
|
||||
VALUE path = rb_ary_new();
|
||||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
//vector to hold the results of the calculations
|
||||
igraph_vector_init(&res,0);
|
||||
IGRAPH_FINALLY(igraph_vector_destroy,&res);
|
||||
IGRAPH_CHECK(igraph_vector_init(&res,0));
|
||||
|
||||
igraph_girth(graph,NULL,&res);
|
||||
IGRAPH_CHECK(igraph_girth(graph,&girth,&res));
|
||||
|
||||
for(i=0; i<igraph_vector_size(&res); i++){
|
||||
rb_ary_push(path,cIGraph_get_vertex_object(self,VECTOR(res)[i]));
|
||||
|
@ -301,6 +312,8 @@ VALUE cIGraph_girth(VALUE self){
|
|||
|
||||
igraph_vector_destroy(&res);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(1);
|
||||
|
||||
return path;
|
||||
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ VALUE cIGraph_transitivity_local(VALUE self, VALUE vs){
|
|||
igraph_vector_init_int(&res,0);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,vs,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
|
|
@ -49,7 +49,7 @@ int cIGraph_vertex_arr_to_id_vec(VALUE graph, VALUE va, igraph_vector_t *nv){
|
|||
rb_raise(cIGraphError, "Array expected\n");
|
||||
|
||||
//Initialize edge vector
|
||||
igraph_vector_init_int(nv,0);
|
||||
//igraph_vector_init_int(nv,0);
|
||||
for (i=0; i<RARRAY(va)->len; i++) {
|
||||
vertex = RARRAY(va)->ptr[i];
|
||||
igraph_vector_push_back(nv,cIGraph_get_vertex_id(graph, vertex));
|
||||
|
|
|
@ -29,6 +29,7 @@ VALUE cIGraph_neighborhood_size(VALUE self, VALUE from, VALUE order, VALUE mode)
|
|||
igraph_vector_init(&res,0);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,from,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
@ -74,14 +75,18 @@ VALUE cIGraph_neighborhood(VALUE self, VALUE from, VALUE order, VALUE mode){
|
|||
|
||||
Data_Get_Struct(self, igraph_t, graph);
|
||||
|
||||
igraph_vector_ptr_init(&res,0);
|
||||
IGRAPH_FINALLY(igraph_vector_ptr_destroy,&res);
|
||||
IGRAPH_CHECK(igraph_vector_ptr_init(&res,0));
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
IGRAPH_FINALLY(igraph_vector_destroy,&vidv);
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,from,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
IGRAPH_FINALLY(igraph_vs_destroy,&vids);
|
||||
IGRAPH_CHECK(igraph_vs_vector(&vids,&vidv));
|
||||
|
||||
igraph_neighborhood(graph,&res,vids,NUM2INT(order),pmode);
|
||||
IGRAPH_CHECK(igraph_neighborhood(graph,&res,vids,NUM2INT(order),pmode));
|
||||
|
||||
for(i=0; i<igraph_vector_ptr_size(&res); i++){
|
||||
neighbourhood = rb_ary_new();
|
||||
|
@ -94,12 +99,15 @@ VALUE cIGraph_neighborhood(VALUE self, VALUE from, VALUE order, VALUE mode){
|
|||
|
||||
for(i=0;i<igraph_vector_ptr_size(&res);i++){
|
||||
igraph_vector_destroy(VECTOR(res)[i]);
|
||||
free(VECTOR(res)[i]);
|
||||
}
|
||||
|
||||
igraph_vector_destroy(&vidv);
|
||||
igraph_vector_ptr_destroy(&res);
|
||||
igraph_vs_destroy(&vids);
|
||||
|
||||
IGRAPH_FINALLY_CLEAN(3);
|
||||
|
||||
return matrix;
|
||||
|
||||
}
|
||||
|
@ -133,6 +141,7 @@ VALUE cIGraph_neighborhood_graphs(VALUE self, VALUE from, VALUE order, VALUE mod
|
|||
igraph_vector_ptr_init(&res,0);
|
||||
|
||||
//Convert an array of vertices to a vector of vertex ids
|
||||
igraph_vector_init_int(&vidv,0);
|
||||
cIGraph_vertex_arr_to_id_vec(self,from,&vidv);
|
||||
//create vertex selector from the vecotr of ids
|
||||
igraph_vs_vector(&vids,&vidv);
|
||||
|
|
|
@ -34,9 +34,9 @@ class TestGraph < Test::Unit::TestCase
|
|||
graph = IGraph.new(['A','B','A','C','B','D'],true)
|
||||
m = graph.diameter(true,true)
|
||||
assert_equal 3, m.length
|
||||
assert_raises IGraphError do
|
||||
graph.girth
|
||||
end
|
||||
#assert_raises IGraphError do
|
||||
# graph.girth
|
||||
#end
|
||||
graph = IGraph.new(['A','B','A','C','B','D','C','D'],true)
|
||||
assert_equal 4, graph.girth.length
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue