00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "metislib.h"
00014
00015
00016
00017
00031
00032 int CheckGraph(graph_t *graph, int numflag, int verbose)
00033 {
00034 idx_t i, j, k, l;
00035 idx_t nvtxs, err=0;
00036 idx_t minedge, maxedge, minewgt, maxewgt;
00037 idx_t *xadj, *adjncy, *adjwgt, *htable;
00038
00039 numflag = (numflag == 0 ? 0 : 1);
00040
00041 nvtxs = graph->nvtxs;
00042 xadj = graph->xadj;
00043 adjncy = graph->adjncy;
00044 adjwgt = graph->adjwgt;
00045
00046 ASSERT(adjwgt != NULL);
00047
00048 htable = ismalloc(nvtxs, 0, "htable");
00049
00050 minedge = maxedge = adjncy[0];
00051 minewgt = maxewgt = adjwgt[0];
00052
00053 for (i=0; i<nvtxs; i++) {
00054 for (j=xadj[i]; j<xadj[i+1]; j++) {
00055 k = adjncy[j];
00056
00057 minedge = (k < minedge) ? k : minedge;
00058 maxedge = (k > maxedge) ? k : maxedge;
00059 minewgt = (adjwgt[j] < minewgt) ? adjwgt[j] : minewgt;
00060 maxewgt = (adjwgt[j] > maxewgt) ? adjwgt[j] : maxewgt;
00061
00062 if (i == k) {
00063 if (verbose)
00064 printf("Vertex %"PRIDX" contains a self-loop "
00065 "(i.e., diagonal entry in the matrix)!\n", i+numflag);
00066 err++;
00067 }
00068 else {
00069 for (l=xadj[k]; l<xadj[k+1]; l++) {
00070 if (adjncy[l] == i) {
00071 if (adjwgt[l] != adjwgt[j]) {
00072 if (verbose)
00073 printf("Edges (u:%"PRIDX" v:%"PRIDX" wgt:%"PRIDX") and "
00074 "(v:%"PRIDX" u:%"PRIDX" wgt:%"PRIDX") "
00075 "do not have the same weight!\n",
00076 i+numflag, k+numflag, adjwgt[j],
00077 k+numflag, i+numflag, adjwgt[l]);
00078 err++;
00079 }
00080 break;
00081 }
00082 }
00083 if (l == xadj[k+1]) {
00084 if (verbose)
00085 printf("Missing edge: (%"PRIDX" %"PRIDX")!\n", k+numflag, i+numflag);
00086 err++;
00087 }
00088 }
00089
00090 if (htable[k] == 0) {
00091 htable[k]++;
00092 }
00093 else {
00094 if (verbose)
00095 printf("Edge %"PRIDX" from vertex %"PRIDX" is repeated %"PRIDX" times\n",
00096 k+numflag, i+numflag, htable[k]++);
00097 err++;
00098 }
00099 }
00100
00101 for (j=xadj[i]; j<xadj[i+1]; j++)
00102 htable[adjncy[j]] = 0;
00103 }
00104
00105
00106 if (err > 0 && verbose) {
00107 printf("A total of %"PRIDX" errors exist in the input file. "
00108 "Correct them, and run again!\n", err);
00109 }
00110
00111 gk_free((void **)&htable, LTERM);
00112
00113 return (err == 0 ? 1 : 0);
00114 }
00115
00116
00117
00119
00120 int CheckInputGraphWeights(idx_t nvtxs, idx_t ncon, idx_t *xadj, idx_t *adjncy,
00121 idx_t *vwgt, idx_t *vsize, idx_t *adjwgt)
00122 {
00123 idx_t i;
00124
00125 if (ncon <= 0) {
00126 printf("Input Error: ncon must be >= 1.\n");
00127 return 0;
00128 }
00129
00130 if (vwgt) {
00131 for (i=ncon*nvtxs; i>=0; i--) {
00132 if (vwgt[i] < 0) {
00133 printf("Input Error: negative vertex weight(s).\n");
00134 return 0;
00135 }
00136 }
00137 }
00138 if (vsize) {
00139 for (i=nvtxs; i>=0; i--) {
00140 if (vsize[i] < 0) {
00141 printf("Input Error: negative vertex sizes(s).\n");
00142 return 0;
00143 }
00144 }
00145 }
00146 if (adjwgt) {
00147 for (i=xadj[nvtxs]-1; i>=0; i--) {
00148 if (adjwgt[i] < 0) {
00149 printf("Input Error: non-positive edge weight(s).\n");
00150 return 0;
00151 }
00152 }
00153 }
00154
00155 return 1;
00156 }
00157
00158
00159
00175
00176 graph_t *FixGraph(graph_t *graph)
00177 {
00178 idx_t i, j, k, l, nvtxs, nedges;
00179 idx_t *xadj, *adjncy, *adjwgt;
00180 idx_t *nxadj, *nadjncy, *nadjwgt;
00181 graph_t *ngraph;
00182 uvw_t *edges;
00183
00184
00185 nvtxs = graph->nvtxs;
00186 xadj = graph->xadj;
00187 adjncy = graph->adjncy;
00188 adjwgt = graph->adjwgt;
00189 ASSERT(adjwgt != NULL);
00190
00191 ngraph = CreateGraph();
00192
00193 ngraph->nvtxs = nvtxs;
00194
00195
00196 ngraph->ncon = graph->ncon;
00197 ngraph->vwgt = icopy(nvtxs*graph->ncon, graph->vwgt,
00198 imalloc(nvtxs*graph->ncon, "FixGraph: vwgt"));
00199
00200 ngraph->vsize = ismalloc(nvtxs, 1, "FixGraph: vsize");
00201 if (graph->vsize)
00202 icopy(nvtxs, graph->vsize, ngraph->vsize);
00203
00204
00205 edges = (uvw_t *)gk_malloc(sizeof(uvw_t)*2*xadj[nvtxs], "FixGraph: edges");
00206
00207 for (nedges=0, i=0; i<nvtxs; i++) {
00208 for (j=xadj[i]; j<xadj[i+1]; j++) {
00209
00210 if (i < adjncy[j]) {
00211 edges[nedges].u = i;
00212 edges[nedges].v = adjncy[j];
00213 edges[nedges].w = adjwgt[j];
00214 nedges++;
00215 }
00216 else if (i > adjncy[j]) {
00217 edges[nedges].u = adjncy[j];
00218 edges[nedges].v = i;
00219 edges[nedges].w = adjwgt[j];
00220 nedges++;
00221 }
00222 }
00223 }
00224
00225 uvwsorti(nedges, edges);
00226
00227
00228
00229 for (k=0, i=1; i<nedges; i++) {
00230 if (edges[k].v != edges[i].v || edges[k].u != edges[i].u) {
00231 edges[++k] = edges[i];
00232 }
00233 }
00234 nedges = k+1;
00235
00236
00237 nxadj = ngraph->xadj = ismalloc(nvtxs+1, 0, "FixGraph: nxadj");
00238 nadjncy = ngraph->adjncy = imalloc(2*nedges, "FixGraph: nadjncy");
00239 nadjwgt = ngraph->adjwgt = imalloc(2*nedges, "FixGraph: nadjwgt");
00240
00241
00242
00243 for (k=0; k<nedges; k++) {
00244 nxadj[edges[k].u]++;
00245 nxadj[edges[k].v]++;
00246 }
00247 MAKECSR(i, nvtxs, nxadj);
00248
00249 for (k=0; k<nedges; k++) {
00250 nadjncy[nxadj[edges[k].u]] = edges[k].v;
00251 nadjncy[nxadj[edges[k].v]] = edges[k].u;
00252 nadjwgt[nxadj[edges[k].u]] = edges[k].w;
00253 nadjwgt[nxadj[edges[k].v]] = edges[k].w;
00254 nxadj[edges[k].u]++;
00255 nxadj[edges[k].v]++;
00256 }
00257 SHIFTCSR(i, nvtxs, nxadj);
00258
00259 gk_free((void **)&edges, LTERM);
00260
00261 return ngraph;
00262 }
00263