Debian Codegraph @

extern/metis/libmetis/compress.c

raw
1 /*
2 * Copyright 1997, Regents of the University of Minnesota
3 *
4 * compress.c
5 *
6 * This file contains code for compressing nodes with identical adjacency
7 * structure and for prunning dense columns
8 *
9 * Started 9/17/97
10 * George
11 */
12
13 #include "metislib.h"
14
15 /*************************************************************************/
16 /*! This function compresses a graph by merging identical vertices
17 The compression should lead to at least 10% reduction.
18
19 The compressed graph that is generated has its adjwgts set to 1.
20
21 \returns 1 if compression was performed, otherwise it returns 0.
22
23 */
24 /**************************************************************************/
25 graph_t *CompressGraph(ctrl_t *ctrl, idx_t nvtxs, const idx_t *xadj, const idx_t *adjncy,
26 idx_t *vwgt, idx_t *cptr, idx_t *cind)
27 {
28 idx_t i, ii, iii, j, jj, k, l, cnvtxs, cnedges;
29 idx_t *cxadj, *cadjncy, *cvwgt, *mark, *map;
30 ikv_t *keys;
31 graph_t *graph=NULL;
32
33 mark = ismalloc(nvtxs, -1);
34 map = ismalloc(nvtxs, -1);
35 keys = ikvmalloc(nvtxs);
36
37 /* Compute a key for each adjacency list */
38 for (i=0; i<nvtxs; i++) {
39 k = 0;
40 for (j=xadj[i]; j<xadj[i+1]; j++)
41 k += adjncy[j];
42 keys[i].key = k+i; /* Add the diagonal entry as well */
43 keys[i].val = i;
44 }
45
46 ikvsorti(nvtxs, keys);
47
48 l = cptr[0] = 0;
49 for (cnvtxs=i=0; i<nvtxs; i++) {
50 ii = keys[i].val;
51 if (map[ii] == -1) {
52 mark[ii] = i; /* Add the diagonal entry */
53 for (j=xadj[ii]; j<xadj[ii+1]; j++)
54 mark[adjncy[j]] = i;
55
56 map[ii] = cnvtxs;
57 cind[l++] = ii;
58
59 for (j=i+1; j<nvtxs; j++) {
60 iii = keys[j].val;
61
62 if (keys[i].key != keys[j].key || xadj[ii+1]-xadj[ii] != xadj[iii+1]-xadj[iii])
63 break; /* Break if keys or degrees are different */
64
65 if (map[iii] == -1) { /* Do a comparison if iii has not been mapped */
66 for (jj=xadj[iii]; jj<xadj[iii+1]; jj++) {
67 if (mark[adjncy[jj]] != i)
68 break;
69 }
70
71 if (jj == xadj[iii+1]) { /* Identical adjacency structure */
72 map[iii] = cnvtxs;
73 cind[l++] = iii;
74 }
75 }
76 }
77
78 cptr[++cnvtxs] = l;
79 }
80 }
81
82 IFSET(ctrl->dbglvl, METIS_DBG_INFO,
83 HIGHS_ORDERING_PRINT(" Compression: reduction in # of vertices: %"PRIDX".\n", nvtxs-cnvtxs));
84
85
86 if (cnvtxs < COMPRESSION_FRACTION*nvtxs) {
87 /* Sufficient compression is possible, so go ahead and create the
88 compressed graph */
89
90 graph = CreateGraph();
91
92 cnedges = 0;
93 for (i=0; i<cnvtxs; i++) {
94 ii = cind[cptr[i]];
95 cnedges += xadj[ii+1]-xadj[ii];
96 }
97
98 /* Allocate memory for the compressed graph */
99 cxadj = graph->xadj = imalloc(cnvtxs+1);
100 cvwgt = graph->vwgt = ismalloc(cnvtxs, 0);
101 cadjncy = graph->adjncy = imalloc(cnedges);
102 graph->adjwgt = ismalloc(cnedges, 1);
103
104 /* Now go and compress the graph */
105 iset(nvtxs, -1, mark);
106 l = cxadj[0] = 0;
107 for (i=0; i<cnvtxs; i++) {
108 mark[i] = i; /* Remove any dioganal entries in the compressed graph */
109 for (j=cptr[i]; j<cptr[i+1]; j++) {
110 ii = cind[j];
111
112 /* accumulate the vertex weights of the constituent vertices */
113 cvwgt[i] += (vwgt == NULL ? 1 : vwgt[ii]);
114
115 /* generate the combined adjacency list */
116 for (jj=xadj[ii]; jj<xadj[ii+1]; jj++) {
117 k = map[adjncy[jj]];
118 if (mark[k] != i) {
119 mark[k] = i;
120 cadjncy[l++] = k;
121 }
122 }
123 }
124 cxadj[i+1] = l;
125 }
126
127 graph->nvtxs = cnvtxs;
128 graph->nedges = l;
129 graph->ncon = 1;
130
131 SetupGraph_tvwgt(graph);
132 SetupGraph_label(graph);
133 }
134
135 gk_free((void **)&keys);
136 gk_free((void**)&map);
137 gk_free((void**)&mark);
138
139 return graph;
140
141 }
142
143
144
145 /*************************************************************************/
146 /*! This function prunes all the vertices in a graph with degree greater
147 than factor*average.
148
149 \returns the number of vertices that were prunned.
150 */
151 /*************************************************************************/
152 graph_t *PruneGraph(ctrl_t *ctrl, idx_t nvtxs, const idx_t *xadj, const idx_t *adjncy,
153 idx_t *vwgt, idx_t *iperm, real_t factor)
154 {
155 idx_t i, j, k, l, nlarge, pnvtxs, pnedges;
156 idx_t *pxadj, *padjncy, *padjwgt, *pvwgt;
157 idx_t *perm;
158 graph_t *graph=NULL;
159
160 perm = imalloc(nvtxs);
161
162 factor = factor*xadj[nvtxs]/nvtxs;
163
164 pnvtxs = pnedges = nlarge = 0;
165 for (i=0; i<nvtxs; i++) {
166 if (xadj[i+1]-xadj[i] < factor) {
167 perm[i] = pnvtxs;
168 iperm[pnvtxs++] = i;
169 pnedges += xadj[i+1]-xadj[i];
170 }
171 else {
172 perm[i] = nvtxs - ++nlarge;
173 iperm[nvtxs-nlarge] = i;
174 }
175 }
176
177 IFSET(ctrl->dbglvl, METIS_DBG_INFO,
178 HIGHS_ORDERING_PRINT(" Pruned %"PRIDX" of %"PRIDX" vertices.\n", nlarge, nvtxs));
179
180
181 if (nlarge > 0 && nlarge < nvtxs) {
182 /* Prunning is possible, so go ahead and create the prunned graph */
183 graph = CreateGraph();
184
185 /* Allocate memory for the prunned graph*/
186 pxadj = graph->xadj = imalloc(pnvtxs+1);
187 pvwgt = graph->vwgt = imalloc(pnvtxs);
188 padjncy = graph->adjncy = imalloc(pnedges);
189 graph->adjwgt = ismalloc(pnedges, 1);
190
191 pxadj[0] = pnedges = l = 0;
192 for (i=0; i<nvtxs; i++) {
193 if (xadj[i+1]-xadj[i] < factor) {
194 pvwgt[l] = (vwgt == NULL ? 1 : vwgt[i]);
195
196 for (j=xadj[i]; j<xadj[i+1]; j++) {
197 k = perm[adjncy[j]];
198 if (k < pnvtxs)
199 padjncy[pnedges++] = k;
200 }
201 pxadj[++l] = pnedges;
202 }
203 }
204
205 graph->nvtxs = pnvtxs;
206 graph->nedges = pnedges;
207 graph->ncon = 1;
208
209 SetupGraph_tvwgt(graph);
210 SetupGraph_label(graph);
211 }
212 else if (nlarge > 0 && nlarge == nvtxs) {
213 IFSET(ctrl->dbglvl, METIS_DBG_INFO,
214 HIGHS_ORDERING_PRINT(" Pruning is ignored as it removes all vertices.\n"));
215 nlarge = 0;
216 }
217
218
219 gk_free((void **)&perm);
220
221 return graph;
222 }
223
224
225
226
227
228
229
230
231