-
Notifications
You must be signed in to change notification settings - Fork 1
/
ULLMANN.c
187 lines (180 loc) · 4.43 KB
/
ULLMANN.c
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
/**
* 这个是自己重写的版本,原文中的版本采用了大量的goto语句,可读性与可维护性都不好,所以采用递归的方式重写了一次
* */
int is_isomorphism(int *A, int *B, int pa, int pb, int *M)
{
int temp[pa * pb];
// M*B
for (int i = 0; i < pa; i++)
{
for (int j = 0; j < pb; j++)
{
temp[i * pb + j] = 0;
for (int k = 0; k < pb; k++)
{
temp[i * pb + j] += *(M + i * pb + k) * (*(B + k * pb + j));
}
}
}
int temp_t[pb * pa];
// 转置
for (int i = 0; i < pb; i++)
{
for (int j = 0; j < pa; j++)
{
temp_t[i * pa + j] = temp[j * pb + i];
}
}
int C[pa * pa];
// M*转置
for (int i = 0; i < pa; i++)
{
for (int j = 0; j < pa; j++)
{
C[i * pa + j] = 0;
for (int k = 0; k < pb; k++)
{
C[i * pa + j] += M[i * pb + k] * temp_t[k * pa + j];
}
}
}
int sign = 1;
for (int i = 0; i < pa; i++)
{
for (int j = 0; j < pa; j++)
{
if (*(A + i * pa + j) == 1 && *(A + i * pa + j) != C[i * pa + j])
{
sign = 0;
break;
}
}
}
return sign;
}
// 判断是否已经被选中
int chosen(int column[], int depth, int value)
{
for (int i = 0; i < depth; i++)
{
if (column[i] == value)
{
return 1;
}
}
return 0;
}
void pick(int *A, int *B, int pa, int pb, int *M0, int *result, int *result_length, int column[], int depth)
{
if (depth == pa)
{
int M[pa * pb];
for (int i = 0; i < pa; i++)
{
int chosen = column[i];
for (int j = 0; j < pb; j++)
{
int value = 0;
if (j == chosen)
{
value = 1;
}
M[i * pb + j] = value;
}
}
if (!is_isomorphism(A, B, pa, pb, M))
{
return;
}
int offset = 0;
for (int i = 0; i < pa; i++)
{
for (int j = 0; j < pb; j++)
{
int value = *(M + i * pb + j);
if (value)
{
*(result + 1 + *result_length * 2 * pa + offset++) = i;
*(result + 1 + *result_length * 2 * pa + offset++) = j;
}
}
}
(*result_length)++;
return;
};
for (int i = 0; i < pb; i++)
{
if (*(M0 + depth * pb + i) == 1 && !chosen(column, depth, i))
{
column[depth] = i;
pick(A, B, pa, pb, M0, result, result_length, column, depth + 1);
column[depth] = -1;
}
}
}
/**
* @param int A[][] 子图
* @param int B[][] 原图
* @param int pa 子图中点的个数
* @param int pb 原图中点的个数
*
**/
int *ULLMANN(int pa, int pb, int A[], int B[])
{
int result_length = 0;
int result[1000000];
// 一个pa * pb 的数组,如果M[i][j]中,Gb中第j个点的度大于等于Ga中第i个点的度,则设为1,这样可以有效排除不能同构的子图的点
int M0[pa * pb];
// 记录被选中列
int column[pa];
for (int i = 0; i < pa; i++)
{
column[i] = -1;
}
for (int i = 0; i < pa; i++)
{
int row = i * pb;
for (int j = 0; j < pb; j++)
{
M0[row + j] = 0;
}
}
for (int i = 0; i < pa; i++)
{
int count_a = 0;
for (int k = 0; k < pa; k++)
{
if (*(A + i * pa + k) == 1)
{
count_a++;
}
}
for (int j = 0; j < pb; j++)
{
int count_b = 0;
for (int k = 0; k < pb; k++)
{
if (*(B + j * pb + k) == 1)
{
count_b++;
}
}
if (count_b >= count_a)
{
M0[i * pb + j] = 1;
}
}
}
pick(A, B, pa, pb, M0, result, &result_length, column, 0);
result[0] = result_length;
return result;
}
int main()
{
int pa = 3;
int pb = 5;
int A[] = {0, 1, 0, 1, 0, 1, 0, 1, 0};
int B[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
ULLMANN(pa, pb, A, B);
return 0;
}