-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgetto.java
More file actions
305 lines (274 loc) · 10.1 KB
/
Copy pathProgetto.java
File metadata and controls
305 lines (274 loc) · 10.1 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
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Progetto {
public static void main(String[] args) {
try {
int A = 1000;
double end;
double start;
int[] N = new int[130];
double a = (Math.log(500000) - Math.log(A)) / 129;
double Err = 0.001;
Scanner in = new Scanner(System.in);
int length = 1274;
int count = 181;
ArrayList<Integer> periodi = new ArrayList<Integer>();
File smart = new File("smart.txt");
File distribution = new File("distribuzione.txt");
File naive = new File("naive.txt");
FileWriter writerSmart = new FileWriter("smart.txt");
FileWriter writerDis = new FileWriter("distribuzione.txt");
FileWriter writerNaive = new FileWriter("naive.txt");
double[] num_periodi = new double[101];
double[] num_periodi_worst = new double[187];
int reps = 0;
System.out.print("Inserire 1 se si vuole usare il metodo di generazione 1, 2 se si vuole usare il secondo, 3 per il terzo e 4 per il caso peggiore: ");
int mode = in.nextInt();
System.out.print("\n" + "Inserire un numero per iniziare la stima dei tempi per il periodo frazionario smart: ");
writerSmart.write("Tempi smart: \n");
if(mode <=3) {
if (in.hasNextInt()) {
System.out.println("Avvio calcolo:" + "\n");
for (int j = 0; j <= 129; j++) {
double r = getResolution();
double Tmin = r * (1 / Err + 1);
N[j] = (int) (A * (Math.pow(Math.exp(a), j))); //A*(B^j)
start = System.currentTimeMillis();
int k = 0;
do {
StringBuilder s = generateString(N[j], mode);
if(N[j] == 1000) {
periodi.add(periodSmart(s));
} else {
periodSmart(s);
}
end = System.currentTimeMillis();
k++;
} while (end - start < Tmin);
double Tn = ((end - start) / k);
writerSmart.write(Tn + ", ");
System.out.print(Tn + ", ");
}
}
} else {
if (in.hasNextInt()) {
System.out.println("Avvio calcolo:" + "\n");
while(length < 500000) {
double r = getResolution();
double Tmin = r * (1 / Err + 1);
start = System.currentTimeMillis();
int k = 0;
do {
StringBuilder s = generateWorstCase(length);
if(length == 1274) {
periodi.add(periodSmart(s));
} else {
periodSmart(s);
}
end = System.currentTimeMillis();
k++;
} while (end - start < Tmin);
double Tn = ((end - start) / k);
writerSmart.write(Tn + ", ");
System.out.print(Tn + ", ");
count = count + 30;
length = length + 2*count;
}
}
}
length = 1274;
count = 181;
System.out.println("\n" + "Processo terminato");
writerDis.write("\nDistribuzione periodi: \n");
int temp;
double r = getResolution();
double Tmin = r * (1 / Err + 1);
start = System.currentTimeMillis();
int k = 0;
do {
StringBuilder s;
if(mode <= 3) {
s = generateString(100, mode);
temp = periodSmart(s);
num_periodi[temp]++;
} else {
s = generateWorstCase(186);
temp = periodSmart(s);
num_periodi_worst[temp]++;
}
end = System.currentTimeMillis();
k++;
} while (end - start < Tmin);
reps = k;
if(mode <= 3) {
for(int i=1; i<=100; i++) {
writerDis.write(num_periodi[i]/reps + ",\n");
}
} else {
for(int i=1; i<=186; i++) {
writerDis.write(num_periodi_worst[i]/reps + ",\n");
}
}
Scanner scan1 = new Scanner(System.in);
writerNaive.write("\nTempi naive: \n");
System.out.println("Inserire un numero per iniziare la stima dei tempi per il periodo frazionario naive: ");
if(mode <=3) {
if (scan1.hasNextInt()) {
System.out.println("Avvio calcolo:" + "\n");
for (int j = 0; j <= 129; j++) {
r = getResolution();
Tmin = r * (1 / Err + 1);
N[j] = (int) (A * (Math.pow(Math.exp(a), j))); //A*(B^j)
start = System.currentTimeMillis();
k = 0;
do {
StringBuilder s = generateString(N[j], mode);
if(N[j] == 1000) {
periodi.add(periodNaive(s));
} else {
periodNaive(s);
}
end = System.currentTimeMillis();
k++;
} while (end - start < Tmin);
double Tn = ((end - start) / k);
writerNaive.write(Tn + ",");
System.out.print(Tn + ", ");
}
}
} else {
if (in.hasNextInt()) {
System.out.println("Avvio calcolo:" + "\n");
while(length < 500000) {
r = getResolution();
Tmin = r * (1 / Err + 1);
start = System.currentTimeMillis();
k = 0;
do {
StringBuilder s = generateWorstCase(length);
if(length == 1274) {
periodi.add(periodNaive(s));
} else {
periodNaive(s);
}
end = System.currentTimeMillis();
k++;
} while (end - start < Tmin);
double Tn = ((end - start) / k);
writerNaive.write(Tn + ",");
System.out.print(Tn + ", ");
count = count + 30;
length = length + 2*count;
}
}
}
System.out.println("\n" + "Processo terminato");
writerSmart.close();
writerNaive.close();
writerDis.close();
} catch(IOException e) {
System.out.println("Errore nella creazione/scrittura del file: \n");
e.printStackTrace();
}
}
public static StringBuilder generateString(int N, int mode) {
StringBuilder s = new StringBuilder(N);
switch (mode) {
case 1:
for (int i = 0; i < N; i++) {
if (Math.random() <= 0.5) {
s.append('a');
} else {
s.append('b');
}
}
break;
case 2:
Random rand = new Random();
int q = rand.nextInt(N - 1) + 1;
for (int i = 0; i < q; i++) {
if (Math.random() <= 0.5) {
s.append('a');
} else {
s.append('b');
}
}
for (int i = q; i < N; i++) {
s.append(s.charAt(i % q));
}
break;
case 3:
Random rand2 = new Random();
int p = rand2.nextInt(N - 1) + 1;
for (int i = 0; i < p; i++) {
if (Math.random() <= 0.5) {
s.append('a');
} else {
s.append('b');
}
}
s.append('c');
for (int i = p + 1; i < N; i++) {
s.append(s.charAt(i %( p+1)));
}
break;
case 4:
}
return s;
}
public static StringBuilder generateWorstCase(int length) {
int i = 1;
int current = 0;
StringBuilder s = new StringBuilder(length);
while(current < length) {
for(int j = 0; j<i; j++) {
s.append('a');
}
for(int j = 0; j<i; j++) {
s.append('b');
}
current += 2*i;
i = i+30;
}
return s;
}
public static double getResolution() {
double end;
double start = System.currentTimeMillis();
do {
end = System.currentTimeMillis();
} while (start == end);
return (end - start);
}
public static int periodNaive(StringBuilder s) {
int n = s.length();
for (int i = 1; i < n; i++) { //costo: O(n)
String s1 = s.substring(0, n - i);
String s2 = s.substring(i, n);
if (s1.equals(s2)) { //costo: O(n)
return i;
}
} //costo tot: O(n^2)
return n;
}
public static int periodSmart(StringBuilder s) { //costo tot: O(n)
int n = s.length();
int[] r = new int[n];
r[0] = 0;
for (int i = 1; i < n; i++) {
int z = r[i - 1];
while ((s.charAt(i) != s.charAt(z)) && z > 0) {
z = r[z - 1];
}
if (s.charAt(i) == s.charAt(z)) {
r[i] = z + 1;
} else {
r[i] = 0;
}
}
return n - r[n - 1]; //r[n-1]=bordo max
}