给出了边缘列表,如以下代码所示:
import networkx as nx
from networkx.algorithms.community import greedy_modularity_communities
from networkx.algorithms.cuts import conductance
# Create a networkx graph object
my_graph = nx.Graph()
# Add edges to to the graph object
# Each tuple represents an edge between two nodes
my_graph.add_edges_from([
(1,2),
(1,3),
(3,4),
(1,5),
(3,5),
(4,2),
(2,3),
(3,0)])
# Draw the resulting graph
nx.draw(my_graph, with_labels=True, font_weight='bold')
# Modularity
c = list(greedy_modularity_communities(my_graph))
我们得到了一个削减:
[frozenset({0, 2, 3, 4}), frozenset({1, 5})]
如果我们看一下它的对应图:
为什么在这里为什么将节点1和5删除或视为一个良好的拆分,而不是将节点0从图的其余部分删除呢?
在此先感谢您的提示 最好的问候
答案 0 :(得分:1)
仅从名称greedy_modularity_communities
和documentation起,算法总是返回最佳分区的近似值。
对于您建议的分区,您可以简单地检查值:
from networkx.algorithms.community.quality import modularity
print(modularity(my_graph, [frozenset({0, 2, 3, 4}), frozenset({1, 5})]))
# 0.0546875
print(modularity(my_graph, [frozenset({1, 2, 3, 4, 5}), frozenset({0})]))
# -0.0078125