# Louvain方法

**Louvain方法**是一种贪心启发式算法，用于检测大型图中的社区：它反复在社区之间移动节点并合并社区，以最大化某个质量函数——最常见的是模块度。该方法由Vincent D. Blondel、Jean-Loup Guillaume、Renaud Lambiotte和Etienne Lefebvre于2008年在《统计力学：理论与实验杂志》（*Journal of Statistical Mechanics: Theory and Experiment*，P10008）上提出<sup>[1](https://iopscience.iop.org/article/10.1088/1742-5468/2008/10/P10008)</sup><sup> • </sup><sup>[2](https://perso.uclouvain.be/vincent.blondel/research/louvain.html)</sup>，它将良好的解质量与接近图规模线性的运行时间结合在一起<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | Blondel、Guillaume、Lambiotte和Lefebvre，JSTAT 2008（P10008）；在一个拥有200万客户的比利时移动电话网络和一个拥有1.18亿节点、超过10亿条链接的网页图上进行了演示<sup>[1](https://iopscience.iop.org/article/10.1088/1742-5468/2008/10/P10008)</sup> |
| 目标 | 贪心最大化质量函数，最初为模块度；该框架还可优化CPM、映射方程（Map equation）和马尔可夫稳定性<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup><sup> • </sup><sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup> |
| 复杂度 | 时间O(L\|E\|)，L为遍历次数；空间O(\|V\| + \|E\|)；igraph文档记载其实现为O(\|V\|+\|E\|)，而精确模块度优化是NP完全的<sup>[5](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)</sup><sup> • </sup><sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup> |
| 已演示的规模 | uk-2007网页图，33亿条边，在NERSC Cori的1,024个核心上32秒处理完毕<sup>[7](https://eecs.wsu.edu/~ananth/papers/Ghosh_IPDPS18.pdf)</sup> |
| 主要缺陷 | 可能产生连接不良或断开的社区：在Amazon、DBLP和Web UK网络上分别有23%、16%和14%的连接不良社区，在一个Web of Science网络中有超过5%的断开社区<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup> |
| 后继方法 | Leiden算法（2019）增加了细化阶段，保证社区连通，并在困难网络上运行更快<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup> |
| 可用性 | 已内置于igraph、NetworkX、Gephi、scanpy、Neo4j Graph Data Science和NVIDIA cuGraph中<sup>[8](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)</sup><sup> • </sup><sup>[9](https://scanpy.scverse.org/en/stable/generated/scanpy.tl.louvain.html)</sup> |

## 概述

该方法回答的是关于网络的一个基本问题：哪些节点集合彼此之间的连接比它们与图中其余部分的连接更密集？它的答案是将节点划分为若干社区，通过对模块度进行爬山搜索得到——模块度是一个分数，用于比较社区内部链接密度与具有相同度数的随机网络会给出的密度<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。2008年的论文在一个拥有200万客户的比利时移动电话网络上演示了该方法，识别出了语言社区；同时还在一个拥有1.18亿节点、超过10亿条链接的网页图上进行了演示<sup>[1](https://iopscience.iop.org/article/10.1088/1742-5468/2008/10/P10008)</sup>。

尽管是为模块度而设计的，这个两阶段方案是灵活的：同样的局部移动和聚合机制可以优化其他质量函数，包括常数Potts模型（CPM）、基于流的方法所使用的映射方程，以及马尔可夫稳定性<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup><sup> • </sup><sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。

## 算法

该算法以两个基本阶段工作，作为遍历反复执行，直到质量函数无法再增加为止<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。

1. **局部移动。** 每个节点从自己的社区开始。然后检查每个节点，看将其移动到某个邻居的社区是否能获得最大的正模块度增益；如果没有移动能带来正增益，节点就留在原地。这一过程持续到达到局部最大值，且没有任何单次移动能改善分数为止<sup>[8](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)</sup><sup> • </sup><sup>[10](https://r.igraph.org/reference/cluster_louvain.html)</sup>。
2. **聚合。** 网络被重建，每个社区对应一个超级节点。同一社区内节点之间的链接变成超级节点上的自环——这正是跨遍历保持模块度的方式；社区之间的链接变成超级节点之间的链接<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。

这两个阶段在聚合后的网络上重复，直到不再获得模块度增益，或增益低于某个阈值（如NetworkX默认的0.0000001），或达到最大层级数<sup>[8](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)</sup>。每次遍历后的划分本身就是一个有效的、更粗粒度的社区结构，因此中间结果给出了网络的层次化表示<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。

一个对速度有影响的实践细节：在局部移动过程中，邻居以随机顺序而非固定顺序检查，这加速了收敛。Leiden方法沿用了这一原则<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。

## 模块度及其分辨率极限

模块度将观测到的社区内部边权重与零模型下的期望值进行比较，零模型在保持节点度数的同时随机放置边。在igraph和NetworkX所使用的形式中，

\[ Q = \frac{1}{2m} \sum_{ij} \left( A_{ij} - \gamma \, \frac{k_i k_j}{2m} \right) \delta(c_i, c_j), \]

其中 \( A_{ij} \) 是邻接矩阵元素，\( k_i \) 和 \( k_j \) 是节点度数，\( m \) 是总边权重，\( c_i \) 是节点 \( i \) 的社区，\( \gamma \) 是加权零模型的分辨率参数<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup>。对于模块度，\( \gamma \) 缩放基于度数的零模型项；它并不强制要求绝对的最小内部密度或最大社区间密度为 \( \gamma \)<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。较高的 \( \gamma \) 通常产生更多、更小的簇；\( \gamma = 1 \) 恢复原始定义<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup><sup> • </sup><sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。（[Gephi](https://www.edgepedia.org/zh/gephi)手册陈述的方向相反，建议使用较低的值以获得更多、更小的社区<sup>[11](https://jveerbeek.gitlab.io/gephi/docs/community.html)</sup>；igraph、NetworkX和scanpy的文档以及Traag等人的文献均指出，较高的分辨率给出更多、更小的簇<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup><sup> • </sup><sup>[8](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)</sup><sup> • </sup><sup>[9](https://scanpy.scverse.org/en/stable/generated/scanpy.tl.louvain.html)</sup>。）

模块度本身带有一种内在尺度。Fortunato和Barthélemy表明，这个尺度取决于网络中链接的总数，因此小于该尺度的模块即使是由单桥连接起来的完全图，也可能无法被分辨；该极限取决于社区对之间的互联程度，可以达到整个网络规模的数量级<sup>[12](https://www.pnas.org/doi/full/10.1073/pnas.0605965104)</sup>。由于Louvain方法优化的是模块度，它继承了这一极限，也继承了模块度过度拟合的倾向——即使在随机图中也能找到社区<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。调整 \( \gamma \) 并不能完全摆脱这个问题：多分辨率模块度受到两种共存偏差的影响——低分辨率下合并小子图，高分辨率下分裂大子图——两者无法同时消除<sup>[13](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.84.066122)</sup>。切换质量函数有所帮助：常数Potts模型不受分辨率极限的影响<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup><sup> • </sup><sup>[5](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)</sup>。

## 数据一览

该方法的实际吸引力在于其扩展行为。其时间复杂度为O(L\|E\|)，其中L是遍历次数，空间为O(\|V\| + \|E\|)<sup>[5](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)</sup>；igraph文档记载其实现为O(\|V\|+\|E\|)<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup>。精确模块度优化是NP完全的，所有已知精确算法都是指数级的，因此近线性启发式是使大型图可处理的关键<sup>[6](https://igraph.org/c/html/main/igraph-Community.html)</sup>。实践中的近线性行为来自操作的局部性：顶点只移动到相邻社区，因此每次遍历的工作量与边数保持成正比<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。

报告中的运行时间展示了这一范围：

- 一个分布式实现处理了uk-2007网页图，33亿条边，在NERSC Cori的1,024个核心（64个节点）上耗时32秒，而共享内存实现在单个128 GB节点上因内存不足而失败<sup>[7](https://eecs.wsu.edu/~ananth/papers/Ghosh_IPDPS18.pdf)</sup>。
- 一个可扩展实现处理了Flickr图，983.7万条边，用时不到10秒，而同一图上Infomap约需15秒，SBP则超过1,000秒<sup>[14](https://vtechworks.lib.vt.edu/server/api/core/bitstreams/7d21b638-86e4-4bb3-a7cc-45cc3a15ad31/content)</sup>。
- 在DBLP（317,080个节点，1,049,866条边）上，Traag等人报告Louvain的最大模块度为0.8262，Leiden为0.8387；在Web UK（39,252,879个节点）上，数字分别为0.9796和0.9801<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。另一个标准实现在同一DBLP数据集上约6分钟终止，模块度为0.805<sup>[15](https://ar5iv.labs.arxiv.org/html/2108.13482)</sup>，低于Traag等人的0.8262，这反映的是实现之间的差异，而非一个确定的数值。
- 对局部移动步骤的一项改进将复杂度从O(m)变为O(n log⟨k⟩)（在具有清晰社区结构的网络中），在基准测试上加速约2到3倍，在某些真实网络上可达10倍<sup>[16](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.92.032801)</sup>。

## 与Leiden、Infomap及其他方法的比较

**Leiden。** Leiden算法是专门为修复Louvain的主要缺陷而创建的。它在局部移动之后增加了一个细化阶段，保证所有社区连通且良好分离，收敛到所有社区的所有子集都得到局部最优分配的划分，并且在已报告的对比中通常比Louvain运行更快<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup><sup> • </sup><sup>[5](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)</sup>。在困难网络上差距更大：对于 \( 10^{7} \) 个节点的网络，混合参数 \( \mu = 0.9 \) 时，Louvain需要近2.5天，而Leiden只需不到10分钟；对于更高的 \( \mu \)，Leiden在最大网络上的运行速度可达Louvain的10到100倍<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。

**Infomap。** 在一项大规模比较中，Louvain在几乎所有实验中超过了Infomap，作者指出这一结果与早期发现Infomap优于Louvain的结论相矛盾；模块度的分辨率极限在社区规模相对较大时有利于Louvain，而Infomap受视野极限的影响，识别出更小的簇<sup>[17](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0159161)</sup>。

**其他方法。** 2024年IEEE的一项比较研究对Louvain与标签传播、Infomap、Girvan–Newman和Walktrap，以及更新的方法PercoMVC、Walkscan和Paris进行了基准测试，使用电导、密度、模块度、割比率等质量标准以及Friedman秩检验，在生物、社交和协作网络上进行评估<sup>[18](https://doi.org/10.1109/compeng60905.2024.10741414)</sup>。

## 已知局限与失效模式

Traag、Waltman和van Eck指出的核心缺陷是，Louvain可能产生连接状况差到任意程度的社区。在他们的实验中，第一次迭代在Amazon、DBLP和Web UK网络上分别平均产生23%、16%和14%的连接不良社区，在一个拥有9,811,130个节点的[Web of Science](https://www.edgepedia.org/zh/web-of-science)网络中，超过5%的社区是断开的<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。这一缺陷与模块度的分辨率极限无关：即使以CPM作为质量函数，Louvain仍然可能找到连接不良的社区<sup>[4](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)</sup>。

其他局限源于设计本身。作为一种启发式方法，它不保证找到模块度的全局最优解<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。NVIDIA的cuGraph文档列出了更多失效模式：具有许多小社区的图，这些小社区往往被合并；重叠社区，因为每个节点只被分配到一个簇；以及稀疏图，低模块度可能使结果毫无用处<sup>[19](https://docs.nvidia.com/cugraph/latest/graph_support/algorithms/louvain_community/index.html)</sup>。

## 应用与实现

该方法作为标准工具遍布网络分析生态系统：NetworkX的`louvain_communities`<sup>[8](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)</sup>、igraph的`cluster_louvain`<sup>[10](https://r.igraph.org/reference/cluster_louvain.html)</sup>、Gephi统计面板中的模块度<sup>[11](https://jveerbeek.gitlab.io/gephi/docs/community.html)</sup>、Neo4j的Graph Data Science库（它还按模块度为每个社区打分<sup>[20](https://neo4j.com/docs/graph-data-science/current/algorithms/louvain/)</sup>），以及GPU上的NVIDIA cuGraph<sup>[19](https://docs.nvidia.com/cugraph/latest/graph_support/algorithms/louvain_community/index.html)</sup>。

一个突出的科学应用是单细胞基因组学：scanpy的`scanpy.tl.louvain`使用Traag 2015年的实现在预计算的邻居图上对细胞进行聚类，该方法由Levine等人在2015年引入单细胞分析，分辨率默认为1.0<sup>[9](https://scanpy.scverse.org/en/stable/generated/scanpy.tl.louvain.html)</sup>。cuGraph的文档将社交网络、产品共同购买和生物网络列为该方法效果良好的场景，因为这类图具有自然的社区<sup>[19](https://docs.nvidia.com/cugraph/latest/graph_support/algorithms/louvain_community/index.html)</sup>。

## 近期发展

该方法的作者于2024年在JSTAT上发表了一篇15年回顾，重申了精度与速度的平衡、继承的分辨率极限以及缺乏全局最优保证<sup>[3](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)</sup>。实现工作已转向并行硬件：2025年1月的一篇预印本提出了面向OpenMP CPU的GVE-Louvain和面向CUDA GPU的ν-Louvain，在[NVIDIA A100](https://www.edgepedia.org/zh/nvidia-a100)上运行<sup>[21](https://arxiv.org/html/2501.19004v1)</sup>；2025年的Arachne框架并行实现了Louvain的局部移动和聚合阶段<sup>[22](https://arxiv.org/html/2507.06471)</sup>；gLeiden为有向图和无向图提供了GPU加速的Leiden<sup>[23](https://pmc.ncbi.nlm.nih.gov/articles/PMC12987761/)</sup>。在共享内存方面，GVE-Leiden在sk-2005图上9.4秒内找到社区，处理速率为每秒4.03亿条边，平均比原始Leiden快436倍，比igraph Leiden快104倍，模块度平均低0.3%，且没有断开社区<sup>[5](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)</sup>。

在单细胞分析中，Leiden已取代Louvain成为Scanpy中首选的聚类方法，这反映在其官方教程中，而Louvain仍是Seurat的默认方法；2025年8月的一项基准测试还指出，Traag等人关于断开社区的证据基于六个经验网络，其中没有一个是生物基因表达数据<sup>[24](https://www.biorxiv.org/content/10.1101/2025.08.20.671270v1)</sup>。NVIDIA的nx-cugraph基准报告称，GPU Leiden在不到4秒内识别出四个社区，而CPU Louvain花费了近21分钟，结果几乎相同，并将Leiden描述为正在迅速成为Louvain的标准替代方案<sup>[25](https://developer.nvidia.com/blog/how-to-accelerate-community-detection-in-python-using-gpu-powered-leiden/)</sup>。与PercoMVC、Walkscan和Paris等更新方法的比较基准测试仍在继续<sup>[18](https://doi.org/10.1109/compeng60905.2024.10741414)</sup>。

## 在Louvain、Leiden和Infomap之间选择

证据支持一个简单的决策规则。当非常大型图上的速度最重要、且轻微连接不良的社区可以接受时，或者当所使用的工具将其作为内置默认方法时，选择Louvain。当必须保证社区连通性时，当网络大型且难以划分（高混合）时，或当优化CPM以避开分辨率极限时，选择Leiden；在这些情况下它也可能更快。当社区规模可能较小时，选择Infomap，因为模块度的分辨率极限主要在社区相对较大时有利于Louvain<sup>[17](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0159161)</sup><sup> • </sup><sup>[12](https://www.pnas.org/doi/full/10.1073/pnas.0605965104)</sup>。

## References

1. [Blondel, Guillaume, Lambiotte, Lefebvre (2008). Fast unfolding of communities in large networks. JSTAT P10008.](https://iopscience.iop.org/article/10.1088/1742-5468/2008/10/P10008)
2. [Louvain method for community detection in large networks, original authors' page, UCLouvain.](https://perso.uclouvain.be/vincent.blondel/research/louvain.html)
3. [Fast unfolding of communities in large networks: 15 years later. JSTAT (2024).](https://iopscience.iop.org/article/10.1088/1742-5468/ad6139)
4. [Traag, Waltman, van Eck (2019). From Louvain to Leiden: guaranteeing well-connected communities. Scientific Reports.](https://pmc.ncbi.nlm.nih.gov/articles/PMC6435756/)
5. [Fast Leiden Algorithm for Community Detection in Shared Memory Setting. ACM (2024).](https://dl.acm.org/doi/fullHtml/10.1145/3673038.3673146)
6. [igraph Reference Manual, Community detection.](https://igraph.org/c/html/main/igraph-Community.html)
7. [Ghosh et al. (2018). Distributed Louvain Algorithm for Graph Community Detection. IPDPS.](https://eecs.wsu.edu/~ananth/papers/Ghosh_IPDPS18.pdf)
8. [louvain_communities, NetworkX documentation.](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.community.louvain.louvain_communities.html)
9. [scanpy.tl.louvain, Scanpy documentation.](https://scanpy.scverse.org/en/stable/generated/scanpy.tl.louvain.html)
10. [cluster_louvain, igraph R manual.](https://r.igraph.org/reference/cluster_louvain.html)
11. [Community detection, Gephi Manual.](https://jveerbeek.gitlab.io/gephi/docs/community.html)
12. [Fortunato, Barthélemy. Resolution limit in community detection. PNAS.](https://www.pnas.org/doi/full/10.1073/pnas.0605965104)
13. [Limits of modularity maximization in community detection. Phys. Rev. E.](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.84.066122)
14. [On the Landscape of Graph Clustering at Scale. Virginia Tech.](https://vtechworks.lib.vt.edu/server/api/core/bitstreams/7d21b638-86e4-4bb3-a7cc-45cc3a15ad31/content)
15. [On Comparing and Enhancing Common Approaches to Network Community Detection. arXiv.](https://ar5iv.labs.arxiv.org/html/2108.13482)
16. [Faster unfolding of communities: Speeding up the Louvain algorithm. Phys. Rev. E (2015).](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.92.032801)
17. [Analysis of Network Clustering Algorithms and Cluster Quality Metrics at Scale. PLOS One.](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0159161)
18. [A comprehensive Evaluation of Community Detection algorithms. IEEE (2024).](https://doi.org/10.1109/compeng60905.2024.10741414)
19. [Louvain Community, NVIDIA cuGraph documentation.](https://docs.nvidia.com/cugraph/latest/graph_support/algorithms/louvain_community/index.html)
20. [Louvain, Neo4j Graph Data Science.](https://neo4j.com/docs/graph-data-science/current/algorithms/louvain/)
21. [CPU vs. GPU for Community Detection: GVE-Louvain and ν-Louvain. arXiv (2025).](https://arxiv.org/html/2501.19004v1)
22. [Designing Parallel Algorithms for Community Detection using Arachne. arXiv (2025).](https://arxiv.org/html/2507.06471)
23. [gLeiden: accelerated community detection algorithms using GPUs.](https://pmc.ncbi.nlm.nih.gov/articles/PMC12987761/)
24. [On the benchmarking of clustering algorithms for cell type detection in single-cell RNA sequencing data. bioRxiv (2025).](https://www.biorxiv.org/content/10.1101/2025.08.20.671270v1)
25. [How to Accelerate Community Detection in Python Using GPU-Powered Leiden. NVIDIA blog.](https://developer.nvidia.com/blog/how-to-accelerate-community-detection-in-python-using-gpu-powered-leiden/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Graph and network algorithms › Network analysis and centrality

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/louvain-method

English original: https://www.edgepedia.org/louvain-method
