ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
match.c
Go to the documentation of this file.
1 /*
2  * Copyright 1997, Regents of the University of Minnesota
3  *
4  * match.c
5  *
6  * This file contains the code that computes matchings and creates the next
7  * level coarse graph.
8  *
9  * Started 7/23/97
10  * George
11  *
12  * $Id: match.c,v 1.1 1998/11/27 17:59:18 karypis Exp $
13  *
14  */
15 
16 #include "metis.h"
17 
18 /*************************************************************************
19 * This function finds a matching using the HEM heuristic
20 **************************************************************************/
21 void Match_SHEMN(CtrlType *ctrl, GraphType *graph)
22 {
23  int i, ii, j, k, nvtxs, cnvtxs, maxidx, avgdegree;
24  idxtype *xadj, *vwgt, *adjncy, *adjwgt, *adjwgtsum;
25  idxtype *match, *cmap, *degrees, *perm, *tperm;
26  float rtemp1, rtemp2, maxwgt;
27 
28  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
29 
30  nvtxs = graph->nvtxs;
31  xadj = graph->xadj;
32  vwgt = graph->vwgt;
33  adjncy = graph->adjncy;
34  adjwgt = graph->adjwgt;
35  adjwgtsum = graph->adjwgtsum;
36 
37  cmap = graph->cmap;
38  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
39 
40  perm = idxwspacemalloc(ctrl, nvtxs);
41  tperm = idxwspacemalloc(ctrl, nvtxs);
42  degrees = idxwspacemalloc(ctrl, nvtxs);
43 
44  RandomPermute(nvtxs, tperm, 1);
45  avgdegree = (int) 0.7*(xadj[nvtxs]/nvtxs);
46  for (i=0; i<nvtxs; i++)
47  degrees[i] = (xadj[i+1]-xadj[i] > avgdegree ? avgdegree : xadj[i+1]-xadj[i]);
48  BucketSortKeysInc(nvtxs, avgdegree, degrees, tperm, perm);
49 
50  cnvtxs = 0;
51 
52  /* Take care any islands. Islands are matched with non-islands due to coarsening */
53  for (ii=0; ii<nvtxs; ii++) {
54  i = perm[ii];
55 
56  if (match[i] == UNMATCHED) { /* Unmatched */
57  if (xadj[i] < xadj[i+1])
58  break;
59 
60  maxidx = i;
61  for (j=nvtxs-1; j>ii; j--) {
62  k = perm[j];
63  if (match[k] == UNMATCHED && xadj[k] < xadj[k+1]) {
64  maxidx = k;
65  break;
66  }
67  }
68 
69  cmap[i] = cmap[maxidx] = cnvtxs++;
70  match[i] = maxidx;
71  match[maxidx] = i;
72  }
73  }
74 
75  /* Continue with normal matching */
76  for (; ii<nvtxs; ii++) {
77  i = perm[ii];
78 
79  if (match[i] == UNMATCHED) { /* Unmatched */
80  maxidx = i;
81  maxwgt = 0;
82  //rtemp1 = 1.0/vwgt[i];
83  rtemp1 = 1.0/adjwgtsum[i];
84  /* Find a heavy-edge matching, subject to maxvwgt constraints */
85  for (j=xadj[i]; j<xadj[i+1]; j++) {
86  k = adjncy[j];
87  //rtemp2 = adjwgt[j] *(rtemp1 + 1.0/vwgt[k]);
88  rtemp2 = adjwgt[j] *(rtemp1 + 1.0/adjwgtsum[k]);
89  if (match[k] == UNMATCHED && maxwgt < rtemp2 && vwgt[i]+vwgt[k] <= ctrl->maxvwgt) {
90  maxwgt = rtemp2;
91  maxidx = adjncy[j];
92  }
93  }
94 
95  cmap[i] = cmap[maxidx] = cnvtxs++;
96  match[i] = maxidx;
97  match[maxidx] = i;
98  }
99  }
100 
101  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
102 
103  idxwspacefree(ctrl, nvtxs); /* degrees */
104  idxwspacefree(ctrl, nvtxs); /* tperm */
105 
106  CreateCoarseGraph(ctrl, graph, cnvtxs, match, perm);
107 
108  idxwspacefree(ctrl, nvtxs);
109  idxwspacefree(ctrl, nvtxs);
110 }
111 
112 
113 /*************************************************************************
114 * This function finds a matching using the HEM heuristic
115 **************************************************************************/
116 void Match_HEMN(CtrlType *ctrl, GraphType *graph)
117 {
118  int i, ii, j, k, nvtxs, cnvtxs, maxidx;
119  idxtype *xadj, *vwgt, *adjncy, *adjwgt, *adjwgtsum;
120  idxtype *match, *cmap, *perm;
121  float rtemp1, rtemp2, maxwgt;
122 
123  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
124 
125  nvtxs = graph->nvtxs;
126  xadj = graph->xadj;
127  vwgt = graph->vwgt;
128  adjncy = graph->adjncy;
129  adjwgt = graph->adjwgt;
130  adjwgtsum = graph->adjwgtsum;
131 
132  cmap = graph->cmap;
133  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
134 
135  perm = idxwspacemalloc(ctrl, nvtxs);
136  RandomPermute(nvtxs, perm, 1);
137 
138  cnvtxs = 0;
139  for (ii=0; ii<nvtxs; ii++) {
140  i = perm[ii];
141 
142  if (match[i] == UNMATCHED) { /* Unmatched */
143  maxidx = i;
144  maxwgt = 0;
145  rtemp1 = 1.0/adjwgtsum[i];
146  //rtemp1 = 1.0/vwgt[i];
147  /* Find a heavy-edge matching, subject to maxvwgt constraints */
148  for (j=xadj[i]; j<xadj[i+1]; j++) {
149  k = adjncy[j];
150  rtemp2 = adjwgt[j] *(rtemp1 + 1.0/adjwgtsum[k]);
151  //rtemp2 = adjwgt[j] *(rtemp1 + 1.0/vwgt[k]);
152  if (match[k] == UNMATCHED && maxwgt < rtemp2 && vwgt[i]+vwgt[k] <= ctrl->maxvwgt) {
153  maxwgt = rtemp2;
154  maxidx = adjncy[j];
155  }
156  }
157 
158  cmap[i] = cmap[maxidx] = cnvtxs++;
159  match[i] = maxidx;
160  match[maxidx] = i;
161  }
162  }
163  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
164 
165  CreateCoarseGraph(ctrl, graph, cnvtxs, match, perm);
166 
167  idxwspacefree(ctrl, nvtxs);
168  idxwspacefree(ctrl, nvtxs);
169 }
170 
171 
172 /*************************************************************************
173 * This function finds a matching using the HEM heuristic
174 **************************************************************************/
175 void Match_RM(CtrlType *ctrl, GraphType *graph)
176 {
177  int i, ii, j, nvtxs, cnvtxs, maxidx;
178  idxtype *xadj, *vwgt, *adjncy, *adjwgt;
179  idxtype *match, *cmap, *perm;
180 
181  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
182 
183  nvtxs = graph->nvtxs;
184  xadj = graph->xadj;
185  vwgt = graph->vwgt;
186  adjncy = graph->adjncy;
187  adjwgt = graph->adjwgt;
188 
189  cmap = graph->cmap;
190  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
191 
192  perm = idxwspacemalloc(ctrl, nvtxs);
193  RandomPermute(nvtxs, perm, 1);
194 
195  cnvtxs = 0;
196  for (ii=0; ii<nvtxs; ii++) {
197  i = perm[ii];
198 
199  if (match[i] == UNMATCHED) { /* Unmatched */
200  maxidx = i;
201 
202  /* Find a random matching, subject to maxvwgt constraints */
203  for (j=xadj[i]; j<xadj[i+1]; j++) {
204  if (match[adjncy[j]] == UNMATCHED && vwgt[i]+vwgt[adjncy[j]] <= ctrl->maxvwgt) {
205  maxidx = adjncy[j];
206  break;
207  }
208  }
209 
210  cmap[i] = cmap[maxidx] = cnvtxs++;
211  match[i] = maxidx;
212  match[maxidx] = i;
213  }
214  }
215 
216  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
217 
218  CreateCoarseGraph(ctrl, graph, cnvtxs, match, perm);
219 
220  idxwspacefree(ctrl, nvtxs);
221  idxwspacefree(ctrl, nvtxs);
222 }
223 
224 
225 /*************************************************************************
226 * This function finds a matching using the HEM heuristic
227 **************************************************************************/
228 void Match_RM_NVW(CtrlType *ctrl, GraphType *graph)
229 {
230  int i, ii, j, nvtxs, cnvtxs, maxidx;
231  idxtype *xadj, *adjncy;
232  idxtype *match, *cmap, *perm;
233 
234  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
235 
236  nvtxs = graph->nvtxs;
237  xadj = graph->xadj;
238  adjncy = graph->adjncy;
239 
240  cmap = graph->cmap;
241  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
242 
243  perm = idxwspacemalloc(ctrl, nvtxs);
244  RandomPermute(nvtxs, perm, 1);
245 
246  cnvtxs = 0;
247  for (ii=0; ii<nvtxs; ii++) {
248  i = perm[ii];
249 
250  if (match[i] == UNMATCHED) { // Unmatched
251  maxidx = i;
252 
253  // Find a random matching, subject to maxvwgt constraints
254  for (j=xadj[i]; j<xadj[i+1]; j++) {
255  if (match[adjncy[j]] == UNMATCHED) {
256  maxidx = adjncy[j];
257  break;
258  }
259  }
260 
261  cmap[i] = cmap[maxidx] = cnvtxs++;
262  match[i] = maxidx;
263  match[maxidx] = i;
264  }
265  }
266 
267  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
268 
269  CreateCoarseGraph_NVW(ctrl, graph, cnvtxs, match, perm);
270 
271  idxwspacefree(ctrl, nvtxs);
272  idxwspacefree(ctrl, nvtxs);
273 }
274 
275 
276 
277 
278 /*************************************************************************
279 * This function finds a matching using the HEM heuristic
280 **************************************************************************/
281 void Match_HEM(CtrlType *ctrl, GraphType *graph)
282 {
283  int i, ii, j, k, nvtxs, cnvtxs, maxidx, maxwgt;
284  idxtype *xadj, *vwgt, *adjncy, *adjwgt;
285  idxtype *match, *cmap, *perm;
286 
287  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
288 
289  nvtxs = graph->nvtxs;
290  xadj = graph->xadj;
291  vwgt = graph->vwgt;
292  adjncy = graph->adjncy;
293  adjwgt = graph->adjwgt;
294 
295  cmap = graph->cmap;
296  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
297 
298  perm = idxwspacemalloc(ctrl, nvtxs);
299  RandomPermute(nvtxs, perm, 1);
300 
301  cnvtxs = 0;
302  for (ii=0; ii<nvtxs; ii++) {
303  i = perm[ii];
304 
305  if (match[i] == UNMATCHED) { /* Unmatched */
306  maxidx = i;
307  maxwgt = 0;
308 
309  /* Find a heavy-edge matching, subject to maxvwgt constraints */
310  for (j=xadj[i]; j<xadj[i+1]; j++) {
311  k = adjncy[j];
312  if (match[k] == UNMATCHED && maxwgt < adjwgt[j] && vwgt[i]+vwgt[k] <= ctrl->maxvwgt) {
313  maxwgt = adjwgt[j];
314  maxidx = adjncy[j];
315  }
316  }
317 
318  cmap[i] = cmap[maxidx] = cnvtxs++;
319  match[i] = maxidx;
320  match[maxidx] = i;
321  }
322  }
323 
324  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
325 
326  CreateCoarseGraph(ctrl, graph, cnvtxs, match, perm);
327 
328  idxwspacefree(ctrl, nvtxs);
329  idxwspacefree(ctrl, nvtxs);
330 }
331 
332 
333 
334 /*************************************************************************
335 * This function finds a matching using the HEM heuristic
336 **************************************************************************/
337 void Match_SHEM(CtrlType *ctrl, GraphType *graph)
338 {
339  int i, ii, j, k, nvtxs, cnvtxs, maxidx, maxwgt, avgdegree;
340  idxtype *xadj, *vwgt, *adjncy, *adjwgt;
341  idxtype *match, *cmap, *degrees, *perm, *tperm;
342 
343  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->MatchTmr));
344 
345  nvtxs = graph->nvtxs;
346  xadj = graph->xadj;
347  vwgt = graph->vwgt;
348  adjncy = graph->adjncy;
349  adjwgt = graph->adjwgt;
350 
351  cmap = graph->cmap;
352  match = idxset(nvtxs, UNMATCHED, idxwspacemalloc(ctrl, nvtxs));
353 
354  perm = idxwspacemalloc(ctrl, nvtxs);
355  tperm = idxwspacemalloc(ctrl, nvtxs);
356  degrees = idxwspacemalloc(ctrl, nvtxs);
357 
358  RandomPermute(nvtxs, tperm, 1);
359  avgdegree = (int) 0.7*(xadj[nvtxs]/nvtxs);
360  for (i=0; i<nvtxs; i++)
361  degrees[i] = (xadj[i+1]-xadj[i] > avgdegree ? avgdegree : xadj[i+1]-xadj[i]);
362  BucketSortKeysInc(nvtxs, avgdegree, degrees, tperm, perm);
363 
364  cnvtxs = 0;
365 
366  /* Take care any islands. Islands are matched with non-islands due to coarsening */
367  for (ii=0; ii<nvtxs; ii++) {
368  i = perm[ii];
369 
370  if (match[i] == UNMATCHED) { /* Unmatched */
371  if (xadj[i] < xadj[i+1])
372  break;
373 
374  maxidx = i;
375  for (j=nvtxs-1; j>ii; j--) {
376  k = perm[j];
377  if (match[k] == UNMATCHED && xadj[k] < xadj[k+1]) {
378  maxidx = k;
379  break;
380  }
381  }
382 
383  cmap[i] = cmap[maxidx] = cnvtxs++;
384  match[i] = maxidx;
385  match[maxidx] = i;
386  }
387  }
388 
389  /* Continue with normal matching */
390  for (; ii<nvtxs; ii++) {
391  i = perm[ii];
392 
393  if (match[i] == UNMATCHED) { /* Unmatched */
394  maxidx = i;
395  maxwgt = 0;
396 
397  /* Find a heavy-edge matching, subject to maxvwgt constraints */
398  for (j=xadj[i]; j<xadj[i+1]; j++) {
399  if (match[adjncy[j]] == UNMATCHED && maxwgt < adjwgt[j] && vwgt[i]+vwgt[adjncy[j]] <= ctrl->maxvwgt) {
400  maxwgt = adjwgt[j];
401  maxidx = adjncy[j];
402  }
403  }
404 
405  cmap[i] = cmap[maxidx] = cnvtxs++;
406  match[i] = maxidx;
407  match[maxidx] = i;
408  }
409  }
410 
411  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->MatchTmr));
412 
413  idxwspacefree(ctrl, nvtxs); /* degrees */
414  idxwspacefree(ctrl, nvtxs); /* tperm */
415 
416  CreateCoarseGraph(ctrl, graph, cnvtxs, match, perm);
417 
418  idxwspacefree(ctrl, nvtxs);
419  idxwspacefree(ctrl, nvtxs);
420 }
421 
#define UNMATCHED
Definition: defs.h:151
#define DBG_TIME
Definition: defs.h:174
#define stoptimer(tmr)
Definition: macros.h:49
#define IFSET(a, flag, cmd)
Definition: macros.h:56
#define starttimer(tmr)
Definition: macros.h:48
void Match_SHEMN(CtrlType *ctrl, GraphType *graph)
Definition: match.c:21
void Match_RM(CtrlType *ctrl, GraphType *graph)
Definition: match.c:175
void Match_SHEM(CtrlType *ctrl, GraphType *graph)
Definition: match.c:337
void Match_RM_NVW(CtrlType *ctrl, GraphType *graph)
Definition: match.c:228
void Match_HEM(CtrlType *ctrl, GraphType *graph)
Definition: match.c:281
void Match_HEMN(CtrlType *ctrl, GraphType *graph)
Definition: match.c:116
#define CreateCoarseGraph_NVW
Definition: rename.h:28
#define RandomPermute
Definition: rename.h:410
#define idxset
Definition: rename.h:390
#define CreateCoarseGraph
Definition: rename.h:26
#define idxwspacemalloc
Definition: rename.h:164
#define BucketSortKeysInc
Definition: rename.h:22
#define idxwspacefree
Definition: rename.h:165
int idxtype
Definition: struct.h:17
int dbglvl
Definition: struct.h:224
int maxvwgt
Definition: struct.h:228
timer MatchTmr
Definition: struct.h:238
idxtype * adjncy
Definition: struct.h:172
int nvtxs
Definition: struct.h:168
idxtype * vwgt
Definition: struct.h:170
idxtype * adjwgtsum
Definition: struct.h:175
idxtype * adjwgt
Definition: struct.h:173
idxtype * xadj
Definition: struct.h:169
idxtype * cmap
Definition: struct.h:179