Skip to contents

Detects communities/clusters in networks using various algorithms. Provides a unified interface to igraph's community detection functions with full parameter exposure.

Usage

communities(
  x,
  method = c("louvain", "leiden", "fast_greedy", "walktrap", "infomap",
    "label_propagation", "edge_betweenness", "leading_eigenvector", "spinglass",
    "optimal", "fluid"),
  weights = NULL,
  resolution = 1,
  directed = NULL,
  seed = NULL,
  ...
)

Arguments

x

Network input: matrix, igraph, network, cograph_network, or tna object

method

Community detection algorithm. One of:

  • "louvain" - Louvain modularity optimization (default, fast)

  • "leiden" - Leiden algorithm (improved Louvain)

  • "fast_greedy" - Fast greedy modularity optimization

  • "walktrap" - Random walk-based detection

  • "infomap" - Information theoretic approach

  • "label_propagation" - Label propagation (very fast)

  • "edge_betweenness" - Girvan-Newman algorithm

  • "leading_eigenvector" - Leading eigenvector method

  • "spinglass" - Spinglass simulation

  • "optimal" - Exact modularity optimization (slow)

  • "fluid" - Fluid communities algorithm

weights

Edge weights. If NULL, uses edge weights from the network if available, otherwise unweighted. Set to NA for explicitly unweighted.

resolution

Resolution parameter for modularity-based methods (louvain, leiden). Higher values yield more communities. Default 1.

directed

Logical; whether to treat the network as directed. Default NULL (auto-detect).

seed

Random seed for reproducibility. Only applies to stochastic algorithms (louvain, leiden, infomap, label_propagation, spinglass).

...

Additional parameters passed to the specific algorithm. See individual functions for details.

Value

A cograph_communities object (extends igraph's communities class) with components:

membership

Integer vector of community assignments

modularity

Modularity score of the partition

algorithm

Name of the algorithm used

names

Node names if available

vcount

Number of nodes

Details

Algorithm Selection Guide:

AlgorithmBest ForTime Complexity
louvainLarge networks, general useO(n log n)
leidenLarge networks, better quality than louvainO(n log n)
fast_greedyMedium networksO(n² log n)
walktrapNetworks with clear community structureO(n² log n)
infomapDirected networks, flow-basedO(E)
label_propagationVery large networks, speed criticalO(E)
edge_betweennessSmall networks, hierarchicalO(E² n)
leading_eigenvectorNetworks with dominant structureO(n²)
spinglassSmall networks, allows negative weightsO(n³)
optimalTiny networks only (<50 nodes)NP-hard
fluidWhen k is knownO(E k)

Examples

# Create a network with community structure
if (requireNamespace("igraph", quietly = TRUE)) {
  g <- igraph::make_graph("Zachary")

  # Default (Louvain)
  comm <- cograph::communities(g)
  print(comm)

  # Walktrap
  comm2 <- cograph::communities(g, method = "walktrap")
  print(comm2)
}
#> Community structure (louvain)
#>   Number of communities: 4 
#>   Modularity: 0.4188 
#>   Community sizes: 12, 5, 11, 6 
#> Community structure (walktrap)
#>   Number of communities: 5 
#>   Modularity: 0.3532 
#>   Community sizes: 9, 7, 9, 4, 5