-
Notifications
You must be signed in to change notification settings - Fork 0
/
burkov.c
260 lines (223 loc) · 7.78 KB
/
burkov.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "burkov.h"
void (*dicho_tree) (node_t*, const int, item_t*) = dicho_tree_notrecursive;
node_t* (*burkovtree)(const task_t*) = optimal_dichotomic_tree;
void (*treesolver) (node_t*, knint) = notrecursive_treesolver;
/*
* building optimum dichotomy tree on Burkov,Burkova's works
*/
node_t* optimal_dichotomic_tree ( const task_t *task){
//{ finding q <= countItems - number of elements on which the maximal symmetric hierarchy must be created
int q = find_q (task->b);
q = (q > task->length) ? task->length : q;
//}
// maximum symmetric hierarchy must be created from top to down!
// optimally sorting items
item_t *diitems, // items for dichotomic part
*dpitems; // items for dynamic programming branch
prep_items(task->length, task->items, q, &diitems, &dpitems);
// head of optimal dichotomic tree
node_t* head = createnodes (2*task->length-1); // number of all nodes of any tree is doubled number of it's leafs minus one.
head->hnode = NULL; // parentof the head of the tree
// DP branch
node_t *p = head;
item_t *pl = dpitems, *tmp;
int i;
for ( i = 0 ; i < task->length-q ; i++ ) { // in fact p move as p = p + 2
p->lnode = (p+1);
p->lnode->items = NULL;
tmp = copyitem(pl);
HASH_ADD_KEYPTR ( hh, p->lnode->items, tmp->w, KNINT_SIZE, tmp);
pl++;
p->lnode->length = 1;
p->lnode->hnode = p;
p->rnode = (p+2);
p->rnode->hnode = p;
p = p->rnode;
}
// Dichotomic branch
dicho_tree(p, q, diitems);
return head;
}
// must be: q <= size
// diitems - items for dichotomic part of tree
// dpitems - items for dynamic programming part of tree
void prep_items ( const int size, item_t *list, const int q, item_t **diitems, item_t **dpitems ){
// it's just a plug
(*dpitems) = list;
(*diitems) = list + (size-q); // dangerous:
}
// b(q)
knint bq[] = {0, 0, 4, 6, 12, 20, 38, 70, 140, 268, 532, 1044, 2086, 4134, 8262, 16454, 32908, 65676, 131340, 262412, 524820, 1049108, 2098196, 4195348, 8390694,
16779302, 33558566, 67112998, 134225990, 268443718, 536887366, 1073758278, 2147516556, 4295000204, 8590000268, 17179934860, 34359869708, 68719608076, 137439215884, 274878169356};
const short bq_size = sizeof(bq) / sizeof(bq[0]);
int find_q (knint b) {
knint *i = bq+2;
for ( ; (i<bq+bq_size && (*i) < b ) ; i++ ) {}
return (i-bq);
}
void dicho_tree_notrecursive(node_t *head, const int size, item_t *items){
int p_size = 1, pnext_size = 2;
node_t *p = head, *pnext, *t1, *t2;
int *sizes = (int*)malloc(2*sizeof(int)), // size of accordingly subtree items
*indexes = sizes + 1, // start index of items that belongs to accordingly subtree
*tmpsizes, *tmpindexes, *ss, *ii, *ss2, *ii2;
*sizes = size; *indexes = 0;
// creating a maximum symmetric tree
int dp = (int)log2f ((float)size); // tree's depth
int i;
for ( i = 0 ; i < dp ; i++ ){
//1: pnext = (node_t*)calloc (pnext_size, NODE_SIZE); // the next level of tree
pnext = p + p_size; // the next level of tree
ss = (int*)malloc (2*pnext_size*sizeof(int));
ii = ss + pnext_size;
t2 = pnext;
ss2 = ss;
ii2 = ii;
tmpsizes = sizes; // for free()
//tmpindexes = indexes; // for free()
int j = 0, d;
for( t1 = p ; t1 < p + p_size ; t1++ ){
d = (*sizes) / 2;
*ss2 = d;
*(ss2+1) = (*sizes) - d;
ss2 = ss2 + 2;
*ii2 = (*indexes);
*(ii2+1) = (*indexes) + d;
ii2 = ii2 + 2;
sizes++;
indexes++;
t1->lnode = t2;
t2->hnode = t1; t2++;
t1->rnode = t2;
t2->hnode = t1; t2++;
j++;
}
sizes = ss;
indexes = ii;
free (tmpsizes);
//free(tmpindexes); commented because ss = malloc; ii = ss + ...
p = pnext;
p_size = pnext_size;
pnext_size <<= 1;
} // for i
// hang up all remaining items (where sizes[i]==2)
//t2 = p;
item_t *tmp;
pnext = p + p_size;
for( i = 0 ; i < p_size ; i++ ){
if( sizes[i] > 2 ) { printf("size of leaf more than 2\n"); fflush(stdout); }
if( sizes[i] == 2 ){
//1: pnext = (node_t*)calloc (2,NODE_SIZE);
pnext->items = NULL;
tmp = copyitem (&items[indexes[i]]);
HASH_ADD_KEYPTR (hh, pnext->items, tmp->w, KNINT_SIZE, tmp);
pnext->length = 1;
p->lnode = pnext;
pnext->hnode = p; pnext++;
pnext->items = NULL;
tmp = copyitem (&items[indexes[i]+1]);
HASH_ADD_KEYPTR (hh, pnext->items, tmp->w, KNINT_SIZE, tmp);
pnext->length = 1;
p->rnode = pnext;
pnext->hnode = p; pnext++;
} else if( sizes[i] == 1 ){
p->items = NULL;
tmp = copyitem (&items[indexes[i]]);
HASH_ADD_KEYPTR (hh, p->items, tmp->w, KNINT_SIZE, tmp);
p->length = 1;
} else {
// error
printf("size of leaf is non-positive?\n");
fflush(stdout);
}
p++;
}
}// dicho_tree_notrecursive()
/* funcs for solving */
void notrecursive_treesolver ( node_t* root, knint cons ){
node_t* runner = root;
if ( root->rnode == NULL || root->lnode == NULL ) return;
int depth = 0;
while ( runner != NULL ) {
while ( (runner->rnode->length == 0) || (runner->lnode->length == 0) ) {
while ( runner->rnode->length == 0 ) { runner = runner->rnode; depth++; }
while ( runner->lnode->length == 0 ) { runner = runner->lnode; depth++; }
}
runner->items = dichosolve(runner->lnode->length, runner->lnode->items,
runner->rnode->length, runner->rnode->items,
cons, &(runner->length) );
printf ( "depth = %d. right length = %d, left length = %d\n", depth, runner->rnode->length, runner->lnode->length );
//print_hash (runner->rnode->items);
//print_hash (runner->lnode->items);
fflush (stdout);
runner = runner->hnode;
depth--;
}
}
void recursive_treesolver(node_t* root, knint cons){
if( root->length != 0 ) return;
if( root->lnode->length == 0 ) treesolver (root->lnode,cons);
if( root->rnode->length == 0 ) treesolver (root->rnode,cons);
root->items = dichosolve(root->lnode->length, root->lnode->items,
root->rnode->length, root->rnode->items,
cons, &(root->length) );
// print_tree(root);
}
item_t* dichosolve(int size1, item_t* first, int size2, item_t* second, knint cons, int* rezsize) {
if( size1 == -1 && size2 == -1 ){
*rezsize = -1;
return NULL;
}
if ( size1 == -1 ) {
*rezsize = size2;
return copyhash (second);
}
if ( size2 == -1 ) {
*rezsize = size1;
return copyhash (first);
}
//item_t *its = createitems0 (cons), *fp, *sp;
item_t *its = NULL, *fp, *sp, *tmp;
knint w, p;
int cnt = 0; // real count of @its
knint *wp, *pp;
// put all elements of first table
for ( fp = first ; fp != NULL && *(fp->w) <= cons ; fp = fp->hh.next ) {
cnt++;
sp = copyitem (fp);
HASH_ADD_KEYPTR ( hh, its, sp->w, KNINT_SIZE, sp );
}
// put new elements of second table or replace elements having less value
for( fp = second ; fp!= NULL && *(fp->w) <= cons ; fp = fp->hh.next ) {
HASH_FIND (hh, its, fp->w, KNINT_SIZE, tmp);
if( tmp == NULL ){
cnt++;
tmp = copyitem (fp);
HASH_ADD_KEYPTR (hh, its, tmp->w, KNINT_SIZE, tmp);
} else {
*(tmp->p) = MAXINT ( *(fp->p) , *(tmp->p) );
}
}
// pairwise addition
for( fp = first ; fp != NULL ; fp = fp->hh.next ){
for( sp = second ; sp != NULL && (p = *(fp->p) + *(sp->p),w = *(fp->w) + *(sp->w), w<=cons) ; sp = sp->hh.next ){
HASH_FIND (hh, its, &w, KNINT_SIZE, tmp);
if( tmp == NULL ){
cnt++;
tmp = copyitem ( sp );
*(tmp->w) = w;
*(tmp->p) = p;
HASH_ADD_KEYPTR (hh, its, tmp->w, KNINT_SIZE, tmp);
} else {
*(tmp->p) = MAXINT( *(tmp->p),p );
}
}
}
// save non-zero elements in solid array
if( cnt == 0 ){
*rezsize = -1;
return NULL;
}
*rezsize = cnt;
return its;
}