int main() { igraph_matrix_t A; igraph_vector_t values; igraph_matrix_t vectors; int i, j; igraph_eigen_which_t which; igraph_rng_seed(igraph_rng_default(), 42 * 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors, 0, 0); igraph_vector_init(&values, 0); /* All eigenvalues and eigenvectors */ for (i=0; i<DIM; i++) { for (j=i; j<DIM; j++) { MATRIX(A, i, j) = MATRIX(A, j, i) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } which.pos=IGRAPH_EIGEN_LM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.howmany=8; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_BE; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_SM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); igraph_vector_destroy(&values); igraph_matrix_destroy(&vectors); igraph_matrix_destroy(&A); return 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 }
int main () { long int i; igraph_matrix_t m; igraph_real_t x, y, z, r; srand(time(0)); /* 2D */ igraph_matrix_init(&m, 1000, 2); for (i=0; i<igraph_matrix_nrow(&m); i++) { MATRIX(m,i,0)=rand()/(double)RAND_MAX; MATRIX(m,i,1)=rand()/(double)RAND_MAX; } igraph_i_layout_sphere_2d(&m, &x, &y, &r); for (i=0; i<igraph_matrix_nrow(&m); i++) { igraph_real_t dist=sqrt((MATRIX(m,i,0)-x)*(MATRIX(m,i,0)-x) + (MATRIX(m,i,1)-y)*(MATRIX(m,i,1)-y)); if (dist > r) { printf("x: %f y: %f r: %f\n", x, y, r); printf("x: %f y: %f dist: %f (%li)\n", MATRIX(m,i,0), MATRIX(m,i,1), dist, i); return 1; } } igraph_matrix_destroy(&m); /* 3D */ igraph_matrix_init(&m, 1000, 3); for (i=0; i<igraph_matrix_nrow(&m); i++) { MATRIX(m,i,0)=rand()/(double)RAND_MAX; MATRIX(m,i,1)=rand()/(double)RAND_MAX; MATRIX(m,i,2)=rand()/(double)RAND_MAX; } igraph_i_layout_sphere_3d(&m, &x, &y, &z, &r); for (i=0; i<igraph_matrix_nrow(&m); i++) { igraph_real_t dist=sqrt((MATRIX(m,i,0)-x)*(MATRIX(m,i,0)-x) + (MATRIX(m,i,1)-y)*(MATRIX(m,i,1)-y) + (MATRIX(m,i,2)-z)*(MATRIX(m,i,2)-z)); if (dist > r) { printf("x: %f y: %f z: %f r: %f\n", x, y, z, r); printf("x: %f y: %f z: %f dist: %f (%li)\n", MATRIX(m,i,0), MATRIX(m,i,1), MATRIX(m,i,2), dist, i); return 1; } } igraph_matrix_destroy(&m); return 0; }
int main() { igraph_matrix_t A; igraph_matrix_t vectors_left, vectors_right; igraph_vector_t values_real, values_imag; int i, j; int info=1; int ilo, ihi; igraph_real_t abnrm; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors_left, 0, 0); igraph_matrix_init(&vectors_right, 0, 0); igraph_vector_init(&values_real, 0); igraph_vector_init(&values_imag, 0); for (i=0; i<DIM; i++) { for (j=0; j<DIM; j++) { MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_lapack_dgeevx(IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH, &A, &values_real, &values_imag, &vectors_left, &vectors_right, &ilo, &ihi, /*scale=*/ 0, &abnrm, /*rconde=*/ 0, /*rcondv=*/ 0, &info); if (check_ev(&A, &values_real, &values_imag, &vectors_left, &vectors_right, /*tol=*/ 1e-8)) { return 1; } /* igraph_matrix_print(&A); */ /* igraph_vector_print(&values_real); */ /* igraph_vector_print(&values_imag); */ /* igraph_matrix_print(&vectors_left); */ /* igraph_matrix_print(&vectors_right); */ igraph_vector_destroy(&values_imag); igraph_vector_destroy(&values_real); igraph_matrix_destroy(&vectors_right); igraph_matrix_destroy(&vectors_left); igraph_matrix_destroy(&A); return 0; }
/* call-seq: * graph.get_adjacency(type) -> Array * * Returns the adjacency matrix of a graph * */ VALUE cIGraph_get_adjacency(VALUE self, VALUE mode){ igraph_t *graph; igraph_get_adjacency_t pmode = NUM2INT(mode); igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n; Data_Get_Struct(self, igraph_t, graph); n = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n,n); igraph_get_adjacency(graph,&res,pmode); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = INT2NUM(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_matrix_destroy(&res); return matrix; }
int main() { const int nodes=10, skip=3; igraph_matrix_t mat2; igraph_vector_complex_t values, values2; igraph_matrix_complex_t vectors, vectors2; igraph_eigen_which_t which; int i; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&mat2, nodes, nodes); for (i=0; i<nodes; i++) { int j; for (j=0; j<nodes; j++) { MATRIX(mat2, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_vector_complex_init(&values, 0); igraph_matrix_complex_init(&vectors, 0, 0); which.pos=IGRAPH_EIGEN_LI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_complex_init(&values2, 0); igraph_matrix_complex_init(&vectors2, 0, 0); which.pos=IGRAPH_EIGEN_SI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values2, &vectors2); igraph_vector_complex_print(&values); igraph_vector_complex_print(&values2); for (i=0; i<nodes; i++) { int j; igraph_real_t d= igraph_complex_abs(igraph_complex_sub(VECTOR(values)[i], VECTOR(values2)[nodes-i-1])); if (d > 1e-15) { DUMP(); return 2; } for (j=0; j<nodes; j++) { igraph_real_t d= igraph_complex_abs(igraph_complex_sub(MATRIX(vectors, j, i), MATRIX(vectors2, j, nodes-i-1))); if (d > 1e-15) { DUMP(); return 3; } } } igraph_vector_complex_destroy(&values); igraph_matrix_complex_destroy(&vectors); igraph_vector_complex_destroy(&values2); igraph_matrix_complex_destroy(&vectors2); igraph_matrix_destroy(&mat2); return 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; }
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; }
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; }
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; }
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; }
/* 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; }
/* call-seq: * graph.dijkstra_shortest_paths(varray,weights,mode) -> Array * * Calculates the length of the shortest paths from each of the vertices in * the varray Array to all of the other vertices in the graph given a set of * edge weights given in the weights Array. The result * is returned as an Array of Array. Each top-level Array contains the results * for a vertex in the varray Array. Each entry in the Array is the path length * to another vertex in the graph in vertex order (the order the vertices were * added to the graph. (This should probalby be changed to give a Hash of Hash * to allow easier look up.) */ VALUE cIGraph_dijkstra_shortest_paths(VALUE self, VALUE from, VALUE weights, VALUE mode){ igraph_t *graph; igraph_vs_t vids; igraph_vector_t vidv; igraph_vector_t wghts; igraph_neimode_t pmode = NUM2INT(mode); igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n_row; int n_col; Data_Get_Struct(self, igraph_t, graph); n_row = NUM2INT(rb_funcall(from,rb_intern("length"),0)); n_col = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n_row,n_col); igraph_vector_init(&wghts,RARRAY_LEN(weights)); for(i=0;i<RARRAY_LEN(weights);i++){ VECTOR(wghts)[i] = NUM2DBL(RARRAY_PTR(weights)[i]); } //Convert an array of vertices to a vector of vertex ids igraph_vector_init_int(&vidv,0); cIGraph_vertex_arr_to_id_vec(self,from,&vidv); //create vertex selector from the vecotr of ids igraph_vs_vector(&vids,&vidv); igraph_dijkstra_shortest_paths(graph,&res,vids,&wghts,pmode); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = MATRIX(res,i,j) == n_col ? Qnil : rb_float_new(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_vector_destroy(&vidv); igraph_matrix_destroy(&res); igraph_vs_destroy(&vids); igraph_vector_destroy(&wghts); return matrix; }
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; }
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; }
VALUE cIGraph_matrix_alloc(VALUE klass){ igraph_matrix_t *m = malloc(sizeof(igraph_matrix_t)); VALUE obj; igraph_matrix_init(m, 0, 0); obj = Data_Wrap_Struct(klass, 0, cIGraph_matrix_free, m); return obj; }
/* call-seq: * graph.layout_random -> IGraphMatrix * * Returns a random layout in 3D. */ VALUE cIGraph_layout_random_3d(VALUE self) { igraph_t *graph; igraph_matrix_t *res = malloc(sizeof(igraph_matrix_t)); Data_Get_Struct(self, igraph_t, graph); igraph_matrix_init(res,0,0); igraph_layout_random_3d(graph,res); return Data_Wrap_Struct(cIGraphMatrix, 0, cIGraph_matrix_free, res); }
symmetric_matrix<double, lower> Graph::adjacency() const { double x; int i,j; igraph_matrix_t m; symmetric_matrix<double, lower> adjmatrix(size, size); igraph_matrix_init(&m, size, size); igraph_get_adjacency(graph, &m,IGRAPH_GET_ADJACENCY_LOWER); /* IGRAPH_GET_ADJACENCY_BOTH);*/ for(i = 0; i < size; i++){ for(j = 0; j <=i; j++) { x = MATRIX(m,i,j) ? 1 : 0; adjmatrix(i,j) = x; } } igraph_matrix_destroy(&m); return adjmatrix; }
/* 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; }
/* call-seq: * graph.cocitation(varray) -> Array * * Cocitation coupling. * * Two vertices are cocited if there is another vertex citing both of them. * igraph_cocitation() simply counts how many types two vertices are cocited. * The cocitation score for each given vertex and all other vertices in the * graph will be calculated. * */ VALUE cIGraph_cocitation(VALUE self, VALUE vs){ igraph_t *graph; igraph_vs_t vids; igraph_vector_t vidv; igraph_matrix_t res; int i; int j; VALUE row; VALUE path_length; VALUE matrix = rb_ary_new(); int n_row; int n_col; Data_Get_Struct(self, igraph_t, graph); n_row = NUM2INT(rb_funcall(vs,rb_intern("length"),0)); n_col = igraph_vcount(graph); //matrix to hold the results of the calculations igraph_matrix_init(&res,n_row,n_col); //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); igraph_cocitation(graph,&res,vids); for(i=0; i<igraph_matrix_nrow(&res); i++){ row = rb_ary_new(); rb_ary_push(matrix,row); for(j=0; j<igraph_matrix_ncol(&res); j++){ path_length = INT2NUM(MATRIX(res,i,j)); rb_ary_push(row,path_length); } } igraph_vector_destroy(&vidv); igraph_matrix_destroy(&res); igraph_vs_destroy(&vids); return matrix; }
int main() { igraph_matrix_t m; igraph_matrix_init(&m, 10, 10); if (igraph_matrix_capacity(&m) != 100) { return 1; } igraph_matrix_add_cols(&m, 5); igraph_matrix_resize(&m, 5, 5); igraph_matrix_resize_min(&m); if (igraph_matrix_capacity(&m) != igraph_matrix_size(&m)) { return 2; } igraph_matrix_destroy(&m); return 0; }
int main() { igraph_t graph; igraph_matrix_t coords; int i; igraph_matrix_init(&coords, 0, 0); for (i=0; i<10; i++) { igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, /*n=*/ 100, /*p=*/ 2.0/100, IGRAPH_UNDIRECTED, /*loops=*/ 0); igraph_layout_mds(&graph, &coords, /*dist=*/ 0, /*dim=*/ 2, /*options=*/ 0); igraph_destroy(&graph); } igraph_matrix_destroy(&coords); return 0; }
int main() { igraph_matrix_t mat; igraph_sparsemat_t spmat, spmat2; int i; igraph_real_t m1, m2; igraph_rng_seed(igraph_rng_default(), 42); igraph_sparsemat_init(&spmat, DIM1, DIM2, 20); igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); for (i=0; i<10; i++) { igraph_sparsemat_entry(&spmat, INT(DIM1-1), INT(DIM2-1), 1.0); } igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); igraph_sparsemat_compress(&spmat, &spmat2); igraph_matrix_init(&mat, 0, 0); igraph_sparsemat_as_matrix(&mat, &spmat2); m1=igraph_sparsemat_min(&spmat2); m2=igraph_matrix_min(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 1; } m1=igraph_sparsemat_max(&spmat2); m2=igraph_matrix_max(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 2; } igraph_sparsemat_minmax(&spmat2, &m1, &m2); if (m1 != igraph_matrix_min(&mat)) { return 3; } if (m2 != igraph_matrix_max(&mat)) { return 4; } igraph_matrix_destroy(&mat); igraph_sparsemat_destroy(&spmat); igraph_sparsemat_destroy(&spmat2); return 0; }
VALUE cIGraph_community_walktrap(VALUE self, VALUE weights, VALUE steps){ igraph_t *graph; igraph_vector_t weights_vec; 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(&weights_vec,0); igraph_vector_init(&modularity,0); for(i=0;i<RARRAY_LEN(weights);i++){ VECTOR(weights_vec)[i] = NUM2DBL(RARRAY_PTR(weights)[i]); } igraph_community_walktrap(graph, igraph_vector_size(&weights_vec) > 0 ? &weights_vec : NULL, NUM2INT(steps),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(&weights_vec); igraph_vector_destroy(&modularity); return res; }
int main() { igraph_t g; igraph_matrix_t m; igraph_small(&g, 0, IGRAPH_DIRECTED, 0,1, 2,1, 2,0, 3,0, -1); igraph_matrix_init(&m, 0, 0); igraph_bibcoupling(&g, &m, igraph_vss_all()); print_matrix(&m, stdout); igraph_cocitation(&g, &m, igraph_vss_all()); print_matrix(&m, stdout); igraph_matrix_destroy(&m); igraph_destroy(&g); return 0; }
VALUE cIGraph_community_eb_get_merges(VALUE self, VALUE edges){ igraph_t *graph; igraph_matrix_t *res = malloc(sizeof(igraph_matrix_t)); igraph_vector_t edges_vec; igraph_vector_t bridges_vec; VALUE result,bridges_a; int i; Data_Get_Struct(self, igraph_t, graph); igraph_matrix_init(res,0,0); igraph_vector_init(&edges_vec,0); igraph_vector_init(&bridges_vec,0); for(i=0;i<RARRAY_LEN(edges);i++){ igraph_vector_push_back(&edges_vec,NUM2INT(RARRAY_PTR(edges)[i])); } igraph_community_eb_get_merges(graph,&edges_vec,res,&bridges_vec); 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])); } igraph_vector_destroy(&bridges_vec); igraph_vector_destroy(&edges_vec); result = rb_ary_new3(2, Data_Wrap_Struct(cIGraphMatrix, 0, cIGraph_matrix_free, res), bridges_a); return result; }
int* readGrafo(){ int *grafo; igraph_matrix_t gMatrix; igraph_t g; igraph_i_set_attribute_table(&igraph_cattribute_table); FILE *ifile; ifile=fopen("/home/john/git/primAlgorithm/celegansneural.gml"/*"/home/john/git/primAlgorithm/grafo.gml"*/, "r"); if (ifile==0) { printf("Problema abriendo archivo de grafo\n"); return NULL; } igraph_read_graph_gml(&g, ifile); fclose(ifile); numNodos = igraph_vcount(&g); grafo = malloc(numNodos*numNodos*sizeof(int)); igraph_matrix_init(&gMatrix,numNodos,numNodos); igraph_get_adjacency(&g,&gMatrix,IGRAPH_GET_ADJACENCY_BOTH,1); igraph_vector_t el; int ii, jj, n; igraph_vector_init(&el, 0); igraph_get_edgelist(&g, &el, 0); n = igraph_ecount(&g); memset(grafo,INT_MAX2,numNodos*numNodos*sizeof(int)); for (ii=0, jj=0; ii<n; ii++, jj+=2) { grafo[((long)VECTOR(el)[jj])+numNodos*((long)VECTOR(el)[jj+1])] = (int)EAN(&g, "weight", ii); grafo[((long)VECTOR(el)[jj+1])+numNodos*((long)VECTOR(el)[jj])] = (int)EAN(&g, "weight", ii); } printf("\nNumero de nodos %d",numNodos); igraph_vector_destroy(&el); igraph_destroy(&g); return grafo; }
/* call-seq: * graph.layout_kamada_kawai_3d -> IGraphMatrix * * Places the vertices on a plane according the Kamada-Kawai algorithm. * * This is a force directed layout, see Kamada, T. and Kawai, S.: An * Algorithm for Drawing General Undirected Graphs. Information Processing * Letters, 31/1, 7--15, 1989. */ VALUE cIGraph_layout_kamada_kawai_3d(VALUE self, VALUE niter, VALUE sigma, VALUE initemp, VALUE coolexp, VALUE kkconst) { igraph_t *graph; igraph_matrix_t *res = malloc(sizeof(igraph_matrix_t)); Data_Get_Struct(self, igraph_t, graph); igraph_matrix_init(res,0,0); igraph_layout_kamada_kawai_3d(graph,res, NUM2INT(niter), NUM2DBL(sigma), NUM2DBL(initemp), NUM2DBL(coolexp), NUM2DBL(kkconst),0); return Data_Wrap_Struct(cIGraphMatrix, 0, cIGraph_matrix_free, res); }
int test_normalized_laplacian(igraph_bool_t dir) { igraph_t g; igraph_matrix_t m; igraph_vector_t vec; igraph_matrix_init(&m, 1, 1); igraph_bool_t ok = 1; /* Undirected graph, no loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); /* 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, 1); ok = ok && check_laplacian(&g, &m); /* 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, 1); ok = ok && check_laplacian(&g, &m); igraph_destroy(&g); igraph_matrix_destroy(&m); if (ok) printf("OK\n"); return !ok; }
int igraph_i_eigen_arpackfun_to_mat(igraph_arpack_function_t *fun, int n, void *extra, igraph_matrix_t *res) { int i; igraph_vector_t v; IGRAPH_CHECK(igraph_matrix_init(res, n, n)); IGRAPH_FINALLY(igraph_matrix_destroy, res); IGRAPH_VECTOR_INIT_FINALLY(&v, n); VECTOR(v)[0]=1; IGRAPH_CHECK(fun(/*to=*/ &MATRIX(*res, 0, 0), /*from=*/ VECTOR(v), n, extra)); for (i=1; i<n; i++) { VECTOR(v)[i-1]=0; VECTOR(v)[i ]=1; IGRAPH_CHECK(fun(/*to=*/ &MATRIX(*res, 0, i), /*from=*/ VECTOR(v), n, extra)); } igraph_vector_destroy(&v); IGRAPH_FINALLY_CLEAN(2); return 0; }