int main() { igraph_t graph; igraph_vector_t walk, weights; igraph_integer_t ec, i; igraph_rng_seed(igraph_rng_default(), 137); igraph_vector_init(&walk, 0); igraph_vector_init(&weights, 0); /* create a small graph, and a compatible weight vector */ igraph_de_bruijn(&graph, 3, 2); /* 9 vertices, 27 edges, average degree: 6 */ ec = igraph_ecount(&graph); igraph_vector_resize(&weights, ec); for (i=0; i < ec; ++i) VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default()); BENCH(" 1 Random edge walk, directed, unweighted, small graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 2 Random edge walk, directed, weighted, small graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 3 Random vertex walk, directed, unweighted, small graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_EACH, NULL); BENCH(" 4 Random edge walk, undirected, unweighted, small graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 5 Random edge walk, undirected, weighted, small graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 6 Random vertex walk, undirected, unweighted, small graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_destroy(&graph); /* create a big graph, and a compatible weight vector */ igraph_de_bruijn(&graph, 8, 5); /* 32768 vertices, 262144 edges, average degree: 16 */ ec = igraph_ecount(&graph); igraph_vector_resize(&weights, ec); for (i=0; i < ec; ++i) VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default()); BENCH(" 7 Random edge walk, directed, unweighted, large graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 8 Random edge walk, directed, weighted, large graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 9 Random vertex walk, directed, unweighted, large graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_EACH, NULL); BENCH("10 Random edge walk, undirected, unweighted, large graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH("11 Random edge walk, undirected, weighted, large graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH("12 Random vertex walk, undirected, unweighted, large graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_destroy(&graph); igraph_vector_destroy(&weights); igraph_vector_destroy(&walk); return 0; }
VALUE cIGraph_initialize(int argc, VALUE *argv, VALUE self){ igraph_t *graph; igraph_vector_t edge_v; VALUE vertex; VALUE directed; VALUE edges; VALUE attrs; VALUE v_ary; int vertex_n = 0; int current_vertex_id; int i; igraph_vector_ptr_t vertex_attr; igraph_vector_ptr_t edge_attr; igraph_i_attribute_record_t v_attr_rec; v_attr_rec.name = "__RUBY__"; v_attr_rec.type = IGRAPH_ATTRIBUTE_PY_OBJECT; v_attr_rec.value = (void*)rb_ary_new(); igraph_i_attribute_record_t e_attr_rec; e_attr_rec.name = "__RUBY__"; e_attr_rec.type = IGRAPH_ATTRIBUTE_PY_OBJECT; e_attr_rec.value = (void*)rb_ary_new(); rb_scan_args(argc,argv,"12", &edges, &directed, &attrs); //Initialize edge vector IGRAPH_FINALLY(igraph_vector_destroy,&edge_v); IGRAPH_FINALLY(igraph_vector_ptr_destroy,&vertex_attr); IGRAPH_FINALLY(igraph_vector_ptr_destroy,&edge_attr); 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_CHECK(igraph_to_undirected(graph,IGRAPH_TO_UNDIRECTED_COLLAPSE)); //Loop through objects in edge Array for (i=0; i<RARRAY_LEN(edges); i++) { vertex = RARRAY_PTR(edges)[i]; if(rb_ary_includes(v_ary,vertex)){ //If @vertices includes this vertex then look up the vertex number current_vertex_id = NUM2INT(rb_funcall(v_ary,rb_intern("index"),1,vertex)); } else { //Otherwise add to the list of vertices rb_ary_push(v_ary,vertex); current_vertex_id = vertex_n; vertex_n++; //Add object to list of vertex attributes rb_ary_push((VALUE)v_attr_rec.value,vertex); } 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_PTR(attrs)[i/2]); } else { rb_ary_push((VALUE)e_attr_rec.value,Qnil); } } } 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_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; }