-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathlabel_propagation.py
More file actions
66 lines (48 loc) · 1.85 KB
/
label_propagation.py
File metadata and controls
66 lines (48 loc) · 1.85 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
import random
import networkx as nx
def propagate_label_sync(graph, max_iteration=100):
old_label_dict = {}
def rand_init_labels():
for n in graph.nodes():
# r_label = random.sample(graph.nodes(), 1) if len(graph.neighbors(n)) != 0 else [n]
r_label = [n]
graph.node[n]['communities'] = r_label
old_label_dict[n] = r_label
return
rand_init_labels()
for iter_num in xrange(max_iteration):
node_to_coms = {}
nodes = list(nx.nodes(graph))
random.shuffle(nodes)
for n in graph.nodes():
n_neighbors = nx.neighbors(graph, n)
if len(n_neighbors) < 1:
continue
label_freq_dict = {}
def update_label_freq_dict():
for nn in n_neighbors:
communities_nn = [nn]
if nn in old_label_dict:
communities_nn = old_label_dict[nn]
for nn_c in communities_nn:
if nn_c not in label_freq_dict:
label_freq_dict[nn_c] = 1
label_freq_dict[nn_c] += 1
update_label_freq_dict()
labels = []
max_freq = -1
for l, c in label_freq_dict.items():
if c > max_freq:
max_freq = c
labels = [l]
elif c == max_freq:
labels.append(l)
node_to_coms[n] = random.sample(labels, 1)
if n not in old_label_dict or set(node_to_coms[n]) != set(old_label_dict[n]):
old_label_dict[n] = node_to_coms[n]
graph.node[n]['communities'] = labels
print old_label_dict
if __name__ == '__main__':
karate_graph = nx.karate_club_graph()
propagate_label_sync(karate_graph)
print