-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_wl.py
233 lines (206 loc) · 10.7 KB
/
k_wl.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
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
import numpy as np
from itertools import permutations
# hash in color initialization
# sub_A_all[i] --> color_all[i]
color_all = []
color_max = 0
sub_A_all = []
# hash in aggregation
# case_all[i] --> color_all_agg[i]
color_all_agg = []
color_max_agg = 0
case_all = []
def subgraph_A(A, index):
subgraph_nodes_count = len(index)
sub_A = np.zeros([subgraph_nodes_count, subgraph_nodes_count])
for i in range(subgraph_nodes_count):
for j in range(subgraph_nodes_count):
sub_A[i, j] = A[index[i], index[j]]
return sub_A
def get_index(index_count, nodes_count, k):
index = []
for i in range(0, k):
index.append(0)
for i in range(0, k):
index_count, index[k - i - 1] = divmod(index_count, nodes_count)
return index
def k_wl_test(A1, A2, k):
nodes_count = A1.shape[0]
size = []
for i in range(0, k):
size.append(nodes_count)
k_tuples_1 = np.zeros(size)
k_tuples_2 = np.zeros(size)
global color_all, color_max, sub_A_all
# color initialization A1===================================
index_count = 0
while index_count < nodes_count**k:
index_now = get_index(index_count, nodes_count, k)
index_count = index_count + 1
flag_is_colored = 0
for perm in permutations(range(k), k):
if flag_is_colored == 1:
break
index_pai = list(np.zeros([k, 1],dtype=int))#[0, 0, 0]
for iii in range(0, k):
index_pai[iii] = index_now[perm[iii]]
sub_A_pai = list(subgraph_A(A1, index_pai).flatten())
if sub_A_pai in sub_A_all:
if k == 2:
k_tuples_1[index_now[0], index_now[1]] = color_all[sub_A_all.index(sub_A_pai)]
if k == 3:
k_tuples_1[index_now[0], index_now[1], index_now[2]] = color_all[sub_A_all.index(sub_A_pai)]
if k == 4:
k_tuples_1[index_now[0], index_now[1], index_now[2], index_now[3]] = color_all[sub_A_all.index(sub_A_pai)]
flag_is_colored = 1
if flag_is_colored == 0:
sub_A_all.append(sub_A_pai)
color_max = color_max + 1
color_all.append(color_max)
if k == 2:
k_tuples_1[index_now[0], index_now[1]] = color_max
if k == 3:
k_tuples_1[index_now[0], index_now[1], index_now[2]] = color_max
if k == 4:
k_tuples_1[index_now[0], index_now[1], index_now[2], index_now[3]] = color_max
# color initialization A2===================================
index_count = 0
while index_count < nodes_count**k:
index_now = get_index(index_count, nodes_count, k)
index_count = index_count + 1
flag_is_colored = 0
for perm in permutations(range(k), k):
if flag_is_colored == 1:
break
index_pai = list(np.zeros([k, 1],dtype=int))#[0, 0, 0]
for iii in range(0, k):
index_pai[iii] = index_now[perm[iii]]
sub_A_pai = list(subgraph_A(A2, index_pai).flatten())
if sub_A_pai in sub_A_all:
if k == 2:
k_tuples_2[index_now[0], index_now[1]] = color_all[sub_A_all.index(sub_A_pai)]
if k == 3:
k_tuples_2[index_now[0], index_now[1], index_now[2]] = color_all[sub_A_all.index(sub_A_pai)]
if k == 4:
k_tuples_2[index_now[0], index_now[1], index_now[2], index_now[3]] = color_all[sub_A_all.index(sub_A_pai)]
flag_is_colored = 1
if flag_is_colored == 0:
sub_A_all.append(sub_A_pai)
color_max = color_max + 1
color_all.append(color_max)
if k == 2:
k_tuples_2[index_now[0], index_now[1]] = color_max
if k == 3:
k_tuples_2[index_now[0], index_now[1], index_now[2]] = color_max
if k == 4:
k_tuples_2[index_now[0], index_now[1], index_now[2], index_now[3]] = color_max
# print('t = 0')
if list(np.sort(k_tuples_1.flatten())) != list(np.sort(k_tuples_2.flatten())):
print('non-isomorphic')
return 0
global color_all_agg, color_max_agg, case_all
# color aggregation===================================
k_tuples_1_t = np.zeros_like(k_tuples_1)
k_tuples_2_t = np.zeros_like(k_tuples_2)
flag_stop = 0
for t in range(1, nodes_count**k + 1):
if flag_stop == 1:
break
print('t =', t)
# A1 color aggregation===================================
index_count = 0
while index_count < nodes_count**k:
index_now = get_index(index_count, nodes_count, k)
index_count = index_count + 1
if k == 2:
neighbor_1th = list(np.sort(k_tuples_1[:,index_now[1]]))
neighbor_2th = list(np.sort(k_tuples_1[index_now[0],:]))
case_now = [k_tuples_1[index_now[0], index_now[1]], neighbor_1th, neighbor_2th]
if k == 3:
neighbor_1th = list(np.sort(k_tuples_1[:, index_now[1], index_now[2]]))
neighbor_2th = list(np.sort(k_tuples_1[index_now[0], :, index_now[2]]))
neighbor_3th = list(np.sort(k_tuples_1[index_now[0], index_now[1], :]))
case_now = [k_tuples_1[index_now[0], index_now[1], index_now[2]], neighbor_1th, neighbor_2th, neighbor_3th]
if k == 4:
neighbor_1th = list(np.sort(k_tuples_1[:, index_now[1], index_now[2], index_now[3]]))
neighbor_2th = list(np.sort(k_tuples_1[index_now[0], :, index_now[2], index_now[3]]))
neighbor_3th = list(np.sort(k_tuples_1[index_now[0], index_now[1], :, index_now[3]]))
neighbor_4th = list(np.sort(k_tuples_1[index_now[0], index_now[1], index_now[2], :]))
case_now = [k_tuples_1[index_now[0], index_now[1], index_now[2], index_now[3]], neighbor_1th, neighbor_2th, neighbor_3th, neighbor_4th]
if case_now in case_all:
if k == 2:
k_tuples_1_t[index_now[0], index_now[1]] = color_all_agg[case_all.index(case_now)]
if k == 3:
k_tuples_1_t[index_now[0], index_now[1], index_now[2]] = color_all_agg[case_all.index(case_now)]
if k == 4:
k_tuples_1_t[index_now[0], index_now[1], index_now[2], index_now[3]] = color_all_agg[case_all.index(case_now)]
else:
color_max_agg = color_max_agg + 1
color_all_agg.append(color_max_agg)
case_all.append(case_now)
if k == 2:
k_tuples_1_t[index_now[0], index_now[1]] = color_max_agg
if k == 3:
k_tuples_1_t[index_now[0], index_now[1], index_now[2]] = color_max_agg
if k == 4:
k_tuples_1_t[index_now[0], index_now[1], index_now[2], index_now[3]] = color_max_agg
if list(k_tuples_1_t.flatten()) == list(k_tuples_1.flatten()):
print('Graph 1 reach equilibrium')
flag_stop = 1
# print(k_tuples_1_t)
k_tuples_1 = k_tuples_1_t
k_tuples_1_t = np.zeros_like(k_tuples_1)
# A2 color aggregation===================================
index_count = 0
while index_count < nodes_count**k:
index_now = get_index(index_count, nodes_count, k)
index_count = index_count + 1
if k == 2:
neighbor_1th = list(np.sort(k_tuples_2[:,index_now[1]]))
neighbor_2th = list(np.sort(k_tuples_2[index_now[0],:]))
case_now = [k_tuples_2[index_now[0], index_now[1]], neighbor_1th, neighbor_2th]
if k == 3:
neighbor_1th = list(np.sort(k_tuples_2[:, index_now[1], index_now[2]]))
neighbor_2th = list(np.sort(k_tuples_2[index_now[0], :, index_now[2]]))
neighbor_3th = list(np.sort(k_tuples_2[index_now[0], index_now[1], :]))
case_now = [k_tuples_2[index_now[0], index_now[1], index_now[2]], neighbor_1th, neighbor_2th, neighbor_3th]
if k == 4:
neighbor_1th = list(np.sort(k_tuples_2[:, index_now[1], index_now[2], index_now[3]]))
neighbor_2th = list(np.sort(k_tuples_2[index_now[0], :, index_now[2], index_now[3]]))
neighbor_3th = list(np.sort(k_tuples_2[index_now[0], index_now[1], :, index_now[3]]))
neighbor_4th = list(np.sort(k_tuples_2[index_now[0], index_now[1], index_now[2], :]))
case_now = [k_tuples_2[index_now[0], index_now[1], index_now[2], index_now[3]], neighbor_1th, neighbor_2th, neighbor_3th, neighbor_4th]
if case_now in case_all:
if k == 2:
k_tuples_2_t[index_now[0], index_now[1]] = color_all_agg[case_all.index(case_now)]
if k == 3:
k_tuples_2_t[index_now[0], index_now[1], index_now[2]] = color_all_agg[case_all.index(case_now)]
if k == 4:
k_tuples_2_t[index_now[0], index_now[1], index_now[2], index_now[3]] = color_all_agg[case_all.index(case_now)]
else:
color_max_agg = color_max_agg + 1
color_all_agg.append(color_max_agg)
case_all.append(case_now)
if k == 2:
k_tuples_2_t[index_now[0], index_now[1]] = color_max_agg
if k == 3:
k_tuples_2_t[index_now[0], index_now[1], index_now[2]] = color_max_agg
if k == 4:
k_tuples_2_t[index_now[0], index_now[1], index_now[2], index_now[3]] = color_max_agg
if list(k_tuples_2_t.flatten()) == list(k_tuples_2.flatten()):
print('Graph 2 reach equilibrium')
flag_stop = 1
# print(k_tuples_2_t)
k_tuples_2 = k_tuples_2_t
k_tuples_2_t = np.zeros_like(k_tuples_2)
if list(np.sort(k_tuples_1.flatten())) != list(np.sort(k_tuples_2.flatten())):
print('non-isomorphic')
return 0
# print('============hash===========')
# for i in range(0, len(case_all)):
# print(case_all[i], 'color:', color_all_agg[i])
if list(np.sort(k_tuples_1.flatten())) != list(np.sort(k_tuples_2.flatten())):
print('non-isomorphic')
return 0
print('maybe isomorphic')
return 1