igraph_bool_t check_ev(const igraph_matrix_t *A, 
		       const igraph_vector_t *values_real,
		       const igraph_vector_t *values_imag,
		       const igraph_matrix_t *vectors_left, 
		       const igraph_matrix_t *vectors_right, 
		       igraph_real_t tol) {

  int n=igraph_matrix_nrow(A);
  igraph_vector_t v_real, v_imag;
  igraph_vector_t AV_real, AV_imag, lv_real, lv_imag;
  igraph_vector_t null;
  int i;
  
  if (igraph_matrix_ncol(A)             != n) { return 1; }
  if (igraph_vector_size(values_real)   != n) { return 1; }
  if (igraph_vector_size(values_imag)   != n) { return 1; }
  if (igraph_matrix_nrow(vectors_left)  != n) { return 1; }
  if (igraph_matrix_ncol(vectors_left)  != n) { return 1; }
  if (igraph_matrix_nrow(vectors_right) != n) { return 1; }
  if (igraph_matrix_ncol(vectors_right) != n) { return 1; }

  igraph_vector_init(&AV_real, n);
  igraph_vector_init(&AV_imag, n);
  igraph_vector_init(&lv_real, n);
  igraph_vector_init(&lv_imag, n);
  igraph_vector_init(&null, n);
  igraph_vector_null(&null);

  for (i=0; i<n; i++) {
    if (VECTOR(*values_imag)[i]==0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_view(&v_imag, VECTOR(null), n);
    } else if (VECTOR(*values_imag)[i] > 0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_view(&v_imag, &MATRIX(*vectors_right, 0, i+1), n);
    } else if (VECTOR(*values_imag)[i] < 0.0) {
      igraph_vector_view(&v_real, &MATRIX(*vectors_right, 0, i-1), n);
      igraph_vector_view(&v_imag, &MATRIX(*vectors_right, 0, i), n);
      igraph_vector_scale(&v_imag, -1.0);
    }
    real_cplx_mult(A, &v_real, &v_imag, &AV_real, &AV_imag);
    sc_cplx_cplx_mult(VECTOR(*values_real)[i], VECTOR(*values_imag)[i],
		      &v_real, &v_imag, &lv_real, &lv_imag);
    
    if (igraph_vector_maxdifference(&AV_real, &lv_real) > tol ||
	igraph_vector_maxdifference(&AV_imag, &lv_imag) > tol) {
      igraph_vector_print(&AV_real); igraph_vector_print(&AV_imag);
      igraph_vector_print(&lv_real); igraph_vector_print(&lv_imag);      
      return 1;
    }
  }

  igraph_vector_destroy(&null);
  igraph_vector_destroy(&AV_imag);
  igraph_vector_destroy(&AV_real);
  igraph_vector_destroy(&lv_imag);
  igraph_vector_destroy(&lv_real);
  
  return 0;
}
int sc_cplx_cplx_mult(igraph_real_t lambda_real,
		      igraph_real_t lambda_imag,
		      const igraph_vector_t *v_real,
		      const igraph_vector_t *v_imag,
		      igraph_vector_t *res_real,
		      igraph_vector_t *res_imag) {
  
  int r;
  int n=igraph_vector_size(v_real);
  
  if (igraph_vector_size(v_imag) != n) {
    printf("Wrong vector sizes");
    return 1;
  }
  
  igraph_vector_resize(res_real, n);
  igraph_vector_resize(res_imag, n);

  for (r=0; r<n; r++) {
    VECTOR(*res_real)[r] = (lambda_real * VECTOR(*v_real)[r] -
			    lambda_imag * VECTOR(*v_imag)[r]);
    VECTOR(*res_imag)[r] = (lambda_imag * VECTOR(*v_real)[r] + 
			    lambda_real * VECTOR(*v_imag)[r]);
  }

  return 0;
}
Beispiel #3
0
int igraph_i_maximal_cliques_store_max_size(const igraph_vector_t* clique, void* data,
    igraph_bool_t* cont) {
  igraph_integer_t* result = (igraph_integer_t*)data;
  if (*result < igraph_vector_size(clique))
    *result = igraph_vector_size(clique);
  return IGRAPH_SUCCESS;
}
Beispiel #4
0
int igraph_i_kleinberg2(igraph_real_t *to, const igraph_real_t *from,
			long int n, void *extra) {

  igraph_adjlist_t *in = ((igraph_i_kleinberg_data_t*)extra)->in;
  igraph_adjlist_t *out = ((igraph_i_kleinberg_data_t*)extra)->out;
  igraph_vector_t *tmp = ((igraph_i_kleinberg_data_t*)extra)->tmp;
  igraph_vector_t *neis;
  long int i, j, nlen;
  
  for (i=0; i<n; i++) {
    neis=igraph_adjlist_get(in, i);
    nlen=igraph_vector_size(neis);
    VECTOR(*tmp)[i]=0.0;
    for (j=0; j<nlen; j++) {
      long int nei=VECTOR(*neis)[j];
      VECTOR(*tmp)[i] += from[nei];
    }
  }
  
  for (i=0; i<n; i++) {
    neis=igraph_adjlist_get(out, i);
    nlen=igraph_vector_size(neis);
    to[i]=0.0;
    for (j=0; j<nlen; j++) {
      long int nei=VECTOR(*neis)[j];
      to[i] += VECTOR(*tmp)[nei];
    }
  }      
  
  return 0;
}
Beispiel #5
0
int igraph_i_largest_cliques_store(const igraph_vector_t* clique, void* data, igraph_bool_t* cont) {
  igraph_vector_ptr_t* result = (igraph_vector_ptr_t*)data;
  igraph_vector_t* vec;
  long int i, n;

  /* Is the current clique at least as large as the others that we have found? */
  if (!igraph_vector_ptr_empty(result)) {
    n = igraph_vector_size(clique);
    if (n < igraph_vector_size(VECTOR(*result)[0]))
      return IGRAPH_SUCCESS;

    if (n > igraph_vector_size(VECTOR(*result)[0])) {
      for (i = 0; i < igraph_vector_ptr_size(result); i++)
        igraph_vector_destroy(VECTOR(*result)[i]);
      igraph_vector_ptr_free_all(result);
      igraph_vector_ptr_resize(result, 0);
    }
  }

  vec = igraph_Calloc(1, igraph_vector_t);
  if (vec == 0)
    IGRAPH_ERROR("cannot allocate memory for storing next clique", IGRAPH_ENOMEM);

  IGRAPH_CHECK(igraph_vector_copy(vec, clique));
  IGRAPH_CHECK(igraph_vector_ptr_push_back(result, vec));

  return IGRAPH_SUCCESS;
}
Beispiel #6
0
int igraph_running_mean(const igraph_vector_t *data, igraph_vector_t *res, 
			igraph_integer_t binwidth) {

  double sum=0;
  long int i;

  /* Check */
  if (igraph_vector_size(data) < binwidth) {
    IGRAPH_ERROR("Vector too short for this binwidth", IGRAPH_EINVAL); 
  }

  /* Memory for result */

  IGRAPH_CHECK(igraph_vector_resize(res, (long int)(igraph_vector_size(data)-binwidth+1)));
  
  /* Initial bin */
  for (i=0; i<binwidth; i++) {
    sum += VECTOR(*data)[i];
  }
  
  VECTOR(*res)[0]=sum/binwidth;
  
  for (i=1; i<igraph_vector_size(data)-binwidth+1; i++) {
    IGRAPH_ALLOW_INTERRUPTION();
    sum -= VECTOR(*data)[i-1];
    sum += VECTOR(*data)[ (long int)(i+binwidth-1)];
    VECTOR(*res)[i] = sum/binwidth;
  }
  
  return 0;
}
Beispiel #7
0
uint8_t cliques_overlap_cliques(cliques* c, int i, int j){
  igraph_vector_t *i_clique, *j_clique;
  uint8_t overlap_size = 0;
  int node_it;
  int clique_size;

  i_clique = (igraph_vector_t*)VECTOR(c->plain_cliques_v_ptr)[i];
  j_clique = (igraph_vector_t*)VECTOR(c->plain_cliques_v_ptr)[j];

  if (i == j)
    return igraph_vector_size(i_clique);
  
  if( (igraph_vector_tail(i_clique) >= VECTOR(*j_clique)[0]) && (VECTOR(*i_clique)[0] <= igraph_vector_tail(j_clique))){		
    clique_size = igraph_vector_size(j_clique);
    for (node_it = 0; node_it < clique_size; node_it++) {
      if (VECTOR(*j_clique)[node_it] < VECTOR(*i_clique)[0])
	// if the current node is lower than i_clique minimum node, then continue
	continue;
      if (VECTOR(*j_clique)[node_it] > igraph_vector_tail(i_clique))
	// if the current node is greater than i_clique maximum node, than exit
	break;
      // search element node_it^th of j_clique within i_clique vector
      if (igraph_vector_binsearch2(i_clique, VECTOR(*j_clique)[node_it]))
	overlap_size++;
    }
  }
  return overlap_size;
}
int real_cplx_mult(const igraph_matrix_t *A,
		   const igraph_vector_t *v_real,
		   const igraph_vector_t *v_imag, 
		   igraph_vector_t *res_real,
		   igraph_vector_t *res_imag) {

  int n=igraph_vector_size(v_real);
  int r, c;

  if (igraph_matrix_nrow(A) != n || 
      igraph_matrix_ncol(A) != n || 
      igraph_vector_size(v_imag) != n) {
    printf("Wrong matrix or vector size");
    return 1;
  }

  igraph_vector_resize(res_real, n);
  igraph_vector_resize(res_imag, n);

  for (r=0; r<n; r++) {
    igraph_real_t s_real=0.0;
    igraph_real_t s_imag=0.0;
    for (c=0; c<n; c++) {
      s_real += MATRIX(*A, r, c) * VECTOR(*v_real)[c];
      s_imag += MATRIX(*A, r, c) * VECTOR(*v_imag)[c];
    }
    VECTOR(*res_real)[r]=s_real;
    VECTOR(*res_imag)[r]=s_imag;
  }
  
  return 0;
}
void print_vector(igraph_vector_t *v, FILE *f) {
  long int i;
  for (i=0; i<igraph_vector_size(v)-1; i+=2) {
    fprintf(f, "[\"%li\" , \"%li\" , \"\"] , ", (long int) VECTOR(*v)[i],(long int) VECTOR(*v)[i+1]);
  }
  fprintf(f, "[\"%li\" , \"%li\" , \"\"] ", (long int) VECTOR(*v)[igraph_vector_size(v)-2],(long int) VECTOR(*v)[igraph_vector_size(v)-1]);
  fprintf(f, "\n");
}
int main() {
  
  igraph_t g;
  igraph_vector_t v, weights;
  long int i;
  igraph_real_t value;
  igraph_arpack_options_t options;
  
  igraph_star(&g, 100, IGRAPH_STAR_UNDIRECTED, 0);

  igraph_arpack_options_init(&options);
  igraph_vector_init(&v, 0);
  igraph_eigenvector_centrality(&g, &v, &value, /*directed=*/ 0, 
				/*scale=*/0, /*weights=*/0, 
				&options);

  if (options.info != 0) {
    return 1;
  }

  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.3f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  
  igraph_destroy(&g);

  /* Special cases: check for empty graph */
  igraph_empty(&g, 10, 0);
  igraph_eigenvector_centrality(&g, &v, &value, 0, 0, 0, &options);
  if (value != 0.0) {
    return 1;
  }
  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.2f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  igraph_destroy(&g);

  /* Special cases: check for full graph, zero weights */
  igraph_full(&g, 10, 0, 0);
  igraph_vector_init(&weights, 45);
  igraph_vector_fill(&weights, 0);
  igraph_eigenvector_centrality(&g, &v, &value, 0, 0, &weights, &options);
  igraph_vector_destroy(&weights);
  if (value != 0.0) {
    return 2;
  }
  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.2f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  igraph_destroy(&g);

  igraph_vector_destroy(&v);

  return 0;
}
int sort_cmp(const void *a, const void *b) {
  const igraph_vector_t **da = (const igraph_vector_t **) a;
  const igraph_vector_t **db = (const igraph_vector_t **) b;
  int i, alen=igraph_vector_size(*da), blen=igraph_vector_size(*db);
  if (alen != blen) { return (alen < blen) - (alen > blen); }
  for (i=0; i<alen; i++) {
    int ea=VECTOR(**da)[i], eb=VECTOR(**db)[i];
    if (ea != eb) { return (ea > eb) - (ea < eb); }
  }
  return 0;
}
Beispiel #12
0
VALUE cIGraph_community_edge_betweenness(VALUE self, VALUE directed){

  igraph_t *graph;

  igraph_vector_t result_vec;
  igraph_vector_t edge_betw_vec;
  igraph_vector_t bridges_vec;
  igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t));
  igraph_bool_t   directed_b = 0;

  int i;

  VALUE result_a, edge_betw_a, bridges_a, res;

  if(directed)
    directed_b = 1;

  Data_Get_Struct(self, igraph_t, graph);

  igraph_matrix_init(merges,0,0);
  igraph_vector_init(&result_vec,0);
  igraph_vector_init(&edge_betw_vec,0);
  igraph_vector_init(&bridges_vec,0);

  igraph_community_edge_betweenness(graph,
				    &result_vec,&edge_betw_vec,
				    merges,&bridges_vec,directed_b);

  result_a = rb_ary_new();
  for(i=0;i<igraph_vector_size(&result_vec);i++){
    rb_ary_push(result_a,INT2NUM(VECTOR(result_vec)[i]));
  }
  edge_betw_a = rb_ary_new();
  for(i=0;i<igraph_vector_size(&edge_betw_vec);i++){
    rb_ary_push(edge_betw_a,INT2NUM(VECTOR(edge_betw_vec)[i]));
  }
  bridges_a = rb_ary_new();
  for(i=0;i<igraph_vector_size(&bridges_vec);i++){
    rb_ary_push(bridges_a,INT2NUM(VECTOR(bridges_vec)[i]));
  }

  res = rb_ary_new3(4,
		    Data_Wrap_Struct(cIGraphMatrix, 0, 
				     cIGraph_matrix_free, merges),
		    result_a, edge_betw_a, bridges_a);

  igraph_vector_destroy(&result_vec);
  igraph_vector_destroy(&edge_betw_vec);
  igraph_vector_destroy(&bridges_vec);

  return res;

}
Beispiel #13
0
VALUE cIGraph_community_leading_eigenvector_naive(VALUE self, VALUE steps){

  igraph_t *graph;

  igraph_vector_t membership;
  igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t));

igraph_arpack_options_t arpack_opt;
igraph_arpack_options_init(&arpack_opt);

  int i,groupid,max_groupid;

  VALUE groups, group, res;

  Data_Get_Struct(self, igraph_t, graph);

  igraph_matrix_init(merges,0,0); 
  igraph_vector_init(&membership,0);

  igraph_community_leading_eigenvector_naive(graph,merges,&membership,
					     NUM2INT(steps), &arpack_opt);

  max_groupid = 0;
  for(i=0;i<igraph_vector_size(&membership);i++){
    if(VECTOR(membership)[i] > max_groupid)
      max_groupid = VECTOR(membership)[i];
  }

  groups = rb_ary_new();
  for(i=0;i<max_groupid+1;i++){
    rb_ary_push(groups,rb_ary_new());
  }

  for(i=0;i<igraph_vector_size(&membership);i++){

    groupid = VECTOR(membership)[i];

    if(groupid == -1)
      groupid = 0;

    group = RARRAY_PTR(groups)[groupid];
    rb_ary_push(group,cIGraph_get_vertex_object(self, i));
  }

  res = rb_ary_new3(2,groups,
		    Data_Wrap_Struct(cIGraphMatrix, 0, 
				     cIGraph_matrix_free, merges));

  igraph_vector_destroy(&membership);

  return res;

}
Beispiel #14
0
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);

    /* This directed graph has loop edges.
       It also has multi-edges when considered as undirected. */
    igraph_de_bruijn(&graph, 3, 2);
    ec = igraph_ecount(&graph);

    /* unweighted, directed */
    igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN);
    assert(igraph_vector_size(&walk) == 1000);

    /* unweighted, undirected */
    igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_ALL, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN);
    assert(igraph_vector_size(&walk) == 1000);

    igraph_vector_resize(&weights, ec);
    for (i=0; i < ec; ++i)
        VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default());

    /* weighted, directed */
    igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN);
    assert(igraph_vector_size(&walk) == 1000);

    /* weighted, undirecetd */
    igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_ALL, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN);
    assert(igraph_vector_size(&walk) == 1000);

    igraph_destroy(&graph);

    /* 1-vertex graph, should get stuck */
    igraph_empty(&graph, 1, /* directed = */ 0);
    igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN);
    assert(igraph_vector_size(&walk) == 0);
    igraph_destroy(&graph);

    igraph_vector_destroy(&weights);
    igraph_vector_destroy(&walk);

    return 0;
}
Beispiel #15
0
void vector_print(igraph_vector_t *v) {
  long int i;
  for (i=0; i<igraph_vector_size(v); i++) {
    printf(" %li", (long int) VECTOR(*v)[i]);
  }
  printf("\n");
}
Beispiel #16
0
int igraph_2dgrid_init(igraph_2dgrid_t *grid, igraph_matrix_t *coords,
                       igraph_real_t minx, igraph_real_t maxx, igraph_real_t deltax,
                       igraph_real_t miny, igraph_real_t maxy, igraph_real_t deltay) {
    long int i;

    grid->coords=coords;
    grid->minx=minx;
    grid->maxx=maxx;
    grid->deltax=deltax;
    grid->miny=miny;
    grid->maxy=maxy;
    grid->deltay=deltay;

    grid->stepsx=(long int) ceil((maxx-minx)/deltax);
    grid->stepsy=(long int) ceil((maxy-miny)/deltay);

    IGRAPH_CHECK(igraph_matrix_init(&grid->startidx,
                                    grid->stepsx, grid->stepsy));
    IGRAPH_FINALLY(igraph_matrix_destroy, &grid->startidx);
    IGRAPH_VECTOR_INIT_FINALLY(&grid->next, igraph_matrix_nrow(coords));
    IGRAPH_VECTOR_INIT_FINALLY(&grid->prev, igraph_matrix_nrow(coords));

    for (i=0; i<igraph_vector_size(&grid->next); i++) {
        VECTOR(grid->next)[i]=-1;
    }

    grid->massx=0;
    grid->massy=0;
    grid->vertices=0;

    IGRAPH_FINALLY_CLEAN(3);
    return 0;
}
Beispiel #17
0
static gboolean _tgengraph_hasSelfLoop(TGenGraph* g, igraph_integer_t vertexIndex) {
    TGEN_ASSERT(g);
    gboolean isLoop = FALSE;

    igraph_vector_t* resultNeighborVertices = g_new0(igraph_vector_t, 1);
    gint result = igraph_vector_init(resultNeighborVertices, 0);

    if(result == IGRAPH_SUCCESS) {
        result = igraph_neighbors(g->graph, resultNeighborVertices, vertexIndex, IGRAPH_OUT);
        if(result == IGRAPH_SUCCESS) {
            glong nVertices = igraph_vector_size(resultNeighborVertices);
            for (gint i = 0; i < nVertices; i++) {
                igraph_integer_t dstVertexIndex = igraph_vector_e(resultNeighborVertices, i);
                if(vertexIndex == dstVertexIndex) {
                    isLoop = TRUE;
                    break;
                }
            }
        }
    }

    igraph_vector_destroy(resultNeighborVertices);
    g_free(resultNeighborVertices);
    return isLoop;
}
Beispiel #18
0
/* call-seq:
 *   igraph.motifs_randesu(size,cut)
 *
 */
VALUE cIGraph_motifs_randesu(VALUE self, VALUE size, VALUE cuts){

  igraph_t *graph;
  igraph_vector_t cutsv;
  igraph_vector_t res;
  int i;
  VALUE hist = rb_ary_new();

  Data_Get_Struct(self, igraph_t, graph);

  igraph_vector_init(&res,0);

  //Convert an array of vertices to a vector of vertex ids
  igraph_vector_init(&cutsv,0);
  for(i=0;i<RARRAY_LEN(cuts);i++){
    igraph_vector_push_back(&cutsv,NUM2DBL(RARRAY_PTR(cuts)[i]));
  }

  igraph_motifs_randesu(graph,&res,NUM2INT(size),&cutsv);

  for(i=0; i<igraph_vector_size(&res); i++){
    rb_ary_push(hist,INT2NUM(VECTOR(res)[i]));
  }

  igraph_vector_destroy(&cutsv);
  igraph_vector_destroy(&res);
 
  return hist;

}
Beispiel #19
0
void print_vector(igraph_vector_t *v) {
  long int i, n=igraph_vector_size(v);
  for (i=0; i<n; i++) {
    printf(" %g", VECTOR(*v)[i]);
  }
  printf("\n");
}
int check_ev(const igraph_matrix_t *A, const igraph_vector_t *values,
	     const igraph_matrix_t *vectors) {

  int i, n=igraph_matrix_nrow(A);
  int ne=igraph_matrix_ncol(vectors);
  igraph_vector_t v, lhs, rhs;

  if (ne != igraph_vector_size(values)) {
    printf("'values' and 'vectors' sizes do not match\n");
    exit(1);
  }

  igraph_vector_init(&lhs, n);
  igraph_vector_init(&rhs, n);

  for (i=0; i<ne; i++) {
    igraph_vector_view(&v, &MATRIX(*vectors, 0, i), n);
    igraph_blas_dgemv(/*transpose=*/ 0, /*alpha=*/ 1, A, &v, 
		      /*beta=*/ 0, &lhs);
    igraph_vector_update(&rhs, &v);
    igraph_vector_scale(&rhs, VECTOR(*values)[i]);
    if (igraph_vector_maxdifference(&lhs, &rhs) > 1e-10) { 
      printf("LHS: "); igraph_vector_print(&lhs);
      printf("RHS: "); igraph_vector_print(&rhs);
      exit(2);
    }
  }
  
  igraph_vector_destroy(&rhs);
  igraph_vector_destroy(&lhs);
  
  return 0;
}
Beispiel #21
0
int main(void) {
 igraph_real_t avg_path;
 igraph_t graph;
 igraph_vector_t dimvector;
 igraph_vector_t edges;
 int i;

 igraph_vector_init(&dimvector, 2);
 VECTOR(dimvector)[0]=30;
 VECTOR(dimvector)[1]=30;
 igraph_lattice(&graph, &dimvector, 0, IGRAPH_UNDIRECTED, 0, 1);
 igraph_rng_seed(igraph_rng_default(), 42);
 igraph_vector_init(&edges, 20);
 for (i=0; i<igraph_vector_size(&edges); i++) {
 VECTOR(edges)[i] = rand() % (int)igraph_vcount(&graph);
 }
 igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
 printf("Average path length (lattice): %f\n", (double) avg_path);
 igraph_add_edges(&graph, &edges, 0);
 igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
 printf("Average path length (randomized lattice): %f\n", (double) avg_path);

 igraph_vector_destroy(&dimvector);
 igraph_vector_destroy(&edges);
 igraph_destroy(&graph);
 return 0;
}
/* call-seq:
 *   graph.neighbours(vertex,mode) -> Array
 *
 * Returns an Array of the neighbouring vertices to vertex. mode defines 
 * the way adjacent vertices are searched for directed graphs. It can have 
 * the following values: IGraph::OUT, vertices reachable by an edge from the 
 * specified vertex are searched, IGraph::IN, vertices from which the 
 * specified vertex is reachable are searched. IGraph::ALL, both kind of 
 * vertices are searched. This parameter is ignored for undirected graphs.
 *
 * Example:
 *
 *   g = IGraph.new([1,2,3,4],true)
 *   g.neighbours(1,IGraph::ALL) # returns [2]
 *
 */
VALUE cIGraph_neighbors(VALUE self, VALUE v, VALUE mode){

  igraph_t *graph;
  igraph_integer_t pnode;
  igraph_neimode_t pmode = NUM2INT(mode);
  igraph_vector_t neis;
  int i;
  VALUE neighbors = rb_ary_new();

  igraph_vector_init_int(&neis,0);

  Data_Get_Struct(self, igraph_t, graph);

  pnode = cIGraph_get_vertex_id(self,v);

  igraph_neighbors(graph,&neis,pnode,pmode);

  for(i=0;i<igraph_vector_size(&neis);i++){
    rb_ary_push(neighbors,cIGraph_get_vertex_object(self,VECTOR(neis)[i]));
  }

  igraph_vector_destroy(&neis);

  return neighbors;

}
/* call-seq:
 *   graph.adjacent(vertex,mode) -> Array
 *
 * Returns an Array of the adjacent edge ids to vertex. mode defines 
 * the way adjacent edges are searched for directed graphs. It can have 
 * the following values: IGraph::OUT means only outgoing edges, IGraph::IN 
 * only incoming edges, IGraph::ALL both. This parameter is ignored for 
 * undirected graphs.
 *
 * Example:
 *
 *   g = IGraph.new([1,2,3,4],true)
 *   g.adjacent(1,IGraph::ALL) # returns [1]
 *
 */
VALUE cIGraph_adjacent(VALUE self, VALUE v, VALUE mode){

  igraph_t *graph;
  igraph_integer_t pnode;
  igraph_neimode_t pmode = NUM2INT(mode);
  igraph_vector_t eids;
  int i;
  VALUE eids_r = rb_ary_new();

  igraph_vector_init_int(&eids,0);

  Data_Get_Struct(self, igraph_t, graph);

  pnode = cIGraph_get_vertex_id(self,v);

  igraph_adjacent(graph,&eids,pnode,pmode);

  for(i=0;i<igraph_vector_size(&eids);i++){
    rb_ary_push(eids_r,INT2NUM(VECTOR(eids)[i]));
  }

  igraph_vector_destroy(&eids);

  return eids_r;

}
/* call-seq:
 *   graph.degree(vs,mode,loops) -> Array
 *
 * Returns an Array of Integers specifying the degree of each of the
 * vertices specified in the vs Array. mode defines the type of the degree. 
 * IGraph::OUT, out-degree, IGraph::IN, in-degree, IGraph::ALL, total degree 
 * (sum of the in- and out-degree). This parameter is ignored for undirected 
 * graphs. 
 *
 * Example:
 *
 *   g = IGraph.new([1,2,3,4],true)
 *   g.is_directed? #returns true
 *
 */
VALUE cIGraph_degree(VALUE self, VALUE v, VALUE mode, VALUE loops){
  igraph_t *graph;
  igraph_vs_t vids;
  igraph_vector_t vidv;
  igraph_neimode_t pmode = NUM2INT(mode);
  igraph_bool_t loop_mode = loops ? 1 : 0;
  igraph_vector_t res;
  int i;
  VALUE degree_r = 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,v,&vidv);
  //create vertex selector from the vecotr of ids
  igraph_vs_vector(&vids,&vidv);

  igraph_degree(graph,&res,vids,pmode,loop_mode);

  for(i=0;i<igraph_vector_size(&res);i++){
    rb_ary_push(degree_r,INT2NUM(VECTOR(res)[i]));
  }

  igraph_vector_destroy(&vidv);
  igraph_vector_destroy(&res);
  igraph_vs_destroy(&vids);

  return degree_r;

}
Beispiel #25
0
VALUE cIGraph_community_fastgreedy(VALUE self){

  igraph_t *graph;

  igraph_vector_t modularity;
  igraph_matrix_t *merges = malloc(sizeof(igraph_matrix_t));

  int i;

  VALUE modularity_a, res;

  Data_Get_Struct(self, igraph_t, graph);

  igraph_matrix_init(merges,0,0);
  igraph_vector_init(&modularity,0);

  igraph_community_fastgreedy(graph,NULL,
			      merges,&modularity);

  modularity_a = rb_ary_new();
  for(i=0;i<igraph_vector_size(&modularity);i++){
    rb_ary_push(modularity_a,rb_float_new(VECTOR(modularity)[i]));
  }

  res = rb_ary_new3(2,
		    Data_Wrap_Struct(cIGraphMatrix, 0, 
				     cIGraph_matrix_free, merges),
		    modularity_a);

  igraph_vector_destroy(&modularity);

  return res;

}
/* call-seq:
 *   graph.transitivity() -> Float
 *
 * Calculates the transitivity (clustering coefficient) of a graph.
 *
 * The transitivity measures the probability that two neighbors of a vertex 
 * are connected. More precisely this is the ratio of the triangles and 
 * connected triples in the graph, the result is a single real number or 
 * NaN (0/0) if there are no connected triples in the graph. Directed graphs 
 * are considered as undirected ones.
 */
VALUE cIGraph_transitivity_local(VALUE self, VALUE vs){

  igraph_t *graph;
  igraph_vs_t vids;
  igraph_vector_t vidv;
  igraph_vector_t res;
  VALUE trans = rb_ary_new();
  int i;

  //vector to hold the results of the calculations
  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);

  Data_Get_Struct(self, igraph_t, graph);

  igraph_transitivity_local_undirected(graph,&res,vids);

  for(i=0;i<igraph_vector_size(&res);i++){
    rb_ary_push(trans,rb_float_new(VECTOR(res)[i]));
  }

  igraph_vector_destroy(&vidv);
  igraph_vector_destroy(&res);
  igraph_vs_destroy(&vids);

  return trans;

}
Beispiel #27
0
void print_vector(igraph_vector_t *v, FILE *f) {
  long int i;
  for (i=0; i<igraph_vector_size(v); i++) {
    fprintf(f, " %4.2f", VECTOR(*v)[i]);
  }
  fprintf(f, "\n");
}
Beispiel #28
0
int igraph_adjlist_simplify(igraph_adjlist_t *al) {
  long int i;
  long int n=al->length;
  igraph_vector_t mark;
  IGRAPH_VECTOR_INIT_FINALLY(&mark, n);
  for (i=0; i<n; i++) {
    igraph_vector_t *v=&al->adjs[i];
    long int j, l=igraph_vector_size(v);
    VECTOR(mark)[i] = i+1;
    for (j=0; j<l; /* nothing */) {
      long int e=VECTOR(*v)[j];
      if (VECTOR(mark)[e] != i+1) {
	VECTOR(mark)[e]=i+1;
	j++;
      } else {
	VECTOR(*v)[j] = igraph_vector_tail(v);
	igraph_vector_pop_back(v);
	l--;
      }
    }
  }
  
  igraph_vector_destroy(&mark);
  IGRAPH_FINALLY_CLEAN(1);
  return 0;
}
int print_vector(igraph_vector_t *v) {
  long int i, l=igraph_vector_size(v);
  for (i=0; i<l; i++) {
    printf(" %li", (long int) VECTOR(*v)[i]);
  }
  printf("\n");
}
Beispiel #30
0
igraph_vector_t *igraph_lazy_adjlist_get_real(igraph_lazy_adjlist_t *al,
						igraph_integer_t pno) {
  long int no=pno;
  int ret;
  if (al->adjs[no] == 0) {
    al->adjs[no] = igraph_Calloc(1, igraph_vector_t);
    if (al->adjs[no] == 0) {
      igraph_error("Lazy adjlist failed", __FILE__, __LINE__, 
		   IGRAPH_ENOMEM);
    }
    ret=igraph_vector_init(al->adjs[no], 0);
    if (ret != 0) {
      igraph_error("", __FILE__, __LINE__, ret);
    }
    ret=igraph_neighbors(al->graph, al->adjs[no], no, al->mode);
    if (ret != 0) {
      igraph_error("", __FILE__, __LINE__, ret);
    }

    if (al->simplify == IGRAPH_SIMPLIFY) {
      igraph_vector_t *v=al->adjs[no];
      long int i, p=0, n=igraph_vector_size(v);
      for (i=0; i<n; i++) {
	if (VECTOR(*v)[i] != no && 
	    (i==n-1 || VECTOR(*v)[i+1] != VECTOR(*v)[i])) {
	  VECTOR(*v)[p]=VECTOR(*v)[i];
	  p++;
	}
      }
      igraph_vector_resize(v, p);
    }
  }
  
  return al->adjs[no];
}