-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
481 lines (458 loc) · 17.9 KB
/
main.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <ctime>
#include <chrono>
#include <thread>
#include <fstream>
using namespace std;
/*Funciones para imprimir...*/
void imprimirVectorInt(vector<int> F){
for (size_t i = 0; i < F.size(); i++){
cout << F.at(i);
}
cout<<endl;
}
//funcion que imprime sets
void imprimirSets(set<int> x){
set<int>::iterator ite;
for (ite = x.begin(); ite != x.end(); ite++) {
cout << *ite << " ";
}
cout << endl;
}
void imprimirVector(vector<set<int>> F){
set<int> element;
set<int>::iterator ite;
if (F.size() < 10){
for (size_t i = 0; i < F.size(); i++){
element.insert(F.at(i).begin(),F.at(i).end());
imprimirSets(element);
element.clear();
}
}
cout<< "Tamaño: "<<F.size()<<endl ;
}
/*Funcion que retorna el universo de elementos*/
set<int> getUniverse(vector<set<int>> F){
set<int> x(F[0]);
for(size_t i = 1; i<F.size(); i++){
x.insert(F[i].begin(),F[i].end());
}
return x;
}
/*Funcion de combinatoria (extraida de un foro) modificada para sets*/
vector<int> comb(int N, int K,set<int> x, vector<set<int>> F)
{
string bitmask(K, 1);
bitmask.resize(N, 0);
vector<int> A;
do {
set<int> s;
for (int i = 0; i < N; ++i)
{
if (bitmask[i]){
s.insert(F[i].begin(),F[i].end());
A.push_back(i);
if(x == s) return A;
}
}
s.clear();
A.clear();
} while (prev_permutation(bitmask.begin(), bitmask.end()));
return A;
}
/*Solucion 1*/
void exhaustiveSearch(set<int> x, vector<set<int>> F){
cout << "Comenzando solucion 1......" << endl;
int mscSize = F.size();
vector<int> solMsc;
string solMscPrint = "";
for(size_t j = F.size()-1; j != 0; j--){
vector<int> msc = comb(F.size(),j,x,F);
if(!msc.empty() && msc.size() <= mscSize){
solMsc = msc;
solMscPrint = "";
for(size_t i = 0; i != msc.size(); i++){
solMscPrint = solMscPrint + "S" + to_string(msc[i]+1) + " ";
}
mscSize = msc.size();
}
msc.clear();
}
cout << "MSC encontrado de tamaño: " << mscSize << endl;
//Print menor a 60 para no generar un flood en la consola
if(mscSize < 60){
cout << solMscPrint << endl;
}
cout << "Fin Solucion 1" << endl;
}
/*Solucion 2*/
/*Funcion que consigue elementos unicos (Optimizacion 1)*/
set<int> getIntersection(set<int> x, vector<set<int>> F){
set<int> inter;
set<int> setComp;
//Buscando los elementos que se encuentren en un solo conjunto
set_intersection(F[0].begin(),F[0].end(),F[1].begin(),F[1].end(), inserter(inter, inter.begin()));
set_symmetric_difference(F[0].begin(),F[0].end(),F[1].begin(),F[1].end(), inserter(setComp, setComp.begin()));
set<int> aux;
for (size_t i = 2; i < F.size(); i++){
aux.clear();
aux.insert(setComp.begin(),setComp.end());
setComp.clear();
set_intersection(aux.begin(),aux.end(),F[i].begin(),F[i].end(), inserter(inter, inter.begin()));
set_symmetric_difference(aux.begin(),aux.end(),F[i].begin(),F[i].end(), inserter(setComp, setComp.begin()));
}
setComp.clear();
set_difference(x.begin(),x.end(),inter.begin(),inter.end(), inserter(setComp, setComp.begin()));
return setComp;
}
void optimizedSearch(set<int> x, vector<set<int>> F){
cout << "Comenzando solucion 2......" << endl;
if(F.size() > 1){
set<int> setComp = getIntersection(x,F);
int mscSize = 0;
string solMscPrint = "";
//setComp : Tiene los elementos que solo estan en un conjunto
for(auto k = setComp.begin(); k!= setComp.end(); k++){
size_t c = 0;
//aca deberia buscar todos los sets que tengan el elemento que solo está en un solo conjunto
while(c != F.size() && F[c].find(*k) == F[c].end()) c++;
if(c != F.size()){
solMscPrint = solMscPrint + "S" + to_string(c+1) + " ";
set<int>::iterator iter;
set<int>::iterator del;
for(iter=F[c].begin(); iter!=F[c].end(); iter++){
x.erase(x.find(*iter));
for(size_t j=0;j < F.size(); j++){
if(j != c)((del = find(F[j].begin(), F[j].end(), *iter)) == F[j].end()) ? del : F[j].erase(del);
}
}
F[c].clear();
mscSize++;
}
}
if(x.size()>0){
for(size_t j = 1; j < F.size(); j++){
vector<int> msc = comb(F.size(),j,x,F);
//Si entra al if, es porque encontro un MSC
if (!msc.empty()){
cout << "MSC encontrado de tamaño: " << msc.size()+mscSize << endl;
//Print menor a 60 para no generar un flood en la consola
if(msc.size() + mscSize < 60){
for(size_t i = 0; i != msc.size(); i++){
solMscPrint = solMscPrint + "S" + to_string(msc[i]+1) + " ";
}
cout << solMscPrint << endl;
msc.clear();
}
break;
}
}
}else{
cout << "MSC encontrado de tamaño: " << mscSize << endl;
cout << solMscPrint << endl;
}
}
cout << "Fin Solucion 2" << endl;
}
/*Solucion 3 -> Algoritmo implementado del pseudocodigo*/
void greedAlgoritms(set<int> x, vector<set<int>> F){
cout << "Comenzando solucion 3......" << endl;
set<int> U = x;
vector<set<int>> C;
set<int>::iterator ite;
set<int> S;
int mayor = 0;
int countDiferencias = 0;
size_t i = 0;
while( U.size() > 0){
for(size_t j = 0; j < F.size(); j++){
countDiferencias = 0;
for (int r : F.at(j)) {
if (U.count(r)){
countDiferencias++;
}
}
if (countDiferencias > mayor){
S.clear();
S.insert(F[j].begin(),F[j].end());
mayor = countDiferencias;
}
}
i = 0;
mayor= 0;
for (ite = S.begin(); ite != S.end(); ite++) {
U.erase(*ite);
}
C.push_back(S);
}
imprimirVector(C);
cout << "Fin Solucion 3" << endl;
}
/*Solucion 4*/
set <int> unionDeConjuntos(vector<set<int>> F){
set <int> Union;
set <int> AC;
set <int> AC2;
AC.insert(F[0].begin(),F[0].end());
for (size_t i = 1;i < F.size(); i++){
set_union(AC.begin(),AC.end(),F[i].begin(),F[i].end(), inserter(Union, Union.begin()));
}
return Union;
}
// Función que realiza la mejora (1) y (2) del gready algorithms
void OptimizedGreedAlgoritmsV3(set<int> x, vector<set<int>> F, int k){
cout << "Comenzando solucion 4......" << endl;
set<int> U = x;
vector<set<int>> C;
set<int>::iterator ite;
set<int> S;
int mayor = 0;
int countDiferencias = 0;
size_t i=0;
if(F.size() > 1){
set<int> setComp = getIntersection(x, F);
while(setComp.size() > 0){
for(size_t j = 0; j < F.size(); j++){
for (int k : setComp){
if (F.at(j).count(k)){
if ( C.size() >0 && *find(C.begin(),C.end(), F.at(j)) == F.at(j)){
setComp.erase(k);
}else{
S.insert(F[j].begin(),F[j].end());
for (ite = S.begin(); ite != S.end(); ite++) {
U.erase(*ite);
}
C.push_back(S);
S.clear();
setComp.erase(k);
}
break;
}
}
}
}
}
while(k > F.size()){
k--;
}
while(U.size() > 0){
int aux = 0;
while(aux < k && U.size() > 0){
//Buscando los k conjuntos
set <int> inter;
int may = inter.size();
int idx;
for (size_t i = 0; i < F.size(); i++){
set_intersection(U.begin(),U.end(),F[i].begin(),F[i].end(), inserter(inter, inter.begin()));
if(may < inter.size()){
idx = i;
may = inter.size();
}
inter.clear();
}
S.insert(F[idx].begin(),F[idx].end());
for (ite = S.begin(); ite != S.end(); ite++) {
U.erase(*ite);
}
C.push_back(S);
S.clear();
inter.clear();
aux++;
}
}
imprimirVector(C);
cout << "Fin Solucion 4.2" << endl;
}
vector <set<int>> readFile(string file){
string line ="";
string sub;
ifstream myfile (file);
vector <set<int>> F;
if (myfile.is_open()){
while ( getline (myfile,line)){
int i = 0;
string text = "";
set <int> A;
while(i < line.length()){
text = "";
while(i < line.length() && (int)line[i] != 32 && (int)line[i] != 13){
text = text + line[i];
i++;
}
if(text.length() != 0){
A.insert(stoi(text));
}
i++;
}
F.push_back(A);
A.clear();
}
myfile.close();
}
return F;
}
vector <set<int>> randSets(){
vector <set<int>> efx;
srand(time(NULL));
int num=1+rand()%(100);
int cantidadSets = 1 + rand()%(100/2);
int probElemUnic = (cantidadSets * 0.05);
if(probElemUnic < 1){
probElemUnic = probElemUnic + 1;
}
int c = 0;
int elecSet = 0 + rand()%(cantidadSets);
int insernum = 1 + rand()%(num);
vector <int> elemunic;
for (int i = 0; i <= cantidadSets; i++){
set<int> rands = {};
int tamanoSets = 1 + rand()%(100/2);
for(int j = 0; j <= tamanoSets; j++){
int numra = 1 + rand()%(num);
if((c <= probElemUnic) && (elecSet == i) ){
for(size_t z = 0; z != efx.size(); z++){
if (efx.size() >0){
for(auto itr = efx[z].begin(); itr != efx[z].end(); itr++){
if(*itr == insernum){
efx[z].erase(insernum);
}
}}
}
rands.insert(insernum);
elemunic.push_back(insernum);
elecSet = i + rand()%(cantidadSets);
insernum = 1 + rand()%(num);
c = c + 1;
}
else if(numra != insernum){
if(elemunic.size() > 0 && !(find(elemunic.begin(), elemunic.end(), numra) != elemunic.end())){
rands.insert(numra);
}
}
}
if (rands.size()>0)
efx.push_back(rands);
rands.clear();
}
return efx;
}
int main(int argc, char **argv){
if(argc != 3){
cout << " Debe ejecutarse como ./tarea e k (e=0->Tiempo de Busqueda, e=1->Tamaño de solucion)" << endl;
return EXIT_FAILURE;
}
int ent = atoi(argv[1]);
int k = atoi(argv[2]);
if(ent != 0 && ent != 1 && k < 0){
cout << "0 = Tiempo de Busqueda, 1 = Tamaño de solucion, k debe ser mayor a 0" << endl;
return EXIT_FAILURE;
}else if(k < 0){
cout << "k debe ser mayor a 0";
return EXIT_FAILURE;
}
if(ent == 0){
cout << "Comenzando ejecución con conjuntos a partir de un archivo..." << endl;
cout << "-----------------------------------------------------------" << endl;
vector <set<int>> F = readFile("pmed1.txt");
set<int> X = getUniverse(F);
cout << "Tamaño del Universo: " << X.size() << endl;
cout << "Cantidad de conjuntos: "<< F.size() <<endl;
cout << "-----------------------------------------------------------" << endl;
auto start = chrono::high_resolution_clock::now();
exhaustiveSearch(X,F);
auto end = chrono::high_resolution_clock::now();
auto int_s = chrono::duration_cast<chrono::seconds>(end - start);
chrono::duration<double, milli> float_ms = end - start;
if(float_ms.count() > 5000) {
cout << "Tiempo de busqueda exhaustiva: " << float_ms.count()*0.001 << " segundos" << endl;
}else{
cout << "Tiempo de busqueda exhaustiva: " << float_ms.count() << " milisegundos" << endl;
}
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
optimizedSearch(X,F);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda exhaustiva optimizada: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
greedAlgoritms(X,F);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda algoritmo greedy clasico: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
OptimizedGreedAlgoritmsV3(X,F,k);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda algoritmo greedy optimizado: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
F = readFile("pmed38.txt");
X = getUniverse(F);
cout << "---Iniciando experimentacion greedy para más conjuntos..---" << endl;
cout << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
greedAlgoritms(X,F);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda algoritmo greedy clasico: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
OptimizedGreedAlgoritmsV3(X,F,k);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
if(float_ms.count() > 5000) {
cout << "Tiempo de busqueda algoritmo greedy optimizado: " << float_ms.count()*0.001 << " segundos";
}else{
cout << "Tiempo de busqueda algoritmo greedy optimizado: " << float_ms.count() << " milisegundos";
}
cout << endl << "-----------------------------------------------------------" << endl;
}else{
cout << "Comenzando ejecución con conjuntos aleatorios..." << endl;
cout << endl << "-----------------------------------------------------------" << endl;
vector <set<int>> F = randSets();
while (F.size() < 0){
vector <set<int>> F = randSets();
}
set<int> X = getUniverse(F);
cout << "Tamaño del Universo: " << X.size() << endl;
cout << "Cantidad de conjuntos: "<< F.size() <<endl;
cout << "-----------------------------------------------------------" << endl;
auto start = chrono::high_resolution_clock::now();
exhaustiveSearch(X,F);
auto end = chrono::high_resolution_clock::now();
auto int_s = chrono::duration_cast<std::chrono::seconds>(end - start);
chrono::duration<double, std::milli> float_ms = end - start;
if(float_ms.count() > 5000) {
cout << "Tiempo de busqueda exhaustiva: " << float_ms.count()*0.001 << " segundos" << endl;
}else{
cout << "Tiempo de busqueda exhaustiva: " << float_ms.count() << " milisegundos" << endl;
}
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
optimizedSearch(X,F);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda exhaustiva optimizada: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
greedAlgoritms(X,F);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
cout << "Tiempo de busqueda algoritmo greedy clasico: " << float_ms.count() << " milisegundos";
cout << endl << "-----------------------------------------------------------" << endl;
start = chrono::high_resolution_clock::now();
OptimizedGreedAlgoritmsV3(X,F,k);
end = chrono::high_resolution_clock::now();
float_ms = end - start;
if(float_ms.count() > 5000) {
cout << "Tiempo de busqueda algoritmo greedy optimizado: " << float_ms.count()*0.001 << " segundos" << endl;;
}else{
cout << "Tiempo de busqueda algoritmo greedy optimizado: " << float_ms.count() << " milisegundos" << endl;
}
}
}