-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.c
331 lines (264 loc) · 8.68 KB
/
partition.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
/**
This file is part of exactcolors.
exactcolors is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
exactcolors is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with exactcolors. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <getopt.h>
#include "graph.h"
#include "heap.h"
#include "color.h"
char* graphfile = (char*) NULL;
int partitionsize = INT_MAX;
static int parseargs (int ac, char **av);
static void usage (char *f);
static int get_problem_name(char* pname,const char* efname);
static int COLORadjgraph_isolate_vertex(COLORadjgraph* G, int v_i,
COLORNWTHeap* heap, int href[])
{
int rval = 0;
int j;
COLORadjnode* v = G->nodelist + v_i;
for (j = 0; j < v->degree; ++j) {
COLORadjnode* w = G->nodelist + v->adj[j];
int k;
if (heap) {
COLORNWTheap_decrease_key(heap,href[v->adj[j]],w->degree -1);
}
for (k = 0; k < w->degree; ++k) {
if (w->adj[k] == v_i) {
printf("c Deleting %d %d\n", v_i + 1, v->adj[j] + 1);
if (k + 1 < w->degree) {
memmove(w->adj + k , w->adj + k + 1,
(w->degree - k -1) * sizeof(int));
}
w->degree--;
k = w->degree;
}
}
}
v->degree = 0;
return rval;
}
COLOR_MAYBE_UNUSED
static
int COLORadjgraph_incr_deled_smallest_degree_vertex(COLORadjgraph* Gd,
const COLORadjgraph* Gs,
int lpartitionsize)
{
int rval = 0;
int* heapref = (int*) NULL;
int* nodeindices = (int*) NULL;
int ndeletednodes = 0;
int ndeletededges = 0;
COLORNWTHeap* heap = (COLORNWTHeap*) NULL;
int i;
int ndelete = Gs->ncount - lpartitionsize;
int* i_ptr;
rval = COLORadjgraph_copy(Gd, Gs);
COLORcheck_rval(rval,"Failed in COLORadjgraph_copy");
/* rval = COLORadjgraph_extract_edgelist(&ecount, &elist, Gd); */
/* COLORcheck_rval(rval,"Failed in COLORadjgraph_extract_edgelist"); */
/* rval = COLORgraph_print(ecount, elist); */
/* COLORcheck_rval(rval,"Failed in COLORgraph_print"); */
rval = COLORNWTheap_init(&heap, Gs->ncount);
COLORcheck_rval(rval,"Failed in COLORNWTheap_init");
heapref = COLOR_SAFE_MALLOC(Gd->ncount, int);
COLORcheck_NULL(heapref,"Failed to allocate heapref");
nodeindices = COLOR_SAFE_MALLOC(Gd->ncount, int);
COLORcheck_NULL(heapref,"Failed to allocate nodeindices");
for (i = 0; i < Gd->ncount; ++i) {
COLORadjnode* node = Gd->nodelist + i;
COLORNWT key = (COLORNWT) node->degree;
int* pos = &(heapref[i]);
nodeindices[i] = i;
COLORNWTheap_insert(heap, pos,key,nodeindices + i);
}
while ( (ndeletednodes < ndelete) &&(i_ptr = (int*) COLORNWTheap_min(heap) ) ) {
i = *i_ptr;
ndeletednodes++;
ndeletededges += Gd->nodelist[i].degree;
rval = COLORadjgraph_isolate_vertex(Gd,i,heap,heapref);
COLORcheck_rval(rval, "Failed in COLORadjgraph_isolate_vertex");
/* rval = COLORadjgraph_extract_edgelist(&ecount, &elist, Gd); */
/* COLORcheck_rval(rval,"Failed in COLORadjgraph_extract_edgelist"); */
/* printf("edgelist after isolating %d.\n",i); */
/* rval = COLORgraph_print(ecount, elist); */
/* COLORcheck_rval(rval,"Failed in COLORgraph_print"); */
}
CLEANUP:
COLORNWTheap_free(heap);
return rval;
}
COLOR_MAYBE_UNUSED
static
int COLORadjgraph_greedy_partition_1(COLORadjgraph* Gd,
const COLORadjgraph* Gs,
int lpartitionsize)
{
int rval = 0;
int* perm = (int*) NULL;
int* iperm = (int*) NULL;
int* len = (int*) NULL;
int i;
int ncount = Gs->ncount;
int nbest_half = (lpartitionsize + 1) / 2;
int nremaining = ncount - nbest_half;
rval = COLORadjgraph_copy(Gd, Gs);
COLORcheck_rval(rval,"Failed in COLORadjgraph_copy");
perm = COLOR_SAFE_MALLOC(ncount,int);
COLORcheck_NULL(perm,"Failed to allocate perm.\n");
iperm = COLOR_SAFE_MALLOC(ncount,int);
COLORcheck_NULL(iperm,"Failed to allocate iperm.\n");
len = COLOR_SAFE_MALLOC(ncount,int);
COLORcheck_NULL(len,"Failed to allocate len.\n");
for (i=0; i < ncount; ++i) {
perm[i] = i;
len[i] = Gd->nodelist[i].degree;
}
COLORutil_perm_quicksort (perm,len,ncount);
for (i=0; i < ncount; ++i) {
iperm[perm[i]] = i;
}
for (i=0; i < nremaining; ++i) {
COLORadjnode* w = Gd->nodelist + perm[i];
int j;
len[perm[i]] = 0;
for (j = 0; j < w->degree; ++j) {
int k = w->adj[j];
if (iperm[k] > nremaining) {
len[perm[i]]++;
}
}
}
COLORutil_perm_quicksort (perm,len,nremaining);
for (i=0; i < nremaining; ++i) {
iperm[perm[i]] = i;
}
for( i = 0; i < ncount - lpartitionsize; i++) {
rval = COLORadjgraph_isolate_vertex(Gd, iperm[i],NULL,NULL);
COLORcheck_rval(rval,"Failed in COLORadjgraph_isolate_vertex");
}
CLEANUP:
COLOR_IFFREE(perm,int);
return rval;
}
static int parseargs (int ac, char **av)
{
int c;
int rval = 0;
while ((c = getopt (ac, av, "dp:")) != EOF) {
switch (c) {
case 'd':
COLORset_dbg_lvl(COLORdbg_lvl() + 1);
break;
case 'p':
partitionsize = atoi(optarg);
break;
default:
usage (av[0]);
rval = 1; goto CLEANUP;
}
}
if (ac <= optind) {
rval = 1; goto CLEANUP;
} else {
graphfile = av[optind++];
}
CLEANUP:
if (rval) usage (av[0]);
return rval;
}
static void usage (char *f)
{
fprintf (stderr, "Usage %s: [-see below-] graph_file\n", f);
fprintf (stderr, " -d turn on debugging\n");
fprintf (stderr, " -p n targeted max. partition size.\n");
}
static int get_problem_name(char* pname,const char* efname)
{
int rval = 0;
int len = 0;
const char *fname = strrchr(efname,'/');
char *lastdot = strrchr(efname,'.');
if(!fname) {
/* no slashes in efname.*/
fname = efname;
} else {
fname++;
}
if (lastdot) {
len = lastdot - fname + 1;
} else {
len = strlen(fname);
}
if (snprintf(pname,len,"%s",fname) < 0) {
fprintf (stderr, "snprintf failed\n");
rval = 1; goto CLEANUP;
}
printf("c Extracted problem name %s\n",pname); fflush (stdout);
CLEANUP:
return rval;
}
int main (int ac, char **av)
{
char pname[256] = "";
int rval = 0;
int ncount, ecount;
int *elist = (int *) NULL;
int *wlen = (int *) NULL;
int *new_ids = (int *) NULL;
int ndelete;
COLORadjgraph G1, G2;
int i, new_ncount;
rval = parseargs (ac, av);
if (rval) goto CLEANUP;
get_problem_name (pname, graphfile);
printf("c Graph generated from %s with <= %d non-isolated vertices.\n",
pname, partitionsize);
rval = COLORread_dimacs (graphfile, &ncount, &ecount, &elist, &wlen);
COLORcheck_rval (rval, "COLORread_dimacs failed");
rval = COLORadjgraph_build (&G1, ncount, ecount, elist);
COLORcheck_rval (rval, "COLORadjgraph_build failed");
new_ids = COLOR_SAFE_MALLOC(ncount, int);
COLORcheck_NULL(new_ids, "Failed to allocate new_ids");
for (i = 0; i < ncount; ++i) { new_ids[i] = i;}
new_ncount = ncount;
ndelete = ncount - partitionsize;
if (ndelete > 0) {
/* rval = COLORadjgraph_greedy_partition_1 (&G2, &G1,partitionsize); */
/* COLORcheck_rval (rval, "COLORadjgraph_build failed"); */
COLORadjgraph_incr_deled_smallest_degree_vertex(&G2,
&G1,
partitionsize);
rval = COLORadjgraph_extract_edgelist(&ecount, &elist, &G2);
COLORcheck_rval(rval,"Failed in COLORadjgraph_extract_edgelist");
new_ncount = 0;
for (i = 0; i < ncount; ++i) {
COLORadjnode* v = G2.nodelist + i;
if (v->degree){
new_ids[i] = new_ncount++;
printf("c node %d renamed to %d\n", i + 1, new_ids[i] + 1);
} else {
new_ids[i] = -1;
}
}
}
printf("p edge %d %d\n", new_ncount,ecount);
for (i = 0; i < ecount; ++i) {
printf("e %d %d\n",new_ids[elist[2*i]] + 1,new_ids[elist[2*i+1]] + 1);
}
CLEANUP:
return rval;
}