本文簡要介紹
networkx.algorithms.community.centrality.girvan_newman
的用法。用法:
girvan_newman(G, most_valuable_edge=None)
使用Girvan-Newman 方法在圖中查找社區。
- G:NetworkX 圖
- most_valuable_edge:函數
將圖形作為輸入並輸出邊的函數。此函數返回的邊將在算法的每次迭代中重新計算並刪除。
如果未指定,將使用具有最高
networkx.edge_betweenness_centrality()
的邊。
- 迭代器
迭代
G
中節點集的元組。每組節點是一個社區,每個元組是算法特定級別的社區序列。
參數:
返回:
注意:
Girvan-Newman 算法通過從原始圖中逐步刪除邊來檢測社區。該算法在每一步都刪除了“most valuable” 邊,傳統上是具有最高中介中心性的邊。隨著圖表分解成碎片,緊密結合的社區結構暴露出來,結果可以描繪為樹狀圖。
例子:
要獲得第一對社區:
>>> G = nx.path_graph(10) >>> comp = girvan_newman(G) >>> tuple(sorted(c) for c in next(comp)) ([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])
要僅獲取社區的前
k
元組,請使用itertools.islice()
:>>> import itertools >>> G = nx.path_graph(8) >>> k = 2 >>> comp = girvan_newman(G) >>> for communities in itertools.islice(comp, k): ... print(tuple(sorted(c) for c in communities)) ... ([0, 1, 2, 3], [4, 5, 6, 7]) ([0, 1], [2, 3], [4, 5, 6, 7])
要在社區數量大於
k
時停止獲取社區元組,請使用itertools.takewhile()
:>>> import itertools >>> G = nx.path_graph(8) >>> k = 4 >>> comp = girvan_newman(G) >>> limited = itertools.takewhile(lambda c: len(c) <= k, comp) >>> for communities in limited: ... print(tuple(sorted(c) for c in communities)) ... ([0, 1, 2, 3], [4, 5, 6, 7]) ([0, 1], [2, 3], [4, 5, 6, 7]) ([0, 1], [2, 3], [4, 5], [6, 7])
僅根據權重選擇要刪除的邊:
>>> from operator import itemgetter >>> G = nx.path_graph(10) >>> edges = G.edges() >>> nx.set_edge_attributes(G, {(u, v): v for u, v in edges}, "weight") >>> def heaviest(G): ... u, v, w = max(G.edges(data="weight"), key=itemgetter(2)) ... return (u, v) ... >>> comp = girvan_newman(G, most_valuable_edge=heaviest) >>> tuple(sorted(c) for c in next(comp)) ([0, 1, 2, 3, 4, 5, 6, 7, 8], [9])
在選擇具有例如最高中介中心性的邊時利用邊權重:
>>> from networkx import edge_betweenness_centrality as betweenness >>> def most_central_edge(G): ... centrality = betweenness(G, weight="weight") ... return max(centrality, key=centrality.get) ... >>> G = nx.path_graph(10) >>> comp = girvan_newman(G, most_valuable_edge=most_central_edge) >>> tuple(sorted(c) for c in next(comp)) ([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])
要為邊指定不同的排名算法,請使用
most_valuable_edge
關鍵字參數:>>> from networkx import edge_betweenness_centrality >>> from random import random >>> def most_central_edge(G): ... centrality = edge_betweenness_centrality(G) ... max_cent = max(centrality.values()) ... # Scale the centrality values so they are between 0 and 1, ... # and add some random noise. ... centrality = {e: c / max_cent for e, c in centrality.items()} ... # Add some random noise. ... centrality = {e: c + random() for e, c in centrality.items()} ... return max(centrality, key=centrality.get) ... >>> G = nx.path_graph(10) >>> comp = girvan_newman(G, most_valuable_edge=most_central_edge)
相關用法
- Python NetworkX get_edge_attributes用法及代碼示例
- Python NetworkX generate_multiline_adjlist用法及代碼示例
- Python NetworkX generic_node_match用法及代碼示例
- Python NetworkX gomory_hu_tree用法及代碼示例
- Python NetworkX generate_gml用法及代碼示例
- Python NetworkX generate_adjlist用法及代碼示例
- Python NetworkX gn_graph用法及代碼示例
- Python NetworkX generic_multiedge_match用法及代碼示例
- Python NetworkX greedy_color用法及代碼示例
- Python NetworkX generate_graphml用法及代碼示例
- Python NetworkX generic_edge_match用法及代碼示例
- Python NetworkX gnp_random_graph用法及代碼示例
- Python NetworkX greedy_modularity_communities用法及代碼示例
- Python NetworkX generate_edgelist用法及代碼示例
- Python NetworkX greedy_tsp用法及代碼示例
- Python NetworkX graphviz_layout用法及代碼示例
- Python NetworkX generic_weighted_projected_graph用法及代碼示例
- Python NetworkX global_reaching_centrality用法及代碼示例
- Python NetworkX global_parameters用法及代碼示例
- Python NetworkX groups用法及代碼示例
- Python NetworkX generate_gexf用法及代碼示例
- Python NetworkX gnr_graph用法及代碼示例
- Python NetworkX generalized_degree用法及代碼示例
- Python NetworkX goldberg_radzik用法及代碼示例
- Python NetworkX gaussian_random_partition_graph用法及代碼示例
注:本文由純淨天空篩選整理自networkx.org大神的英文原創作品 networkx.algorithms.community.centrality.girvan_newman。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。