1

我有一个图表g,我想使用流体社区算法Girvan-Newman 方法来检测g. 我预定数量的社区是其中之一[2, 3, 4, 5]。为了选择最佳数量的社区best_k,我使用了质量覆盖

因为 Girvan-Newman 方法返回best_k = 2其中一个社区仅包含3节点。我怀疑我做错了什么。您能否检查我是否正确实施了这些方法?

import urllib3
import io
import networkx as nx
from networkx.algorithms import community
import matplotlib.pyplot as plt

## Import dataset
http = urllib3.PoolManager()
url = 'https://raw.githubusercontent.com/leanhdung1994/WebMining/main/lesmis.gml'
f = http.request('GET', url)
data = io.BytesIO(f.data)
g = nx.read_gml(data)

## Define a function to add community attributes
def add_att(g, att, att_name):
    att = list(att)
    for i in g.nodes:
        for j in range(len(att)):
            if i in list(att[j]):
                nx.set_node_attributes(g, {i: j}, name = att_name)
                break            

list_k = [2, 3, 4, 5]
best_k = 2
qua_cov = 0

## Fluid communities algorithm
for k in list_k:
    fluid = community.asyn_fluid.asyn_fluidc(g, k) # fluid is the partition of nodes
    fluid = list(fluid)
    cur_qua_cov = community.quality.coverage(g, fluid) # compute quality coverage
    if cur_qua_cov >= qua_cov:
        qua_cov = cur_qua_cov
        best_k = k

commu_fluid = community.asyn_fluid.asyn_fluidc(g, best_k)
            
## Girvan-Newman method
commu_gn = community.centrality.girvan_newman(g) 
commu_gn = list(commu_gn) # commu_gn is the list of partitions of nodes
best_k = 2
qua_cov = 0

for k in list_k:
    cur_qua_cov = community.quality.coverage(g, commu_gn[k - 2])
    if cur_qua_cov >= qua_cov:
        qua_cov = cur_qua_cov
        best_k = k

commu_gn = commu_gn[best_k - 2]

## Add attributes
att = [commu_fluid, commu_gn]
att_name = ['commu_fluid', 'commu_gn']
for i in range(len(att)):
    add_att(g, att[i], att_name[i])
4

0 回答 0