broccoli.h 4.53 KB
Newer Older
Danil V. Perevozkin's avatar
Danil V. Perevozkin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef BROCCOLI_RETURNING
#define BROCCOLI_RETURNING

#include <math.h>
#include "clrqueue.h"
#include "adjmx.h"
#include "paint.h"

#include "shex.h"
#include "krdefs.h"
//----------------------------------------------
// perform (n*m / d) w/o integer overflow
static int cb_funny_div(int n, int m, int d)
{
return (int) ((double) n * (double) m / (double) d);
}
//----------------------------------------------
static int *cb_broccoli(cmatrix31 *P,
    int np,
    int wtotal,
    int *weight)
{
clrqueue *q;
int *irow = P->irow, *icol = P->icol;
int ntgt, wtgt, ncov, wcov, n = P->n;
int seed, color, cv, j;
int *result;

KR_TRAP(P->n < np);

q = cq_create(n);
color = 0;

/*
Coverage targets:
1. have no. nodes more than or equal to no. subdomains remaining
2. have remaining weight >= (total / np * remaining)
3. cover at least one node per subdomain without regard to 1,2
*/

/*
Coverage targets implementation:
  wtgt - next weight coverage target = W * (color+1) / np
  wcov - current weight coverage
  loop while wcur < wtgt
  
  ntgt - node quantity target = min(n * (color+1) / np, n - (np - (color+1)))
  ncov - current node coverage
  loop while ncur < ntgt
*/

wcov = 0;
ncov = 0;

if (wtotal == 0) wtotal = n;

while (wcov < wtotal)
    {
    wtgt = cb_funny_div(wtotal, color+1, np);
    ntgt = cb_funny_div(n, color+1, np);
    ntgt = kr_min(ntgt, n - (np - (color + 1)));
    
    if (!cq_seed(q, &seed))
        {
        KR_TRAP(wcov != wtotal);
        break;
        }

    // Again, we have some fragility here. Not enough mana to grasp it all.
    cq_push(q, seed);
    
    if (wcov >= wtgt  ||  ncov >= ntgt)
        {
        KR_TRAP(ncov > ntgt);  // Should never happen. Don't remove this trap.
        cv = cq_next(q);
        cq_mark(q, cv, color);
        if (weight) wcov += weight[cv]; else wcov += 1;
        ncov += 1;
        }
    
    while (!cq_empty(q)  &&  ncov < ntgt  &&  wcov < wtgt)
        {
        cv = cq_next(q);
        cq_mark(q, cv, color);
        if (weight) wcov += weight[cv]; else wcov += 1;
        ncov += 1;
        
        for (j = irow[cv]; j < irow[cv+1]; j++)
            cq_nudge(q, icol[j]);
        }
    
    cq_droptail(q);
    color++;
    }

KR_TRAP(ncov != n);

result = malloct(n, int);

for (cv = 0; cv < n; cv++)
    result[cv] = cq_color(q, cv);

cq_free(q);

return result;
}
//----------------------------------------------
static cmatrix31 *cb_splitting_adjmx(cmatrix31 *P, int *map)
{
adjmx *J;
int n = P->n, i, j;
cmatrix31 *result;

J = adj_create(map_color_count(n, map), 0);

for (i = 0; i < n; i++)
for (j = P->irow[i]; j < P->irow[i+1]; j++)
    adj_add_node_color(J, map[i], map[P->icol[j]]);

result = adj_to_cmatrix31(J);
adj_free(J);

return result;
}
//----------------------------------------------
static int *cb_weight(int n, int nclr, int *map)
{
int *w = calloct(nclr, int);
int i;

for (i = 0; i < n; i++)
    w[map[i]]++;

return w;
}
//----------------------------------------------
static void cb_merge(int np, int *cmap, cmatrix31 *Q)
{
int n = Q->n;
int nc, *w, i, smallest = -1, adjacent = -1, c, size;

nc = map_color_count(n, cmap);
if (nc == np) return;

w = cb_weight(n, nc, cmap);

while (nc > np)
    {
    // find the smallest subset (target):
    size = 0;
    
    for (i = 0; i < nc; i++)
    if (size < w[i])
        {
        size = w[i];
        smallest = i;
        }
    
    // find the smallest adjacent to the target:
    size = 0;
    
    for (i = Q->irow[smallest]; i < Q->irow[smallest+1]; i++)
        {
        c = Q->icol[i];
        
        if (c == smallest) continue;
        
        if (size < w[c])
            {
            size = w[c];
            adjacent = c;
            }            
        }
    
    // merge the smallest to the adjacent
    w[adjacent] += w[smallest];
    w[smallest] = w[nc-1];
    
    for (i = 0; i < n; i++)
    if (cmap[i] == smallest)
        cmap[i] = adjacent;
    
    for (i = 0; i < n; i++)
    if (cmap[i] == nc-1)
        cmap[i] = smallest;
    
    nc -= 1;
    }

free2(w);
}
//----------------------------------------------
static int *cmap_broccoli(cmatrix31 *P, int np, int fmap_shexy)
{
int *fmap, *cmap, *weight;
cmatrix31 *Q;
int i;

fmap = cb_broccoli(P, kr_max(np, (int) sqrt((double) P->n)), P->n, NULL);

if (fmap_shexy)
    shex(P, fmap);

Q = cb_splitting_adjmx(P, fmap);
weight = cb_weight(P->n, Q->n, fmap);
cmap = cb_broccoli(Q, np, P->n, weight);
cb_merge(np, cmap, Q);

for (i = 0; i < P->n; i++)
    fmap[i] = cmap[fmap[i]];

free2(weight);
mx_free(Q);
free2(cmap);

return fmap;
}
//----------------------------------------------
#endif