LFM算法是来源于论文《Detecting the overlapping and hieerarchical community structure in complex networks》,文档中包含该算法的python的源码,以及用到的数据集,仅供大家学习参考。
使用networkx工具包实现了LFM社区发现算法,并提供了模块度评价方法和可视化的方法。数据分析结果和可视化图片都已保存在文件中,代码可直接运行。
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 17:13:54 2019
@author: suibian
"""
import random
import networkx as nx
import matplotlib.pyplot as plt
import zipfile
#import urllib.request as urllib
class Community():
''' use set operation to optimize calculation '''
def __init__(self,G,alpha=1.0):
self.G = G
self.alpha = alpha
self._nodes = set()
self._k_in = 0
self._k_out = 0
def add_node(self,node):
neighbors = set(self.G.neighbors(node))
node_k_in = len(neighbors & self._nodes)
node_k_out = len(neighbors) - node_k_in
self._n