示例#1
0
/* Erdos-Renyi : G(n,p)
*/
igraph_t *ggen_generate_erdos_gnp(gsl_rng *r, unsigned long n, double p)
{
    igraph_matrix_t m;
    igraph_t *g = NULL;
    int err;
    unsigned long i,j;

    ggen_error_start_stack();
    if(r == NULL)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    if(p < 0.0 || p > 1.0)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    g = malloc(sizeof(igraph_t));
    GGEN_CHECK_ALLOC(g);
    GGEN_FINALLY3(free,g,1);

    if(p == 0.0)
    {
        GGEN_CHECK_IGRAPH(igraph_empty(g,n,1));
        goto end;
    }
    if(p == 1.0)
    {
        GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1));
        goto end;
    }

    GGEN_CHECK_IGRAPH(igraph_matrix_init(&m,n,n));
    GGEN_FINALLY(igraph_matrix_destroy,&m);

    for(i = 0; i < n; i++)
        for(j = 0; j < n; j++)
            if(i < j)
                // coin flipping to determine if we add an edge or not
                igraph_matrix_set(&m,i,j,gsl_ran_bernoulli(r,p));
            else
                igraph_matrix_set(&m,i,j,0);

    GGEN_CHECK_IGRAPH(igraph_adjacency(g,&m,IGRAPH_ADJ_DIRECTED));
end:
    ggen_error_clean(1);
    return g;
ggen_error_label:
    return NULL;
}
示例#2
0
/* Erdos-Renyi : G(n,M)
*/
igraph_t *ggen_generate_erdos_gnm(gsl_rng *r, unsigned long n, unsigned long m)
{
    igraph_matrix_t adj;
    igraph_t *g = NULL;
    int err;
    unsigned long i,j;
    unsigned long added_edges = 0;

    ggen_error_start_stack();
    if(r == NULL)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    if(m > (n*(n-1)/2))
        GGEN_SET_ERRNO(GGEN_EINVAL);

    g = malloc(sizeof(igraph_t));
    GGEN_CHECK_ALLOC(g);
    GGEN_FINALLY3(free,g,1);

    if(m == 0 || n <= 1)
    {
        GGEN_CHECK_IGRAPH(igraph_empty(g,n,1));
        goto end;
    }
    if(m == (n*(n-1))/2)
    {
        GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1));
        goto end;
    }

    GGEN_CHECK_IGRAPH(igraph_matrix_init(&adj,n,n));
    GGEN_FINALLY(igraph_matrix_destroy,&adj);

    igraph_matrix_null(&adj);

    while(added_edges < m) {
        GGEN_CHECK_GSL_DO(i = gsl_rng_uniform_int(r,n));
        GGEN_CHECK_GSL_DO(j = gsl_rng_uniform_int(r,n));

        if(i < j && igraph_matrix_e(&adj,i,j) == 0)
        {
            igraph_matrix_set(&adj,i,j,1);
            added_edges++;
        }

    }

    GGEN_CHECK_IGRAPH(igraph_adjacency(g,&adj,IGRAPH_ADJ_DIRECTED));
end:
    ggen_error_clean(1);
    return g;
ggen_error_label:
    return NULL;
}
示例#3
0
/* Random Orders Method :
*/
igraph_t * ggen_generate_random_orders(gsl_rng *r, unsigned long n, unsigned int orders)
{
    igraph_t *g = NULL;
    igraph_matrix_t m,edge_validity;
    int err = 0;
    long long i = 0,j,k;
    igraph_vector_ptr_t posets;
    igraph_vector_ptr_t indexes;
    igraph_vector_t *v,*w;

    ggen_error_start_stack();
    if(r == NULL)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    if(orders == 0)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    // init structures
    g = malloc(sizeof(igraph_t));
    GGEN_CHECK_ALLOC(g);
    GGEN_FINALLY3(free,g,1);

    GGEN_CHECK_IGRAPH(igraph_matrix_init(&m,n,n));
    GGEN_FINALLY(igraph_matrix_destroy,&m);

    GGEN_CHECK_IGRAPH(igraph_matrix_init(&edge_validity,n,n));
    GGEN_FINALLY(igraph_matrix_destroy,&edge_validity);

    GGEN_CHECK_IGRAPH(igraph_vector_ptr_init(&posets,orders));
    IGRAPH_VECTOR_PTR_SET_ITEM_DESTRUCTOR(&posets,igraph_vector_destroy);
    GGEN_FINALLY(igraph_vector_ptr_destroy_all,&posets);

    GGEN_CHECK_IGRAPH(igraph_vector_ptr_init(&indexes,orders));
    IGRAPH_VECTOR_PTR_SET_ITEM_DESTRUCTOR(&indexes,igraph_vector_destroy);
    GGEN_FINALLY(igraph_vector_ptr_destroy_all,&indexes);

    for(i = 0; i < orders; i++)
    {
        // posets is used for permutation computations
        // it should contain vertices
        VECTOR(posets)[i] = calloc(1,sizeof(igraph_vector_t));
        GGEN_CHECK_ALLOC(VECTOR(posets)[i]);
        GGEN_CHECK_IGRAPH_VECTPTR(igraph_vector_init_seq(VECTOR(posets)[i],0,n-1),posets,i);

        VECTOR(indexes)[i] = calloc(1,sizeof(igraph_vector_t));
        GGEN_CHECK_ALLOC(VECTOR(indexes)[i]);
        GGEN_CHECK_IGRAPH_VECTPTR(igraph_vector_init(VECTOR(indexes)[i],n),indexes,i);
    }

    // zero all structs
    igraph_matrix_null(&m);
    igraph_matrix_null(&edge_validity);

    // use gsl to shuffle each poset
    for( j = 0; j < orders; j++)
    {
        v = VECTOR(posets)[j];
        GGEN_CHECK_GSL_DO(gsl_ran_shuffle(r,VECTOR(*v), n, sizeof(VECTOR(*v)[0])));
    }
    // index saves the indices of each vertex in each permutation
    for( i = 0; i < orders; i++)
        for( j = 0; j < n; j++)
        {
            v = VECTOR(posets)[i];
            w = VECTOR(indexes)[i];
            k = VECTOR(*v)[j];
            VECTOR(*w)[k] = j;
        }

    // edge_validity count if an edge is in all permutations
    for( i = 0; i < n; i++)
        for( j = 0; j < n; j++)
            for( k = 0; k < orders; k++)
            {
                v = VECTOR(indexes)[k];
                if( VECTOR(*v)[i] < VECTOR(*v)[j])
                    igraph_matrix_set(&edge_validity,i,j,
                                      igraph_matrix_e(&edge_validity,i,j)+1);
            }

    // if an edge is present in all permutations then add it to the graph
    for( i = 0; i < n; i++)
        for( j = 0; j < n; j++)
            if(igraph_matrix_e(&edge_validity,i,j) == orders)
                igraph_matrix_set(&m,i,j,1);

    // translate the matrix to a graph
    GGEN_CHECK_IGRAPH(igraph_adjacency(g,&m,IGRAPH_ADJ_DIRECTED));

    ggen_error_clean(1);
    return g;
ggen_error_label:
    return NULL;
}
示例#4
0
igraph_t *ggen_generate_erdos_lbl(gsl_rng *r, unsigned long n, double p, unsigned long nbl)
{
    igraph_t *g = NULL;
    igraph_matrix_t m;
    igraph_vector_t layers;
    unsigned long i,j;
    int err;

    ggen_error_start_stack();
    if(r == NULL)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    if(p < 0.0 || p > 1.0)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    if(nbl > n || nbl == 0)
        GGEN_SET_ERRNO(GGEN_EINVAL);

    g = malloc(sizeof(igraph_t));
    GGEN_CHECK_ALLOC(g);
    GGEN_FINALLY3(free,g,1);

    if(p == 0.0)
    {
        GGEN_CHECK_IGRAPH(igraph_empty(g,n,1));
        goto end;
    }
    if(p == 1.0 && nbl == n)
    {
        GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1));
        goto end;
    }

    GGEN_CHECK_IGRAPH(igraph_matrix_init(&m,n,n));
    GGEN_FINALLY(igraph_matrix_destroy,&m);

    GGEN_CHECK_IGRAPH(igraph_vector_init(&layers,n));
    GGEN_FINALLY(igraph_vector_destroy,&layers);

    // asign to each vertex a layer
    for(i = 0; i < n; i++)
    {
        GGEN_CHECK_GSL_DO(j = gsl_rng_uniform_int(r,nbl));
        VECTOR(layers)[i] = j;
    }

    // create edges
    for(i = 0; i < n; i++)
        for(j = 0; j < n; j++)
            // if the layer allocation allows the edge, we test for it
            if(VECTOR(layers)[i]<VECTOR(layers)[j])
                igraph_matrix_set(&m,i,j,gsl_ran_bernoulli(r,p));
            else
                igraph_matrix_set(&m,i,j,0);

    //translate the matrix to a graph
    GGEN_CHECK_IGRAPH(igraph_adjacency(g,&m,IGRAPH_ADJ_DIRECTED));
end:
    ggen_error_clean(1);
    return g;
ggen_error_label:
    return NULL;
}
示例#5
0
int main() {
  igraph_matrix_t m, m2;
  igraph_vector_t v;
  long int i, j, i2, j2;
  igraph_real_t r1, r2;

  igraph_matrix_init(&m, 4, 3);
  byrow(&m);
  
  /* igraph_matrix_e */
  printf("igraph_matrix_e\n");
  apply(m, printf("%i ", (int)igraph_matrix_e(&m, i, j)), printf("\n"));

  /* igraph_matrix_e_ptr */
  printf("igraph_matrix_e_ptr\n");
  apply(m, printf("%i ", (int)igraph_matrix_e_ptr(&m, i, j)[0]), printf("\n"));

  /* igraph_matrix_set */
  printf("igraph_matrix_set\n");
  apply(m, igraph_matrix_set(&m, i, j, i), (void) 0 );
  print_matrix(&m);
  apply(m, igraph_matrix_set(&m, i, j, j), (void) 0 );
  print_matrix(&m);

  /* igraph_matrix_fill */
  printf("igraph_matrix_fill\n");
  igraph_matrix_fill(&m, 42);
  print_matrix(&m);
  igraph_matrix_fill(&m, -42.1);
  print_matrix(&m);
  
  /* igraph_matrix_update */
  printf("igraph_matrix_update\n");
  igraph_matrix_init(&m2, 0, 0);
  byrow(&m);
  igraph_matrix_update(&m2, &m);
  print_matrix(&m2);

  /* igraph_matrix_rbind */
  printf("igraph_matrix_rbind\n");
  igraph_matrix_rbind(&m2, &m);
  print_matrix(&m2);
  printf("\n");
  igraph_matrix_resize(&m, 0, igraph_matrix_ncol(&m2));
  igraph_matrix_rbind(&m2, &m);
  print_matrix(&m2);
  printf("\n");
  igraph_matrix_rbind(&m, &m2);
  print_matrix(&m);

  /* igraph_matrix_cbind */
  printf("igraph_matrix_cbind\n");
  igraph_matrix_resize(&m, 4, 3);
  igraph_matrix_resize(&m2, 4, 2);
  byrow(&m);
  byrow(&m2);
  igraph_matrix_cbind(&m, &m2);
  print_matrix(&m);

  /* igraph_matrix_swap */
  printf("igraph_matrix_swap\n");
  igraph_matrix_update(&m, &m2);
  igraph_matrix_null(&m);
  igraph_matrix_swap(&m, &m2);
  print_matrix(&m);
  print_matrix(&m2);
  
  /* igraph_matrix_get_row */
  /* igraph_matrix_set_row */
  printf("igraph_matrix_get_row\n");
  printf("igraph_matrix_set_row\n");
  igraph_vector_init(&v, 0);
  for (i=0; i<igraph_matrix_nrow(&m); i++) {
    igraph_matrix_get_row(&m, &v, i);
    igraph_matrix_set_row(&m2, &v, i);
  }
  print_matrix(&m2);

  /* igraph_matrix_set_col */
  printf("igraph_matrix_set_col\n");
  igraph_matrix_null(&m2);
  for (i=0; i<igraph_matrix_ncol(&m); i++) {
    igraph_matrix_get_col(&m, &v, i);
    igraph_matrix_set_col(&m2, &v, i);
  }
  print_matrix(&m2);
  
  /* igraph_matrix_swap_rows */
  printf("igraph_matrix_swap_rows\n");
  igraph_matrix_swap_rows(&m2, 0, 0);
  igraph_matrix_swap_rows(&m2, 0, 2);
  print_matrix(&m2);
  
  /* igraph_matrix_swap_cols */
  printf("igraph_matrix_swap_cols\n");
  igraph_matrix_swap_cols(&m2, 0, 0);
  igraph_matrix_swap_cols(&m2, 0, 1);
  print_matrix(&m2);

  /* igraph_matrix_add_constant */
  printf("igraph_matrix_add_constant\n");
  igraph_matrix_add_constant(&m2, 0);
  print_matrix(&m2);  
  igraph_matrix_add_constant(&m2, -1);
  print_matrix(&m2);
  
  /* igraph_matrix_add */
  printf("igraph_matrix_add\n");
  byrow(&m2);
  byrow(&m);
  igraph_matrix_add(&m2, &m);
  print_matrix(&m2);

  /* igraph_matrix_sub */
  printf("igraph_matrix_sub\n");
  igraph_matrix_sub(&m2, &m);
  print_matrix(&m2);

  /* igraph_matrix_mul_elements */
  printf("igraph_matrix_mul_elements\n");
  igraph_matrix_mul_elements(&m2, &m);
  print_matrix(&m2);

  /* igraph_matrix_div_elements */
  printf("igraph_matrix_div_elements\n");
  igraph_matrix_fill(&m, 2);
  igraph_matrix_div_elements(&m2, &m);
  print_matrix(&m2);

  /* igraph_matrix_min */
  printf("igraph_matrix_min\n");
  if (igraph_matrix_min(&m2) != 0) {
    return 1;
  }
  if (igraph_matrix_min(&m) != 2) {
    return 1;
  }

  /* igraph_matrix_which_min */
  printf("igraph_matrix_which_min\n");
  igraph_matrix_which_min(&m2, &i, &j);
  if (i != 0 || j != 0) { return 2; }
  MATRIX(m2,0,1) = -1;
  igraph_matrix_which_min(&m2, &i, &j);
  if (i != 0 || j != 1) { return 2; }
  MATRIX(m2,3,1) = -2;
  igraph_matrix_which_min(&m2, &i, &j);
  if (i != 3 || j != 1) { return 2; }

  /* igraph_matrix_which_max */
  printf("igraph_matrix_which_max\n");
  MATRIX(m2,3,0) = 100;
  igraph_matrix_which_max(&m2, &i, &j);
  if (i != 3 || j != 0) { return 3; }
  
  /* igraph_matrix_minmax */
  printf("igraph_matrix_minmax\n");
  igraph_matrix_minmax(&m2, &r1, &r2);
  printf("%g %g\n", r1, r2);
  
  /* igraph_matrix_which_minmax */
  printf("igraph_matrix_which_minmax\n");
  igraph_matrix_which_minmax(&m2, &i, &j, &i2, &j2);
  if (i != 3 || j != 1 || i2 != 3 || j2 != 0) { return 4; }

  /* igraph_matrix_isnull */
  printf("igraph_matrix_isnull\n");
  if (igraph_matrix_isnull(&m2)) { return 5; }
  igraph_matrix_null(&m);
  if (!igraph_matrix_isnull(&m)) { return 5; }
  igraph_matrix_resize(&m2, 5, 0);
  if (!igraph_matrix_isnull(&m2)) { return 5; }  
  
  /* igraph_matrix_empty */
  printf("igraph_matrix_empty\n");
  if (!igraph_matrix_empty(&m2)) { return 6; }
  igraph_matrix_resize(&m2, 5, 5);
  if (igraph_matrix_empty(&m2)) { return 6; }

  /* igraph_matrix_is_symmetric */
  printf("igraph_matrix_is_symmetric\n");
  byrow(&m2);
  if (igraph_matrix_is_symmetric(&m2)) { return 7; }
  igraph_matrix_update(&m, &m2);
  igraph_matrix_transpose(&m);
  igraph_matrix_add(&m, &m2);
  if (!igraph_matrix_is_symmetric(&m)) { return 7; }

  /* igraph_matrix_prod */
  printf("igraph_matrix_prod\n");
  igraph_matrix_resize(&m, 3,2);
  byrow(&m);
  igraph_matrix_add_constant(&m, 1);
  print_matrix(&m);
  printf("product: %g\n", igraph_matrix_prod(&m));

  /* igraph_matrix_rowsum */
  printf("igraph_matrix_rowsum\n");
  igraph_matrix_rowsum(&m, &v);
  print_vector(&v);

  /* igraph_matrix_colsum */
  printf("igraph_matrix_colsum\n");
  igraph_matrix_colsum(&m, &v);
  print_vector(&v);

  /* igraph_matrix_contains */
  printf("igraph_matrix_contains\n");
  if (igraph_matrix_contains(&m, 0)) { return 8; }
  if (igraph_matrix_contains(&m, 6.0001)) { return 8; }
  if (igraph_matrix_contains(&m, 7)) { return 8; }
  if (!igraph_matrix_contains(&m, 1)) { return 8; }
  if (!igraph_matrix_contains(&m, 6)) { return 8; }
  
  /* igraph_matrix_search */
  printf("igraph_matrix_search\n");
  if (!igraph_matrix_search(&m, 0, 6.0, &i2, &i, &j)) { return 9; }
  if (i2 != 5 || i != 2 || j != 1) { return 9; }
  
  /* igraph_matrix_remove_row */
  printf("igraph_matrix_remove_row\n");
  igraph_matrix_remove_row(&m, 1);
  print_matrix(&m);
  igraph_matrix_resize(&m,5,4);
  byrow(&m);
  igraph_matrix_remove_row(&m, 4);
  print_matrix(&m);
  igraph_matrix_remove_row(&m, 0);
  print_matrix(&m);

  /* igraph_matrix_select_cols */
  printf("igraph_matrix_select_cols\n");
  igraph_matrix_resize(&m, 6, 5);
  apply(m, igraph_matrix_set(&m, i, j, j), (void) 0 );
  igraph_vector_resize(&v, 3);
  VECTOR(v)[0]=0; VECTOR(v)[1]=4; VECTOR(v)[2]=2;
  igraph_matrix_select_cols(&m, &m2, &v);
  print_matrix(&m2);
  igraph_vector_resize(&v, 1);
  igraph_matrix_select_cols(&m, &m2, &v);
  print_matrix(&m2);
  igraph_vector_clear(&v);
  igraph_matrix_select_cols(&m, &m2, &v);
  if (!igraph_matrix_empty(&m2)) { return 9; }

  igraph_vector_destroy(&v);
  igraph_matrix_destroy(&m2);
  igraph_matrix_destroy(&m);

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

  return 0;
}