communities: Community Detection

View source: R/communities.R

communitiesR Documentation

Community Detection

Description

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

Usage

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

Arguments

x

Network input: matrix, igraph, network, CographNetwork, 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

community

Optional integer or character vector. If supplied, the returned data frame is filtered to rows whose community column matches one of the given values. Default NULL (keep all communities).

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 edge-betweenness should treat the network as directed. Default NULL (auto-detect for edge-betweenness). Other methods use their own directed/undirected handling.

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.

Details

When called through this wrapper, methods that require undirected graphs ("louvain", "leiden", "fast_greedy", "leading_eigenvector", and "fluid") fall back to "walktrap" if the input graph is directed.

Algorithm Selection Guide:

Algorithm Best For Time Complexity
louvain Large networks, general use O(n log n)
leiden Large networks, better quality than louvain O(n log n)
fast_greedy Medium networks O(n² log n)
walktrap Networks with clear community structure O(n² log n)
infomap Directed networks, flow-based O(E)
label_propagation Very large networks, speed critical O(E)
edge_betweenness Small networks, hierarchical O(E² n)
leading_eigenvector Networks with dominant structure O(n²)
spinglass Small networks, allows negative weights O(n³)
optimal Tiny networks only (<50 nodes) NP-hard
fluid When k is known O(E k)

Value

A tidy cograph_communities data frame with columns:

node

Node label (character)

community

Community assignment (integer)

Metadata stored as attributes: "algorithm", "modularity", "network" (original input), "igraph_result".

See Also

community_louvain, community_leiden, community_fast_greedy, community_walktrap, community_infomap, community_label_propagation, community_edge_betweenness, community_leading_eigenvector, community_spinglass, community_optimal, community_fluid

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)
}

cograph documentation built on May 31, 2026, 5:06 p.m.