-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReWeighting.py
150 lines (87 loc) · 4.27 KB
/
ReWeighting.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 11:05:46 2019
@author: georgiabaltsou
Re Weighting of edges based on 2011-Berry-Tolerating the community detection resolution limit with edge weighting
taking into account only the hint node and its adjacent edges
"""
import networkx as nx
import numpy as np
pastSeed = ' '
def findNeighboorOfu(graphRW,u):
neighbors = []
for i in graphRW.neighbors(u):
neighbors.append(i)
return neighbors
def calculateCycles(seed, graphRW, graphRWdict):
global pastSeed
global cliques
global cycls_3
if seed != pastSeed:
cliques = nx.cliques_containing_node(graphRW, seed) #triangles
cycls_3 = [ x for x in cliques if len(x)==3 ]
#Compute the denominator We
WeDict = {}
for edge in graphRW.edges(seed):
We = 0
EuUEv = []
source, target = edge
EuUEv = np.union1d(list(graphRW.neighbors(source)), list(graphRW.neighbors(target)))
for node in EuUEv:
if node == source:
continue
if (source,node) in graphRW.edges(seed):
temp1 = float(graphRWdict[source][node])
We += temp1
elif (target,node) in graphRW.edges(seed):
temp1 = float(graphRWdict[target][node])
We += temp1
#to add the edge weight too
temp1 = float(graphRWdict[source][target])
We += temp1
WeDict[edge] = We
#Compute the nominator Ge
GeDict = {}
for edge in graphRW.edges(seed):
Ge = 0
EuUEv = []
source, target = edge
EuUEv = np.union1d(list(graphRW.neighbors(source)), list(graphRW.neighbors(target)))
Te = 0 #number of cyrcles e participates in
TeList = []
for i in cycls_3:
if source in i and target in i:
Te += 1
TeList.append(i)
intersection = np.intersect1d(EuUEv,TeList )
for node in intersection:
if node == source:
continue
if (source,node) in graphRW.edges(seed):
temp1 = float(graphRWdict[source][node])
Ge += temp1
elif (target,node) in graphRW.edges(seed):
temp1 = float(graphRWdict[target][node])
Ge += temp1
#to add the edge weight too
temp1 = float(graphRWdict[source][target])
Ge += temp1
GeDict[edge] = Ge
Ce = {}
for key, value in WeDict.items():
for key2, value2 in GeDict.items():
if(key==key2):
Ce[key] = value/value2
for u,v in graphRW.edges(seed):
graphRWdict[u][v] = Ce[u,v]
graphRWdict[v][u] = Ce[u,v]
pastSeed = seed
return nx.Graph(graphRWdict), graphRWdict
def reWeighting(seeds, graphRW, graphRWdict):
for node in graphRWdict:
if node in seeds:
graphRW, graphRWdict = calculateCycles(node, graphRW, graphRWdict)
for source, target in graphRW.edges():
graphRW[source][target]['weight'] = graphRWdict[source][target]
return graphRW, graphRWdict