Exemplo n.º 1
0
int main() {

  igraph_t g1, g2;
  igraph_vector_t v1, v2;

  igraph_vector_init(&v1, 8);
  VECTOR(v1)[0]=0; VECTOR(v1)[1]=1;
  VECTOR(v1)[2]=1; VECTOR(v1)[3]=2;
  VECTOR(v1)[4]=2; VECTOR(v1)[5]=3;
  VECTOR(v1)[6]=2; VECTOR(v1)[7]=2;

  igraph_create(&g1, &v1, 0, 0);
  igraph_copy(&g2, &g1);

  igraph_vector_init(&v2, 0);
  igraph_get_edgelist(&g2, &v2, 0);
  if (!igraph_vector_is_equal(&v1, &v2)) {
    return 1;
  }

  igraph_vector_destroy(&v1);
  igraph_vector_destroy(&v2);
  igraph_destroy(&g1);
  igraph_destroy(&g2);

  return 0;
}
Exemplo n.º 2
0
int main() {

  igraph_t g;
  int ret;

  igraph_atlas(&g, 45);
  igraph_write_graph_edgelist(&g, stdout);
  printf("\n");
  igraph_destroy(&g);

  igraph_atlas(&g, 0);
  igraph_write_graph_edgelist(&g, stdout);
  printf("\n");
  igraph_destroy(&g);

  igraph_atlas(&g, 1252);
  igraph_write_graph_edgelist(&g, stdout);
  printf("\n");
  igraph_destroy(&g);

  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_atlas(&g, -1);
  if (ret != IGRAPH_EINVAL) {
    return 1;
  }

  ret=igraph_atlas(&g, 1253);
  if (ret != IGRAPH_EINVAL) {
    return 2;
  }

  return 0;
}
Exemplo n.º 3
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;
}
int main() {
  
  igraph_t g;
  igraph_vector_t result;
  long i;
  
  igraph_vector_init(&result, 0);

  igraph_small(&g, 7, 0, 0,1,0,2,0,3,1,2,1,3,2,3,3,4,4,5,4,6,5,6, -1);
  igraph_convergence_degree(&g, &result, 0, 0);
  for (i=0; i<igraph_ecount(&g); i++)
    printf("%.4f ", (float)igraph_vector_e(&result, i));
  printf("\n");
  igraph_destroy(&g);

  igraph_small(&g, 6, 1, 1,0,2,0,3,0,4,0,0,5, -1);
  igraph_convergence_degree(&g, &result, 0, 0);
  for (i=0; i<igraph_ecount(&g); i++)
    printf("%.4f ", (float)igraph_vector_e(&result, i));
  printf("\n");
  igraph_destroy(&g);

  igraph_vector_destroy(&result);
  
  return 0;
}
Exemplo n.º 5
0
int main() {

  igraph_t g;
  igraph_vector_t vids, layers, parents;

  igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 0);
  igraph_vector_init(&vids, 0);
  igraph_vector_init(&layers, 0);
  igraph_vector_init(&parents, 0);
  igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents);
  vector_print(&vids);
  vector_print(&layers);
  vector_print(&parents);
  igraph_destroy(&g);  

  igraph_tree(&g, 20, 2, IGRAPH_TREE_UNDIRECTED);
  igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents);
  vector_print(&vids);
  vector_print(&layers);
  vector_print(&parents);
  igraph_destroy(&g);  
  
  igraph_vector_destroy(&vids);
  igraph_vector_destroy(&layers);
  igraph_vector_destroy(&parents);

  return 0;
}
Exemplo n.º 6
0
int main() {
  
  igraph_t g;
  igraph_integer_t result;
  igraph_integer_t from, to;
  igraph_vector_t path;
  igraph_rng_t rng;
  igraph_rng_init(&rng, &igraph_rngtype_mt19937);
  
  igraph_barabasi_game(&g, 30, /*power=*/ 1, 30, 0, 0, /*A=*/ 1, 
		       IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, 
		       /*start_from=*/ 0, &rng);
  igraph_diameter(&g, &result, 0, 0, 0, IGRAPH_UNDIRECTED, 1);
  
/*   printf("Diameter: %li\n", (long int) result); */
  
  igraph_destroy(&g);

  igraph_ring(&g, 10, IGRAPH_DIRECTED, 0, 0);
  igraph_vector_init(&path, 0);
  igraph_diameter(&g, &result, &from, &to, &path, IGRAPH_DIRECTED, 1);
  printf("diameter: %li, from %li to %li\n", (long int) result,
	 (long int) from, (long int) to);
  print_vector(&path);
  
  igraph_vector_destroy(&path);
  igraph_destroy(&g);
  igraph_rng_destroy(&rng);

  return 0;
}
Exemplo n.º 7
0
int main() {
  
  igraph_t g;
  igraph_vector_t v1, v2;
  int ret;
  
  /* simple use */
  igraph_vector_init(&v1, 8);
  VECTOR(v1)[0]=0; VECTOR(v1)[1]=1;
  VECTOR(v1)[2]=1; VECTOR(v1)[3]=2;
  VECTOR(v1)[4]=2; VECTOR(v1)[5]=3;
  VECTOR(v1)[6]=2; VECTOR(v1)[7]=2;
  igraph_create(&g, &v1, 0, 0);
  if (igraph_vcount(&g) != 4) {
    return 1;
  }
  igraph_vector_init(&v2, 0);
  igraph_get_edgelist(&g, &v2, 0);
  igraph_vector_sort(&v1);
  igraph_vector_sort(&v2);
  if (!igraph_vector_all_e(&v1, &v2)) {
    return 2;
  }
  igraph_destroy(&g);
  
  /* higher number of vertices */
  igraph_create(&g, &v1, 10, 0);
  if (igraph_vcount(&g) != 10) {
    return 1;
  }
  igraph_get_edgelist(&g, &v2, 0);
  igraph_vector_sort(&v1);
  igraph_vector_sort(&v2);
  if (!igraph_vector_all_e(&v1, &v2)) {
    return 3;
  }
  igraph_destroy(&g);

  /* error: IGRAPH_EINVEVECTOR */
  igraph_set_error_handler(igraph_error_handler_ignore);
  igraph_vector_resize(&v1, 9);
  VECTOR(v1)[8]=0;
  ret=igraph_create(&g, &v1, 0, 0);
  if (ret != IGRAPH_EINVEVECTOR) {
    return 4;
  }
  
  /* error: IGRAPH_EINVVID */
  igraph_vector_resize(&v1, 8);
  VECTOR(v1)[7]=-1;
  ret=igraph_create(&g, &v1, 10, 1);
  if (ret != IGRAPH_EINVVID) {
    return 5;
  }
  igraph_vector_destroy(&v1);
  igraph_vector_destroy(&v2);

  return 0;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
0
int main() {
  
  igraph_t g;
  int ret;

  /* empty directed graph, zero vertices */
  igraph_empty(&g, 0, 1);
  if (igraph_vcount(&g) != 0) {
    return 1;
  }
  if (igraph_ecount(&g) != 0) {
    return 2;
  }
  igraph_destroy(&g);
  
  /* empty undirected graph, zero vertices */
  igraph_empty(&g, 0, 0);
  if (igraph_vcount(&g) != 0) {
    return 3;
  }
  if (igraph_ecount(&g) != 0) {
    return 4;
  }
  igraph_destroy(&g);

  /* empty directed graph, 20 vertices */
  igraph_empty(&g, 20, 1);
  if (igraph_vcount(&g) != 20) {
    return 5;
  }
  if (igraph_ecount(&g) != 0) {
    return 6;
  }
  igraph_destroy(&g);
  
  /* empty undirected graph, 30 vertices */
  igraph_empty(&g, 30, 0);
  if (igraph_vcount(&g) != 30) {
    return 7;
  }
  if (igraph_ecount(&g) != 0) {
    return 8;
  }
  igraph_destroy(&g);

  /* error: negative number of vertices */
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_empty(&g, -1, 0);
  if (ret != IGRAPH_EINVAL) {
    return 9;
  }

  return 0;
}
int main() {
  
  igraph_t g;
  long int i;
  igraph_integer_t size;

  /* DIRECTED */
  
  igraph_star(&g, 10, IGRAPH_STAR_OUT, 0);  

  for (i=0; i<100; i++) {
    igraph_es_t es;
    igraph_eit_t it;
    igraph_es_pairs_small(&es, IGRAPH_DIRECTED, 
			  0,1,0,2,0,5,0,2,0,3,0,4,0,7,0,9, -1);
    igraph_eit_create(&g, es, &it);
    igraph_es_size(&g, &es, &size);
    IGRAPH_EIT_RESET(it);
    while (!IGRAPH_EIT_END(it)) {
      IGRAPH_EIT_GET(it);
      IGRAPH_EIT_NEXT(it);
      size--;
    }
    if (size != 0) return 1;
    igraph_eit_destroy(&it);
    igraph_es_destroy(&es);
  }

  igraph_destroy(&g);

  /* UNDIRECTED */
  
  igraph_star(&g, 10, IGRAPH_STAR_UNDIRECTED, 0);
  
  for (i=0; i<100; i++) {
    igraph_es_t es;
    igraph_eit_t it;
    igraph_es_pairs_small(&es, IGRAPH_DIRECTED,
			  0,1,2,0,5,0,0,2,3,0,0,4,7,0,0,9, -1);
    igraph_eit_create(&g, es, &it);
    IGRAPH_EIT_RESET(it);
    while (!IGRAPH_EIT_END(it)) {
      IGRAPH_EIT_GET(it);
      IGRAPH_EIT_NEXT(it);
    }
    igraph_eit_destroy(&it);
    igraph_es_destroy(&es);
  }

  igraph_destroy(&g);

  return 0;
}
Exemplo n.º 11
0
int main() {
  igraph_t small, big;
  igraph_matrix_t small_coords, big_coords, merged_coords;
  igraph_vector_ptr_t graph_ptr, coords_ptr;
  igraph_arpack_options_t arpack_opts;

  /* To make things reproducible */
  igraph_rng_seed(igraph_rng_default(), 42);
  
  igraph_small(&big, 10, IGRAPH_UNDIRECTED, 
	       0,1, 1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,0,
	       -1);
  
  igraph_small(&small, 3, IGRAPH_UNDIRECTED,
	       0,1, 1,2, 2,0,
	       -1);

  igraph_arpack_options_init(&arpack_opts);

  igraph_matrix_init(&big_coords, 0, 0);
  igraph_layout_mds(&big, &big_coords, /*dist=*/ 0, /*dim=*/ 2,
		    &arpack_opts);
  
  igraph_matrix_init(&small_coords, 0, 0);
  igraph_layout_mds(&small, &small_coords, /*dist=*/ 0, /*dim=*/ 2,
		    &arpack_opts);
  
  igraph_vector_ptr_init(&graph_ptr, 2);
  igraph_vector_ptr_init(&coords_ptr, 2);
  igraph_matrix_init(&merged_coords, 0, 0);
  VECTOR(graph_ptr)[0] = &big; 
  VECTOR(graph_ptr)[1] = &small;
  VECTOR(coords_ptr)[0] = &big_coords;
  VECTOR(coords_ptr)[1] = &small_coords;
  
  igraph_layout_merge_dla(&graph_ptr, &coords_ptr, &merged_coords);
  
  igraph_matrix_print(&merged_coords);
  
  igraph_matrix_destroy(&merged_coords);
  igraph_matrix_destroy(&small_coords);
  igraph_matrix_destroy(&big_coords);
  igraph_vector_ptr_destroy(&graph_ptr);
  igraph_vector_ptr_destroy(&coords_ptr);
  igraph_destroy(&small);
  igraph_destroy(&big);
  
#ifdef __APPLE__
  return 0;
#else
  return 77;
#endif
}
Exemplo n.º 12
0
int main() {
  
  igraph_t graph, ring;
  igraph_vector_t order,order_out, father,dist;
  long int i;
  
  igraph_ring(&ring, 10, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1);
  igraph_disjoint_union(&graph, &ring, &ring);
  igraph_destroy(&ring);
  
  igraph_vector_init(&order, 0);
  igraph_vector_init(&order_out, 0);
  igraph_vector_init(&father, 0);
  igraph_vector_init(&dist, 0);
  
  igraph_dfsd(&graph, /*root=*/0, /*neimode=*/ IGRAPH_OUT, 
       /*unreachable=*/ 1,&order,&order_out,&father,&dist, 
       /*in_callback=*/ 0,/*out_callback*/0, /*extra=*/ 0);
  
  igraph_vector_print(&order);
  igraph_vector_print(&order_out);
  igraph_vector_print(&father);
  igraph_vector_print(&dist);

  igraph_vector_destroy(&order);
  igraph_vector_destroy(&order_out);
  igraph_vector_destroy(&father);
  igraph_vector_destroy(&dist);

  /* Test the callback */

  igraph_dfsd(&graph, /*root=*/ 0,/*neimode=*/ IGRAPH_OUT, 
       /*unreachable=*/ 1,0, 0, 0, 0,&dfs_callback,0, 0);
  printf("\n");
  
  igraph_dfsd(&graph, /*root=*/ 0,/*neimode=*/ IGRAPH_OUT, 
       /*unreachable=*/ 1,0, 0, 0, 0,0,&dfs_callback, 0);
  printf("\n");
  /* Test different roots */

  igraph_dfsd(&graph, /*root=*/ 2,/*neimode=*/ IGRAPH_OUT, 
       /*unreachable=*/ 1, 0, 0, 0, 0,&dfs_callback,0, 0);
  printf("\n");

  igraph_dfsd(&graph, /*root=*/ 2,/*neimode=*/ IGRAPH_OUT, 
       /*unreachable=*/ 1, 0, 0, 0, 0,0,&dfs_callback,0);
  printf("\n");
  igraph_destroy(&graph);
  
  return 0;
}
Exemplo n.º 13
0
int main() {
  
  igraph_t g;
  igraph_vector_ptr_t result;
  igraph_es_t es;
  igraph_integer_t omega;
  long int i, j, n;
  const int params[] = {4, -1, 2, 2, 0, 0, -1, -1};
 
  igraph_set_warning_handler(warning_handler_ignore);

  igraph_vector_ptr_init(&result, 0);
  igraph_full(&g, 6, 0, 0);
  igraph_es_pairs_small(&es, 0, 0, 1, 0, 2, 3, 5, -1);
  igraph_delete_edges(&g, es);
  igraph_es_destroy(&es);
  
  for (j=0; j<sizeof(params)/(2*sizeof(params[0])); j++) {
    if (params[2*j+1] != 0) {
      igraph_cliques(&g, &result, params[2*j], params[2*j+1]);  
    } else {
      igraph_largest_cliques(&g, &result);
    }
    n = igraph_vector_ptr_size(&result);
    printf("%ld cliques found\n", (long)n);
    canonicalize_list(&result);
    for (i=0; i<n; i++) {
      igraph_vector_t* v = (igraph_vector_t*) igraph_vector_ptr_e(&result,i);
      print_vector(v);
      igraph_vector_destroy(v);
      free(v);
    }
  }
   
  igraph_clique_number(&g, &omega);
  printf("omega=%ld\n", (long)omega);

  test_callback(&g);

  igraph_destroy(&g);

  igraph_tree(&g, 5, 2, IGRAPH_TREE_OUT);
  igraph_cliques(&g, &result, 5, 5);
  if (igraph_vector_ptr_size(&result) != 0) return 1;

  igraph_destroy(&g);
  igraph_vector_ptr_destroy(&result);

  return 0;
}
int main() {

  igraph_t g;
  igraph_vector_t v;
  int ret;

  /* without edges */
  igraph_empty(&g, 5, IGRAPH_DIRECTED);
  igraph_add_vertices(&g, 2, 0);
  igraph_add_vertices(&g, 3, 0);
  igraph_add_vertices(&g, 1, 0);
  igraph_add_vertices(&g, 4, 0);
  if (igraph_vcount(&g) != 15)  {
    return 1;
  }
  igraph_delete_vertices(&g, igraph_vss_1(2));
  if (igraph_vcount(&g) != 14)  {
    return 2;
  }
  igraph_destroy(&g);
   
  igraph_vector_init(&v, 8);
  VECTOR(v)[0]=0; VECTOR(v)[1]=1;
  VECTOR(v)[2]=1; VECTOR(v)[3]=2;
  VECTOR(v)[4]=2; VECTOR(v)[5]=3;
  VECTOR(v)[6]=2; VECTOR(v)[7]=2;
  igraph_create(&g, &v, 0, 0);
  igraph_vector_destroy(&v);

  /* resize vector */
  igraph_delete_vertices(&g, igraph_vss_1(2));
  if (igraph_vcount(&g) != 3) {
    return 3;
  }
  if (igraph_ecount(&g) != 1) {
    return 4;
  }

  /* error test */
  igraph_set_error_handler(igraph_error_handler_ignore);
  ret=igraph_delete_vertices(&g, igraph_vss_1(3));
  if (ret != IGRAPH_EINVVID) {
    return 5;
  }
  
  igraph_destroy(&g);

  return 0;
}
int main() {

  igraph_t g;
  igraph_vector_t eb; 

  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);
  
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness(&g, &eb, IGRAPH_UNDIRECTED);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  igraph_small(&g, 0, IGRAPH_UNDIRECTED,
    0, 1, 0, 2, 0, 3, 1, 4, -1);
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness_estimate(&g, &eb, IGRAPH_UNDIRECTED, 2);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  igraph_small(&g, 0, IGRAPH_UNDIRECTED,
    0, 1, 0, 3, 1, 2, 1, 4, 2, 5, 3, 4, 3, 6, 4, 5, 4, 7, 5, 8,
    6, 7, 7, 8, -1);
  igraph_vector_init(&eb, igraph_ecount(&g));
  igraph_edge_betweenness_estimate(&g, &eb, IGRAPH_UNDIRECTED, 2);
  print_vector(&eb, stdout);
  igraph_vector_destroy(&eb);
  igraph_destroy(&g);

  return 0;
}
Exemplo n.º 16
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;
}
/* call-seq:
 *   IGraph::GenerateRandom.citing_cited_type_game(nodes,types,pref,edges_per_step_directed) -> IGraph
 *
 * This game is similar to igraph_cited_type_game() but here the category of
 * the citing vertex is also considered.
 *
 * An evolving citation network is modeled here, a single vertex and its
 * edges_per_step citation are added in each time step. The odds the a given
 * vertex is cited by the new vertex depends on the category of both the
 * citing and the cited vertex and is given in the pref matrix. The categories
 * of the citing vertex correspond to the rows, the categories of the cited
 * vertex to the columns of this matrix. Ie. the element in row i and column
 * j gives the probability that a j vertex is cited, if the category of the
 * citing vertex is i.
 *
 * Note that this function might generate networks with multiple edges if
 * edges_per_step is greater than one. You might want to call
 * igraph_simplify() on the result to remove multiple edges.
 *
 * nodes: The number of vertices in the network.
 *
 * types: A numeric IGraphMatrix of length nodes, containing the categories
 * of the vertices. The categories are numbered from zero.
 *
 * pref: The preference IGraphMatrix, a square matrix is required, both the
 * number of rows and columns should be the maximum element in types plus
 * one (types are numbered from zero).
 *
 * edges_per_step: Integer constant, the number of edges to add in each time
 * step.
 *
 * directed: Logical constant, whether to create a directed network.
 */
VALUE cIGraph_citing_cited_type_game(VALUE self, VALUE nodes, VALUE types, VALUE pref, VALUE e_per_s, VALUE directed) {

    igraph_t *graph;
    VALUE new_graph;
    igraph_vector_t typev;
    igraph_matrix_t *prefm;
    int i;

    new_graph = cIGraph_alloc(cIGraph);
    Data_Get_Struct(new_graph, igraph_t, graph);
    Data_Get_Struct(pref, igraph_matrix_t, prefm);

    igraph_vector_init(&typev,0);

    for(i=0; i<RARRAY_LEN(types); i++) {
        igraph_vector_push_back(&typev,NUM2INT(RARRAY_PTR(types)[i]));
    }

    /*printf("ok\n");*/

    igraph_destroy(graph);
    igraph_citing_cited_type_game(graph, NUM2INT(nodes),
                                  &typev,
                                  prefm,
                                  NUM2INT(e_per_s),
                                  directed == Qtrue ? 1: 0);

    /*printf("death\n");*/

    igraph_vector_destroy(&typev);

    return new_graph;


}
Exemplo n.º 18
0
int initIgraph(int *m){

	igraph_t g;
	igraph_matrix_t mat;
	long int i, j;

	igraph_matrix_init(&mat, numNodos, numNodos);
	for (i=0; i<numNodos; i++) for (j=0; j<numNodos; j++) MATRIX(mat, i, j) = m[i+numNodos*j];
	igraph_i_set_attribute_table(&igraph_cattribute_table);


	igraph_weighted_adjacency(&g, &mat, IGRAPH_ADJ_UPPER, 0, /*loops=*/ 1);
	//print(&g);

	FILE *stp;
	stp = fopen("/home/john/git/primAlgorithm/grafo2.gml", "w");
	if (stp==0) {
		printf("Problema abriendo archivo de grafo\n");
		return EXIT_FAILURE;
	}

	igraph_write_graph_gml(&g, stp, 0, "gml Test");
	fclose(stp);

	igraph_destroy(&g);
	return EXIT_SUCCESS;
}
void test_unweighted() {
  igraph_t g;
  igraph_vector_t edges, eb;
  long int i;
  long int no_of_edges;

  /* Zachary Karate club */
  igraph_small(&g, 0, IGRAPH_UNDIRECTED, 
	       0,  1,  0,  2,  0,  3,  0,  4,  0,  5,
	       0,  6,  0,  7,  0,  8,  0, 10,  0, 11,
	       0, 12,  0, 13,  0, 17,  0, 19,  0, 21,
	       0, 31,  1,  2,  1,  3,  1,  7,  1, 13,
	       1, 17,  1, 19,  1, 21,  1, 30,  2,  3,
	       2,  7,  2,  8,  2,  9,  2, 13,  2, 27,
	       2, 28,  2, 32,  3,  7,  3, 12,  3, 13,
	       4,  6,  4, 10,  5,  6,  5, 10,  5, 16,
	       6, 16,  8, 30,  8, 32,  8, 33,  9, 33,
	       13, 33, 14, 32, 14, 33, 15, 32, 15, 33,
	       18, 32, 18, 33, 19, 33, 20, 32, 20, 33,
	       22, 32, 22, 33, 23, 25, 23, 27, 23, 29,
	       23, 32, 23, 33, 24, 25, 24, 27, 24, 31,
	       25, 31, 26, 29, 26, 33, 27, 33, 28, 31,
	       28, 33, 29, 32, 29, 33, 30, 32, 30, 33,
	       31, 32, 31, 33, 32, 33,
	       -1);  
  
  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=*/ 0);
  
  no_of_edges=igraph_ecount(&g);
  for (i=0; i<no_of_edges; i++) {
    printf("%li ", (long int)VECTOR(edges)[i]);
  }
  printf("\n");
  
  for (i=0; i<no_of_edges; i++) {
    printf("%.2f ", VECTOR(eb)[i]);
  }
  printf("\n");

  /* 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=*/ 0);
  for (i=0; i<no_of_edges; i++) {
    printf("%.2f ", VECTOR(eb)[i]);
  }
  printf("\n");

  igraph_vector_destroy(&eb);
  igraph_vector_destroy(&edges);
  igraph_destroy(&g);
}
Exemplo n.º 20
0
int main() {
  
  igraph_vs_t vs;
  igraph_vit_t vit;
  igraph_t g;
  igraph_integer_t size;

  igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 1);
  igraph_vs_seq(&vs, 0, 9);
  igraph_vit_create(&g, vs, &vit);
  igraph_vs_size(&g, &vs, &size);
  printf("%li", (long int) size);
  
  while (!IGRAPH_VIT_END(vit)) {
    printf(" %li", (long int)IGRAPH_VIT_GET(vit));
    IGRAPH_VIT_NEXT(vit);
  }
  printf("\n");

  igraph_vit_destroy(&vit);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g);
  
  return 0;
}
/* call-seq:
 *   IGraph::GenerateRandom.cited_type_game(nodes,types,pref,edges_per_step_directed) -> IGraph
 *
 * Function to create a network based on some vertex categories. This function
 * creates a citation network, in each step a single vertex and edges_per_step
 * citating edges are added, nodes with different categories (may) have
 * different probabilities to get cited, as given by the pref vector.
 *
 * Note that this function might generate networks with multiple edges if
 * edges_per_step is greater than one. You might want to call
 * igraph_simplify() on the result to remove multiple edges.
 *
 * nodes: The number of vertices in the network.
 *
 * types: Numeric Array giving the categories of the vertices, so it should
 * contain nodes non-negative integer numbers. Types are numbered from zero.
 *
 * pref: The attractivity of the different vertex categories in an Array. Its
 * length should be the maximum element in types plus one (types are numbered
 * from zero).
 *
 * edges_per_step: Integer constant, the number of edges to add in each time
 * step.
 *
 * directed: Logical constant, whether to create a directed network.
 */
VALUE cIGraph_cited_type_game(VALUE self, VALUE nodes, VALUE types, VALUE pref, VALUE e_per_s, VALUE directed) {

    igraph_t *graph;
    VALUE new_graph;
    igraph_vector_t type_distv;
    igraph_vector_t prefv;
    int i;

    new_graph = cIGraph_alloc(cIGraph);
    Data_Get_Struct(new_graph, igraph_t, graph);

    igraph_vector_init(&type_distv,0);
    igraph_vector_init(&prefv,0);

    for(i=0; i<RARRAY_LEN(types); i++) {
        igraph_vector_push_back(&type_distv,NUM2DBL(RARRAY_PTR(types)[i]));
    }
    for(i=0; i<RARRAY_LEN(pref); i++) {
        igraph_vector_push_back(&prefv,NUM2DBL(RARRAY_PTR(pref)[i]));
    }

    igraph_destroy(graph);
    igraph_cited_type_game(graph, NUM2INT(nodes),
                           &type_distv,
                           &prefv,
                           NUM2INT(e_per_s),
                           directed == Qtrue ? 1: 0);

    igraph_vector_destroy(&type_distv);
    igraph_vector_destroy(&prefv);

    return new_graph;


}
Exemplo n.º 22
0
int main() {
  
  igraph_t graph;
  igraph_vector_ptr_t separators;
  long int i, n;
  
  igraph_famous(&graph, "zachary");
  igraph_vector_ptr_init(&separators, 0);
  igraph_all_minimal_st_separators(&graph, &separators);

  n=igraph_vector_ptr_size(&separators);
  for (i=0; i<n; i++) {
    igraph_bool_t res;
    igraph_vector_t *sep=VECTOR(separators)[i];
    igraph_is_separator(&graph, igraph_vss_vector(sep), &res);
    if (!res) { 
      printf("Vertex set %li is not a separator!\n", i);
      igraph_vector_print(sep);
      return 1;
    }
  }

  igraph_destroy(&graph);
  for (i=0; i<n; i++) {
    igraph_vector_t *v=VECTOR(separators)[i];
    igraph_vector_destroy(v);
    igraph_Free(v);
  }
  igraph_vector_ptr_destroy(&separators);
  
  return 0;
}
Exemplo n.º 23
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;
}
Exemplo n.º 24
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;
}
Exemplo n.º 25
0
int main() {
  
  igraph_t g;
  igraph_integer_t result;
  igraph_vector_t edges, res;
  igraph_vs_t vids;
  long i, n;
  
  igraph_tree(&g, 10, 3, IGRAPH_TREE_OUT);
  igraph_rewire(&g, 1000, IGRAPH_REWIRING_SIMPLE);
  
  n=igraph_vcount(&g);
  igraph_vector_init(&res, 0);
  igraph_degree(&g, &res, igraph_vss_all(), IGRAPH_IN, 0);
  for (i=0; i<n; i++)
    printf("%ld ", (long)igraph_vector_e(&res, i));
  printf("\n");
  
  igraph_degree(&g, &res, igraph_vss_all(), IGRAPH_OUT, 0);
  for (i=0; i<n; i++)
    printf("%ld ", (long)igraph_vector_e(&res, i));
  printf("\n");
    
  igraph_destroy(&g);
  igraph_vector_destroy(&res);
  
  return 0;
}
int test_unnormalized_laplacian(igraph_bool_t dir) {
  igraph_t g;
  igraph_matrix_t m;
  igraph_vector_t vec;
  igraph_matrix_init(&m, 1, 1);

  /* No loop or multiple edges */
  igraph_ring(&g, 5, dir, 0, 1);
  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);
  printf("===\n");

  /* Add some loop edges */
  igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);
  printf("===\n");

  /* Duplicate some edges */
  igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0);
  igraph_add_edges(&g, &vec, 0);
  igraph_vector_destroy(&vec);

  igraph_laplacian(&g, &m, 0);
  print_matrix(&m);

  igraph_destroy(&g);

  igraph_matrix_destroy(&m);

  return 0;
}
int main() {

  igraph_t g;
  igraph_vector_ptr_t vecs;
  long int i;
  igraph_vs_t vs;

  igraph_ring(&g, 10, IGRAPH_DIRECTED, 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(&g, &vecs, 0, vs, 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);
  
  return 0;
}
Exemplo n.º 28
0
int main() {
  
  igraph_t g;
  igraph_vector_t y;  

  /* turn on attribute handling */
  igraph_i_set_attribute_table(&igraph_cattribute_table);

  /* Create a graph, add some attributes and save it as a GraphML file */
  igraph_famous(&g, "Petersen");
  SETGAS(&g, "name", "Petersen's graph");
  SETGAN(&g, "vertices", igraph_vcount(&g));
  SETGAN(&g, "edges", igraph_ecount(&g));

  igraph_vector_init_seq(&y, 1, igraph_vcount(&g));
  SETVANV(&g, "id", &y);
  igraph_vector_destroy(&y);

  SETVAS(&g, "name", 0, "foo");
  SETVAS(&g, "name", 1, "foobar");
  
  igraph_vector_init_seq(&y, 1, igraph_ecount(&g));
  SETEANV(&g, "id", &y);
  igraph_vector_destroy(&y);

  SETEAS(&g, "name", 0, "FOO");
  SETEAS(&g, "name", 1, "FOOBAR");

  igraph_write_graph_gml(&g, stdout, 0, "");
  igraph_write_graph_graphml(&g, stdout);
   
  igraph_destroy(&g);
  
  return 0;
}
Exemplo n.º 29
0
Arquivo: scan.c Projeto: abeham/igraph
int igraph_local_scan_0_them(const igraph_t *us, const igraph_t *them,
			     igraph_vector_t *res,
			     const igraph_vector_t *weights_them,
			     igraph_neimode_t mode) {

  igraph_t is;

  if (igraph_vcount(us) != igraph_vcount(them)) {
    IGRAPH_ERROR("Number of vertices don't match in scan-0", IGRAPH_EINVAL);
  }
  if (igraph_is_directed(us) != igraph_is_directed(them)) {
    IGRAPH_ERROR("Directedness don't match in scan-0", IGRAPH_EINVAL);
  }

  if (weights_them) {
    return igraph_i_local_scan_0_them_w(us, them, res, weights_them, mode);
  }

  igraph_intersection(&is, us, them, /*edgemap1=*/ 0, /*edgemap2=*/ 0);
  IGRAPH_FINALLY(igraph_destroy, &is);

  igraph_degree(&is, res, igraph_vss_all(), mode, IGRAPH_LOOPS);

  igraph_destroy(&is);
  IGRAPH_FINALLY_CLEAN(1);

  return 0;
}
Exemplo n.º 30
0
int main() {
  
  igraph_t g;
  igraph_vs_t vertices;
  igraph_vector_t result;
  long int i;
  
  igraph_vs_seq(&vertices, 1, 101);
  igraph_barabasi_game(&g, 100000, /*power=*/ 1, 3, 0, 0, /*A=*/ 1,
		       IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, 
		       /*start_from=*/ 0);
  igraph_vector_init(&result, 0);
  
  for (i=0; i<1; i++) {
    igraph_transitivity_local_undirected2(&g, &result, igraph_vss_all(),
			IGRAPH_TRANSITIVITY_NAN);
  }

  for (i=0; i<1; i++) {
    igraph_transitivity_local_undirected4(&g, &result, igraph_vss_all(),
			IGRAPH_TRANSITIVITY_NAN);
  }
  
/*   for (i=0; i<igraph_vector_size(&result); i++) { */
/*     printf("%f ", VECTOR(result)[i]); */
/*   } */
/*   printf("\n"); */
  
  igraph_vector_destroy(&result);
  igraph_vs_destroy(&vertices);
  igraph_destroy(&g);
  
  return 0;
}