以邻接矩阵的密度作为分解参数,得到了满意的结果.
例如.
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)
这给出的结果与卢瓦因方法非常相似.
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,按如下方式验证模块化:
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"