ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
debug.c
Go to the documentation of this file.
1 /*
2  * Copyright 1997, Regents of the University of Minnesota
3  *
4  * debug.c
5  *
6  * This file contains code that performs self debuging
7  *
8  * Started 7/24/97
9  * George
10  *
11  * $Id: debug.c,v 1.1 1998/11/27 17:59:13 karypis Exp $
12  *
13  */
14 
15 #include "metis.h"
16 
17 /*************************************************************************
18 * This function computes the ratio assoc. given the graph and a where vector
19 **************************************************************************/
20 float ComputeRAsso(GraphType *graph, idxtype *where, int npart)
21 {
22  int i, j, cm, nvtxs;
23  idxtype *rasso, *clusterSize, *xadj, *adjncy;
24  float result;
25  idxtype * adjwgt;
26 
27  rasso = idxsmalloc(npart, 0, "ComputeNCut: ncut");
28  clusterSize = idxsmalloc(npart, 0, "ComputeNCut: degree");
29  nvtxs = graph->nvtxs;
30  xadj = graph->xadj;
31  adjncy = graph->adjncy;
32  adjwgt = graph->adjwgt;
33 
34  for (i=0; i<nvtxs; i++)
35  clusterSize[where[i]] ++;
36 
37  if (graph->adjwgt == NULL) {
38  for (i=0; i<nvtxs; i++) {
39  cm = where[i];
40  for (j=xadj[i]; j<xadj[i+1]; j++)
41  if (cm == where[adjncy[j]])
42  rasso[where[adjncy[j]]] ++;
43  }
44  }
45  else {
46  for (i=0; i<nvtxs; i++){
47  cm = where[i];
48  for (j=xadj[i]; j<xadj[i+1]; j++)
49  if (cm == where[adjncy[j]])
50  rasso[where[adjncy[j]]] += adjwgt[j];
51  }
52  }
53 
54  result =0;
55  for (i=0; i<npart; i++){
56  if (clusterSize[i] >0)
57  result += rasso[i] *1.0/ clusterSize[i];
58  }
59  free(rasso);
60  free(clusterSize);
61  return result;
62 }
63 
64 /*************************************************************************
65 * This function computes the normalized cut given the graph and a where vector
66 **************************************************************************/
67 float ComputeNCut(GraphType *graph, idxtype *where, int npart)
68 {
69  int i, j, cm, nvtxs;
70  idxtype *ncut, *degree, *xadj, *adjncy;
71  float result;
72  idxtype * adjwgt;
73 
74  ncut = idxsmalloc(npart, 0, "ComputeNCut: ncut");
75  degree = idxsmalloc(npart, 0, "ComputeNCut: degree");
76  nvtxs = graph->nvtxs;
77  xadj = graph->xadj;
78  adjncy = graph->adjncy;
79  adjwgt = graph->adjwgt;
80 
81  if (graph->adjwgt == NULL) {
82  for (i=0; i<nvtxs; i++) {
83  cm = where[i];
84  for (j=xadj[i]; j<xadj[i+1]; j++){
85  degree[cm] ++;
86  if (cm != where[adjncy[j]])
87  ncut[cm] ++;
88  }
89  }
90  }
91  else {
92  for (i=0; i<nvtxs; i++) {
93  cm = where[i];
94  for (j=xadj[i]; j<xadj[i+1]; j++){
95  degree[cm] += adjwgt[j];
96  if (cm != where[adjncy[j]])
97  ncut[cm] += adjwgt[j];
98  }
99  }
100  }
101  int empty = 0;
102  result =0;
103  for (i=0; i<npart; i++){
104  if (degree[i] == 0)
105  empty++;
106  if (degree[i] >0)
107  result += ncut[i] *1.0/ degree[i];
108  }
109  //printf("Empty clusters: %d\n", empty);
110  free(ncut);
111  free(degree);
112  return result+empty;
113 }
114 
115 
116 /*************************************************************************
117 * This function computes the cut given the graph and a where vector
118 **************************************************************************/
119 int ComputeCut(GraphType *graph, idxtype *where)
120 {
121  int i, j, cut;
122 
123  if (graph->adjwgt == NULL) {
124  for (cut=0, i=0; i<graph->nvtxs; i++) {
125  for (j=graph->xadj[i]; j<graph->xadj[i+1]; j++)
126  if (where[i] != where[graph->adjncy[j]])
127  cut++;
128  }
129  }
130  else {
131  for (cut=0, i=0; i<graph->nvtxs; i++) {
132  for (j=graph->xadj[i]; j<graph->xadj[i+1]; j++)
133  if (where[i] != where[graph->adjncy[j]])
134  cut += graph->adjwgt[j];
135  }
136  }
137 
138  return cut/2;
139 }
140 
141 
142 /*************************************************************************
143 * This function checks whether or not the boundary information is correct
144 **************************************************************************/
145 int CheckBnd(GraphType *graph)
146 {
147  int i, j, nvtxs, nbnd;
148  idxtype *xadj, *adjncy, *where, *bndptr, *bndind;
149 
150  nvtxs = graph->nvtxs;
151  xadj = graph->xadj;
152  adjncy = graph->adjncy;
153  where = graph->where;
154  bndptr = graph->bndptr;
155  bndind = graph->bndind;
156 
157  for (nbnd=0, i=0; i<nvtxs; i++) {
158  if (xadj[i+1]-xadj[i] == 0)
159  nbnd++; /* Islands are considered to be boundary vertices */
160 
161  for (j=xadj[i]; j<xadj[i+1]; j++) {
162  if (where[i] != where[adjncy[j]]) {
163  nbnd++;
164  ASSERT(bndptr[i] != -1);
165  ASSERT(bndind[bndptr[i]] == i);
166  break;
167  }
168  }
169  }
170 
171  ASSERTP(nbnd == graph->nbnd, ("%d %d\n", nbnd, graph->nbnd));
172 
173  return 1;
174 }
175 
176 
177 
178 /*************************************************************************
179 * This function checks whether or not the boundary information is correct
180 **************************************************************************/
181 int CheckBnd2(GraphType *graph)
182 {
183  int i, j, nvtxs, nbnd, id, ed;
184  idxtype *xadj, *adjncy, *where, *bndptr, *bndind;
185 
186  nvtxs = graph->nvtxs;
187  xadj = graph->xadj;
188  adjncy = graph->adjncy;
189  where = graph->where;
190  bndptr = graph->bndptr;
191  bndind = graph->bndind;
192 
193  for (nbnd=0, i=0; i<nvtxs; i++) {
194  id = ed = 0;
195  for (j=xadj[i]; j<xadj[i+1]; j++) {
196  if (where[i] != where[adjncy[j]])
197  ed += graph->adjwgt[j];
198  else
199  id += graph->adjwgt[j];
200  }
201  if (ed - id >= 0 && xadj[i] < xadj[i+1]) {
202  nbnd++;
203  ASSERTP(bndptr[i] != -1, ("%d %d %d\n", i, id, ed));
204  ASSERT(bndind[bndptr[i]] == i);
205  }
206  }
207 
208  ASSERTP(nbnd == graph->nbnd, ("%d %d\n", nbnd, graph->nbnd));
209 
210  return 1;
211 }
212 
213 /*************************************************************************
214 * This function checks whether or not the boundary information is correct
215 **************************************************************************/
216 int CheckNodeBnd(GraphType *graph, int onbnd)
217 {
218  int i, j, nvtxs, nbnd;
219  idxtype *xadj, *adjncy, *where, *bndptr, *bndind;
220 
221  nvtxs = graph->nvtxs;
222  xadj = graph->xadj;
223  adjncy = graph->adjncy;
224  where = graph->where;
225  bndptr = graph->bndptr;
226  bndind = graph->bndind;
227 
228  for (nbnd=0, i=0; i<nvtxs; i++) {
229  if (where[i] == 2)
230  nbnd++;
231  }
232 
233  ASSERTP(nbnd == onbnd, ("%d %d\n", nbnd, onbnd));
234 
235  for (i=0; i<nvtxs; i++) {
236  if (where[i] != 2) {
237  ASSERTP(bndptr[i] == -1, ("%d %d\n", i, bndptr[i]));
238  }
239  else {
240  ASSERTP(bndptr[i] != -1, ("%d %d\n", i, bndptr[i]));
241  }
242  }
243 
244  return 1;
245 }
246 
247 
248 
249 /*************************************************************************
250 * This function checks whether or not the rinfo of a vertex is consistent
251 **************************************************************************/
253 {
254  int i, j;
255 
256  for (i=0; i<rinfo->ndegrees; i++) {
257  for (j=i+1; j<rinfo->ndegrees; j++)
258  ASSERTP(rinfo->edegrees[i].pid != rinfo->edegrees[j].pid, ("%d %d %d %d\n", i, j, rinfo->edegrees[i].pid, rinfo->edegrees[j].pid));
259  }
260 
261  return 1;
262 }
263 
264 
265 
266 /*************************************************************************
267 * This function checks the correctness of the NodeFM data structures
268 **************************************************************************/
270 {
271  int i, j, k, l, nvtxs, me, other;
272  idxtype *xadj, *adjncy, *adjwgt, *vwgt, *where;
273  idxtype edegrees[2], pwgts[3];
274 
275  nvtxs = graph->nvtxs;
276  xadj = graph->xadj;
277  vwgt = graph->vwgt;
278  adjncy = graph->adjncy;
279  adjwgt = graph->adjwgt;
280 
281  where = graph->where;
282 
283  /*------------------------------------------------------------
284  / Compute now the separator external degrees
285  /------------------------------------------------------------*/
286  pwgts[0] = pwgts[1] = pwgts[2] = 0;
287  for (i=0; i<nvtxs; i++) {
288  me = where[i];
289  pwgts[me] += vwgt[i];
290 
291  if (me == 2) { /* If it is on the separator do some computations */
292  edegrees[0] = edegrees[1] = 0;
293 
294  for (j=xadj[i]; j<xadj[i+1]; j++) {
295  other = where[adjncy[j]];
296  if (other != 2)
297  edegrees[other] += vwgt[adjncy[j]];
298  }
299  if (edegrees[0] != graph->nrinfo[i].edegrees[0] || edegrees[1] != graph->nrinfo[i].edegrees[1]) {
300  printf("Something wrong with edegrees: %d %d %d %d %d\n", i, edegrees[0], edegrees[1], graph->nrinfo[i].edegrees[0], graph->nrinfo[i].edegrees[1]);
301  return 0;
302  }
303  }
304  }
305 
306  if (pwgts[0] != graph->pwgts[0] || pwgts[1] != graph->pwgts[1] || pwgts[2] != graph->pwgts[2])
307  printf("Something wrong with part-weights: %d %d %d %d %d %d\n", pwgts[0], pwgts[1], pwgts[2], graph->pwgts[0], graph->pwgts[1], graph->pwgts[2]);
308 
309  return 1;
310 }
311 
312 
313 /*************************************************************************
314 * This function checks if the separator is indeed a separator
315 **************************************************************************/
317 {
318  int i, j, nvtxs, other;
319  idxtype *xadj, *adjncy, *where;
320 
321  nvtxs = graph->nvtxs;
322  xadj = graph->xadj;
323  adjncy = graph->adjncy;
324  where = graph->where;
325 
326  for (i=0; i<nvtxs; i++) {
327  if (where[i] == 2)
328  continue;
329  other = (where[i]+1)%2;
330  for (j=xadj[i]; j<xadj[i+1]; j++) {
331  ASSERTP(where[adjncy[j]] != other, ("%d %d %d %d %d %d\n", i, where[i], adjncy[j], where[adjncy[j]], xadj[i+1]-xadj[i], xadj[adjncy[j]+1]-xadj[adjncy[j]]));
332  }
333  }
334 
335  return 1;
336 }
337 
338 
#define NULL
core::Tensor result
Definition: VtkUtils.cpp:76
float ComputeRAsso(GraphType *graph, idxtype *where, int npart)
Definition: debug.c:20
float ComputeNCut(GraphType *graph, idxtype *where, int npart)
Definition: debug.c:67
int CheckNodePartitionParams(GraphType *graph)
Definition: debug.c:269
int CheckBnd2(GraphType *graph)
Definition: debug.c:181
int ComputeCut(GraphType *graph, idxtype *where)
Definition: debug.c:119
int CheckBnd(GraphType *graph)
Definition: debug.c:145
int IsSeparable(GraphType *graph)
Definition: debug.c:316
int CheckRInfo(RInfoType *rinfo)
Definition: debug.c:252
int CheckNodeBnd(GraphType *graph, int onbnd)
Definition: debug.c:216
#define ASSERT(expr)
Definition: macros.h:125
#define ASSERTP(expr, msg)
Definition: macros.h:137
void clusterSize(GraphType *graph, int *clustersize)
Definition: util.c:33
#define idxsmalloc
Definition: rename.h:386
int idxtype
Definition: struct.h:17
idxtype pid
Definition: struct.h:85
int nbnd
Definition: struct.h:184
idxtype * adjncy
Definition: struct.h:172
idxtype * bndind
Definition: struct.h:185
idxtype * where
Definition: struct.h:183
int nvtxs
Definition: struct.h:168
idxtype * vwgt
Definition: struct.h:170
NRInfoType * nrinfo
Definition: struct.h:197
idxtype * pwgts
Definition: struct.h:183
idxtype * adjwgt
Definition: struct.h:173
idxtype * bndptr
Definition: struct.h:185
idxtype * xadj
Definition: struct.h:169
idxtype edegrees[2]
Definition: struct.h:154
int ndegrees
Definition: struct.h:128
EDegreeType * edegrees
Definition: struct.h:129