Debian Codegraph @

packages/seacas/libraries/chaco/assign/median.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2022 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "defs.h" // for TRUE, FALSE
10 #include "structs.h" // for vtx_data
11
12 void median_assign(struct vtx_data **graph, /* data structure with vertex weights */
13 double *vals, /* values of which to find median */
14 int nvtxs, /* number of values I own */
15 double *goal, /* desired sizes for sets */
16 int using_vwgts, /* are vertex weights being used? */
17 int *sets, /* assigned set for each vertex */
18 double wlow, /* sum of weights below guess */
19 double whigh, /* sum of weights above guess */
20 double guess /* median value */
21 )
22 {
23 int i; /* loop counter */
24
25 for (i = 1; i <= nvtxs; i++) {
26 if (vals[i] < guess) {
27 sets[i] = 0;
28 }
29 else if (vals[i] > guess) {
30 sets[i] = 1;
31 }
32 else {
33 if (goal[0] - wlow > goal[1] - whigh) {
34 sets[i] = 0;
35 if (using_vwgts) {
36 wlow += graph[i]->vwgt;
37 }
38 else {
39 wlow++;
40 }
41 }
42 else {
43 sets[i] = 1;
44 if (using_vwgts) {
45 whigh += graph[i]->vwgt;
46 }
47 else {
48 whigh++;
49 }
50 }
51 }
52 }
53 }
54
55 /* Find the median of set of values. */
56 /* Can also find nested medians of several sets of values */
57 /* Routine works by repeatedly guessing a value, and discarding those */
58 /* values which are on the wrong side of the guess. */
59
60 void median(struct vtx_data **graph, /* data structure with vertex weights */
61 double *vals, /* values of which to find median */
62 int nvtxs, /* number of values I own */
63 int *active, /* space for list of nvtxs ints */
64 double *goal, /* desired sizes for sets */
65 int using_vwgts, /* are vertex weights being used? */
66 int *sets /* set each vertex gets assigned to */
67 )
68 {
69 double *vptr; /* loops through vals array */
70 double val; /* value in vals array */
71 double maxval; /* largest active value */
72 double minval; /* smallest active value */
73 double guess = 0.0; /* approximate median value */
74 double nearup; /* lowest guy above guess */
75 double neardown; /* highest guy below guess */
76 double whigh; /* total weight of values above maxval */
77 double wlow; /* total weight of values below minval */
78 double wabove; /* total weight of active values above guess */
79 double wbelow; /* total weight of active values below guess */
80 double wexact; /* weight of vertices exactly at guess */
81 double lweight; /* desired weight of lower values in set */
82 double uweight; /* desired weight of upper values in set */
83 double frac; /* fraction of values I want less than guess */
84 int *aptr; /* loops through active array */
85 int *aptr2; /* helps update active array */
86 int myactive; /* number of active values I own */
87 double wfree; /* weight of vtxs not yet divided */
88 int removed; /* number of my values eliminated */
89 /*int npass = 0;*/ /* counts passes required to find median */
90 int done; /* check for termination criteria */
91 int vtx; /* vertex being considered */
92 int i; /* loop counters */
93
94 /* Initialize. */
95
96 /* Determine the desired weight sums for the two different sets. */
97 lweight = goal[0];
98 uweight = goal[1];
99
100 myactive = nvtxs;
101 whigh = wlow = 0;
102
103 /* Find largest and smallest values in vector, and construct active list. */
104 vptr = vals;
105 aptr = active;
106 minval = maxval = *(++vptr);
107 *aptr++ = 1;
108 for (i = 2; i <= nvtxs; i++) {
109 *aptr++ = i;
110 val = *(++vptr);
111 if (val > maxval) {
112 maxval = val;
113 }
114 if (val < minval) {
115 minval = val;
116 }
117 }
118
119 /* Loop until all sets are partitioned correctly. */
120 done = FALSE;
121 while (!done) {
122 /*npass++;*/
123
124 /* Select a potential dividing value. */
125 /* Currently, this assumes a linear distribution. */
126 wfree = lweight + uweight - (wlow + whigh);
127 frac = (lweight - wlow) / wfree;
128
129 /* Overshoot a bit to try to cut into largest set. */
130 frac = .5 * (frac + .5);
131
132 guess = minval + frac * (maxval - minval);
133
134 /* Now count the guys above and below this guess. */
135 /* Also find nearest values on either side of guess. */
136 wabove = wbelow = wexact = 0;
137 nearup = maxval;
138 neardown = minval;
139
140 aptr = active;
141 for (i = 0; i < myactive; i++) {
142 vtx = *aptr++;
143 val = vals[vtx];
144 if (val > guess) {
145 if (using_vwgts) {
146 wabove += graph[vtx]->vwgt;
147 }
148 else {
149 wabove++;
150 }
151 if (val < nearup) {
152 nearup = val;
153 }
154 }
155 else if (val < guess) {
156 if (using_vwgts) {
157 wbelow += graph[vtx]->vwgt;
158 }
159 else {
160 wbelow++;
161 }
162 if (val > neardown) {
163 neardown = val;
164 }
165 }
166 else {
167 if (using_vwgts) {
168 wexact += graph[vtx]->vwgt;
169 }
170 else {
171 wexact++;
172 }
173 }
174 }
175
176 /* Select a half to discard. */
177 /* And remove discarded vertices from active list. */
178 removed = 0;
179 if (wlow + wbelow - lweight > whigh + wabove - uweight && whigh + wabove + wexact < uweight) {
180 /* Discard upper set. */
181 whigh += wabove + wexact;
182 maxval = neardown;
183 done = FALSE;
184 aptr = aptr2 = active;
185 for (i = 0; i < myactive; i++) {
186 if (vals[*aptr] >= guess) {
187 ++removed;
188 }
189 else {
190 *aptr2++ = *aptr;
191 }
192 aptr++;
193 }
194 myactive -= removed;
195 if (myactive == 0) {
196 done = TRUE;
197 }
198 }
199 else if (whigh + wabove - uweight > wlow + wbelow - lweight &&
200 wlow + wbelow + wexact < lweight) {
201 /* Discard lower set. */
202 wlow += wbelow + wexact;
203 minval = nearup;
204 done = FALSE;
205 aptr = aptr2 = active;
206 for (i = 0; i < myactive; i++) {
207 if (vals[*aptr] <= guess) {
208 ++removed;
209 }
210 else {
211 *aptr2++ = *aptr;
212 }
213 aptr++;
214 }
215 myactive -= removed;
216 if (myactive == 0) {
217 done = TRUE;
218 }
219 }
220 else { /* Perfect partition! */
221 wlow += wbelow;
222 whigh += wabove;
223 done = TRUE;
224 }
225
226 /* Check for alternate termination criteria. */
227 if (!done && maxval == minval) {
228 guess = maxval;
229 done = TRUE;
230 }
231 }
232 median_assign(graph, vals, nvtxs, goal, using_vwgts, sets, wlow, whigh, guess);
233 }