Convert 2D python list of communities to VertexCluster

I have a file containing the nodes of a graph and their respective communities using infomap community detection algorithm. I have read the file line by line and converted it into a 2D python list, where the list[0] gives you all the nodes in the 0th community and so on. Now to compare this infomap algorithm with the one provided in igraph using the compare_communities (from igraph) I need both the community outputs in a vertexCluster object. How can I convert the 2D list to a vertexClustering Object?

You can simply initalise a new VertexClustering object. The only thing is that you need to supply a membership list, instead of a list of a list of clusters.

Let us assume that clusters is the list of clusters, such that clusters[i] is a list of nodes that belong to cluster i, and graph contains the appropriate graph. One way to convert clusters to a membership list is as follows

membership = [None]*graph.vcount()
for c, cluster in enumerate(clusters):
  for v in cluster:
    membership[v] = c

you can then create a new VertexClustering using

clustering = VertexClustering(graph, membership=membership)

If there are any nodes that are incorrectly labelled (i.e. not between 0 and graph.vcount()-1), this should throw an error.

1 Like