% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/communities.R
\name{communities}
\alias{communities}
\title{Community Detection}
\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{
\item{x}{Network input: matrix, igraph, network, CographNetwork,
cograph_network, or tna object}

\item{method}{Community detection algorithm. One of:
\itemize{
\item \code{"louvain"} - Louvain modularity optimization (default, fast)
\item \code{"leiden"} - Leiden algorithm (improved Louvain)
\item \code{"fast_greedy"} - Fast greedy modularity optimization
\item \code{"walktrap"} - Random walk-based detection
\item \code{"infomap"} - Information theoretic approach
\item \code{"label_propagation"} - Label propagation (very fast)
\item \code{"edge_betweenness"} - Girvan-Newman algorithm
\item \code{"leading_eigenvector"} - Leading eigenvector method
\item \code{"spinglass"} - Spinglass simulation
\item \code{"optimal"} - Exact modularity optimization (slow)
\item \code{"fluid"} - Fluid communities algorithm
}}

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

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

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

\item{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.}

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

\item{...}{Additional parameters passed to the specific algorithm.
See individual functions for details.}
}
\value{
A tidy \code{cograph_communities} data frame with columns:
\describe{
\item{node}{Node label (character)}
\item{community}{Community assignment (integer)}
}
Metadata stored as attributes: \code{"algorithm"}, \code{"modularity"},
\code{"network"} (original input), \code{"igraph_result"}.
}
\description{
Detects communities/clusters in networks using various algorithms.
Provides a unified interface to igraph's community detection functions.
}
\details{
When called through this wrapper, methods that require undirected graphs
(\code{"louvain"}, \code{"leiden"}, \code{"fast_greedy"},
\code{"leading_eigenvector"}, and \code{"fluid"}) fall back to
\code{"walktrap"} if the input graph is directed.

\strong{Algorithm Selection Guide:}

\tabular{lll}{
Algorithm \tab Best For \tab Time Complexity \cr
louvain \tab Large networks, general use \tab O(n log n) \cr
leiden \tab Large networks, better quality than louvain \tab O(n log n) \cr
fast_greedy \tab Medium networks \tab O(n² log n) \cr
walktrap \tab Networks with clear community structure \tab O(n² log n) \cr
infomap \tab Directed networks, flow-based \tab O(E) \cr
label_propagation \tab Very large networks, speed critical \tab O(E) \cr
edge_betweenness \tab Small networks, hierarchical \tab O(E² n) \cr
leading_eigenvector \tab Networks with dominant structure \tab O(n²) \cr
spinglass \tab Small networks, allows negative weights \tab O(n³) \cr
optimal \tab Tiny networks only (<50 nodes) \tab NP-hard \cr
fluid \tab When k is known \tab O(E k) \cr
}
}
\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)
}
}
\seealso{
\code{\link{community_louvain}}, \code{\link{community_leiden}},
\code{\link{community_fast_greedy}}, \code{\link{community_walktrap}},
\code{\link{community_infomap}}, \code{\link{community_label_propagation}},
\code{\link{community_edge_betweenness}}, \code{\link{community_leading_eigenvector}},
\code{\link{community_spinglass}}, \code{\link{community_optimal}},
\code{\link{community_fluid}}
}
