I am using the Leiden algorithm and trying to save the community membersip of each vertex to a file. Howerver i seem to get 1 as the community membership of all vertices in the graph. Should i use membership[i] instead of VECTOR(membership)[i]?

Code used:

for (i = 0; i < igraph_vcount(&graph); ++i) {
fprintf(fout, "%zu %zu\n", (size_t) i, (size_t) VECTOR(membership)[i]);
}
printf("Written communities to %s\n", outfilename);

You are using it correctly, VECTOR(membership)[i] provides the community assignment, i.e. membership, of node i.

Note that you use the format %zu which is incorrect, you should use %li. Alternatively, you can print the entire vector using igraph_vector_int_print, which is more straightforward.

Most likely, the algorithm simply finds one cluster.

It seems I might be using the algorithm incorrectly as the modularity of returned membership is close to 0. I am trying to use igraph_community_leiden() with modularity optimization as below.

/* Read graph from file. */
igraph_read_graph_edgelist(&graph, file, 0, 0);
/* Initialize membership vector to use for storing communities */
igraph_vector_int_init(&membership, igraph_vcount(&graph));
/* Initialize degree vector to use for optimizing modularity */
igraph_vector_int_init(°ree, igraph_vcount(&graph));
igraph_degree(&graph, °ree, igraph_vss_all(), IGRAPH_ALL, 1);
igraph_vector_init(&weights, igraph_vector_int_size(°ree));
/* Perform Leiden algorithm using modularity until stable iteration */
igraph_community_leiden(&graph, NULL, &weights, 1.0 / (2 * igraph_ecount(&graph)), 0.01, 0, -1, &membership, NULL, NULL);

This is from the given examples, and I am not sure what is wrong here.