Exemple #1
0
int main(void) {

    // This needs to be done *first*. See igraph doc for why.
    igraph_i_set_attribute_table(&igraph_cattribute_table);

    printf("sizeof(int)=%d sizeof(long)=%d sizeof(igraph_integer_t)=%d\n", (int) sizeof(int), (int) sizeof(long), (int) sizeof(igraph_integer_t));

    pause();

    printf("Loading graph from file...\n");
    FILE* f = fopen("donnees/arretes.test", "r");
    //FILE* f = fopen("graph_a.ncol", "r");
    igraph_t gr;
    igraph_read_graph_ncol(&gr, f, NULL, 1, 0, 0);
    fclose(f);
    f = NULL;
    long vcount = igraph_vcount(&gr);
    long ecount = igraph_ecount(&gr);
    printf("Main graph: |V| = %ld, |E| = %ld\n", vcount, ecount);

    pause();

    // get connected components
    printf("Computing connected components...\n");
    igraph_vector_t membership;
    igraph_vector_t csize;
    igraph_integer_t cnum = 0;
    igraph_vector_init(&membership, 1);
    igraph_vector_init(&csize, 1);
    igraph_clusters(&gr, &membership, &csize, &cnum, IGRAPH_STRONG);
    printf("There are %ld connected components.\n", (long) cnum);

    // work with connected components
    {  
        printf("Writing connected components to file...\n");
        // open file
        FILE* filout = fopen("groupes", "w");

        long membership_size = igraph_vector_size(&membership);
        if (membership_size != vcount) {
            fprintf(stderr, "FATAL ERROR: membership_size != vcount\n");
            exit(1);
        }
        for (long i = 0; i < membership_size; i++) {
            fprintf(filout,
                "%ld\t%s\n",
                // connected component id:
                (long) igraph_vector_e(&membership , i),
                // veretex name:
                igraph_cattribute_VAS(&gr, "name", i));
        }

        fclose(filout);
        filout = NULL;
        printf("Connected components written.\n");
    }


    pause();


    // free connected components
    igraph_vector_destroy(&membership);
    igraph_vector_destroy(&csize);
    // free main graph
    igraph_destroy(&gr);

    printf("Program ends.\n");

    pause();

    return 0;
}
int main(int argc, char** argv) {

	int response;
	igraph_t graph;
	igraph_vector_ptr_t complist;
	iclust_collection * collection = NULL;
	time_t time_start, time_end;

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

	double minimal_weight;
	unsigned int maximal_steps_delimieter;
	char graphncol[1024], logconfig[1024];

	Config *cfg = ConfigNew();
	const char * configuration = getopt_configfile(argc, argv, "./graphtocluster.conf");
	massert((ConfigReadFile(configuration, &cfg) == CONFIG_OK), "Configuration file is not readable");
	ConfigReadString(cfg, "sources", "graphncol", graphncol, sizeof(graphncol), 0);
	ConfigReadString(cfg, "sources", "logconfig", logconfig, sizeof(logconfig), 0);
	ConfigReadDouble(cfg, "limits", "minimal_weight", &minimal_weight, 0);
	ConfigReadUnsignedInt(cfg, "limits", "maximal_steps_delimieter", &maximal_steps_delimieter, 1);
	massert((maximal_steps_delimieter > 0), "Delimiter can not be equal to zero");
	ConfigFree(cfg);

	logger_init(logconfig, "graphtocluster");
	logger_info("File:\t configuration %s", configuration);
	logger_info("File:\t configuration logger %s", logconfig);
	logger_info("File:\t ncol graph source %s", graphncol);
	logger_info("Min:\t edge weight %f", minimal_weight);
	logger_info("Max:\t step delimeter %u", maximal_steps_delimieter);

	FILE *graph_source = fopen(graphncol, "r");
	response = igraph_read_graph_ncol(&graph, graph_source, NULL, true, IGRAPH_ADD_WEIGHTS_YES, 0);
	massert((response == IGRAPH_SUCCESS), "Can not read a graph");
	logger_info("Count:\t edges at start: %d", igraph_ecount(&graph));
	fclose(graph_source);

	time(&time_start);
	igraph_edges_remove_by(&graph, "weight", minimal_weight, double_lt);
	time(&time_end);
	logger_info("Time:\t remove edges: %f", difftime(time_end, time_start));
	logger_info("Count:\t edges after remove: %d", igraph_ecount(&graph));

	response = igraph_vector_ptr_init(&complist, 0);
	massert((response == IGRAPH_SUCCESS), "Can not initialize vector pointer");

	response = igraph_decompose(&graph, &complist, IGRAPH_WEAK, -1, 0);
	massert((response == IGRAPH_SUCCESS), "Can not decompose graph");

	unsigned int n = igraph_vector_ptr_size(&complist);

	collection = iclust_collection_new();
	massert((collection != NULL), "Cluster collection object can not be empty");

	time(&time_start);
	for (unsigned int i = 0; i < n; i++) {

		igraph_t *subgraph = VECTOR(complist)[i];
		massert((subgraph != NULL), "Subgraph object can not be empty");

		iclust_collection_fill_leading_eigenvector(collection, subgraph, (i + 1), maximal_steps_delimieter);
		igraph_destroy(subgraph);
	}
	time(&time_end);

	logger_info("Time:\t cluster: %f", difftime(time_end, time_start));

	/* Sort collection by cluster id to be
	 * able to build a second column correct*/
	time(&time_start);
	iclust_collection_sort(collection);
	time(&time_end);

	logger_info("Time:\t cluster sorting: %f", difftime(time_end, time_start));

	unsigned long cluster_index = 1, cluster = 0;
	for (unsigned long i = 0; i < collection->length; i++) {

		iclust_collection_element * element = collection->collection[i];
		massert((element != NULL), "Cluster collection element object can not be empty");

		if (element->cluster != cluster) {
			cluster = element->cluster;
			cluster_index = 0;
		}

		printf("%lu\t%lu\t%s\n", cluster, ++cluster_index, element->name);
	}

	iclust_collection_destroy(collection);
	igraph_vector_ptr_destroy(&complist);
	igraph_destroy(&graph);
	logger_destroy();

	return EXIT_SUCCESS;
}