Пример #1
0
int igraph_dot_product_game(igraph_t *graph, const igraph_matrix_t *vecs,
			    igraph_bool_t directed) {

  igraph_integer_t nrow=igraph_matrix_nrow(vecs);
  igraph_integer_t ncol=igraph_matrix_ncol(vecs);
  int i, j;
  igraph_vector_t edges;
  igraph_bool_t warned_neg=0, warned_big=0;
  
  IGRAPH_VECTOR_INIT_FINALLY(&edges, 0);
    
  RNG_BEGIN();

  for (i = 0; i < ncol; i++) {
    int from=directed ? 0 : i+1;
    igraph_vector_t v1;
    igraph_vector_view(&v1, &MATRIX(*vecs, 0, i), nrow);
    for (j = from; j < ncol; j++) {
      igraph_real_t prob;
      igraph_vector_t v2;
      if (i==j) { continue; }
      igraph_vector_view(&v2, &MATRIX(*vecs, 0, j), nrow);
      igraph_lapack_ddot(&v1, &v2, &prob);
      if (prob < 0 && ! warned_neg) {
	warned_neg=1;
	IGRAPH_WARNING("Negative connection probability in "
		       "dot-product graph");
      } else if (prob > 1 && ! warned_big) {
	warned_big=1;
	IGRAPH_WARNING("Greater than 1 connection probability in "
		       "dot-product graph");
	IGRAPH_CHECK(igraph_vector_push_back(&edges, i));
	IGRAPH_CHECK(igraph_vector_push_back(&edges, j));
      } else if (RNG_UNIF01() < prob) { 
	IGRAPH_CHECK(igraph_vector_push_back(&edges, i));
	IGRAPH_CHECK(igraph_vector_push_back(&edges, j));
      }
    }
  }

  RNG_END();
  
  igraph_create(graph, &edges, ncol, directed);
  igraph_vector_destroy(&edges);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;
}
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;
}
Пример #3
0
int main() {
  
  igraph_t g;
  igraph_vector_t weights;
  igraph_real_t weights_data[] = { 0,2,1, 0,5,2, 1,1,0, 2,2,8, 1,1,3, 1,1,4, 2,1 };
  igraph_matrix_t res;
  
  igraph_small(&g, 10, IGRAPH_DIRECTED, 
	       0,1, 0,2, 0,3,    1,2, 1,4, 1,5,
	       2,3, 2,6,         3,2, 3,6,
	       4,5, 4,7,         5,6, 5,8, 5,9,
	       7,5, 7,8,         8,9,
	       5,2,
	       2,1,
	       -1);
  
  igraph_vector_view(&weights, weights_data, 
		     sizeof(weights_data)/sizeof(igraph_real_t));
  
  igraph_matrix_init(&res, 0, 0);
  igraph_shortest_paths_dijkstra(&g, &res, igraph_vss_all(), igraph_vss_all(),
				 &weights, IGRAPH_OUT);
  print_matrix(&res);
  
  igraph_matrix_destroy(&res);
  igraph_destroy(&g);

  return 0;
}
Пример #4
0
int check_ring(const ring_test_t *test) {
  igraph_t graph, othergraph;
  igraph_vector_t otheredges;
  igraph_bool_t iso;
  int ret;

  /* Create ring */
  igraph_ring(&graph, test->n, test->directed, test->mutual, test->circular);

  /* Check its properties */
  if ((ret=check_ring_properties(&graph, test->directed, test->mutual, 
				 test->circular))) { return ret;}

  /* Check that it is isomorphic to the stored graph */
  igraph_vector_view(&otheredges, test->edges, test->m * 2);
  igraph_create(&othergraph, &otheredges, test->n, test->directed);
  igraph_isomorphic(&graph, &othergraph, &iso);
  if (!iso) { return 50; }

  /* Clean up */
  igraph_destroy(&graph);
  igraph_destroy(&othergraph);

  return 0;
}
Пример #5
0
int igraph_sample_dirichlet(igraph_integer_t n, const igraph_vector_t *alpha,
			    igraph_matrix_t *res) {

  igraph_integer_t len=igraph_vector_size(alpha);
  igraph_integer_t i;
  igraph_vector_t vec;

  if (n < 0) {
    IGRAPH_ERROR("Number of samples should be non-negative",
		 IGRAPH_EINVAL);
  }
  if (len < 2) {
    IGRAPH_ERROR("Dirichlet parameter vector too short, must "
		 "have at least two entries", IGRAPH_EINVAL);
  }
  if (igraph_vector_min(alpha) <= 0) {
    IGRAPH_ERROR("Dirichlet concentration parameters must be positive",
		 IGRAPH_EINVAL);
  }

  IGRAPH_CHECK(igraph_matrix_resize(res, len, n));

  RNG_BEGIN();

  for (i = 0; i < n; i++) {
    igraph_vector_view(&vec, &MATRIX(*res, 0, i), len);
    igraph_rng_get_dirichlet(igraph_rng_default(), alpha, &vec);
  }

  RNG_END();

  return 0;
}
Пример #6
0
int main() {

  igraph_t g;
  igraph_vector_t v=IGRAPH_VECTOR_NULL;
  igraph_real_t edges[] = { 0,1, 1,2, 2,2, 2,3, 2,4, 3,4 };
  igraph_vector_t v2;
  long int i;
  igraph_vit_t vit;
  igraph_vs_t vs;
  igraph_integer_t size;

  igraph_vector_view(&v, edges, sizeof(edges)/sizeof(igraph_real_t));
  igraph_create(&g, &v, 0, IGRAPH_DIRECTED);

  /* Create iterator based on a vector (view) */
  igraph_vector_init(&v2, 6);
  VECTOR(v2)[0]=0;   VECTOR(v2)[1]=2;
  VECTOR(v2)[2]=4;   VECTOR(v2)[3]=0;
  VECTOR(v2)[4]=2;   VECTOR(v2)[5]=4;

  igraph_vit_create(&g, igraph_vss_vector(&v2), &vit);

  i=0;
  while (!IGRAPH_VIT_END(vit)) {
    if (IGRAPH_VIT_GET(vit) != VECTOR(v2)[i]) {
      return 1;
    }
    IGRAPH_VIT_NEXT(vit);
    i++;
  }
  if (i != igraph_vector_size(&v2)) {
    return 2;
  }

  igraph_vit_destroy(&vit);
  igraph_vector_destroy(&v2);

  /* Create small vector iterator */

  igraph_vs_vector_small(&vs, 0, 2, 4, 0, 2, 4, 2, -1);
  igraph_vit_create(&g, vs, &vit);
  igraph_vs_size(&g, &vs, &size);
  printf("%li ", (long int) size);
  for (; !IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit)) {
    printf("%li ", (long int) IGRAPH_VIT_GET(vit));
  }
  printf("\n");
  
  igraph_vit_destroy(&vit);
  igraph_vs_destroy(&vs);

  /* Clean up */

  igraph_destroy(&g);

  return 0;
}
Пример #7
0
int igraph_i_eigen_matrix_sym_arpack_cb(igraph_real_t *to, 
					const igraph_real_t *from,
					int n, void *extra) {
  
  igraph_i_eigen_matrix_sym_arpack_data_t *data=
    (igraph_i_eigen_matrix_sym_arpack_data_t *) extra;

  if (data->A) {
    igraph_blas_dgemv_array(/*transpose=*/ 0, /*alpha=*/ 1.0,
			    data->A, from, /*beta=*/ 0.0, to);
  } else { /* data->sA */
    igraph_vector_t vto, vfrom;
    igraph_vector_view(&vto, to, n);
    igraph_vector_view(&vfrom, to, n);
    igraph_vector_null(&vto);
    igraph_sparsemat_gaxpy(data->sA, &vfrom, &vto);
  }
  return 0;
}
Пример #8
0
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;
}
Пример #9
0
int check_lattice(const lat_test_t *test) {
  igraph_t graph, othergraph;
  igraph_vector_t otheredges;
  igraph_vector_t dimvector;
  igraph_bool_t iso;
  int ret;  
    
  /* Create lattice */
  igraph_vector_view(&dimvector, test->dimedges, test->dim);
  igraph_lattice(&graph, &dimvector, test->nei, test->directed,
		 test->mutual, test->circular);

  /* Check its properties */
  if ((ret=check_lattice_properties(&graph, &dimvector, test->directed, 
				    test->mutual, test->circular))) {
    igraph_destroy(&graph);
    printf("Lattice properties are not satisfied\n");
    return ret;
  }
  
  /* Check that it is isomorphic to the stored graph */
  igraph_vector_view(&otheredges, test->dimedges+test->dim, test->m * 2);
  igraph_create(&othergraph, &otheredges, igraph_vector_prod(&dimvector),
		test->directed);
  igraph_isomorphic(&graph, &othergraph, &iso);
  if (!iso) {
    printf("--\n");
    igraph_write_graph_edgelist(&graph, stdout);
    printf("--\n");
    igraph_write_graph_edgelist(&othergraph, stdout);
    igraph_destroy(&graph);
    igraph_destroy(&othergraph);
    return 50;    
  }  

  igraph_destroy(&graph);
  igraph_destroy(&othergraph);
  return 0;
}
int main() {

  igraph_t g;
  igraph_vector_ptr_t vecs;
  long int i;
  igraph_real_t weights[] = { 1, 2, 3, 4, 5, 1, 1, 1, 1, 1 }; 
  igraph_real_t weights2[] = { 0,2,1, 0,5,2, 1,1,0, 2,2,8, 1,1,3, 1,1,4, 2,1 };
  igraph_vector_t weights_vec;
  igraph_vs_t vs;

  /* Simple ring graph without weights */

  igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 1);
  
  igraph_vector_ptr_init(&vecs, 5);
  for (i=0; i<igraph_vector_ptr_size(&vecs); i++) {
    VECTOR(vecs)[i] = calloc(1, sizeof(igraph_vector_t));
    igraph_vector_init(VECTOR(vecs)[i], 0);
  }
  igraph_vs_vector_small(&vs, 1, 3, 5, 2, 1,  -1);
  
  igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, 0, IGRAPH_OUT);
  
  for (i=0; i<igraph_vector_ptr_size(&vecs); i++) 
    print_vector(VECTOR(vecs)[i]);

  /* Same ring, but with weights */

  igraph_vector_view(&weights_vec, weights, sizeof(weights)/sizeof(igraph_real_t));
  igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, &weights_vec, IGRAPH_OUT);
  
  for (i=0; i<igraph_vector_ptr_size(&vecs); i++) 
    print_vector(VECTOR(vecs)[i]);

  igraph_destroy(&g);

  /* More complicated example */

  igraph_small(&g, 10, IGRAPH_DIRECTED, 
	       0,1, 0,2, 0,3,    1,2, 1,4, 1,5,
	       2,3, 2,6,         3,2, 3,6,
	       4,5, 4,7,         5,6, 5,8, 5,9,
	       7,5, 7,8,         8,9,
	       5,2,
	       2,1,
	       -1);
  
  igraph_vector_view(&weights_vec, weights2, sizeof(weights2)/sizeof(igraph_real_t));
  igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, &weights_vec, IGRAPH_OUT);
  
  for (i=0; i<igraph_vector_ptr_size(&vecs); i++) {
    print_vector(VECTOR(vecs)[i]);
    igraph_vector_destroy(VECTOR(vecs)[i]);
    free(VECTOR(vecs)[i]);
  }

  igraph_vector_ptr_destroy(&vecs);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g);

  if (!IGRAPH_FINALLY_STACK_EMPTY) return 1;

  return 0;
}
Пример #11
0
int main() {
  
  igraph_t g;
  igraph_vector_t bet, bet2, weights, edges;
  igraph_vector_t bbet, bbet2;
  
  igraph_real_t nontriv[] = { 0, 19, 0, 16, 0, 20, 1, 19, 2, 5, 3, 7, 3, 8, 
			      4, 15, 4, 11, 5, 8, 5, 19, 6, 7, 6, 10, 6, 8, 
			      6, 9, 7, 20, 9, 10, 9, 20, 10, 19, 
			      11, 12, 11, 20, 12, 15, 13, 15, 
			      14, 18, 14, 16, 14, 17, 15, 16, 17, 18 };
  
  igraph_real_t nontriv_weights[] = { 0.5249, 1, 0.1934, 0.6274, 0.5249, 
				      0.0029, 0.3831, 0.05, 0.6274, 0.3831, 
				      0.5249, 0.0587, 0.0579, 0.0562, 0.0562, 
				      0.1934, 0.6274, 0.6274, 0.6274, 0.0418, 
				      0.6274, 0.3511, 0.3511, 0.1486, 1, 1, 
				      0.0711, 0.2409 };

  igraph_real_t nontriv_res[] = { 20, 0, 0, 0, 0, 19, 80, 85, 32, 0, 10, 
				  75, 70, 0, 36, 81, 60, 0, 19, 19, 86 };

  /*******************************************************/

  igraph_barabasi_game(/* graph= */    &g,
		       /* n= */        1000,
		       /* power= */    1,
		       /* m= */        3,
		       /* outseq= */   0,
		       /* outpref= */  0,
		       /* A= */        1,
		       /* directed= */ 0, 
		       /* algo= */     IGRAPH_BARABASI_BAG,
		       /* start_from= */ 0);
  
  igraph_simplify(&g, /* multiple= */ 1, /* loops= */ 1, /*edge_comb=*/ 0);
  
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 2,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 2,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 0);  

  check(&bet, &bbet, 10);

  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bbet);
  igraph_destroy(&g);

  /*******************************************************/

  igraph_tree(&g, 20000, 10, IGRAPH_TREE_UNDIRECTED);
  
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ 0, 
			      /* nobigint=  */ 0);

  check(&bet, &bbet, 20);

  igraph_vector_init(&bet2, 0);
  igraph_vector_init(&bbet2, 0);
  igraph_vector_init(&weights, igraph_ecount(&g));
  igraph_vector_fill(&weights, 1.0);
  
  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bet2,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ &weights, 
			      /* nobigint=  */ 1);

  igraph_betweenness_estimate(/* graph=     */ &g,
			      /* res=       */ &bbet2,
			      /* vids=      */ igraph_vss_all(),
			      /* directed = */ 0,
			      /* cutoff=    */ 3,
			      /* weights=   */ &weights, 
			      /* nobigint=  */ 0);

  if (!igraph_vector_all_e(&bet, &bet2)) {
    return 1;
  }

/*   if (!igraph_vector_all_e(&bbet, &bbet2)) { */
/*     return 2; */
/*   } */

  check(&bet, &bbet, 30);
  check(&bet2, &bbet2, 40);

  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bet2);
  igraph_vector_destroy(&bbet);
  igraph_vector_destroy(&bbet2);
  igraph_vector_destroy(&weights);
  igraph_destroy(&g);

  /* Non-trivial weighted graph */
  igraph_vector_view(&edges, nontriv, sizeof(nontriv)/sizeof(igraph_real_t));
  igraph_create(&g, &edges, 0, /* directed= */ 0);
  igraph_vector_view(&weights, nontriv_weights, 
		     sizeof(nontriv_weights)/sizeof(igraph_real_t));
  igraph_vector_init(&bet, 0);
  igraph_vector_init(&bbet, 0);

  igraph_betweenness(/*graph=*/ &g, /*res=*/ &bet, /*vids=*/ igraph_vss_all(), 
		     /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 1);

  igraph_betweenness(/*graph=*/ &g, /*res=*/ &bbet, /*vids=*/ igraph_vss_all(), 
		     /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 0);

  igraph_vector_view(&bet2, nontriv_res, 
		     sizeof(nontriv_res)/sizeof(igraph_real_t));

  if (!igraph_vector_all_e(&bet, &bet2)) {
    return 2;
  }

  check(&bet, &bbet, 50);
  
  igraph_vector_destroy(&bet);
  igraph_vector_destroy(&bbet);
  igraph_destroy(&g);

  if (IGRAPH_FINALLY_STACK_SIZE() != 0) return 3;

  return 0;
}
Пример #12
0
int main() {

  igraph_t g;
  FILE *karate, *neural;
  igraph_real_t res;
  igraph_vector_t types;
  igraph_vector_t degree, outdegree, indegree;

  igraph_real_t football_types[] = { 
    7,0,2,3,7,3,2,8,8,7,3,10,6,2,6,2,7,9,6,1,9,8,8,7,10,0,6,9,
    11,1,1,6,2,0,6,1,5,0,6,2,3,7,5,6,4,0,11,2,4,11,10,8,3,11,6,
    1,9,4,11,10,2,6,9,10,2,9,4,11,8,10,9,6,3,11,3,4,9,8,8,1,5,3,
    5,11,3,6,4,9,11,0,5,4,4,7,1,9,9,10,3,6,2,1,3,0,7,0,2,3,8,0,
    4,8,4,9,11 };

  karate=fopen("karate.gml", "r");
  igraph_read_graph_gml(&g, karate);
  fclose(karate);
  
  igraph_vector_init(&types, 0);
  igraph_degree(&g, &types, igraph_vss_all(), IGRAPH_ALL, /*loops=*/ 1);

  igraph_assortativity_nominal(&g, &types, &res, /*directed=*/ 0);
  printf("%.5f\n", res);
  
  igraph_destroy(&g);

  /*---------------------*/

  neural=fopen("celegansneural.gml", "r");
  igraph_read_graph_gml(&g, neural);
  fclose(neural);
  
  igraph_degree(&g, &types, igraph_vss_all(), IGRAPH_ALL, /*loops=*/ 1);

  igraph_assortativity_nominal(&g, &types, &res, /*directed=*/ 1);
  printf("%.5f\n", res);  
  igraph_assortativity_nominal(&g, &types, &res, /*directed=*/ 0);
  printf("%.5f\n", res);  

  igraph_destroy(&g);
  igraph_vector_destroy(&types);

  /*---------------------*/
  
  karate=fopen("karate.gml", "r");
  igraph_read_graph_gml(&g, karate);
  fclose(karate);
  
  igraph_vector_init(&degree, 0);
  igraph_degree(&g, &degree, igraph_vss_all(), IGRAPH_ALL, /*loops=*/ 1);
  igraph_vector_add_constant(&degree, -1);

  igraph_assortativity(&g, &degree, 0, &res, /*directed=*/ 0);
  printf("%.5f\n", res);
  
  igraph_destroy(&g);

  /*---------------------*/

  neural=fopen("celegansneural.gml", "r");
  igraph_read_graph_gml(&g, neural);
  fclose(neural);
  
  igraph_degree(&g, &degree, igraph_vss_all(), IGRAPH_ALL, /*loops=*/ 1);
  igraph_vector_add_constant(&degree, -1);

  igraph_assortativity(&g, &degree, 0, &res, /*directed=*/ 1);
  printf("%.5f\n", res);  
  igraph_assortativity(&g, &degree, 0, &res, /*directed=*/ 0);
  printf("%.5f\n", res);  

  igraph_vector_destroy(&degree);

  /*---------------------*/
  
  igraph_vector_init(&indegree, 0);
  igraph_vector_init(&outdegree, 0);
  igraph_degree(&g, &indegree, igraph_vss_all(), IGRAPH_IN, /*loops=*/ 1);
  igraph_degree(&g, &outdegree, igraph_vss_all(), IGRAPH_OUT, /*loops=*/ 1);
  igraph_vector_add_constant(&indegree, -1);
  igraph_vector_add_constant(&outdegree, -1);
  
  igraph_assortativity(&g, &outdegree, &indegree, &res, /*directed=*/ 1);
  printf("%.5f\n", res);

  igraph_vector_destroy(&indegree);
  igraph_vector_destroy(&outdegree);

  /*---------------------*/
  
  igraph_assortativity_degree(&g, &res, /*directed=*/ 1);
  printf("%.5f\n", res);

  igraph_destroy(&g);  

  /*---------------------*/

  karate=fopen("karate.gml", "r");
  igraph_read_graph_gml(&g, karate);
  fclose(karate);
  
  igraph_assortativity_degree(&g, &res, /*directed=*/ 1);
  printf("%.5f\n", res);
  
  igraph_destroy(&g);

  /*---------------------*/
  
  igraph_small(&g, sizeof(football_types)/sizeof(igraph_real_t), 
	       IGRAPH_UNDIRECTED,
	       0,1,2,3,0,4,4,5,3,5,2,6,6,7,7,8,8,9,0,9,4,9,5,10,10,11,5,11,
	       3,11,12,13,2,13,2,14,12,14,14,15,13,15,2,15,4,16,9,16,0,16,
	       16,17,12,17,12,18,18,19,17,20,20,21,8,21,7,21,9,22,7,22,21,
	       22,8,22,22,23,9,23,4,23,16,23,0,23,11,24,24,25,1,25,3,26,12,
	       26,14,26,26,27,17,27,1,27,17,27,4,28,11,28,24,28,19,29,29,
	       30,19,30,18,31,31,32,21,32,15,32,13,32,6,32,0,33,1,33,25,33,
	       19,33,31,34,26,34,12,34,18,34,34,35,0,35,29,35,19,35,30,35,
	       18,36,12,36,20,36,19,36,36,37,1,37,25,37,33,37,18,38,16,38,
	       28,38,26,38,14,38,12,38,38,39,6,39,32,39,13,39,15,39,7,40,3,
	       40,40,41,8,41,4,41,23,41,9,41,0,41,16,41,34,42,29,42,18,42,
	       26,42,42,43,36,43,26,43,31,43,38,43,12,43,14,43,19,44,35,44,
	       30,44,44,45,13,45,33,45,1,45,37,45,25,45,21,46,46,47,22,47,
	       6,47,15,47,2,47,39,47,32,47,44,48,48,49,32,49,46,49,30,50,
	       24,50,11,50,28,50,50,51,40,51,8,51,22,51,21,51,3,52,40,52,5,
	       52,52,53,25,53,48,53,49,53,46,53,39,54,31,54,38,54,14,54,34,
	       54,18,54,54,55,31,55,6,55,35,55,29,55,19,55,30,55,27,56,56,
	       57,1,57,42,57,44,57,48,57,3,58,6,58,17,58,36,58,36,59,58,59,
	       59,60,10,60,39,60,6,60,47,60,13,60,15,60,2,60,43,61,47,61,
	       54,61,18,61,26,61,31,61,34,61,61,62,20,62,45,62,17,62,27,62,
	       56,62,27,63,58,63,59,63,42,63,63,64,9,64,32,64,60,64,2,64,6,
	       64,47,64,13,64,0,65,27,65,17,65,63,65,56,65,20,65,65,66,59,
	       66,24,66,44,66,48,66,16,67,41,67,46,67,53,67,49,67,67,68,15,
	       68,50,68,21,68,51,68,7,68,22,68,8,68,4,69,24,69,28,69,50,69,
	       11,69,69,70,43,70,65,70,20,70,56,70,62,70,27,70,60,71,18,71,
	       14,71,34,71,54,71,38,71,61,71,31,71,71,72,2,72,10,72,3,72,
	       40,72,52,72,7,73,49,73,53,73,67,73,46,73,73,74,2,74,72,74,5,
	       74,10,74,52,74,3,74,40,74,20,75,66,75,48,75,57,75,44,75,75,
	       76,27,76,59,76,20,76,70,76,66,76,56,76,62,76,73,77,22,77,7,
	       77,51,77,21,77,8,77,77,78,23,78,50,78,28,78,22,78,8,78,68,
	       78,7,78,51,78,31,79,43,79,30,79,19,79,29,79,35,79,55,79,79,
	       80,37,80,29,80,16,81,5,81,40,81,10,81,72,81,3,81,81,82,74,
	       82,39,82,77,82,80,82,30,82,29,82,7,82,53,83,81,83,69,83,73,
	       83,46,83,67,83,49,83,83,84,24,84,49,84,52,84,3,84,74,84,10,
	       84,81,84,5,84,3,84,6,85,14,85,38,85,43,85,80,85,12,85,26,85,
	       31,85,44,86,53,86,75,86,57,86,48,86,80,86,66,86,86,87,17,87,
	       62,87,56,87,24,87,20,87,65,87,49,88,58,88,83,88,69,88,46,88,
	       53,88,73,88,67,88,88,89,1,89,37,89,25,89,33,89,55,89,45,89,
	       5,90,8,90,23,90,0,90,11,90,50,90,24,90,69,90,28,90,29,91,48,
	       91,66,91,69,91,44,91,86,91,57,91,80,91,91,92,35,92,15,92,86,
	       92,48,92,57,92,61,92,66,92,75,92,0,93,23,93,80,93,16,93,4,
	       93,82,93,91,93,41,93,9,93,34,94,19,94,55,94,79,94,80,94,29,
	       94,30,94,82,94,35,94,70,95,69,95,76,95,62,95,56,95,27,95,17,
	       95,87,95,37,95,48,96,17,96,76,96,27,96,56,96,65,96,20,96,87,
	       96,5,97,86,97,58,97,11,97,59,97,63,97,97,98,77,98,48,98,84,
	       98,40,98,10,98,5,98,52,98,81,98,89,99,34,99,14,99,85,99,54,
	       99,18,99,31,99,61,99,71,99,14,99,99,100,82,100,13,100,2,100,
	       15,100,32,100,64,100,47,100,39,100,6,100,51,101,30,101,94,
	       101,1,101,79,101,58,101,19,101,55,101,35,101,29,101,100,102,
	       74,102,52,102,98,102,72,102,40,102,10,102,3,102,102,103,33,
	       103,45,103,25,103,89,103,37,103,1,103,70,103,72,104,11,104,
	       0,104,93,104,67,104,41,104,16,104,87,104,23,104,4,104,9,104,
	       89,105,103,105,33,105,62,105,37,105,45,105,1,105,80,105,25,
	       105,25,106,56,106,92,106,2,106,13,106,32,106,60,106,6,106,
	       64,106,15,106,39,106,88,107,75,107,98,107,102,107,72,107,40,
	       107,81,107,5,107,10,107,84,107,4,108,9,108,7,108,51,108,77,
	       108,21,108,78,108,22,108,68,108,79,109,30,109,63,109,1,109,
	       33,109,103,109,105,109,45,109,25,109,89,109,37,109,67,110,
	       13,110,24,110,80,110,88,110,49,110,73,110,46,110,83,110,53,
	       110,23,111,64,111,46,111,78,111,8,111,21,111,51,111,7,111,
	       108,111,68,111,77,111,52,112,96,112,97,112,57,112,66,112,63,
	       112,44,112,92,112,75,112,91,112,28,113,20,113,95,113,59,113,
	       70,113,17,113,87,113,76,113,65,113,96,113,83,114,88,114,110,
	       114,53,114,49,114,73,114,46,114,67,114,58,114,15,114,104,114,
	       -1);
  igraph_simplify(&g, /*multiple=*/ 1, /*loops=*/ 1, /*edge_comb=*/ 0);
  igraph_vector_view(&types, football_types, 
		     sizeof(football_types) / sizeof(igraph_real_t));
  igraph_assortativity_nominal(&g, &types, &res, /*directed=*/ 0);
  printf("%.5f\n", res);
  
  igraph_destroy(&g);
  
  return 0;
}
void test_weighted() {
  igraph_t g;
  igraph_vector_t edges, eb, weights;
  igraph_real_t weights_array[] = { 4, 1, 3, 2, 5, 8, 6, 7 };

  igraph_real_t edges_array1[] = { 2, 3, 0, 1, 4, 7, 5, 6 };
  igraph_real_t edges_array2[] = { 2, 3, 6, 5, 0, 1, 4, 7 };
  igraph_real_t eb_array1_lo[] = { 4, 5, 3+1/3.0-EPS, 4, 2.5, 4, 1, 1 };
  igraph_real_t eb_array1_hi[] = { 4, 5, 3+1/3.0+EPS, 4, 2.5, 4, 1, 1 };
  igraph_real_t eb_array2_lo[] = { 4, 5, 3+1/3.0-EPS, 6, 1.5, 2, 1, 1 };
  igraph_real_t eb_array2_hi[] = { 4, 5, 3+1/3.0+EPS, 6, 1.5, 2, 1, 1 };

  igraph_vector_t edges_sol1, edges_sol2, eb_sol1_lo, eb_sol1_hi, eb_sol2_lo, eb_sol2_hi;

  igraph_vector_view(&edges_sol1, edges_array1, 
		     sizeof(edges_array1)/sizeof(double));
  igraph_vector_view(&edges_sol2, edges_array2, 
		     sizeof(edges_array2)/sizeof(double));
  igraph_vector_view(&eb_sol1_lo, eb_array1_lo, sizeof(eb_array1_lo)/sizeof(double));
  igraph_vector_view(&eb_sol2_lo, eb_array2_lo, sizeof(eb_array2_lo)/sizeof(double));
  igraph_vector_view(&eb_sol1_hi, eb_array1_hi, sizeof(eb_array1_hi)/sizeof(double));
  igraph_vector_view(&eb_sol2_hi, eb_array2_hi, sizeof(eb_array2_hi)/sizeof(double));

  /* Small graph as follows: A--B--C--A, A--D--E--A, B--D, C--E */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
      0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 1, 3, 2, 4, 3, 4, -1);
  igraph_vector_view(&weights, weights_array, igraph_ecount(&g));

  igraph_vector_init(&edges, 0);
  igraph_vector_init(&eb, 0);
  igraph_community_edge_betweenness(&g, &edges, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    &weights);
  
  if (!igraph_vector_all_e(&edges_sol1, &edges) && 
      !igraph_vector_all_e(&edges_sol2, &edges)) {
    printf("Error, edges vector was: \n");
	igraph_vector_print(&edges);
    exit(2);
  }
  if (!igraph_vector_between(&eb, &eb_sol1_lo, &eb_sol1_hi) &&
      !igraph_vector_between(&eb, &eb_sol2_lo, &eb_sol2_hi)) {
    printf("Error, eb vector was: \n");
	igraph_vector_print(&eb);
    exit(2);
  }

  /* Try it once again without storage space for edges */
  igraph_community_edge_betweenness(&g, 0, &eb, 0 /*merges */,
				    0 /*bridges */, /*modularity=*/ 0,
				    /*membership=*/ 0,
				    IGRAPH_UNDIRECTED,
				    &weights);

  if (!igraph_vector_between(&eb, &eb_sol1_lo, &eb_sol1_hi) &&
      !igraph_vector_between(&eb, &eb_sol2_lo, &eb_sol2_hi)) {
    printf("Error, eb vector was: \n");
	igraph_vector_print(&eb);
    exit(2);
  }

  igraph_vector_destroy(&eb);
  igraph_vector_destroy(&edges);
  igraph_destroy(&g);
}
Пример #14
0
int main() {
  
  igraph_t g;
  igraph_vector_t weights;
  igraph_real_t weights_data_0[] = { 0,2,1, 0,5,2, 1,1,0, 2,2,8, 1,1,3, 1,1,4, 2,1 };
  igraph_real_t weights_data_1[] = { 6,7,8,-4,-2,-3,9,2,7 };
  igraph_real_t weights_data_2[] = { 6,7,2,-4,-2,-3,9,2,7 };
  igraph_matrix_t res;
  
  /* Graph with only positive weights */
  igraph_small(&g, 10, IGRAPH_DIRECTED, 
	       0,1, 0,2, 0,3,    1,2, 1,4, 1,5,
	       2,3, 2,6,         3,2, 3,6,
	       4,5, 4,7,         5,6, 5,8, 5,9,
	       7,5, 7,8,         8,9,
	       5,2,
	       2,1,
	       -1);
  
  igraph_vector_view(&weights, weights_data_0, 
		     sizeof(weights_data_0)/sizeof(igraph_real_t));
  
  igraph_matrix_init(&res, 0, 0);
  igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(), igraph_vss_all(),
				     &weights, IGRAPH_OUT);
  print_matrix(&res);
  
  igraph_matrix_destroy(&res);
  igraph_destroy(&g);

  printf("\n");

  /***************************************/

  /* Graph with negative weights */
  igraph_small(&g, 5, IGRAPH_DIRECTED, 
	       0,1, 0,3, 1,3, 1,4, 2,1, 3,2, 3,4, 4,0, 4,2, -1);
  
  igraph_vector_view(&weights, weights_data_1, 
		     sizeof(weights_data_1)/sizeof(igraph_real_t));
  
  igraph_matrix_init(&res, 0, 0);
  igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(), 
				     igraph_vss_all(), &weights, IGRAPH_OUT);
  print_matrix(&res);
  
  /***************************************/

  /* Same graph with negative loop */
  igraph_set_error_handler(igraph_error_handler_ignore);
  igraph_vector_view(&weights, weights_data_2, 
		     sizeof(weights_data_2)/sizeof(igraph_real_t));
  if (igraph_shortest_paths_bellman_ford(&g, &res, igraph_vss_all(),
					 igraph_vss_all(),
                                         &weights, IGRAPH_OUT) != IGRAPH_ENEGLOOP)
    return 1;
  
  igraph_matrix_destroy(&res);
  igraph_destroy(&g);

  if (!IGRAPH_FINALLY_STACK_EMPTY) return 1;

  return 0;
}
Пример #15
0
QList<int> MWBM::run(QList<int> input_weights, int numberOfLeft, int numberOfRight, bool &isMatched)
{
    igraph_t graph;
    igraph_vector_bool_t types;
    igraph_vector_long_t matching;
    igraph_vector_t weights;
    igraph_integer_t matching_size;
    igraph_real_t matching_weight;
    igraph_bool_t is_matching;
    igraph_vector_t v;

    int i;
    QList<int> out;

    igraph_real_t weight_array[input_weights.size()];
    for(int j=0;j<input_weights.size();j++)
    {
        weight_array[j] = input_weights.at(j);
    };

    QList<int> edges_list;
    for(int j=0;j<numberOfLeft;j++)
    {
        for(int k=numberOfLeft;k<numberOfRight+numberOfLeft;k++)
        {
            edges_list.append(j);
            edges_list.append(k);
        }
    }

    igraph_real_t edges[edges_list.size()];

    for(int j=0;j<edges_list.size();j++)
    {
        edges[j] = edges_list.at(j);
    }

    igraph_vector_view(&v, edges, sizeof(edges)/sizeof(double));
    igraph_create(&graph, &v, 0, IGRAPH_DIRECTED);

    igraph_vector_bool_init(&types, numberOfLeft+numberOfRight);
    for (i = 0; i < numberOfLeft+numberOfRight; i++)
        VECTOR(types)[i] = (i >= numberOfLeft);
    igraph_vector_long_init(&matching, 0);
    igraph_vector_init_copy(&weights, weight_array,
                            sizeof(weight_array) / sizeof(weight_array[0]));

    igraph_maximum_bipartite_matching(&graph, &types, &matching_size,
                                      &matching_weight, &matching, &weights,DBL_EPSILON);

    igraph_is_maximal_matching(&graph, &types, &matching, &is_matching);
    if (!is_matching) {
        isMatched = false;
    }
    else
    {
        isMatched=true;
    }

    for(int j=0;j<numberOfLeft*2;j++)
        out.append(VECTOR(matching)[j]);

    igraph_vector_destroy(&weights);
    igraph_vector_long_destroy(&matching);
    igraph_vector_bool_destroy(&types);
    igraph_destroy(&graph);

    return out;
}
Пример #16
0
int check_multi() {

  igraph_t g;
  igraph_vector_t vec;
  igraph_vector_t eids, eids2;
  int ret;
  long int i;

  igraph_real_t q1[] = { 0,1, 0,1 };
  igraph_real_t q2[] = { 0,1, 0,1, 0,1 };
  igraph_real_t q3[] = { 1,0, 3,4, 1,0, 0,1, 3,4, 0,1 };

  igraph_vector_init(&eids, 0);

  /*********************************/
  igraph_small(&g, /*n=*/ 10, /*directed=*/ 1, 
	       0,1, 0,1, 1,0, 1,2, 3,4, 3,4, 3,4, 3,5, 3,7, 
	       9,8,
	       -1);

  igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t));
  igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/ 1);
  igraph_vector_sort(&eids);
  print_vector(&eids, stdout);

  igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t));
  igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 0, /*error=*/ 1);
  igraph_vector_sort(&eids);
  print_vector(&eids, stdout);

  igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t));
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/1);
  if (ret != IGRAPH_EINVAL) { return 1; } 
  igraph_set_error_handler(igraph_error_handler_abort);

  igraph_destroy(&g);
  /*********************************/

  /*********************************/
  igraph_small(&g, /*n=*/10, /*directed=*/0, 
	       0,1, 1,0, 0,1, 3,4, 3,4, 5,4, 9,8, 
	       -1);
  
  igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t));
  igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/1, /*error=*/ 1);
  igraph_vector_sort(&eids);
  print_vector(&eids, stdout);

  igraph_vector_view(&vec, q3, sizeof(q3) / sizeof(igraph_real_t));
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/0, /*error=*/ 1);
  if (ret != IGRAPH_EINVAL) { return 2; }
  igraph_set_error_handler(igraph_error_handler_abort);
  
  igraph_destroy(&g);

  /*********************************/

  igraph_vector_destroy(&eids);

  /*********************************/
  /* Speed tests */

#define NODES 10000
  igraph_barabasi_game(&g, /*n=*/ NODES, /*power=*/ 1.0, /*m=*/ 3, 
		       /*outseq=*/ 0, /*outpref=*/ 0, /*A=*/ 1,
		       /*directed=*/ 1, IGRAPH_BARABASI_BAG,
		       /*start_from=*/ 0);
  igraph_simplify(&g, /*multiple=*/ 1, /*loops=*/ 0, /*edge_comb=*/ 0);

  igraph_vector_init(&eids, NODES/2);
  igraph_random_sample(&eids, 0, igraph_ecount(&g)-1, NODES/2);
  igraph_vector_init(&vec, NODES);
  for (i=0; i<NODES/2; i++) {
    VECTOR(vec)[2*i]   = IGRAPH_FROM(&g, VECTOR(eids)[i]);
    VECTOR(vec)[2*i+1] = IGRAPH_TO(&g, VECTOR(eids)[i]);
  }
  igraph_vector_init(&eids2, 0);
  igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 1, /*error=*/ 1);
  if (!igraph_vector_all_e(&eids, &eids2)) {
    return 3;
  }

  /**/

  for (i=0; i<NODES/2; i++) {
    VECTOR(vec)[2*i]   = IGRAPH_TO(&g, VECTOR(eids)[i]);
    VECTOR(vec)[2*i+1] = IGRAPH_FROM(&g, VECTOR(eids)[i]);
  }
  igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 0, /*error=*/ 1);
  if (!igraph_vector_all_e(&eids, &eids2)) {
    return 4;
  }

  igraph_vector_destroy(&eids);
  igraph_vector_destroy(&eids2);
  igraph_vector_destroy(&vec);
  igraph_destroy(&g);
		  
  /*********************************/

  return 0;
}