在Python中使用 Girvan Newman 算法检测社交网络中的社区
先决条件Python基础、NetworkX基础
我们将使用 Girvan Newman 算法将图的节点划分为两个或多个社区。 Girvan Newman 算法移除具有最高介数的边缘,直到没有边缘剩余。介数是穿过它的节点对之间的最短路径的数量。
我们将使用 Girvan Newman 算法来完成这项任务。
算法:
- 创建一个包含 N 个节点及其边的图,或者采用像杠铃图这样的内置图。
- 计算图中所有存在边的介数。
- 现在删除所有具有最高介数的边。
- 现在重新计算受到边缘移除影响的所有边缘的介数。
- 现在重复步骤 3 和 4,直到没有边缘。
Python代码:
Python3
import networkx as nx
def edge_to_remove(g):
d1 = nx.edge_betweenness_centrality(g)
list_of_tuples = list(d1.items())
sorted(list_of_tuples, key = lambda x:x[1], reverse = True)
# Will return in the form (a,b)
return list_of_tuples[0][0]
def girvan(g):
a = nx.connected_components(g)
lena = len(list(a))
print (' The number of connected components are ', lena)
while (lena == 1):
# We need (a,b) instead of ((a,b))
u, v = edge_to_remove(g)
g.remove_edge(u, v)
a = nx.connected_components(g)
lena=len(list(a))
print (' The number of connected components are ', lena)
return a
# Driver Code
g = nx.barbell_graph(5,0)
a = girvan(g)
print ('Barbell Graph')
for i in a:
print (i.nodes())
print ('.............')
g1 = nx.karate_club_graph()
a1 = girvan(g1)
print ('Karate Club Graph')
for i in a1:
print (i.nodes())
print ('.............')
输出:
Barbell Graph
The number of connected components are 1
The number of connected components are 2
[0, 1, 2, 3, 4]
………….
[8, 9, 5, 6, 7]
………….
Karate Club Graph
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 1
The number of connected components are 2
[32, 33, 2, 8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
………….
[0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21]
………….