Use this if you are using igraph from R
cluster_leiden {igraph}  R Documentation 
The Leiden algorithm is similar to the Louvain algorithm,
cluster_louvain
, but it is faster and yields higher quality
solutions. It can optimize both modularity and the Constant Potts Model,
which does not suffer from the resolutionlimit (see preprint
http://arxiv.org/abs/1104.3083).
cluster_leiden(
graph,
objective_function = c("CPM", "modularity"),
weights = NULL,
resolution_parameter = 1,
beta = 0.01,
initial_membership = NULL,
n_iterations = 2,
vertex_weights = NULL
)
graph 
The input graph, only undirected graphs are supported. 
objective_function 
Whether to use the Constant Potts Model (CPM) or
modularity. Must be either 
weights 
The weights of the edges. It must be a positive numeric vector,

resolution_parameter 
The resolution parameter to use. Higher resolutions lead to more smaller communities, while lower resolutions lead to fewer larger communities. 
beta 
Parameter affecting the randomness in the Leiden algorithm. This affects only the refinement step of the algorithm. 
initial_membership 
If provided, the Leiden algorithm will try to improve this provided membership. If no argument is provided, the aglorithm simply starts from the singleton partition. 
n_iterations 
the number of iterations to iterate the Leiden algorithm. Each iteration may improve the partition further. 
vertex_weights 
the vertex weights used in the Leiden algorithm.
If this is not provided, it will be automatically determined on the basis
of the 
The Leiden algorithm consists of three phases: (1) local moving of nodes, (2) refinement of the partition and (3) aggregation of the network based on the refined partition, using the nonrefined partition to create an initial partition for the aggregate network. In the local move procedure in the Leiden algorithm, only nodes whose neighborhood has changed are visited. The refinement is done by restarting from a singleton partition within each cluster and gradually merging the subclusters. When aggregating, a single cluster may then be represented by several nodes (which are the subclusters identified in the refinement).
The Leiden algorithm provides several guarantees. The Leiden algorithm is typically iterated: the output of one iteration is used as the input for the next iteration. At each iteration all clusters are guaranteed to be connected and wellseparated. After an iteration in which nothing has changed, all nodes and some parts are guaranteed to be locally optimally assigned. Finally, asymptotically, all subsets of all clusters are guaranteed to be locally optimally assigned. For more details, please see Traag, Waltman & van Eck (2019).
The objective function being optimized is
\frac{1}{2m} \sum_{ij} (A_{ij}  \gamma n_i n_j)\delta(\sigma_i, \sigma_j)
where m
is the total edge weight, A_{ij}
is the weight
of edge (i, j)
, \gamma
is the socalled resolution
parameter, n_i
is the node weight of node i
, \sigma_i
is the cluster of node i
and \delta(x, y) = 1
if and
only if x = y
and 0
otherwise. By setting n_i = k_i
, the
degree of node i
, and dividing \gamma
by 2m
, you
effectively obtain an expression for modularity.
Hence, the standard modularity will be optimized when you supply the degrees
as vertex_weights
and by supplying as a resolution parameter
\frac{1}{2m}
, with m
the number of edges. If you do not
specify any vertex_weights
, the correct vertex weights and scaling of
\gamma
is determined automatically by the
objective_function
argument.
cluster_leiden
returns a communities
object, please see the communities
manual page for details.
Vincent Traag
Traag, V. A., Waltman, L., & van Eck, N. J. (2019). From Louvain to Leiden: guaranteeing wellconnected communities. Scientific reports, 9(1), 5233. doi: 10.1038/s4159801941695z, arXiv:1810.08473v3 [cs.SI]
See communities
for extracting the membership,
modularity scores, etc. from the results.
Other community detection algorithms: cluster_walktrap
,
cluster_spinglass
,
cluster_leading_eigen
,
cluster_edge_betweenness
,
cluster_fast_greedy
,
cluster_label_prop
cluster_louvain
cluster_fluid_communities
cluster_infomap
cluster_optimal
cluster_walktrap
g < make_graph("Zachary")
# By default CPM is used
r < quantile(strength(g))[2] / (gorder(g)  1)
# Set seed for sake of reproducibility
set.seed(1)
ldc < cluster_leiden(g, resolution_parameter=r)
print(ldc)
plot(ldc, g)