-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReWeightingAllNodes.py
142 lines (93 loc) · 3.64 KB
/
ReWeightingAllNodes.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 15 13:13:39 2019
@author: georgiabaltsou
"""
import networkx as nx
import numpy as np
import csv
import os
def findNeighboorOfu(G,u):
neighbors = []
for i in G.neighbors(u):
neighbors.append(i)
return neighbors
def reWeighting(file):
G = nx.Graph()
G = nx.read_weighted_edgelist(file, create_using=nx.Graph(), delimiter=";", encoding='utf-8-sig')
cycls_3 = [c for c in nx.cycle_basis(G) if len(c)==3] #cycles
#cycls_4 = [c for c in nx.cycle_basis(G) if len(c)==4] #rectangles
#Compute the denominator We
WeDict = {}
for edge in G.edges():
We = 0
EuUEv = []
source, target = edge
EuUEv = np.union1d(list(G.neighbors(source)), list(G.neighbors(target)))
for node in EuUEv:
if node == source:
continue
if (source,node) in G.edges:
weightForux = G.get_edge_data(source, node)
temp1 = weightForux['weight']
We += temp1
elif (target,node) in G.edges:
weightForux = G.get_edge_data(target, node)
temp1 = weightForux['weight']
We += temp1
#to add the edge weight too
weightForux = G.get_edge_data(source, target)
temp1 = weightForux['weight']
We += temp1
WeDict[edge] = We
#Compute the nominator Ge
GeDict = {}
for edge in G.edges():
Ge = 0
EuUEv = []
source, target = edge
EuUEv = np.union1d(list(G.neighbors(source)), list(G.neighbors(target)))
Te = 0 #number of cyrcles e participates in
# Re = 0 #number of rectangles e participates in
TeList = []
# ReList = []
for i in cycls_3:
if source in i and target in i:
Te += 1
TeList.append(i)
# for i in cycls_4:
# if source in i and target in i:
# Re += 1
# ReList.append(i)
# TeListUReList = np.union1d(TeList, ReList)
intersection = np.intersect1d(EuUEv,TeList )
for node in intersection:
if node == source:
continue
if (source,node) in G.edges:
weightForux = G.get_edge_data(source, node)
temp1 = weightForux['weight']
Ge += temp1
elif (target,node) in G.edges:
weightForux = G.get_edge_data(target, node)
temp1 = weightForux['weight']
Ge += temp1
#to add the edge weight too
weightForux = G.get_edge_data(source, target)
temp1 = weightForux['weight']
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
#change the graph's weights after the re-calculating
for u,v,d in G.edges(data=True):
d['weight'] = Ce[u,v]
with open(str(file), 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=';')
for key, value in Ce.items():
writer.writerow([key[0], key[1], value])
return str(file), G