-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG3.cpp
More file actions
352 lines (333 loc) · 8.91 KB
/
G3.cpp
File metadata and controls
352 lines (333 loc) · 8.91 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
struct BNode{
std::string val;
BNode* left;
BNode* right;
};
BNode* bt_node(std::string e,BNode* l,BNode* r){
BNode* tmp = new BNode;
tmp -> val = e;
tmp -> left = l;
tmp -> right = r;
return tmp;
}
//finds next combination
void next_order(std::vector<int>& tmp,int size,int i){
int y = 0,factor=tgamma(size);
if(size == 0){
return;
}
for(int j=0;j<=(i/factor)+y;j++){
if(tmp[j]!=-1){
y++;
}
}
tmp[(i/factor)+y]=size-1;
i= i%factor;
size--;
next_order(tmp,size,i);
}
//finds every combination of numbers inputed
std::vector<std::vector<int>> all_possible_orders(int size){
std::vector<std::vector<int>> out;
std::vector<int> tmp,neg = {};
for(int i = 0;i<size;i++){
neg.push_back(-1);
}
for(int i =0;i<tgamma(size+1);i++){
tmp = neg;
next_order(tmp,size,i);
out.push_back(tmp);
}
return out;
}
std::vector<std::string> in_order(std::vector<std::string> r,std::vector<int> order){
std::vector<std::string> out=r;
for(int i=0;i<r.size();i++){
for(int j=0;j<r[0].size();j++){
out[i][j]=r[i][order[j]];
}
}
return out;
}
// predicts the number of nodes if the tree is built
int predicted_number_of_nodes(std::vector<std::string> r){
std::vector<std::string> same_branch, different_branch;
int y=1;
if(r[0].size()==0){
return 1;
}
if(r[0][0]=='x'){
for(int i=0;i<r.size();i++){
r[i] = r[i].substr(1);
}
return predicted_number_of_nodes(r);
}
while(y < r.size()){
if(r[0][0]==r[y][0]){
same_branch.push_back(r[y].substr(1));
}else{
different_branch.push_back(r[y].substr(1));
}
y++;
}
same_branch.push_back(r[0].substr(1));
if(different_branch.size()==0){
return 2 + predicted_number_of_nodes(same_branch);
}
return 1 + predicted_number_of_nodes(same_branch) + predicted_number_of_nodes(different_branch);
}
//reduces the inputs into rows with dont cares
std::vector<std::string> shorthand(std::vector<std::string> r){
std::vector<std::string> tmp;
for(int y=r[0].size()-1; y>=0;y--){
int i=0;
while(i<r.size()){
int j = i+1,check = 1;
while((j < r.size())&&(check!=-1)){
int x =0;
check=-1;
while((x != r[0].size())&&(check == -1)){
if((r[i][x]!=r[j][x])&&(x!=y)){
check = 0;
}
x++;
}
if(check == -1){
r[i][y]='x';
r[j]="";
}
j++;
if(r[j]==""){
j++;
}
}
if(r[i]!=""){
tmp.push_back(r[i]);
}
i++;
if(r[i]==""){
i++;
}
}
r = tmp;
tmp.clear();
}
return r;
}
//Bases the order of the varible the least dont cares to the most
std::vector<int> order(std::vector<std::string> in){
std::vector <int> rank;
std::vector<int> out;
for(int i=0;i<in[0].size();i++){
int counter = 0;
for(int j=0;j<in.size();j++){
if(in[j][i]=='x'){
counter++;
}
}
rank.push_back(counter);
}
for(int a = 0; a < in[0].size();a++){
int min = -1, track;
for(int k=0;k<rank.size();k++){
if(((rank[k] < min)&&(rank[k]!=-1))||(min==-1)){
min = rank[k];
track = k;
}
}
rank[track]=-1;
out.push_back(track);
}
out.push_back(-1);
return out;
}
//Checks if an order and compressed input is valid
bool checker(std::vector<std::string> r,std::vector <int> orders){
int s=0,che = 0;
while((s<r.size())&&(che==0)){
int x=s+1;
while((x<r.size())&&(che==0)){
int y =0,same = 0;
while((y<r[0].size())&&(same==0)){
same=1;
if(r[s][orders[y]]==r[x][orders[y]]){
same=0;
}
else if((r[s][orders[y]]=='x')||(r[x][orders[y]]=='x')){
che++;
}
y++;
}
x++;
}
s++;
}
if(che == 1){
return 0;
}
return 1;
}
//Finds the problem and expands it to fix it
std::vector<std::string> replace(std::vector<std::string> r,std::vector <int> orders){
std::string tmp;
int saves,savey;
int s=0,che = 0;
while((s<r.size())&&(che==0)){
int x=s+1;
while((x<r.size())&&(che==0)){
int y =0,same = 0;
while((y<r[0].size())&&(same==0)){
same=1;
if(r[s][orders[y]]==r[x][orders[y]]){
same=0;
}
else if(r[s][orders[y]]=='x'){
che++;
saves =s;
savey =y;
}else if(r[x][orders[y]]=='x'){
che++;
saves =x;
savey =y;
}
y++;
}
x++;
}
s++;
}
if(che == 1){
tmp = r[saves];
tmp[orders[savey]]='1';
r[saves][orders[savey]]='0';
r.push_back(tmp);
return replace(r,order(r));
}
return r;
}
//Works through all orders and finds the valid one with the least nodes
std::vector<int> min_order(std::vector<std::string> r){
std::vector<std::vector<int>> pos_order = all_possible_orders(r[0].size());
int size = -1,tmp;
std::vector<int> min;
for(int i=0;i<pos_order.size();i++){
if(checker(r,pos_order[i])){
tmp = predicted_number_of_nodes(in_order(r,pos_order[i]));
if((tmp<size)||(size==-1)){
size=tmp;
min = pos_order[i];
}
}
}
min.push_back(-1);
return min;
}
//builds a branch of the exsisting tree
BNode* hit_0(std::string const direction,std::vector<int> const ord,int counter){
std::string name;
char x01 = direction[ord[counter]];
name = "x" + std::to_string(ord[counter] + 1);
if(x01 == 'x'){
return hit_0(direction,ord,counter+1);
}
if(ord[counter]==-1){
return bt_node("1",NULL,NULL);
}
if(x01 == '1'){
return bt_node(name,bt_node("0",NULL,NULL),hit_0(direction,ord,counter+1));
}
return bt_node(name,hit_0(direction,ord,counter+1),bt_node("0",NULL,NULL));
}
//finds the zero on the path to place the one
BNode* Build_tree(std::string const direction,std::vector<int> const ord,BNode* t,int counter){
char x01 = direction[ord[counter]];
if(t -> val != "0"){
if(x01=='0'){
t -> left = Build_tree(direction,ord,t->left,counter+1);
return t;
}
if(x01=='1'){
t -> right = Build_tree(direction,ord,t->right,counter+1);
return t;
}
return Build_tree(direction,ord,t,counter+1);
}
return hit_0(direction,ord,counter);
}
BNode* build_bt(const std::vector<std::string>& fvalues){
BNode* t;
t = bt_node("0",NULL,NULL);
if (fvalues.size() > 0){
std::vector<std::string> r = shorthand(fvalues);
std::vector<int> the_order = min_order(r);
if(the_order[0]==-1){
do{
the_order = order(r);
r=replace(r,the_order);
}while(checker(r,the_order)==0);
}
for(int i=0;i<r.size();i++){
t=Build_tree(r[i],the_order,t,0);
}
}
return t;
}
int label_to_idx(const std::string& label){
std::string out;
for(int i = 1; i < label.size(); i++){
out.push_back(label[i]);
}
return std::stoi(out) - 1;
}
void destroy_tree(BNode* T){
if(T != NULL){
destroy_tree(T->left);
destroy_tree(T->right);
delete T;
}
}
std::string eval_bt(BNode* bt, const std::string& input){
if( (bt->left == NULL) && (bt->right == NULL) ){
return bt->val;
}
else{
int idx = label_to_idx(bt->val);
std::string input_idx;
input_idx.push_back(input[idx]);
if(input_idx == "0"){
return eval_bt(bt->left, input);
}
else{
return eval_bt(bt->right, input);
}
}
}
int n_nodes_bt(BNode* t){
if(t == NULL){
return 0;
}
else{
return 1 + n_nodes_bt(t->left) + n_nodes_bt(t->right);
}
}
class BoolTree{
public:
BoolTree(const std::vector<std::string>& fvalues){
t = build_bt(fvalues);
}
std::string eval(const std::string& s){
return eval_bt(t, s);
}
int n_nodes(){
return n_nodes_bt(t);
}
~BoolTree(){
destroy_tree(t);
}
private:
BNode* t;
};