I have a graph G and I applied the Leiden algorithm which resulted in me in 12 clusters. As I wanted to do Hierarchical clustering, I searched in igraph and found methods like subgraphs, where I can pass the clustered index and do clustering individually for each clustered index. I followed the Louvain algorithm and the result of the layer 1 cluster is given into a graph and then given it as input for the second layer. But it is taking a lot of time and then I discovered Leiden and started applying it to my data. For the Leiden algorithm, how can the result of leid1 can be re-clustering again.

The result of calling community_leiden() on a graph is a VertexClustering object. Iterating over such an object gives you lists containing the vertices in each community:

g = Graph.GRG(100, 0.2)
clustering = g.community_leiden()
for members in clustering:
print(members)

You can then use the induced_subgraph() method of the original graph to extract another graph that contains the vertices and edges within each cluster:

for members in clustering:
sg = g.induced_subgraph(members)
print(sg.get_edgelist())

Note that the IDs of vertices and edges are renumbered in the subgraph because they always have to start from zero and must be a continuous range. Nevertheless, you can use the vertex attributes to correlate the vertices of the subgraph back to the original vertices based on their IDs.

Thank you for your response. Can I know the reason why we are applying the method GRG for the graph.

g = Graph.GRG(100, 0.2)
clustering = g.community_leiden()
for members in clustering:
print(members)

When I apply the code on graph G without GRG, I am getting clusters, but I have found some of the clusters are empty which didn’t happen with my code. And when I apply G.GRG and then community_leiden, I have empty members.

g = G.GRG(100, 0.2)
clustering = g.community_leiden()
for members in clustering:
print(members)

This is the code I have followed. Please let me know if I am missing anything.

Graph.GRG() is just a quick way to construct a graph that usually has some kind of a clustered structure. (GRG stands for “geometric random graph”). This is my go-to example for creating such graphs in examples. Other than that, there is no specific reason for that, I just needed something for illustrative purposes.