| 1 |
/* |
| 2 |
* Copyright 1997, Regents of the University of Minnesota |
| 3 |
* |
| 4 |
* sfm.c |
| 5 |
* |
| 6 |
* This file contains code that implements an FM-based separator refinement |
| 7 |
* |
| 8 |
* Started 8/1/97 |
| 9 |
* George |
| 10 |
* |
| 11 |
* $Id: sfm.c 10874 2011-10-17 23:13:00Z karypis $ |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "metislib.h" |
| 16 |
|
| 17 |
|
| 18 |
/*************************************************************************/ |
| 19 |
/*! This function performs a node-based FM refinement */ |
| 20 |
/**************************************************************************/ |
| 21 |
void FM_2WayNodeRefine2Sided(ctrl_t *ctrl, graph_t *graph, idx_t niter) |
| 22 |
{ |
| 23 |
idx_t i, ii, j, k, jj, kk, nvtxs, nbnd, nswaps, nmind; |
| 24 |
const idx_t *xadj, *adjncy; |
| 25 |
idx_t *vwgt, *where, *pwgts, *edegrees, *bndind, *bndptr; |
| 26 |
idx_t *mptr, *mind, *moved, *swaps; |
| 27 |
rpq_t *queues[2]; |
| 28 |
nrinfo_t *rinfo; |
| 29 |
idx_t higain, oldgain, mincut, initcut, mincutorder; |
| 30 |
idx_t pass, to, other, limit; |
| 31 |
idx_t badmaxpwgt, mindiff, newdiff; |
| 32 |
idx_t u[2], g[2]; |
| 33 |
real_t mult; |
| 34 |
|
| 35 |
WCOREPUSH; |
| 36 |
|
| 37 |
nvtxs = graph->nvtxs; |
| 38 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 39 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 40 |
vwgt = graph->vwgt; |
| 41 |
|
| 42 |
bndind = graph->bndind; |
| 43 |
bndptr = graph->bndptr; |
| 44 |
where = graph->where; |
| 45 |
pwgts = graph->pwgts; |
| 46 |
rinfo = graph->nrinfo; |
| 47 |
|
| 48 |
queues[0] = rpqCreate(nvtxs); |
| 49 |
queues[1] = rpqCreate(nvtxs); |
| 50 |
|
| 51 |
moved = iwspacemalloc(ctrl, nvtxs); |
| 52 |
swaps = iwspacemalloc(ctrl, nvtxs); |
| 53 |
mptr = iwspacemalloc(ctrl, nvtxs+1); |
| 54 |
mind = iwspacemalloc(ctrl, 2*nvtxs); |
| 55 |
|
| 56 |
mult = 0.5*ctrl->ubfactors[0]; |
| 57 |
badmaxpwgt = (idx_t)(mult*(pwgts[0]+pwgts[1]+pwgts[2])); |
| 58 |
|
| 59 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 60 |
HIGHS_ORDERING_PRINT("Partitions-N2: [%6"PRIDX" %6"PRIDX"] Nv-Nb[%6"PRIDX" %6"PRIDX"]. ISep: %6"PRIDX"\n", pwgts[0], pwgts[1], graph->nvtxs, graph->nbnd, graph->mincut)); |
| 61 |
|
| 62 |
for (pass=0; pass<niter; pass++) { |
| 63 |
iset(nvtxs, -1, moved); |
| 64 |
rpqReset(queues[0]); |
| 65 |
rpqReset(queues[1]); |
| 66 |
|
| 67 |
mincutorder = -1; |
| 68 |
initcut = mincut = graph->mincut; |
| 69 |
nbnd = graph->nbnd; |
| 70 |
|
| 71 |
/* use the swaps array in place of the traditional perm array to save memory */ |
| 72 |
irandArrayPermute(nbnd, swaps, nbnd, 1, &ctrl->rng_state); |
| 73 |
for (ii=0; ii<nbnd; ii++) { |
| 74 |
i = bndind[swaps[ii]]; |
| 75 |
rpqInsert(queues[0], i, vwgt[i]-rinfo[i].edegrees[1]); |
| 76 |
rpqInsert(queues[1], i, vwgt[i]-rinfo[i].edegrees[0]); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
limit = (ctrl->compress ? gk_min(5*nbnd, 400) : gk_min(2*nbnd, 300)); |
| 81 |
|
| 82 |
/****************************************************** |
| 83 |
* Get into the FM loop |
| 84 |
*******************************************************/ |
| 85 |
mptr[0] = nmind = 0; |
| 86 |
mindiff = iabs(pwgts[0]-pwgts[1]); |
| 87 |
to = (pwgts[0] < pwgts[1] ? 0 : 1); |
| 88 |
for (nswaps=0; nswaps<nvtxs; nswaps++) { |
| 89 |
u[0] = rpqSeeTopVal(queues[0]); |
| 90 |
u[1] = rpqSeeTopVal(queues[1]); |
| 91 |
if (u[0] != -1 && u[1] != -1) { |
| 92 |
g[0] = vwgt[u[0]]-rinfo[u[0]].edegrees[1]; |
| 93 |
g[1] = vwgt[u[1]]-rinfo[u[1]].edegrees[0]; |
| 94 |
|
| 95 |
to = (g[0] > g[1] ? 0 : (g[0] < g[1] ? 1 : pass%2)); |
| 96 |
|
| 97 |
if (pwgts[to]+vwgt[u[to]] > badmaxpwgt) |
| 98 |
to = (to+1)%2; |
| 99 |
} |
| 100 |
else if (u[0] == -1 && u[1] == -1) { |
| 101 |
break; |
| 102 |
} |
| 103 |
else if (u[0] != -1 && pwgts[0]+vwgt[u[0]] <= badmaxpwgt) { |
| 104 |
to = 0; |
| 105 |
} |
| 106 |
else if (u[1] != -1 && pwgts[1]+vwgt[u[1]] <= badmaxpwgt) { |
| 107 |
to = 1; |
| 108 |
} |
| 109 |
else |
| 110 |
break; |
| 111 |
|
| 112 |
other = (to+1)%2; |
| 113 |
|
| 114 |
higain = rpqGetTop(queues[to]); |
| 115 |
if (moved[higain] == -1) /* Delete if it was in the separator originally */ |
| 116 |
rpqDelete(queues[other], higain); |
| 117 |
|
| 118 |
|
| 119 |
/* The following check is to ensure we break out if there is a possibility |
| 120 |
of over-running the mind array. */ |
| 121 |
if (nmind + xadj[higain+1]-xadj[higain] >= 2*nvtxs-1) |
| 122 |
break; |
| 123 |
|
| 124 |
pwgts[2] -= (vwgt[higain]-rinfo[higain].edegrees[other]); |
| 125 |
|
| 126 |
newdiff = iabs(pwgts[to]+vwgt[higain] - (pwgts[other]-rinfo[higain].edegrees[other])); |
| 127 |
if (pwgts[2] < mincut || (pwgts[2] == mincut && newdiff < mindiff)) { |
| 128 |
mincut = pwgts[2]; |
| 129 |
mincutorder = nswaps; |
| 130 |
mindiff = newdiff; |
| 131 |
} |
| 132 |
else { |
| 133 |
if (nswaps - mincutorder > 2*limit || |
| 134 |
(nswaps - mincutorder > limit && pwgts[2] > 1.10*mincut)) { |
| 135 |
pwgts[2] += (vwgt[higain]-rinfo[higain].edegrees[other]); |
| 136 |
break; /* No further improvement, break out */ |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
BNDDelete(nbnd, bndind, bndptr, higain); |
| 141 |
pwgts[to] += vwgt[higain]; |
| 142 |
where[higain] = to; |
| 143 |
moved[higain] = nswaps; |
| 144 |
swaps[nswaps] = higain; |
| 145 |
|
| 146 |
|
| 147 |
/********************************************************** |
| 148 |
* Update the degrees of the affected nodes |
| 149 |
***********************************************************/ |
| 150 |
for (j=xadj[higain]; j<xadj[higain+1]; j++) { |
| 151 |
k = adjncy[j]; |
| 152 |
if (where[k] == 2) { /* For the in-separator vertices modify their edegree[to] */ |
| 153 |
oldgain = vwgt[k]-rinfo[k].edegrees[to]; |
| 154 |
rinfo[k].edegrees[to] += vwgt[higain]; |
| 155 |
if (moved[k] == -1 || moved[k] == -(2+other)) |
| 156 |
rpqUpdate(queues[other], k, oldgain-vwgt[higain]); |
| 157 |
} |
| 158 |
else if (where[k] == other) { /* This vertex is pulled into the separator */ |
| 159 |
BNDInsert(nbnd, bndind, bndptr, k); |
| 160 |
|
| 161 |
mind[nmind++] = k; /* Keep track for rollback */ |
| 162 |
where[k] = 2; |
| 163 |
pwgts[other] -= vwgt[k]; |
| 164 |
|
| 165 |
edegrees = rinfo[k].edegrees; |
| 166 |
edegrees[0] = edegrees[1] = 0; |
| 167 |
for (jj=xadj[k]; jj<xadj[k+1]; jj++) { |
| 168 |
kk = adjncy[jj]; |
| 169 |
if (where[kk] != 2) |
| 170 |
edegrees[where[kk]] += vwgt[kk]; |
| 171 |
else { |
| 172 |
oldgain = vwgt[kk]-rinfo[kk].edegrees[other]; |
| 173 |
rinfo[kk].edegrees[other] -= vwgt[k]; |
| 174 |
if (moved[kk] == -1 || moved[kk] == -(2+to)) |
| 175 |
rpqUpdate(queues[to], kk, oldgain+vwgt[k]); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/* Insert the new vertex into the priority queue. Only one side! */ |
| 180 |
if (moved[k] == -1) { |
| 181 |
rpqInsert(queues[to], k, vwgt[k]-edegrees[other]); |
| 182 |
moved[k] = -(2+to); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
mptr[nswaps+1] = nmind; |
| 187 |
|
| 188 |
IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO, |
| 189 |
HIGHS_ORDERING_PRINT("Moved %6"PRIDX" to %3"PRIDX", Gain: %5"PRIDX" [%5"PRIDX"] [%4"PRIDX" %4"PRIDX"] \t[%5"PRIDX" %5"PRIDX" %5"PRIDX"]\n", higain, to, g[to], g[other], vwgt[u[to]], vwgt[u[other]], pwgts[0], pwgts[1], pwgts[2])); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/**************************************************************** |
| 195 |
* Roll back computation |
| 196 |
*****************************************************************/ |
| 197 |
for (nswaps--; nswaps>mincutorder; nswaps--) { |
| 198 |
higain = swaps[nswaps]; |
| 199 |
|
| 200 |
|
| 201 |
to = where[higain]; |
| 202 |
other = (to+1)%2; |
| 203 |
INC_DEC(pwgts[2], pwgts[to], vwgt[higain]); |
| 204 |
where[higain] = 2; |
| 205 |
BNDInsert(nbnd, bndind, bndptr, higain); |
| 206 |
|
| 207 |
edegrees = rinfo[higain].edegrees; |
| 208 |
edegrees[0] = edegrees[1] = 0; |
| 209 |
for (j=xadj[higain]; j<xadj[higain+1]; j++) { |
| 210 |
k = adjncy[j]; |
| 211 |
if (where[k] == 2) |
| 212 |
rinfo[k].edegrees[to] -= vwgt[higain]; |
| 213 |
else |
| 214 |
edegrees[where[k]] += vwgt[k]; |
| 215 |
} |
| 216 |
|
| 217 |
/* Push nodes out of the separator */ |
| 218 |
for (j=mptr[nswaps]; j<mptr[nswaps+1]; j++) { |
| 219 |
k = mind[j]; |
| 220 |
where[k] = other; |
| 221 |
INC_DEC(pwgts[other], pwgts[2], vwgt[k]); |
| 222 |
BNDDelete(nbnd, bndind, bndptr, k); |
| 223 |
for (jj=xadj[k]; jj<xadj[k+1]; jj++) { |
| 224 |
kk = adjncy[jj]; |
| 225 |
if (where[kk] == 2) |
| 226 |
rinfo[kk].edegrees[other] += vwgt[k]; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 232 |
HIGHS_ORDERING_PRINT("\tMinimum sep: %6"PRIDX" at %5"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, mincutorder, pwgts[0], pwgts[1], nbnd)); |
| 233 |
|
| 234 |
graph->mincut = mincut; |
| 235 |
graph->nbnd = nbnd; |
| 236 |
|
| 237 |
if (mincutorder == -1 || mincut >= initcut) |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
rpqDestroy(queues[0]); |
| 242 |
rpqDestroy(queues[1]); |
| 243 |
|
| 244 |
WCOREPOP; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/*************************************************************************/ |
| 249 |
/*! This function performs a node-based FM refinement. |
| 250 |
Each refinement iteration is split into two sub-iterations. |
| 251 |
In each sub-iteration only moves to one of the left/right partitions |
| 252 |
is allowed; hence, it is one-sided. |
| 253 |
*/ |
| 254 |
/**************************************************************************/ |
| 255 |
void FM_2WayNodeRefine1Sided(ctrl_t *ctrl, graph_t *graph, idx_t niter) |
| 256 |
{ |
| 257 |
idx_t i, ii, j, k, jj, kk, nvtxs, nbnd, nswaps, nmind, iend; |
| 258 |
const idx_t *xadj, *adjncy; |
| 259 |
idx_t *vwgt, *where, *pwgts, *edegrees, *bndind, *bndptr; |
| 260 |
idx_t *mptr, *mind, *swaps; |
| 261 |
rpq_t *queue; |
| 262 |
nrinfo_t *rinfo; |
| 263 |
idx_t higain, mincut, initcut, mincutorder; |
| 264 |
idx_t pass, to, other, limit; |
| 265 |
idx_t badmaxpwgt, mindiff, newdiff; |
| 266 |
real_t mult; |
| 267 |
|
| 268 |
WCOREPUSH; |
| 269 |
|
| 270 |
nvtxs = graph->nvtxs; |
| 271 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 272 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 273 |
vwgt = graph->vwgt; |
| 274 |
|
| 275 |
bndind = graph->bndind; |
| 276 |
bndptr = graph->bndptr; |
| 277 |
where = graph->where; |
| 278 |
pwgts = graph->pwgts; |
| 279 |
rinfo = graph->nrinfo; |
| 280 |
|
| 281 |
queue = rpqCreate(nvtxs); |
| 282 |
|
| 283 |
swaps = iwspacemalloc(ctrl, nvtxs); |
| 284 |
mptr = iwspacemalloc(ctrl, nvtxs+1); |
| 285 |
mind = iwspacemalloc(ctrl, 2*nvtxs); |
| 286 |
|
| 287 |
mult = 0.5*ctrl->ubfactors[0]; |
| 288 |
badmaxpwgt = (idx_t)(mult*(pwgts[0]+pwgts[1]+pwgts[2])); |
| 289 |
|
| 290 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 291 |
HIGHS_ORDERING_PRINT("Partitions-N1: [%6"PRIDX" %6"PRIDX"] Nv-Nb[%6"PRIDX" %6"PRIDX"]. ISep: %6"PRIDX"\n", pwgts[0], pwgts[1], graph->nvtxs, graph->nbnd, graph->mincut)); |
| 292 |
|
| 293 |
to = (pwgts[0] < pwgts[1] ? 1 : 0); |
| 294 |
for (pass=0; pass<2*niter; pass++) { /* the 2*niter is for the two sides */ |
| 295 |
other = to; |
| 296 |
to = (to+1)%2; |
| 297 |
|
| 298 |
rpqReset(queue); |
| 299 |
|
| 300 |
mincutorder = -1; |
| 301 |
initcut = mincut = graph->mincut; |
| 302 |
nbnd = graph->nbnd; |
| 303 |
|
| 304 |
/* use the swaps array in place of the traditional perm array to save memory */ |
| 305 |
irandArrayPermute(nbnd, swaps, nbnd, 1, &ctrl->rng_state); |
| 306 |
for (ii=0; ii<nbnd; ii++) { |
| 307 |
i = bndind[swaps[ii]]; |
| 308 |
rpqInsert(queue, i, vwgt[i]-rinfo[i].edegrees[other]); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
limit = (ctrl->compress ? gk_min(5*nbnd, 500) : gk_min(3*nbnd, 300)); |
| 313 |
|
| 314 |
/****************************************************** |
| 315 |
* Get into the FM loop |
| 316 |
*******************************************************/ |
| 317 |
mptr[0] = nmind = 0; |
| 318 |
mindiff = iabs(pwgts[0]-pwgts[1]); |
| 319 |
for (nswaps=0; nswaps<nvtxs; nswaps++) { |
| 320 |
if ((higain = rpqGetTop(queue)) == -1) |
| 321 |
break; |
| 322 |
|
| 323 |
/* The following check is to ensure we break out if there is a possibility |
| 324 |
of over-running the mind array. */ |
| 325 |
if (nmind + xadj[higain+1]-xadj[higain] >= 2*nvtxs-1) |
| 326 |
break; |
| 327 |
|
| 328 |
if (pwgts[to]+vwgt[higain] > badmaxpwgt) |
| 329 |
break; /* No point going any further. Balance will be bad */ |
| 330 |
|
| 331 |
pwgts[2] -= (vwgt[higain]-rinfo[higain].edegrees[other]); |
| 332 |
|
| 333 |
newdiff = iabs(pwgts[to]+vwgt[higain] - (pwgts[other]-rinfo[higain].edegrees[other])); |
| 334 |
if (pwgts[2] < mincut || (pwgts[2] == mincut && newdiff < mindiff)) { |
| 335 |
mincut = pwgts[2]; |
| 336 |
mincutorder = nswaps; |
| 337 |
mindiff = newdiff; |
| 338 |
} |
| 339 |
else { |
| 340 |
if (nswaps - mincutorder > 3*limit || |
| 341 |
(nswaps - mincutorder > limit && pwgts[2] > 1.10*mincut)) { |
| 342 |
pwgts[2] += (vwgt[higain]-rinfo[higain].edegrees[other]); |
| 343 |
break; /* No further improvement, break out */ |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
BNDDelete(nbnd, bndind, bndptr, higain); |
| 348 |
pwgts[to] += vwgt[higain]; |
| 349 |
where[higain] = to; |
| 350 |
swaps[nswaps] = higain; |
| 351 |
|
| 352 |
|
| 353 |
/********************************************************** |
| 354 |
* Update the degrees of the affected nodes |
| 355 |
***********************************************************/ |
| 356 |
for (j=xadj[higain]; j<xadj[higain+1]; j++) { |
| 357 |
k = adjncy[j]; |
| 358 |
|
| 359 |
if (where[k] == 2) { /* For the in-separator vertices modify their edegree[to] */ |
| 360 |
rinfo[k].edegrees[to] += vwgt[higain]; |
| 361 |
} |
| 362 |
else if (where[k] == other) { /* This vertex is pulled into the separator */ |
| 363 |
BNDInsert(nbnd, bndind, bndptr, k); |
| 364 |
|
| 365 |
mind[nmind++] = k; /* Keep track for rollback */ |
| 366 |
where[k] = 2; |
| 367 |
pwgts[other] -= vwgt[k]; |
| 368 |
|
| 369 |
edegrees = rinfo[k].edegrees; |
| 370 |
edegrees[0] = edegrees[1] = 0; |
| 371 |
for (jj=xadj[k], iend=xadj[k+1]; jj<iend; jj++) { |
| 372 |
kk = adjncy[jj]; |
| 373 |
if (where[kk] != 2) |
| 374 |
edegrees[where[kk]] += vwgt[kk]; |
| 375 |
else { |
| 376 |
rinfo[kk].edegrees[other] -= vwgt[k]; |
| 377 |
|
| 378 |
/* Since the moves are one-sided this vertex has not been moved yet */ |
| 379 |
rpqUpdate(queue, kk, vwgt[kk]-rinfo[kk].edegrees[other]); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* Insert the new vertex into the priority queue. Safe due to one-sided moves */ |
| 384 |
rpqInsert(queue, k, vwgt[k]-edegrees[other]); |
| 385 |
} |
| 386 |
} |
| 387 |
mptr[nswaps+1] = nmind; |
| 388 |
|
| 389 |
|
| 390 |
IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO, |
| 391 |
HIGHS_ORDERING_PRINT("Moved %6"PRIDX" to %3"PRIDX", Gain: %5"PRIDX" [%5"PRIDX"] \t[%5"PRIDX" %5"PRIDX" %5"PRIDX"] [%3"PRIDX" %2"PRIDX"]\n", |
| 392 |
higain, to, (vwgt[higain]-rinfo[higain].edegrees[other]), vwgt[higain], |
| 393 |
pwgts[0], pwgts[1], pwgts[2], nswaps, limit)); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
/**************************************************************** |
| 398 |
* Roll back computation |
| 399 |
*****************************************************************/ |
| 400 |
for (nswaps--; nswaps>mincutorder; nswaps--) { |
| 401 |
higain = swaps[nswaps]; |
| 402 |
|
| 403 |
INC_DEC(pwgts[2], pwgts[to], vwgt[higain]); |
| 404 |
where[higain] = 2; |
| 405 |
BNDInsert(nbnd, bndind, bndptr, higain); |
| 406 |
|
| 407 |
edegrees = rinfo[higain].edegrees; |
| 408 |
edegrees[0] = edegrees[1] = 0; |
| 409 |
for (j=xadj[higain]; j<xadj[higain+1]; j++) { |
| 410 |
k = adjncy[j]; |
| 411 |
if (where[k] == 2) |
| 412 |
rinfo[k].edegrees[to] -= vwgt[higain]; |
| 413 |
else |
| 414 |
edegrees[where[k]] += vwgt[k]; |
| 415 |
} |
| 416 |
|
| 417 |
/* Push nodes out of the separator */ |
| 418 |
for (j=mptr[nswaps]; j<mptr[nswaps+1]; j++) { |
| 419 |
k = mind[j]; |
| 420 |
where[k] = other; |
| 421 |
INC_DEC(pwgts[other], pwgts[2], vwgt[k]); |
| 422 |
BNDDelete(nbnd, bndind, bndptr, k); |
| 423 |
for (jj=xadj[k], iend=xadj[k+1]; jj<iend; jj++) { |
| 424 |
kk = adjncy[jj]; |
| 425 |
if (where[kk] == 2) |
| 426 |
rinfo[kk].edegrees[other] += vwgt[k]; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 432 |
HIGHS_ORDERING_PRINT("\tMinimum sep: %6"PRIDX" at %5"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, mincutorder, pwgts[0], pwgts[1], nbnd)); |
| 433 |
|
| 434 |
graph->mincut = mincut; |
| 435 |
graph->nbnd = nbnd; |
| 436 |
|
| 437 |
if (pass%2 == 1 && (mincutorder == -1 || mincut >= initcut)) |
| 438 |
break; |
| 439 |
} |
| 440 |
|
| 441 |
rpqDestroy(queue); |
| 442 |
|
| 443 |
WCOREPOP; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
/*************************************************************************/ |
| 448 |
/*! This function balances the left/right partitions of a separator |
| 449 |
tri-section */ |
| 450 |
/*************************************************************************/ |
| 451 |
void FM_2WayNodeBalance(ctrl_t *ctrl, graph_t *graph) |
| 452 |
{ |
| 453 |
idx_t i, ii, j, k, jj, kk, nvtxs, nbnd, nswaps, gain; |
| 454 |
idx_t badmaxpwgt, higain, oldgain, pass, to, other; |
| 455 |
const idx_t *xadj, *adjncy; |
| 456 |
idx_t *vwgt, *where, *pwgts, *edegrees, *bndind, *bndptr; |
| 457 |
idx_t *perm, *moved; |
| 458 |
rpq_t *queue; |
| 459 |
nrinfo_t *rinfo; |
| 460 |
real_t mult; |
| 461 |
|
| 462 |
nvtxs = graph->nvtxs; |
| 463 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 464 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 465 |
vwgt = graph->vwgt; |
| 466 |
|
| 467 |
bndind = graph->bndind; |
| 468 |
bndptr = graph->bndptr; |
| 469 |
where = graph->where; |
| 470 |
pwgts = graph->pwgts; |
| 471 |
rinfo = graph->nrinfo; |
| 472 |
|
| 473 |
mult = 0.5*ctrl->ubfactors[0]; |
| 474 |
|
| 475 |
badmaxpwgt = (idx_t)(mult*(pwgts[0]+pwgts[1])); |
| 476 |
if (gk_max(pwgts[0], pwgts[1]) < badmaxpwgt) |
| 477 |
return; |
| 478 |
if (iabs(pwgts[0]-pwgts[1]) < 3*graph->tvwgt[0]/nvtxs) |
| 479 |
return; |
| 480 |
|
| 481 |
WCOREPUSH; |
| 482 |
|
| 483 |
to = (pwgts[0] < pwgts[1] ? 0 : 1); |
| 484 |
other = (to+1)%2; |
| 485 |
|
| 486 |
queue = rpqCreate(nvtxs); |
| 487 |
|
| 488 |
perm = iwspacemalloc(ctrl, nvtxs); |
| 489 |
moved = iset(nvtxs, -1, iwspacemalloc(ctrl, nvtxs)); |
| 490 |
|
| 491 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 492 |
HIGHS_ORDERING_PRINT("Partitions: [%6"PRIDX" %6"PRIDX"] Nv-Nb[%6"PRIDX" %6"PRIDX"]. ISep: %6"PRIDX" [B]\n", pwgts[0], pwgts[1], graph->nvtxs, graph->nbnd, graph->mincut)); |
| 493 |
|
| 494 |
nbnd = graph->nbnd; |
| 495 |
irandArrayPermute(nbnd, perm, nbnd, 1, &ctrl->rng_state); |
| 496 |
for (ii=0; ii<nbnd; ii++) { |
| 497 |
i = bndind[perm[ii]]; |
| 498 |
rpqInsert(queue, i, vwgt[i]-rinfo[i].edegrees[other]); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
/****************************************************** |
| 503 |
* Get into the FM loop |
| 504 |
*******************************************************/ |
| 505 |
for (nswaps=0; nswaps<nvtxs; nswaps++) { |
| 506 |
if ((higain = rpqGetTop(queue)) == -1) |
| 507 |
break; |
| 508 |
|
| 509 |
moved[higain] = 1; |
| 510 |
|
| 511 |
gain = vwgt[higain]-rinfo[higain].edegrees[other]; |
| 512 |
badmaxpwgt = (idx_t)(mult*(pwgts[0]+pwgts[1])); |
| 513 |
|
| 514 |
/* break if other is now underwight */ |
| 515 |
if (pwgts[to] > pwgts[other]) |
| 516 |
break; |
| 517 |
|
| 518 |
/* break if balance is achieved and no +ve or zero gain */ |
| 519 |
if (gain < 0 && pwgts[other] < badmaxpwgt) |
| 520 |
break; |
| 521 |
|
| 522 |
/* skip this vertex if it will violate balance on the other side */ |
| 523 |
if (pwgts[to]+vwgt[higain] > badmaxpwgt) |
| 524 |
continue; |
| 525 |
|
| 526 |
pwgts[2] -= gain; |
| 527 |
|
| 528 |
BNDDelete(nbnd, bndind, bndptr, higain); |
| 529 |
pwgts[to] += vwgt[higain]; |
| 530 |
where[higain] = to; |
| 531 |
|
| 532 |
IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO, |
| 533 |
HIGHS_ORDERING_PRINT("Moved %6"PRIDX" to %3"PRIDX", Gain: %3"PRIDX", \t[%5"PRIDX" %5"PRIDX" %5"PRIDX"]\n", higain, to, vwgt[higain]-rinfo[higain].edegrees[other], pwgts[0], pwgts[1], pwgts[2])); |
| 534 |
|
| 535 |
|
| 536 |
/********************************************************** |
| 537 |
* Update the degrees of the affected nodes |
| 538 |
***********************************************************/ |
| 539 |
for (j=xadj[higain]; j<xadj[higain+1]; j++) { |
| 540 |
k = adjncy[j]; |
| 541 |
if (where[k] == 2) { /* For the in-separator vertices modify their edegree[to] */ |
| 542 |
rinfo[k].edegrees[to] += vwgt[higain]; |
| 543 |
} |
| 544 |
else if (where[k] == other) { /* This vertex is pulled into the separator */ |
| 545 |
BNDInsert(nbnd, bndind, bndptr, k); |
| 546 |
|
| 547 |
where[k] = 2; |
| 548 |
pwgts[other] -= vwgt[k]; |
| 549 |
|
| 550 |
edegrees = rinfo[k].edegrees; |
| 551 |
edegrees[0] = edegrees[1] = 0; |
| 552 |
for (jj=xadj[k]; jj<xadj[k+1]; jj++) { |
| 553 |
kk = adjncy[jj]; |
| 554 |
if (where[kk] != 2) |
| 555 |
edegrees[where[kk]] += vwgt[kk]; |
| 556 |
else { |
| 557 |
oldgain = vwgt[kk]-rinfo[kk].edegrees[other]; |
| 558 |
rinfo[kk].edegrees[other] -= vwgt[k]; |
| 559 |
|
| 560 |
if (moved[kk] == -1) |
| 561 |
rpqUpdate(queue, kk, oldgain+vwgt[k]); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* Insert the new vertex into the priority queue */ |
| 566 |
rpqInsert(queue, k, vwgt[k]-edegrees[other]); |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
IFSET(ctrl->dbglvl, METIS_DBG_REFINE, |
| 572 |
HIGHS_ORDERING_PRINT("\tBalanced sep: %6"PRIDX" at %4"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", pwgts[2], nswaps, pwgts[0], pwgts[1], nbnd)); |
| 573 |
|
| 574 |
graph->mincut = pwgts[2]; |
| 575 |
graph->nbnd = nbnd; |
| 576 |
|
| 577 |
rpqDestroy(queue); |
| 578 |
|
| 579 |
WCOREPOP; |
| 580 |
} |
| 581 |
|