-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExo1.cpp
executable file
·480 lines (383 loc) · 8.54 KB
/
Exo1.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Data
{
private:
string data;
public:
Data()
{
}
Data(char * input)
{
data ="";
ifstream fichier(input, ios::in); // on ouvre le fichier en lecture
if(fichier) // si l'ouverture a réussi
{
// instructions
string ligne;
while(getline(fichier, ligne)) // tant que l'on peut mettre la ligne dans "contenu"
{
data += ligne;
}
fichier.close(); // on ferme le fichier
}
else // sinon
cout << "Impossible d'ouvrir le fichier !" << endl;
}
string toString()
{
// cout << &data ;
return data;
}
inline bool operator== (const Data &D2) const
{
//cout << "Je compare derp derp" << endl;
//usleep(200);
for (int i = 0; i < 1000000; ++i)
{
}
//cout << "Data source : " << data << " == " << D2.data << endl;
return (data == D2.data);
}
inline bool operator!= (const Data &D2) const
{
//sleep(500);
return (data != D2.data);
}
};
typedef Data* Element;
vector<Element> vector_element;
vector<Data> my_vector;
int N =100;
struct Resultat
{
bool b;
Element e;
int n;
};
int occ(Element e,int i,int j)
{
int result = 0;
for(int k = i;k<j;k++)
{
if (e != NULL && *vector_element[k] == *e)
{
result++;
}
}
return result;
}
void initSame()
{
for(int i =0; i<N ;i++)
{
int valeur = rand() % my_vector.size() ;
vector_element.push_back(&my_vector[1]);
}
}
void initMaj()
{
for(int i =0; i<N/2 ;i++)
{
int valeur = rand() % my_vector.size() ;
vector_element.push_back(&my_vector[valeur]);
}
for(int i =N/2; i<N ;i++)
{
int valeur = rand() % my_vector.size() ;
vector_element.push_back(&my_vector[1]);
}
}
void initRand()
{
for(int i =0; i<N ;i++)
{
int valeur = rand() % my_vector.size() ;
vector_element.push_back(&my_vector[valeur]);
}
}
struct Resultat naiveMaj()
{
struct Resultat result;
result.b = false;
result.e = NULL;
for (int i = 0; i < N/2; ++i)
{
if (occ(vector_element[i],i,N)>N/2)
{
//cout << "We got a winner here " << vector_element[i]->toString() << endl;
result.e = vector_element[i];
result.b = true;
return result;
}
}
}
struct Resultat RecurrenceMaj(int i, int j)
{
struct Resultat result;
result.b = false;
result.e = NULL;
if(i == j)
{
result.b = true ;
result.e = vector_element[i];
return result;
}
struct Resultat gauche = RecurrenceMaj(i,(j+i)/2);
struct Resultat droite = RecurrenceMaj((j+i+1)/2,j);
if (!gauche.b && !droite.b)
{
return result;
}
if (gauche.b && droite.b)
{
if(droite.e != NULL && gauche.e != NULL && *gauche.e == *droite.e)
{
result.b = true;
result.e = droite.e;
return result;
}
int occD = occ(droite.e,i,j);
int occG = occ(gauche.e,i,j);
if (occG > (j-i+1)/2)
{
result.e = gauche.e;
result.b = true;
return result;
}
if (occD > (j-i+1)/2)
{
result.e = droite.e;
result.b = true;
return result;
}
}
if (gauche.b && !droite.b)
{
int occG = occ(gauche.e,i,j);
if (occG > (j-i+1)/2)
{
result.e = gauche.e;
result.b = true;
return result;
}
}
if (!gauche.b && droite.b)
{
int occD = occ(droite.e,i,j);
if (occD > (j-i+1)/2)
{
result.e = droite.e;
result.b = true;
return result;
}
}
}
struct Resultat PseudoMajoritaire(int i, int j)
{
struct Resultat result;
result.b = false;
result.e = NULL;
result.n = 0;
if(i == j)
{
result.b = true ;
result.e = vector_element[i];
result.n = 1;
return result;
}
struct Resultat gauche = RecurrenceMaj(i,(j+i)/2);
struct Resultat droite = RecurrenceMaj((j+i+1)/2,j);
int tmpValue = j-i+1;
if (!gauche.b && !droite.b)
{
return result;
}
if (gauche.b && droite.b)
{
if (droite.e != NULL && gauche.e != NULL && *gauche.e == *droite.e)
{
result.b = true;
result.e = gauche.e;
result.n = gauche.n + droite.n;
return result;
}
if (gauche.n == droite.n)
{
return result;
}
if (gauche.n > droite.n)
{
result.b = true;
result.e = gauche.e;
result.n = tmpValue/2 + gauche.n - droite.n;
return result;
}
else
{
result.b = true;
result.e = droite.e;
result.n = tmpValue/2 + droite.n - gauche.n;
return result;
}
}
if (gauche.b && !droite.b)
{
result.e = gauche.e;
result.b = true;
result.n = gauche.n + tmpValue;
return result;
}
if (!gauche.b && droite.b)
{
result.e = droite.e;
result.b = true;
result.n = droite.n + tmpValue;
return result;
}
}
struct Resultat PseudoMajoritaireLaunch(int i, int j)
{
struct Resultat result = PseudoMajoritaire(i,j);
if(result.b)
{
if (occ(result.e,i,j)>j/2)
{
return result;
}
else
{
result.b = false;
result.e = NULL;
return result;
}
}
}
void launchTests()
{
struct Resultat result;
vector_element.clear();
initMaj();
cout << "START MAJORITAIRE END" << endl;
clock_t startTime = clock();
// some code here
result = naiveMaj();
// to compute its execution duration in runtime
cout << "naiveMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = RecurrenceMaj(0,N);
// to compute its execution duration in runtime
cout << "RecurrenceMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = PseudoMajoritaireLaunch(0,N);
// to compute its execution duration in runtime
cout << "PseudoMajoritaireLaunch = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
if(result.b)
{
cout << "MAJ = " << result.e->toString() << endl;
}
vector_element.clear();
initRand();
cout << "START RANDOM" << endl;
startTime = clock();
// some code here
result = naiveMaj();
// to compute its execution duration in runtime
cout << "naiveMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = RecurrenceMaj(0,N);
// to compute its execution duration in runtime
cout << "RecurrenceMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = PseudoMajoritaireLaunch(0,N);
// to compute its execution duration in runtime
cout << "PseudoMajoritaireLaunch = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
if(result.b)
{
cout << "MAJ = " << result.e->toString() << endl;
}
vector_element.clear();
initSame();
cout << "START SAME" << endl;
startTime = clock();
// some code here
result = naiveMaj();
// to compute its execution duration in runtime
cout << "naiveMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = RecurrenceMaj(0,N);
// to compute its execution duration in runtime
cout << "RecurrenceMaj = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
startTime = clock();
// some code here
result = PseudoMajoritaireLaunch(0,N);
// to compute its execution duration in runtime
cout << "PseudoMajoritaireLaunch = " << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
if(result.b)
{
cout << "MAJ = " << result.e->toString() << endl;
}
}
int main(int argc, char * argv[])
{
//
// INIT
// Change N to increase the vector size
srand (time(NULL));
Data d1("DATA1");
Data d2("DATA2");
Data d3("void.txt");
Data d4("DATA4");
Data d5("DATA3");
Data d6("DATA6");
Data d7("DATA7");
Data d8("DATA8");
Data d9("DATA9");
my_vector.push_back(d1);
my_vector.push_back(d2);
my_vector.push_back(d3);
my_vector.push_back(d4);
my_vector.push_back(d5);
my_vector.push_back(d6);
my_vector.push_back(d7);
my_vector.push_back(d8);
my_vector.push_back(d9);
int N = 100;
launchTests();
my_vector.clear();
my_vector.push_back(d1);
my_vector.push_back(d2);
my_vector.push_back(d3);
my_vector.push_back(d4);
my_vector.push_back(d6);
my_vector.push_back(d7);
my_vector.push_back(d8);
my_vector.push_back(d9);
N = 1000;
launchTests();
my_vector.clear();
my_vector.push_back(d1);
my_vector.push_back(d2);
my_vector.push_back(d8);
my_vector.push_back(d9);
N = 100;
launchTests();
my_vector.clear();
my_vector.push_back(d1);
my_vector.push_back(d2);
my_vector.push_back(d3);
my_vector.push_back(d9);
N = 1000;
launchTests();
}