-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymbc.c
500 lines (432 loc) · 13.9 KB
/
pymbc.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <Python.h>
#include <numpy/arrayobject.h>
#include "structmember.h"
#include <stdbool.h>
#include <signal.h>
#ifndef NDEBUG
#include <stdio.h>
#endif
#include "bitset.h"
#include "bipartite_graph.h"
#include "lyu.h"
#if (PY_VERSION_HEX < 0x03000000)
#error "require Python 3.0"
#endif
extern int lyu_stop;
static void my_sigint_handler(int signum)
{
lyu_stop = 1;
}
#define SIGINT_TRAP sighandler_t old_sigint_handler = NULL; old_sigint_handler = signal(SIGINT, my_sigint_handler)
#define SIGINT_RECOVER if(old_sigint_handler) signal(SIGINT, old_sigint_handler)
static bitset python_numpy_uint64_to_bitset(PyObject *obj, size_t ndata)
{
bitset S = NULL;
void *ptr = NULL;
npy_intp shape[1];
if(obj == Py_None) {
S = bitset_alloc(ndata);
if(S == NULL) return NULL;
bitset_setall(S,ndata);
}
else {
if(PyArray_AsCArray(&obj,
(void *)&ptr,
shape,
1,
PyArray_DescrFromType(NPY_UINT64)) < 0) {
goto fail;
}
if(bitset_from_C_uint64_array(ptr, shape[0], &S, ndata, NULL) < 0) {
goto fail;
}
}
return S;
fail:
if(S) bitset_free(S);
if(ptr) PyArray_Free(obj, ptr);
return NULL;
}
static PyObject *bitset_to_python_list(bitset S, size_t ndata)
{
PyObject *ret = NULL;
bitset_iterator it;
Py_ssize_t idx;
ret = PyList_New(bitset_length(S, ndata));
if(ret == NULL) goto fail;
bitset_iterator_begin(&it, S, ndata);
idx = 0;
while(bitset_iterator_next(&it)) {
PyList_SetItem(ret, idx++, PyLong_FromUnsignedLongLong(it.current));
}
return ret;
fail:
Py_XDECREF(ret);
return NULL;
}
/*
(U,V,D, tau_u, tau_v, init_type, init_iter, seed, use_star, star_max_iter, optimize)
*/
static PyObject *maximum_biclique_search(PyObject *self, PyObject *args)
{
PyObject *objU, *objV, *objD;
uint64_t tau_u, tau_v;
unsigned int seed;
int init_type, init_iter;
int star, star_max_iter;
unsigned int optimize;
npy_intp shape[2];
uint64_t M, N;
size_t M_ndata, N_ndata;
int8_t **Dptr = NULL;
bitset U = NULL, V = NULL;
biclique *C = NULL;
bipartite_graph *G = NULL;
int ret_c;
PyObject *ret, *retU = NULL, *retV = NULL;
SIGINT_TRAP;
if(!PyArg_ParseTuple(args, "OOOKKiiIpiI",
&objU, &objV, &objD,
&tau_u, &tau_v,
&init_type, &init_iter,
&seed,
&star, &star_max_iter,
&optimize)) {
PyErr_SetString(PyExc_TypeError, "Unable to parse arguments");
goto fail;
}
if(init_iter <= 0) {
PyErr_SetString(PyExc_ValueError, "init_iter should be > 0");
goto fail;
}
if(star_max_iter <= 0) {
PyErr_SetString(PyExc_ValueError, "star_max_iter should be > 0");
goto fail;
}
if(PyArray_AsCArray(&objD,
(void *)&Dptr,
shape,
2,
PyArray_DescrFromType(NPY_BOOL)) < 0) {
PyErr_SetString(PyExc_TypeError, "Unable to convert D");
goto fail;
}
M = (uint64_t)shape[0];
M_ndata = bitset_required_capacity(M);
N = (uint64_t)shape[1];
N_ndata = bitset_required_capacity(N);
U = python_numpy_uint64_to_bitset(objU,M_ndata);
if(U == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to convert U");
goto fail;
}
V = python_numpy_uint64_to_bitset(objV,N_ndata);
if(V == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to convert V");
goto fail;
}
G = bipartite_graph_from_C_bool_array(M,N,U,V,Dptr);
if(!G) {
PyErr_SetString(PyExc_MemoryError, "Failed to allocate G");
goto fail;
}
if(lyu_random_init(seed)<0) {
PyErr_SetString(PyExc_RuntimeError, "Failed to setup random generator");
goto fail;
}
Py_BEGIN_ALLOW_THREADS;
lyu_stop = 0;
switch(init_type) {
case 0:
C = InitMBC_Best(G, init_iter);
break;
case 1:
C = InitMBC_Greedy(G, init_iter);
break;
case 2:
C = InitMBC_Prune(G);
break;
case 3:
C = InitMBC_Star(G, 0);
break;
case 4:
C = InitMBC_Star(G, 1);
break;
default:
C = NULL;
break;
}
Py_END_ALLOW_THREADS;
if(lyu_stop) {
PyErr_SetString(PyExc_KeyboardInterrupt, "SIGINT received inside pymbc");
goto fail;
}
if(!C) {
PyErr_SetString(PyExc_ValueError, "Failed to InitMBC, try another approach");
goto fail;
}
#ifndef NDEBUG
// bipartite_graph_dump(G);
/* printf("initial size (%lu,%lu) => %lu\n", */
/* biclique_U_length(C), */
/* biclique_V_length(C), */
/* biclique_size(C)); */
if(!biclique_is_biclique(G,C)) {
PyErr_SetString(PyExc_RuntimeError, "BUG #1");
goto fail;
}
#endif
Py_BEGIN_ALLOW_THREADS;
lyu_stop = 0;
if(star) {
ret_c = MBC_star(G, C, tau_u, tau_v, star_max_iter, optimize);
}
else {
ret_c = MBC(G, C, tau_u, tau_v);
}
Py_END_ALLOW_THREADS;
if(lyu_stop) {
PyErr_SetString(PyExc_KeyboardInterrupt, "SIGINT received inside pymbc");
goto fail;
}
if(ret_c < 0) {
PyErr_SetString(PyExc_RuntimeError, "MBC return error");
goto fail;
}
#ifndef NDEBUG
if(!biclique_is_biclique(G,C)) {
PyErr_SetString(PyExc_RuntimeError, "BUG #2");
goto fail;
}
#endif
retU = bitset_to_python_list(C->U, M_ndata);
if(retU == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to get back U");
goto fail;
}
retV = bitset_to_python_list(C->V, N_ndata);
if(retV == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to get back V");
goto fail;
}
bitset_free(U);
bitset_free(V);
PyArray_Free(objD, Dptr);
bipartite_graph_free(G);
biclique_free(C);
ret = Py_BuildValue("(OO)", retU, retV);
Py_DECREF(retU);
Py_DECREF(retV);
SIGINT_RECOVER;
return ret;
fail:
Py_XDECREF(retU);
Py_XDECREF(retV);
if(U) bitset_free(U);
if(V) bitset_free(V);
if(Dptr) PyArray_Free(objD, Dptr);
if(G) bipartite_graph_free(G);
if(C) biclique_free(C);
SIGINT_RECOVER;
return NULL;
}
static const char * const maximum_biclique_search_docstring =
"maximum_biclique_search(U: Optional[numpy.typing.NDArray[np.uint64]],\n"
" V: Optional[numpy.typing.NDArray[np.uint64]],\n"
" D: numpy.typing.NDArray[np.bool],\n"
" tau_u: int,\n"
" tau_v: int,\n"
" init_type: int,\n"
" init_iter: int,\n"
" seed: int,\n"
" ues_star: bool,\n"
" star_max_iter: int,\n"
" optimize: int) -> Tuple[List,List]\n"
"\n"
"Perform a maximum biclique search by using the algorithm MBC or MBC* on the paper.\n"
"Algorithm of InitMBC can be selected from 5 different options.\n"
"\n"
"Parameters:\n"
"U -- array of integers (starts from 0) indicate the vertices of one side of a bipartite graph (e.g. [0,1,2,4])) or None\n"
"V -- array of integers (starts from 0) indicate the vertices of another side of a bipartite graph (e.g. [0,3,5,6]) or None\n"
"D -- Boolean matrix with entries (i,j)=True if (U_i,V_j) is connected with an edge, False otherwise\n"
" **The code determines the size of the graph based on the shape of D. U and V should not contain entries greater than the size**\n"
"tau_u -- tau_u as on the paper (minimum number of U to search)\n"
"tau_v -- tau_v as on the paper (minimum number of V to search)\n"
"init_type -- Algorithm for InitMBC:\n"
" 0 - Best (try all below and use the one with maximum size)\n"
" 1 - Greedy (start from an empty graph and add vertex until it is not possible)\n"
" 2 - Prune (start from everything and remove vertex until it finds a biclique)\n"
" 3 - Star (start from a star with maximum size, check from U, i.e. U has one vertex)\n"
" 4 - Star (start from a star with maximum size, check from V, i.e. V has one vertex)\n"
"init_iter -- Greedy algorithm is randomized and indicate how many iterations to run\n"
"seed -- Random seed for any randomness in the algorithm\n"
"use_star -- Whether to use MBC* on the paper, if use_star==False, the program will only use MBC\n"
"star_max_iter -- the MAX_ITER variable on the paper, i.e. maximum of pruning step in MBC*\n"
"optimize -- Flags to indicate whether to use further optimization of Reduce2Hop written on the paper\n"
" 0 - Disable\n"
" 1 - Enable #1 (Early Pruning)\n"
" 2 - Enable #2 (Early Skipping)\n"
" 3 - Enable Both\n"
"\n"
"Return:\n"
"U -- List of the vertices of the found biclique on one side\n"
"V -- List of the vertices of the found biclique on another side\n"
"\n"
"Remarks:\n"
"(1) len(U) >= tau_u and len(V) >= tau_v are not guaranteed. The code will return the one found by InitMBC if MBC/MBC* is unable to find a better biclique\n"
"(2) The code raises a ValueError if no biclique is found during InitMBC. User should try different value of init_type. If init_type = 0 is already indicated, then there are no bicliques in the bipartite graph.\n"
"(3) If you get a TypeError exception, try to recast all arguments according to the type annotations";
static PyObject *greedy_biclique(PyObject *self, PyObject *args)
{
PyObject *objU, *objV, *objD;
unsigned int seed;
int iter;
npy_intp shape[2];
uint64_t M, N;
size_t M_ndata, N_ndata;
int8_t **Dptr = NULL;
bitset U = NULL, V = NULL;
biclique *C = NULL;
bipartite_graph *G = NULL;
PyObject *ret, *retU = NULL, *retV = NULL;
SIGINT_TRAP;
if(!PyArg_ParseTuple(args, "OOOIi",
&objU, &objV, &objD,
&seed, &iter)) {
PyErr_SetString(PyExc_TypeError, "Unable to parse arguments");
goto fail;
}
if(PyArray_AsCArray(&objD,
(void *)&Dptr,
shape,
2,
PyArray_DescrFromType(NPY_BOOL)) < 0) {
PyErr_SetString(PyExc_TypeError, "Unable to convert D");
goto fail;
}
M = (uint64_t)shape[0];
M_ndata = bitset_required_capacity(M);
N = (uint64_t)shape[1];
N_ndata = bitset_required_capacity(N);
U = python_numpy_uint64_to_bitset(objU,M_ndata);
if(U == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to convert U");
goto fail;
}
V = python_numpy_uint64_to_bitset(objV,N_ndata);
if(V == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to convert V");
goto fail;
}
G = bipartite_graph_from_C_bool_array(M,N,U,V,Dptr);
if(!G) {
PyErr_SetString(PyExc_MemoryError, "Failed to allocate G");
goto fail;
}
if(lyu_random_init(seed)<0) {
PyErr_SetString(PyExc_RuntimeError, "Failed to setup random generator");
goto fail;
}
Py_BEGIN_ALLOW_THREADS;
lyu_stop = 0;
C = InitMBC_Greedy(G, iter);
Py_END_ALLOW_THREADS;
if(lyu_stop) {
PyErr_SetString(PyExc_KeyboardInterrupt, "SIGINT received inside pymbc");
goto fail;
}
if(!C) {
PyErr_SetString(PyExc_ValueError, "Failed to run InitMBC_Greedy");
goto fail;
}
#ifndef NDEBUG
if(!biclique_is_biclique(G,C)) {
PyErr_SetString(PyExc_RuntimeError, "BUG #3");
goto fail;
}
#endif
retU = bitset_to_python_list(C->U, M_ndata);
if(retU == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to get back U");
goto fail;
}
retV = bitset_to_python_list(C->V, N_ndata);
if(retV == NULL) {
PyErr_SetString(PyExc_TypeError, "Unable to get back V");
goto fail;
}
bitset_free(U);
bitset_free(V);
PyArray_Free(objD, Dptr);
bipartite_graph_free(G);
biclique_free(C);
ret = Py_BuildValue("(OO)", retU, retV);
Py_DECREF(retU);
Py_DECREF(retV);
SIGINT_RECOVER;
return ret;
fail:
Py_XDECREF(retU);
Py_XDECREF(retV);
if(U) bitset_free(U);
if(V) bitset_free(V);
if(Dptr) PyArray_Free(objD, Dptr);
if(G) bipartite_graph_free(G);
if(C) biclique_free(C);
SIGINT_RECOVER;
return NULL;
}
/*
if(!PyArg_ParseTuple(args, "OOOIi",
&objU, &objV, &objD,
&seed, &iter)) {
*/
static const char * const greedy_biclique_docstring =
"greedy_biclique(U: Optional[numpy.typing.NDArray[np.uint64]],\n"
" V: Optional[numpy.typing.NDArray[np.uint64]],\n"
" D: numpy.typing.NDArray[np.bool],\n"
" seed: int,\n"
" iter: bool) -> Tuple[List,List]\n"
"\n"
"This method exposes an interface for the greedy algorithm used in InitMBC.\n"
"\n"
"Parameters:\n"
"U -- array of integers (starts from 0) indicate the vertices of one side of a bipartite graph (e.g. [0,1,2,4])) or None\n"
"V -- array of integers (starts from 0) indicate the vertices of another side of a bipartite graph (e.g. [0,3,5,6]) or None\n"
"D -- Boolean matrix with entries (i,j)=True if (U_i,V_j) is connected with an edge, False otherwise\n"
" **The code determines the size of the graph based on the shape of D. U and V should not contain entries greater than the size**\n"
"seed -- Random seed for any randomness in the algorithm\n"
"iter -- This algorithm is randomized and indicate how many iterations to run\n"
"\n"
"Return:\n"
"U -- List of the vertices of the found biclique on one side\n"
"V -- List of the vertices of the found biclique on another side\n"
"\n"
"Remarks:\n"
"(1) If you get a TypeError exception, try to recast all arguments according to the type annotations";
static PyMethodDef pymbcmethods[] =
{
{ "maximum_biclique_search", (PyCFunction)maximum_biclique_search, METH_VARARGS, maximum_biclique_search_docstring },
{ "greedy_biclique", (PyCFunction)greedy_biclique, METH_VARARGS, greedy_biclique_docstring },
{ NULL, NULL, 0, NULL }
};
static const char * const pymbc_docstring =
"Maximum Biclique Search\n"
"\n"
"An implementation of\n"
"Lyu, Bingqing, et al. \"Maximum biclique search at billion scale.\" Proceedings of the VLDB Endowment (2020).\n"
"\n";
static PyModuleDef pymbcmodule =
{
PyModuleDef_HEAD_INIT,
"pymbc",
pymbc_docstring,
-1,
pymbcmethods, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_pymbc(void)
{
import_array();
return PyModule_Create(&pymbcmodule);
}