-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclusteror.py
More file actions
85 lines (71 loc) · 2.93 KB
/
Copy pathclusteror.py
File metadata and controls
85 lines (71 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 集成多种图聚类方式的聚类器
# 包括:结构熵极小化、Louvain、Leiden、算法信息论算法、Infomap、CNM、GNN
# 输入:nx.Graph无向无权连通图
# 输出:聚类结果(嵌套列表)
import networkx as nx
from igraph import Graph
import networkx.algorithms.community as nx_comm
from encoding_tree import EncodingTree
from utils.example_graph import example_g1
class Clusteror:
def __init__(self):
pass
def label_propagation_cluster(self, graph: nx.Graph):
part = nx_comm.label_propagation_communities(graph)
# print('partition num:', len(part))
part = [list(x) for x in part]
return part
def louvain_cluster(self, graph: nx.Graph):
part = nx_comm.louvain_communities(graph)
# print('partition num:', len(part))
part = [list(x) for x in part]
return part
def infomap_cluster(self, graph: nx.Graph):
g = Graph.from_networkx(graph)
part = Graph.community_infomap(g)
result = [[g.vs['_nx_name'][j] for j in part[i]] for i in range(len(part))]
return result
# too fast
def leading_eigenvector_cluster(self, graph: nx.Graph):
g = Graph.from_networkx(graph)
part = Graph.community_leading_eigenvector(g)
result = [[g.vs['_nx_name'][j] for j in part[i]] for i in range(len(part))]
return result
# too fast
def multilevel_cluster(self, graph: nx.Graph):
g = Graph.from_networkx(graph)
part = Graph.community_multilevel(g)
result = [[g.vs['_nx_name'][j] for j in part[i]] for i in range(len(part))]
return result
# too slow
def minSE_cluster(self, graph: nx.Graph):
etc = EncodingTree(graph)
etc.greedy_minSE_2d()
return etc.get_2d_communities()
# too slow
def girvan_newman_cluster(self, graph: nx.Graph):
part = nx_comm.girvan_newman(graph)
# print('partition num:', len(part))
part = [list(x) for x in part]
return part
# too slow
def asyn_fluidc_cluster(self, graph: nx.Graph):
part = nx_comm.asyn_fluidc(graph)
# print('partition num:', len(part))
part = [list(x) for x in part]
return part
# bad performance
def leiden_cluster(self, graph: nx.Graph):
g = Graph.from_networkx(graph)
part = Graph.community_leiden(g, objective_function='modularity')
result = [[g.vs['_nx_name'][j] for j in part[i]] for i in range(len(part))]
return result
# too fast & pending
# def label_propagation_cluster(self, graph: nx.Graph):
# g = Graph.from_networkx(graph)
# part = Graph.community_label_propagation(g)
# result = [[g.vs['_nx_name'][j] for j in part[i]] for i in range(len(part))]
# return result
if __name__ == '__main__':
cl = Clusteror()
print(cl.infomap_cluster(example_g1))