-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_refinement.py
More file actions
252 lines (180 loc) · 5.8 KB
/
color_refinement.py
File metadata and controls
252 lines (180 loc) · 5.8 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 26 15:15:12 2022
@author: longlee
"""
import numpy as np
import networkx as nx
G1 = nx.Graph()
# G1.add_edge(1,2)
# G1.add_edge(1,4)
# G1.add_edge(1,5)
# G1.add_edge(1,6)
# G1.add_edge(2,3)
# G1.add_edge(2,4)
# G1.add_edge(3,4)
G1.add_edge(1,2)
G1.add_edge(2,3)
G1.add_edge(3,4)
G1.add_edge(4,5)
G1.add_edge(5,6)
G1.add_edge(6,1)
G1.add_edge(4,7)
G1.add_edge(7,8)
G1.add_edge(8,9)
G1.add_edge(9,10)
G1.add_edge(10,5)
G2 = nx.Graph()
# G2.add_edge(1,2)
# G2.add_edge(1,3)
# G2.add_edge(1,4)
# G2.add_edge(1,6)
# G2.add_edge(2,3)
# G2.add_edge(3,4)
# G2.add_edge(4,5)
G2.add_edge(1,2)
G2.add_edge(2,3)
G2.add_edge(3,4)
G2.add_edge(4,5)
G2.add_edge(5,1)
G2.add_edge(4,6)
G2.add_edge(6,7)
G2.add_edge(7,8)
G2.add_edge(8,9)
G2.add_edge(9,10)
G2.add_edge(10,6)
ini_colo_1 = {}
for node in G1.nodes():
ini_colo_1[node] = str(1)
col_va_1 = {}
col_va_1[str(1)] = len(ini_colo_1.values())
ini_colo_2 = {}
for node in G2.nodes():
ini_colo_2[node] = str(1)
col_va_2 = {}
col_va_2[str(1)] = len(ini_colo_2.values())
Col = {1:[str(1)]}
def col_refinement(G_1,G_2,col_1,col_2,colv_1,colv_2,Col):
'''
Parameters
----------
G_1 : Graph1.
G_2 : Graph2.
col_1 :{v1:color1,v2:color2,...}.
col_2 :{vi:colori,vj:colorj,...}.
colv_1 :{color1:num1,...}.
colv_2 :{coloro:numi}.
Col : TYPE
DESCRIPTION.
Returns
-------
None.
'''
#1.聚合颜色
Nei_col_1 = {}
for node in G_1.nodes():
tem = []
tem.append(col_1[node])
# tem = col_1[node]
for adj in G_1[node]:
tem.append(col_1[adj])
Nei_col_1[node] = tem
#将邻居的颜色做到映射里面
L = []
for tup_val in list(Col.values()):
L.append(len(tup_val))
if len(tem) not in L:
Col[len(Col) +1] = tem
else:
t = 0
T = 0
for tup_val in list(Col.values()):
if len(tup_val) == len(tem):
T += 1
if set(tup_val) == set(tem):
mui_col = []
for ide in range(len(tup_val)):
non = tup_val[ide]
mui_col.append(tem.count(non)-tup_val.count(non))
if mui_col != [0]*len(mui_col):
Col[len(Col) +1] = tem
break
else:
t += 1
if T == t != 0:
Col[len(Col) +1] = tem
# print(Nei_col_1)
# print(Col)
Nei_col_2 = {}
for node in G_2.nodes():
tem = list()
tem.append(col_2[node])
for adj in G_2[node]:
tem.append(col_2[adj])
Nei_col_2[node] = tem
L = []
for tup_val in list(Col.values()):
L.append(len(tup_val))
if len(tem) not in L:
Col[len(Col) +1] = tem
else:
t = 0
T = 0
for tup_val in list(Col.values()):
if len(tup_val) == len(tem):
T += 1
if set(tup_val) == set(tem):
mui_col = []
for ide in range(len(tup_val)):
non = tup_val[ide]
mui_col.append(tem.count(non)-tup_val.count(non))
if mui_col != [0]*len(mui_col):
Col[len(Col) +1] = tem
break
else:
t += 1
if T == t != 0:
Col[len(Col) +1] = tem
print(Nei_col_1)
# print(Nei_col_2)
print(Col)
#2.哈希映射
for node in G_1.nodes():
nei = Nei_col_1[node]
for k,v in Col.items():
if len(v) == len(nei):
if set(v) == set(nei):
mui_col = []
for ide in range(len(v)):
non = v[ide]
mui_col.append(nei.count(non)-v.count(non))
if mui_col == [0]*len(mui_col):
col_1[node] = str(k)
print('G1:',col_1)
for val in Col.keys():
if str(val) not in colv_1.keys():
colv_1[str(val)] = list(col_1.values()).count(str(val))
for node in G_2.nodes():
nei = Nei_col_2[node]
for k,v in Col.items():
if len(v) == len(nei):
if set(v) == set(nei):
mui_col = []
for ide in range(len(v)):
non = v[ide]
mui_col.append(nei.count(non)-v.count(non))
if mui_col == [0]*len(mui_col):
col_2[node] = str(k)
for val in Col.keys():
if str(val) not in colv_2.keys():
colv_2[str(val)] = list(col_2.values()).count(str(val))
return col_1,col_2,colv_1,colv_2,Col
ite = 0
while ite < 4:
ini_colo_1,ini_colo_2,col_va_1,col_va_2,Col = col_refinement(G1,G2,ini_colo_1,ini_colo_2,col_va_1,col_va_2,Col)
ite += 1
V1 = np.array(list(col_va_1.values()))
V2 = np.array(list(col_va_2.values()))
prod_1 = np.dot(V1.T,V2)
prod_true = np.dot(V1.T,V1)