The Louvain method for community detection内含Java代码。
Louvain method是一个非重叠社团发现的算法,该方法具有较快的执行效率。
对应的paper地址:Louvain method: Finding communities in large networks
Paper作者对算法的介绍网址:http://perso.uclouvain.be/vincent.blondel/research/louvain.html
c++版原作者代码:https://sourceforge.net/projects/louvain/
Rabbit: 复现+并行版本
Engineering Parallel Algorithms forCommunity Detection in Massive Networks, 并行版本
Fast parallel algorithm for unfolding of communities in large graphs
博客介绍:
[社区发现相关算法]
Modularity的计算方法——社团检测中模块度计算公式详解
模块度与Louvain社区发现算法
https://www.cnblogs.com/allanspark/p/4197980.html
基于模块度在有向图中进行社区发现
Community structure in directed networks
pip3 install python-louvain
pip3 install networkx
测试:
# basic usage
import community as community_louvain
import networkx as nx
G = nx.erdos_renyi_graph(100, 0.01)
partion = community_louvain.best_partition(G)
print(partion)
result
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 3, 7: 6, 8: 7, 9: 8, 10: 9, 11: 10, 12: 11, 13: 12, 14: 6, 15: 5, 16: 1, 17: 31, 18: 14, 19: 3, 20: 15, 21: 16, 22: 17, 23: 10, 24: 6, 25: 31, 26: 18, 27: 0, 28: 19, 29: 5, 30: 20, 31: 21, 32: 20, 33: 20, 34: 22, 35: 23, 36: 24, 37: 25, 38: 26, 39: 3, 40: 27, 41: 15, 42: 3, 43: 20, 44: 1, 45: 1, 46: 28, 47: 5, 48: 29, 49: 30, 50: 6, 51: 20, 52: 32, 53: 7, 54: 1, 55: 20, 56: 5, 57: 31, 58: 33, 59: 31, 60: 15, 61: 6, 62: 34, 63: 15, 64: 35, 65: 24, 66: 31, 67: 36, 68: 37, 69: 3, 70: 6, 71: 1, 72: 15, 73: 38, 74: 39, 75: 37, 76: 20, 77: 1, 78: 5, 79: 31, 80: 31, 81: 6, 82: 36, 83: 31, 84: 41, 85: 42, 86: 3, 87: 43, 88: 15, 89: 31, 90: 20, 91: 6, 92: 5, 93: 3, 94: 40, 95: 13, 96: 6, 97: 20, 98: 4, 99: 31}
测试真实数据:
import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
edgestring = ''
path = 'test_weighted_0.e'
# path = 'dataset/web3.txt'
with open(path, 'r+', encoding='utf-8') as f:
edgestring = f.read()
edgeList = edgestring.split('\n')
G = nx.DiGraph()
print('加入边集...')
nodes_set = set()
for edge in edgeList:
if len(edge) <= 0:
continue
edge = edge.split(' ')
G.add_edge(int(edge[0]) , int(edge[1]) ) #一次添加一条边
print(edge[0], edge[1])
# 绘图
# nx.draw(G, with_labels=True , node_size = 200)
# plt.show()
start = datetime.datetime.now()
pr = nx.pagerank(G, alpha=0.85, personalization=None,
max_iter=100, tol=1.0e-5, nstart=None,
dangling=None)
end = datetime.datetime.now()
for i in range(0, len(G.nodes)):
print(i, pr[i])
print('用时:', end-start)
# print('G:', G.edges)
# print('G:', G.nodes)
paper
code