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 v, res, reset, weights; igraph_arpack_options_t arpack_options; igraph_real_t value; int ret; igraph_pagerank_power_options_t power_options; /* Test graphs taken from http://www.iprcom.com/papers/pagerank/ */ igraph_vector_init(&v, 10); VECTOR(v)[0]=0; VECTOR(v)[1]=1; VECTOR(v)[2]=1; VECTOR(v)[3]=2; VECTOR(v)[4]=2; VECTOR(v)[5]=0; VECTOR(v)[6]=3; VECTOR(v)[7]=2; VECTOR(v)[8]=0; VECTOR(v)[9]=2; igraph_create(&g, &v, 0, 1); igraph_vector_init(&res, 0); oldwarn=igraph_set_warning_handler(warning_handler_stdout); igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 1000, 0.001, 0.85, 0); print_vector(&res, stdout); igraph_vector_destroy(&res); igraph_vector_destroy(&v); igraph_destroy(&g); igraph_vector_init(&v, 28); VECTOR(v)[ 0]=0; VECTOR(v)[ 1]=1; VECTOR(v)[ 2]=0; VECTOR(v)[ 3]=2; VECTOR(v)[ 4]=0; VECTOR(v)[ 5]=3; VECTOR(v)[ 6]=1; VECTOR(v)[ 7]=0; VECTOR(v)[ 8]=2; VECTOR(v)[ 9]=0; VECTOR(v)[10]=3; VECTOR(v)[11]=0; VECTOR(v)[12]=3; VECTOR(v)[13]=4; VECTOR(v)[14]=3; VECTOR(v)[15]=5; VECTOR(v)[16]=3; VECTOR(v)[17]=6; VECTOR(v)[18]=3; VECTOR(v)[19]=7; VECTOR(v)[20]=4; VECTOR(v)[21]=0; VECTOR(v)[22]=5; VECTOR(v)[23]=0; VECTOR(v)[24]=6; VECTOR(v)[25]=0; VECTOR(v)[26]=7; VECTOR(v)[27]=0; igraph_create(&g, &v, 0, 1); igraph_vector_init(&res, 0); igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 10000, 0.0001, 0.85, 0); print_vector(&res, stdout); igraph_vector_destroy(&res); igraph_vector_destroy(&v); igraph_destroy(&g); igraph_set_warning_handler(oldwarn); /* New PageRank */ igraph_star(&g, 11, IGRAPH_STAR_UNDIRECTED, 0); igraph_vector_init(&res, 0); igraph_arpack_options_init(&arpack_options); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); /* Check twice more for consistency */ igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, 0, 0); print_vector(&res, stdout); /* Check personalized PageRank */ igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.5, igraph_vss_1(1), 0, &arpack_options); print_vector(&res, stdout); igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.5, igraph_vss_1(1), 0, 0); print_vector(&res, stdout); /* Errors */ power_options.niter = -1; power_options.eps=0.0001; igraph_set_error_handler(igraph_error_handler_ignore); igraph_set_warning_handler(igraph_warning_handler_ignore); ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 0.85, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 1; } power_options.niter=10000; power_options.eps=-1; ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 0.85, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 2; } power_options.niter=10000; power_options.eps=0.0001; ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res, /*value=*/ 0, igraph_vss_all(), 1, 1.2, /*weights=*/ 0, &power_options); if (ret != IGRAPH_EINVAL) { return 3; } igraph_vector_init(&reset, 2); ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, &arpack_options); if (ret != IGRAPH_EINVAL) { return 4; } ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, 0); if (ret != IGRAPH_EINVAL) { return 4; } igraph_vector_resize(&reset, 10); igraph_vector_fill(&reset, 0); ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, &arpack_options); if (ret != IGRAPH_EINVAL) { return 5; } ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 0, 0.85, &reset, 0, 0); if (ret != IGRAPH_EINVAL) { return 5; } igraph_vector_destroy(&reset); igraph_destroy(&g); igraph_set_error_handler(igraph_error_handler_abort); /* Special cases: check for empty graph */ igraph_empty(&g, 10, 0); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value, igraph_vss_all(), 1, 0.85, 0, &arpack_options); if (value != 1.0) { return 6; } igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value, igraph_vss_all(), 1, 0.85, 0, 0); if (value != 1.0) { return 6; } print_vector(&res, stdout); igraph_destroy(&g); /* Special cases: check for full graph, zero weights */ igraph_full(&g, 10, 0, 0); igraph_vector_init(&v, 45); igraph_vector_fill(&v, 0); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value, igraph_vss_all(), 1, 0.85, &v, &arpack_options); if (value != 1.0) { return 7; } igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value, igraph_vss_all(), 1, 0.85, &v, 0); if (value != 1.0) { return 7; } igraph_vector_destroy(&v); print_vector(&res, stdout); igraph_destroy(&g); /* Another test case for PageRank (bug #792352) */ igraph_small(&g, 9, 1, 0, 5, 1, 5, 2, 0, 3, 1, 5, 4, 5, 7, 6, 0, 8, 0, 8, 1, -1); igraph_vector_init(&weights, 9); VECTOR(weights)[0] = 4; VECTOR(weights)[1] = 5; VECTOR(weights)[2] = 5; VECTOR(weights)[3] = 4; VECTOR(weights)[4] = 4; VECTOR(weights)[5] = 4; VECTOR(weights)[6] = 3; VECTOR(weights)[7] = 4; VECTOR(weights)[8] = 4; igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0, igraph_vss_all(), 1, 0.85, &weights, &arpack_options); print_vector(&res, stdout); igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0, igraph_vss_all(), 1, 0.85, &weights, 0); print_vector(&res, stdout); igraph_vector_destroy(&weights); igraph_destroy(&g); igraph_vector_destroy(&res); return 0; }
int main() { igraph_t g, g2; FILE *ifile; igraph_vector_t gtypes, vtypes, etypes; igraph_strvector_t gnames, vnames, enames; long int i; igraph_vector_t y; igraph_strvector_t id; char str[20]; /* turn on attribute handling */ igraph_i_set_attribute_table(&igraph_cattribute_table); ifile=fopen("LINKS.NET", "r"); if (ifile==0) { return 10; } igraph_read_graph_pajek(&g, ifile); fclose(ifile); igraph_vector_init(>ypes, 0); igraph_vector_init(&vtypes, 0); igraph_vector_init(&etypes, 0); igraph_strvector_init(&gnames, 0); igraph_strvector_init(&vnames, 0); igraph_strvector_init(&enames, 0); igraph_cattribute_list(&g, &gnames, >ypes, &vnames, &vtypes, &enames, &etypes); /* List attribute names and types */ printf("Graph attributes: "); for (i=0; i<igraph_strvector_size(&gnames); i++) { printf("%s (%i) ", STR(gnames, i), (int)VECTOR(gtypes)[i]); } printf("\n"); printf("Vertex attributes: "); for (i=0; i<igraph_strvector_size(&vnames); i++) { printf("%s (%i) ", STR(vnames, i), (int)VECTOR(vtypes)[i]); } printf("\n"); printf("Edge attributes: "); for (i=0; i<igraph_strvector_size(&enames); i++) { printf("%s (%i) ", STR(enames, i), (int)VECTOR(etypes)[i]); } printf("\n"); print_attributes(&g); /* Copying a graph */ igraph_copy(&g2, &g); print_attributes(&g2); igraph_destroy(&g2); /* Adding vertices */ igraph_add_vertices(&g, 3, 0); print_attributes(&g); /* Adding edges */ igraph_add_edge(&g, 1, 1); igraph_add_edge(&g, 2, 5); igraph_add_edge(&g, 3, 6); print_attributes(&g); /* Deleting vertices */ igraph_delete_vertices(&g, igraph_vss_1(1)); igraph_delete_vertices(&g, igraph_vss_1(4)); print_attributes(&g); /* Deleting edges */ igraph_delete_edges(&g, igraph_ess_1(igraph_ecount(&g)-1)); igraph_delete_edges(&g, igraph_ess_1(0)); print_attributes(&g); /* Set graph attributes */ SETGAN(&g, "id", 10); if (GAN(&g, "id") != 10) { return 11; } SETGAS(&g, "name", "toy"); if (strcmp(GAS(&g, "name"), "toy")) { return 12; } /* Delete graph attributes */ DELGA(&g, "id"); DELGA(&g, "name"); igraph_cattribute_list(&g, &gnames, 0,0,0,0,0); if (igraph_strvector_size(&gnames) != 0) { return 14; } /* Delete vertex attributes */ DELVA(&g, "x"); DELVA(&g, "shape"); DELVA(&g, "xfact"); DELVA(&g, "yfact"); igraph_cattribute_list(&g, 0,0, &vnames, 0,0,0); if (igraph_strvector_size(&vnames) != 2) { return 15; } /* Delete edge attributes */ igraph_cattribute_list(&g, 0,0,0,0,&enames,0); i=igraph_strvector_size(&enames); DELEA(&g, "hook1"); DELEA(&g, "hook2"); DELEA(&g, "label"); igraph_cattribute_list(&g, 0,0,0,0,&enames,0); if (igraph_strvector_size(&enames) != i-3) { return 16; } /* Set vertex attributes */ SETVAN(&g, "y", 0, -1); SETVAN(&g, "y", 1, 2.1); if (VAN(&g, "y", 0) != -1 || VAN(&g, "y", 1) != 2.1) { return 17; } SETVAS(&g, "id", 0, "foo"); SETVAS(&g, "id", 1, "bar"); if (strcmp(VAS(&g, "id", 0), "foo") || strcmp(VAS(&g, "id", 1), "bar")) { return 18; } /* Set edge attributes */ SETEAN(&g, "weight", 2, 100.0); SETEAN(&g, "weight", 0, -100.1); if (EAN(&g, "weight", 2) != 100.0 || EAN(&g, "weight", 0) != -100.1) { return 19; } SETEAS(&g, "color", 2, "RED"); SETEAS(&g, "color", 0, "Blue"); if (strcmp(EAS(&g, "color", 2), "RED") || strcmp(EAS(&g, "color", 0), "Blue")) { return 20; } /* Set vector attributes as vector */ igraph_vector_init(&y, igraph_vcount(&g)); igraph_vector_fill(&y, 1.23); SETVANV(&g, "y", &y); igraph_vector_destroy(&y); for (i=0; i<igraph_vcount(&g); i++) { if (VAN(&g, "y", i) != 1.23) { return 21; } } igraph_vector_init_seq(&y, 0, igraph_vcount(&g)-1); SETVANV(&g, "foobar", &y); igraph_vector_destroy(&y); for (i=0; i<igraph_vcount(&g); i++) { if (VAN(&g, "foobar", i) != i) { return 22; } } igraph_strvector_init(&id, igraph_vcount(&g)); for (i=0; i<igraph_vcount(&g); i++) { snprintf(str, sizeof(str)-1, "%li", i); igraph_strvector_set(&id, i, str); } SETVASV(&g, "foo", &id); igraph_strvector_destroy(&id); for (i=0; i<igraph_vcount(&g); i++) { printf("%s ", VAS(&g, "foo", i)); } printf("\n"); igraph_strvector_init(&id, igraph_vcount(&g)); for (i=0; i<igraph_vcount(&g); i++) { snprintf(str, sizeof(str)-1, "%li", i); igraph_strvector_set(&id, i, str); } SETVASV(&g, "id", &id); igraph_strvector_destroy(&id); for (i=0; i<igraph_vcount(&g); i++) { printf("%s ", VAS(&g, "id", i)); } printf("\n"); /* Set edge attributes as vector */ igraph_vector_init(&y, igraph_ecount(&g)); igraph_vector_fill(&y, 12.3); SETEANV(&g, "weight", &y); igraph_vector_destroy(&y); for (i=0; i<igraph_ecount(&g); i++) { if (EAN(&g, "weight", i) != 12.3) { return 23; } } igraph_vector_init_seq(&y, 0, igraph_ecount(&g)-1); SETEANV(&g, "foobar", &y); igraph_vector_destroy(&y); for (i=0; i<igraph_ecount(&g); i++) { if (VAN(&g, "foobar", i) != i) { return 24; } } igraph_strvector_init(&id, igraph_ecount(&g)); for (i=0; i<igraph_ecount(&g); i++) { snprintf(str, sizeof(str)-1, "%li", i); igraph_strvector_set(&id, i, str); } SETEASV(&g, "foo", &id); igraph_strvector_destroy(&id); for (i=0; i<igraph_ecount(&g); i++) { printf("%s ", EAS(&g, "foo", i)); } printf("\n"); igraph_strvector_init(&id, igraph_ecount(&g)); for (i=0; i<igraph_ecount(&g); i++) { snprintf(str, sizeof(str)-1, "%li", i); igraph_strvector_set(&id, i, str); } SETEASV(&g, "color", &id); igraph_strvector_destroy(&id); for (i=0; i<igraph_ecount(&g); i++) { printf("%s ", EAS(&g, "color", i)); } printf("\n"); /* Delete all remaining attributes */ DELALL(&g); igraph_cattribute_list(&g, &gnames, >ypes, &vnames, &vtypes, &enames, &etypes); if (igraph_strvector_size(&gnames) != 0 || igraph_strvector_size(&vnames) != 0 || igraph_strvector_size(&enames) != 0) { return 25; } /* Destroy */ igraph_vector_destroy(>ypes); igraph_vector_destroy(&vtypes); igraph_vector_destroy(&etypes); igraph_strvector_destroy(&gnames); igraph_strvector_destroy(&vnames); igraph_strvector_destroy(&enames); igraph_destroy(&g); return 0; }