/* call-seq: * graph.closeness(vs,mode) -> Array * * Returns an Array of closeness centrality measures for the vertices given in * the vs Array. mode defines the type of shortest paths used for the * calculation */ VALUE cIGraph_closeness(VALUE self, VALUE vs, VALUE mode){ igraph_t *graph; igraph_vs_t vids; igraph_vector_t vidv; igraph_neimode_t pmode = NUM2INT(mode); igraph_vector_t res; int i; VALUE closeness = rb_ary_new(); //vector to hold the results of the degree calculations igraph_vector_init_int(&res,0); 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); igraph_closeness(graph,&res,vids,pmode); for(i=0;i<igraph_vector_size(&res);i++){ rb_ary_push(closeness,rb_float_new(VECTOR(res)[i])); } igraph_vector_destroy(&vidv); igraph_vector_destroy(&res); igraph_vs_destroy(&vids); return closeness; }
std::vector<double> Graph::closeness() const { igraph_vector_t results; igraph_vs_t allnods; std::vector<double> clos; igraph_vector_init(&results, size); igraph_vector_null(&results); igraph_vs_all(&allnods); igraph_closeness(graph, &results, allnods, IGRAPH_IN); clos.insert(clos.begin(), VECTOR(results), VECTOR(results) + size); igraph_vector_destroy(&results); igraph_vs_destroy(&allnods); return clos; }