Debian Codegraph @

extern/metis/libmetis/initpart.c

raw
1 /*
2 * Copyright 1997, Regents of the University of Minnesota
3 *
4 * initpart.c
5 *
6 * This file contains code that performs the initial partition of the
7 * coarsest graph
8 *
9 * Started 7/23/97
10 * George
11 *
12 */
13
14 #include "metislib.h"
15
16 /*************************************************************************/
17 /*! This function computes the initial separator of the coarsest graph */
18 /*************************************************************************/
19 void InitSeparator(ctrl_t *ctrl, graph_t *graph, idx_t niparts)
20 {
21 real_t ntpwgts[2] = {0.5, 0.5};
22 mdbglvl_et dbglvl;
23
24 dbglvl = ctrl->dbglvl;
25 IFSET(ctrl->dbglvl, METIS_DBG_REFINE, ctrl->dbglvl -= METIS_DBG_REFINE);
26 IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO, ctrl->dbglvl -= METIS_DBG_MOVEINFO);
27
28 /* this is required for the cut-based part of the refinement */
29 Setup2WayBalMultipliers(ctrl, graph, ntpwgts);
30
31 switch (ctrl->iptype) {
32 case METIS_IPTYPE_EDGE:
33 if (graph->nedges == 0)
34 RandomBisection(ctrl, graph, ntpwgts, niparts);
35 else
36 GrowBisection(ctrl, graph, ntpwgts, niparts);
37
38 Compute2WayPartitionParams(ctrl, graph);
39 ConstructSeparator(ctrl, graph);
40 break;
41
42 case METIS_IPTYPE_NODE:
43 GrowBisectionNode(ctrl, graph, ntpwgts, niparts);
44 break;
45
46 default:
47 GK_ERREXIT("Unknown iptype of %"PRIDX"\n", ctrl->iptype);
48 }
49
50 IFSET(ctrl->dbglvl, METIS_DBG_IPART, HIGHS_ORDERING_PRINT("Initial Sep: %"PRIDX"\n", graph->mincut));
51
52 ctrl->dbglvl = dbglvl;
53
54 }
55
56
57 /*************************************************************************/
58 /*! This function computes a bisection of a graph by randomly assigning
59 the vertices followed by a bisection refinement.
60 The resulting partition is returned in graph->where.
61 */
62 /*************************************************************************/
63 void RandomBisection(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts,
64 idx_t niparts)
65 {
66 idx_t i, ii, j, k, nvtxs, pwgts[2], zeromaxpwgt, from, me,
67 bestcut=0, icut, mincut, inbfs;
68 const idx_t *xadj, *adjncy;
69 idx_t *vwgt, *adjwgt, *where;
70 idx_t *perm, *bestwhere;
71
72 WCOREPUSH;
73
74 nvtxs = graph->nvtxs;
75 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
76 vwgt = graph->vwgt;
77 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
78 adjwgt = graph->adjwgt;
79
80 Allocate2WayPartitionMemory(ctrl, graph);
81 where = graph->where;
82
83 bestwhere = iwspacemalloc(ctrl, nvtxs);
84 perm = iwspacemalloc(ctrl, nvtxs);
85
86 zeromaxpwgt = ctrl->ubfactors[0]*graph->tvwgt[0]*ntpwgts[0];
87
88 for (inbfs=0; inbfs<niparts; inbfs++) {
89 iset(nvtxs, 1, where);
90
91 if (inbfs > 0) {
92 irandArrayPermute(nvtxs, perm, nvtxs/2, 1, &ctrl->rng_state);
93 pwgts[1] = graph->tvwgt[0];
94 pwgts[0] = 0;
95
96 for (ii=0; ii<nvtxs; ii++) {
97 i = perm[ii];
98 if (pwgts[0]+vwgt[i] < zeromaxpwgt) {
99 where[i] = 0;
100 pwgts[0] += vwgt[i];
101 pwgts[1] -= vwgt[i];
102 if (pwgts[0] > zeromaxpwgt)
103 break;
104 }
105 }
106 }
107
108 /* Do some partition refinement */
109 Compute2WayPartitionParams(ctrl, graph);
110
111 Balance2Way(ctrl, graph, ntpwgts);
112
113 FM_2WayRefine(ctrl, graph, ntpwgts, 4);
114
115 if (inbfs==0 || bestcut > graph->mincut) {
116 bestcut = graph->mincut;
117 icopy(nvtxs, where, bestwhere);
118 if (bestcut == 0)
119 break;
120 }
121 }
122
123 graph->mincut = bestcut;
124 icopy(nvtxs, bestwhere, where);
125
126 WCOREPOP;
127 }
128
129
130 /*************************************************************************/
131 /*! This function takes a graph and produces a bisection by using a region
132 growing algorithm. The resulting bisection is refined using FM.
133 The resulting partition is returned in graph->where.
134 */
135 /*************************************************************************/
136 void GrowBisection(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts,
137 idx_t niparts)
138 {
139 idx_t i, j, k, nvtxs, drain, nleft, first, last,
140 pwgts[2], oneminpwgt, onemaxpwgt,
141 from, me, bestcut=0, icut, mincut, inbfs;
142 const idx_t *xadj, *adjncy;
143 idx_t *vwgt, *adjwgt, *where;
144 idx_t *queue, *touched, *gain, *bestwhere;
145
146 WCOREPUSH;
147
148 nvtxs = graph->nvtxs;
149 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
150 vwgt = graph->vwgt;
151 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
152 adjwgt = graph->adjwgt;
153
154 Allocate2WayPartitionMemory(ctrl, graph);
155 where = graph->where;
156
157 bestwhere = iwspacemalloc(ctrl, nvtxs);
158 queue = iwspacemalloc(ctrl, nvtxs);
159 touched = iwspacemalloc(ctrl, nvtxs);
160
161 onemaxpwgt = ctrl->ubfactors[0]*graph->tvwgt[0]*ntpwgts[1];
162 oneminpwgt = (1.0/ctrl->ubfactors[0])*graph->tvwgt[0]*ntpwgts[1];
163
164 for (inbfs=0; inbfs<niparts; inbfs++) {
165 iset(nvtxs, 1, where);
166
167 iset(nvtxs, 0, touched);
168
169 pwgts[1] = graph->tvwgt[0];
170 pwgts[0] = 0;
171
172
173 queue[0] = irandInRange(nvtxs, &ctrl->rng_state);
174 touched[queue[0]] = 1;
175 first = 0;
176 last = 1;
177 nleft = nvtxs-1;
178 drain = 0;
179
180 /* Start the BFS from queue to get a partition */
181 for (;;) {
182 if (first == last) { /* Empty. Disconnected graph! */
183 if (nleft == 0 || drain)
184 break;
185
186 k = irandInRange(nleft, &ctrl->rng_state);
187 for (i=0; i<nvtxs; i++) {
188 if (touched[i] == 0) {
189 if (k == 0)
190 break;
191 else
192 k--;
193 }
194 }
195
196 queue[0] = i;
197 touched[i] = 1;
198 first = 0;
199 last = 1;
200 nleft--;
201 }
202
203 i = queue[first++];
204 if (pwgts[0] > 0 && pwgts[1]-vwgt[i] < oneminpwgt) {
205 drain = 1;
206 continue;
207 }
208
209 where[i] = 0;
210 INC_DEC(pwgts[0], pwgts[1], vwgt[i]);
211 if (pwgts[1] <= onemaxpwgt)
212 break;
213
214 drain = 0;
215 for (j=xadj[i]; j<xadj[i+1]; j++) {
216 k = adjncy[j];
217 if (touched[k] == 0) {
218 queue[last++] = k;
219 touched[k] = 1;
220 nleft--;
221 }
222 }
223 }
224
225 /* Check to see if we hit any bad limiting cases */
226 if (pwgts[1] == 0)
227 where[irandInRange(nvtxs, &ctrl->rng_state)] = 1;
228 if (pwgts[0] == 0)
229 where[irandInRange(nvtxs, &ctrl->rng_state)] = 0;
230
231 /*************************************************************
232 * Do some partition refinement
233 **************************************************************/
234 Compute2WayPartitionParams(ctrl, graph);
235
236 Balance2Way(ctrl, graph, ntpwgts);
237
238 FM_2WayRefine(ctrl, graph, ntpwgts, ctrl->niter);
239
240 if (inbfs == 0 || bestcut > graph->mincut) {
241 bestcut = graph->mincut;
242 icopy(nvtxs, where, bestwhere);
243 if (bestcut == 0)
244 break;
245 }
246 }
247
248 graph->mincut = bestcut;
249 icopy(nvtxs, bestwhere, where);
250
251 WCOREPOP;
252 }
253
254 /*************************************************************************/
255 /* This function takes a graph and produces a tri-section into left, right,
256 and separator using a region growing algorithm. The resulting separator
257 is refined using node FM.
258 The resulting partition is returned in graph->where.
259 */
260 /**************************************************************************/
261 void GrowBisectionNode(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts,
262 idx_t niparts)
263 {
264 idx_t i, j, k, nvtxs, drain, nleft, first, last, pwgts[2], oneminpwgt,
265 onemaxpwgt, from, me, bestcut=0, icut, mincut, inbfs;
266 const idx_t *xadj, *adjncy;
267 idx_t *vwgt, *adjwgt, *where, *bndind;
268 idx_t *queue, *touched, *gain, *bestwhere;
269
270 WCOREPUSH;
271
272 nvtxs = graph->nvtxs;
273 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
274 vwgt = graph->vwgt;
275 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
276 adjwgt = graph->adjwgt;
277
278 bestwhere = iwspacemalloc(ctrl, nvtxs);
279 queue = iwspacemalloc(ctrl, nvtxs);
280 touched = iwspacemalloc(ctrl, nvtxs);
281
282 onemaxpwgt = ctrl->ubfactors[0]*graph->tvwgt[0]*0.5;
283 oneminpwgt = (1.0/ctrl->ubfactors[0])*graph->tvwgt[0]*0.5;
284
285
286 /* Allocate refinement memory. Allocate sufficient memory for both edge and node */
287 graph->pwgts = imalloc(3);
288 graph->where = imalloc(nvtxs);
289 graph->bndptr = imalloc(nvtxs);
290 graph->bndind = imalloc(nvtxs);
291 graph->id = imalloc(nvtxs);
292 graph->ed = imalloc(nvtxs);
293 graph->nrinfo = (nrinfo_t *)malloc(nvtxs*sizeof(nrinfo_t));
294
295 where = graph->where;
296 bndind = graph->bndind;
297
298 for (inbfs=0; inbfs<niparts; inbfs++) {
299 iset(nvtxs, 1, where);
300 iset(nvtxs, 0, touched);
301
302 pwgts[1] = graph->tvwgt[0];
303 pwgts[0] = 0;
304
305 queue[0] = irandInRange(nvtxs, &ctrl->rng_state);
306 touched[queue[0]] = 1;
307 first = 0; last = 1;
308 nleft = nvtxs-1;
309 drain = 0;
310
311 /* Start the BFS from queue to get a partition */
312 for (;;) {
313 if (first == last) { /* Empty. Disconnected graph! */
314 if (nleft == 0 || drain)
315 break;
316
317 k = irandInRange(nleft, &ctrl->rng_state);
318 for (i=0; i<nvtxs; i++) { /* select the kth untouched vertex */
319 if (touched[i] == 0) {
320 if (k == 0)
321 break;
322 else
323 k--;
324 }
325 }
326
327 queue[0] = i;
328 touched[i] = 1;
329 first = 0;
330 last = 1;
331 nleft--;
332 }
333
334 i = queue[first++];
335 if (pwgts[1]-vwgt[i] < oneminpwgt) {
336 drain = 1;
337 continue;
338 }
339
340 where[i] = 0;
341 INC_DEC(pwgts[0], pwgts[1], vwgt[i]);
342 if (pwgts[1] <= onemaxpwgt)
343 break;
344
345 drain = 0;
346 for (j=xadj[i]; j<xadj[i+1]; j++) {
347 k = adjncy[j];
348 if (touched[k] == 0) {
349 queue[last++] = k;
350 touched[k] = 1;
351 nleft--;
352 }
353 }
354 }
355
356 /*************************************************************
357 * Do some partition refinement
358 **************************************************************/
359 Compute2WayPartitionParams(ctrl, graph);
360 Balance2Way(ctrl, graph, ntpwgts);
361 FM_2WayRefine(ctrl, graph, ntpwgts, 4);
362
363 /* Construct and refine the vertex separator */
364 for (i=0; i<graph->nbnd; i++) {
365 j = bndind[i];
366 if (xadj[j+1]-xadj[j] > 0) /* ignore islands */
367 where[j] = 2;
368 }
369
370 Compute2WayNodePartitionParams(ctrl, graph);
371 FM_2WayNodeRefine2Sided(ctrl, graph, 1);
372 FM_2WayNodeRefine1Sided(ctrl, graph, 4);
373
374
375 if (inbfs == 0 || bestcut > graph->mincut) {
376 bestcut = graph->mincut;
377 icopy(nvtxs, where, bestwhere);
378 }
379 }
380
381 graph->mincut = bestcut;
382 icopy(nvtxs, bestwhere, where);
383
384 WCOREPOP;
385 }