igraph Leiden algorithm

152 Views Asked by At

I would like to know if there is a known problem with the Leiden algorithm implemented by igraph. When I run it, it generates partitions with too many communities compared to other similar algorithms (even with the leiden implemented by other packages). Even affecting the resolution parameter it generates a large number of communities.

library(igraph)
library(leidenAlg)
G <- graph_from_literal(1-2:3-4:5-6:7-8:9-10:11-12-13-14:15-16,1-7,10:8-13)
clLouv <- cluster_louvain(G)
clLeid <- cluster_leiden(G, resolution_parameter = .5)
cl_Leid_r <- find_partition_with_rep(G, edge_weights=E(G))
clLouv$membership
clLeid$membership
cl_Leid_r

Thanks

1

There are 1 best solutions below

0
clp On BEST ANSWER

I have satisfactory results with the density of the adjacency matrix as the resolution parameter.

For example.

library(igraph)
G <- graph_from_literal(1-2:3-4:5-6:7-8:9-10:11-12-13-14:15-16,1-7,10:8-13)
r <- gsize(G)*2 / (gorder(G) * (gorder(G)-1))         # density of the adjacency matrix, or    
r <- mean(degree(G)) / gorder(G)                      # or
r <- quantile(strength(G))[2] / (gorder(G) - 1)       # from ?cluster_leiden documentation.
set.seed(20231206)                                    # For sake of reproducibility.
clLeid <- cluster_leiden(G, resolution_parameter = r)
plot(clLeid, G)

This gives a result very similar to the Louvain method.

clLeid
#IGRAPH clustering leiden, groups: 3, mod: NA
#+ groups:
#  $`1`
#  [1] "1" "2" "3" "4" "5" "7"
#  
#  $`2`
#  [1] "6"  "8"  "9"  "10" "11" "12" "13"
#  
#  $`3`
#  [1] "14" "15" "16"

clLeid$membership
[1] 1 1 1 1 1 2 1 2 2 2 2 2 2 3 3 3

Update, Verify the modularity as follows:

print(modularity_matrix(G, resolution = r, directed = FALSE), digits=4)

print.default(clLeid)
## $membership
##  [1] 1 1 1 1 1 2 1 2 2 2 2 2 2 3 3 3
## 
## $nb_clusters
## [1] 3
## 
## $quality
## [1] 0.45
## 
## $algorithm
## [1] "leiden"
## 
## $vcount
## [1] 16
## 
## $names
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15" "16"
## 
## attr(,"class")
## [1] "communities"