Пример #1
0
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;
}
Пример #2
0
/* call-seq:
 *   graph.eigenvector_centrality(scale, weights) -> Array
 *
 * Returns a two-element arrar, the first element of which is an Array of 
 * eigenvector centrality scores for graph, and the second of which is the
 * eigenvalue.
 *
 * scale is a boolean value. If true, the scores will be weighted so that the
 * absolute value of the maximum centrality is one.
 *
 * weights is an Array giving the weights of the edges. If nil, the edges are unweighted.
 */
VALUE cIGraph_eigenvector_centrality(VALUE self, VALUE scale, VALUE weights) {
  int i;
  igraph_t *graph;
  igraph_vector_t vec;
  igraph_real_t val;
  igraph_vector_t wgts;
  igraph_arpack_options_t arpack_opt;
  igraph_bool_t sc = 0;
  VALUE eigenvector = rb_ary_new();
  VALUE rb_res = rb_ary_new();

  IGRAPH_FINALLY(igraph_vector_destroy, &vec);
  IGRAPH_FINALLY(igraph_vector_destroy, &wgts);
  IGRAPH_CHECK(igraph_vector_init(&vec,0));
  IGRAPH_CHECK(igraph_vector_init(&wgts,0));

  igraph_arpack_options_init(&arpack_opt);

  if (scale == Qtrue) sc = 1;

  Data_Get_Struct(self, igraph_t, graph);

  if (weights == Qnil) {
    IGRAPH_CHECK(igraph_eigenvector_centrality(graph, &vec, &val, sc, NULL, &arpack_opt));
  } else {
    for(i = 0; i < RARRAY_LEN(weights); i++)
      IGRAPH_CHECK(igraph_vector_push_back(&wgts, NUM2DBL(RARRAY_PTR(weights)[i])));

    IGRAPH_CHECK(igraph_eigenvector_centrality(graph, &vec, &val, sc, &wgts, &arpack_opt));
  }

  for(i = 0; i < igraph_vector_size(&vec); i++)
    rb_ary_push(eigenvector, rb_float_new(VECTOR(vec)[i]));

  igraph_vector_destroy(&vec);
  igraph_vector_destroy(&wgts);

  rb_ary_push(rb_res, eigenvector);
  rb_ary_push(rb_res, rb_float_new(val));

  IGRAPH_FINALLY_CLEAN(2);

  return rb_res;
}