-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADSGen.java
More file actions
290 lines (242 loc) · 7.89 KB
/
ADSGen.java
File metadata and controls
290 lines (242 loc) · 7.89 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
import java.util.*;
public class ADSGen {
// fields
protected int dimension; // dimension of the space in concern
protected int[] dim; // array holding info for R_i in index i
protected int v; // v - |G|
protected int t; // number of elements to be covered for lambda times
protected int lambda; // t elements covered LAMBDA times
protected int k; // ADS order
private final int A = (int)'A';
// keep track of times each elem is covered
protected Map<String, Integer> counter;
protected List<String> tElements;
/* constructor
public ADSGen() {
//this.readInput();
this.initCntr();
for (String s: counter.keySet()) {
System.out.println(s + ", " + counter.get(s));
}
// elements that are covered lambda times
tElements = new ArrayList<>();
}
*/
/* another constructor
*/
public ADSGen(int v, int k, int lambda, int t) {
this.v = v;
this.k = k;
this.lambda = lambda;
this.t = t;
dim = new int[1];
dim[0] = 32;
dimension = 1;
this.initCntr();
/*
for (String s: counter.keySet()) {
System.out.println(s + ", " + counter.get(s));
}
*/
// elements that are covered lambda times
tElements = new ArrayList<>();
}
public void dummyPrint() {
System.out.println(2);
}
private void initCntr() {
counter = new LinkedHashMap<>();
Queue<String> q1 = new LinkedList<>();
Queue<String> q2 = new LinkedList<>();
q1.add("");
for (int d = 0; d < dimension; d++) {
while (!q1.isEmpty()) {
String s = q1.poll();
// for each str, attach next dim
for (int i = 0; i < dim[d]; i++) {
q2.add(new String(s + (char)((int)'A' + i)));
}
}
q1 = q2;
q2 = new LinkedList<>();
}
while (!q1.isEmpty()) {
counter.put(q1.poll(), 0);
}
}
/* readInput from user, error checking before initializing all fields
* returns an array containing [dimension, dim[0]..[n - 1], v, k, lambda,
* t]
*/
private void readInput() throws IllegalArgumentException {
Scanner in = new Scanner(System.in);
System.out.println("input format:");
System.out.println("dimension\n" +
"n1 n2 n3 ... n_dimension\n" +
"k");
dimension = in.nextInt();
if (dimension < 1) {
throw new IllegalArgumentException("dimension should be positive");
}
dim = new int[dimension];
v = 1;
for (int i = 0; i < dim.length; i++) {
// read in n in Z_n, and store n in each index
dim[i] = in.nextInt();
if (dim[i] < 1) {
throw new IllegalArgumentException("n should be positive");
}
v *= dim[i];
}
k = in.nextInt();
if (k > v) {
throw new IllegalArgumentException("k should be smaller than v");
}
lambda = findLambda(v, k);
t = k * (k - 1) - (lambda - 1) * (v - 1);
// print out all inputs
for (int i = 0; i < dim.length - 1; i++) {
System.out.print("Z_" + dim[i] + " * ");
}
System.out.println("Z_" + dim[dim.length - 1]);
System.out.printf("ads: (%s, %s, %s, %s)\n", v, k, lambda, t);
}
/* given v and k, find corresponding lambda
*/
private int findLambda(int v, int k) {
int l = 0;
while ((v - 1) * l < k * (k - 1)) {
l++;
}
return l;
}
/* print out all ADS
*/
public void printADS(Set<List<String>> ads) {
for (List<String> set : ads) {
System.out.print("{ ");
for (String str: set) {
System.out.print("( ");
for (char c : str.toCharArray()) {
System.out.print((int)(c - 'A') + " ");
}
System.out.print(") ");
}
System.out.println("}");
}
}
/* generate all possible sets of order ADSOrder
*/
public Set<List<String>> getADSCandidates() {
List<String> elemOfG = new ArrayList<>(this.counter.keySet());
List<String> currSet = new ArrayList<>();
Set<List<String>> allSets = new LinkedHashSet<>();
int firstElemInd = 0;
// fix 0 in the set
currSet.add(elemOfG.get(firstElemInd));
firstElemInd++;
getNext(firstElemInd, k - 1, elemOfG, currSet, allSets, 0);
System.out.println(allSets.size());
return allSets;
}
/* recursive method to help generate next ADS candidate
*/
private void getNext(int currInd, int numLeft, List<String> elemOfG,
List<String> currSet, Set<List<String>> allSets,
int numLambdaCovered) {
/*System.out.println("currSet: ");
for (String s : currSet) {
System.out.print(s + " ");
}
System.out.println("numLeft " + numLeft);
System.out.println();
*/
if (numLeft <= 0) {
allSets.add(new ArrayList<>(currSet));
// put the t elements into record
for (String diff : counter.keySet()) {
if (counter.get(diff) == lambda) {
tElements.add(diff);
return;
}
}
} else if (elemOfG.size() - currInd >= numLeft) {
int i = currInd;
boolean stop = false;
//while (elemOfG.size() - i >= numLeft) {
// the new differences caused by adding current element
List<String> differences = new ArrayList<>();
for (String prevElem: currSet) {
String newDiff1 = new String();
String newDiff2 = new String();
for (int d = 0; d < dimension; d++) {
char diff1Char = (char)(((int)(prevElem.charAt(d) - elemOfG.get(i).charAt(d)) +
dim[d]) % dim[d] + A);
char diff2Char = (char)(((int)(elemOfG.get(i).charAt(d) - prevElem.charAt(d)) +
dim[d]) % dim[d] + A);
newDiff1 += diff1Char;
newDiff2 += diff2Char;
}
differences.add(newDiff1);
differences.add(newDiff2);
//System.out.printf("diff btw %s and %s : %s, %s\n",
//prevElem, elemOfG.get(i), newDiff1, newDiff2);
int count1 = counter.put(newDiff1, counter.get(newDiff1) + 1) + 1;
int count2 = counter.put(newDiff2, counter.get(newDiff2) + 1) + 1;
/*
System.out.printf("count for %s is %s\n", newDiff1, count1);
System.out.printf("count for %s is %s\n", newDiff2, count2);
*/
if (count1 == lambda) {
//System.out.println(newDiff1 + " reached lambda");
numLambdaCovered++;
}
if (!newDiff1.equals(newDiff2) && count2 == lambda) {
numLambdaCovered++;
}
if (count1 > lambda || count2 > lambda || numLambdaCovered > this.t) {
stop = true;
break;
}
}
if (!stop) {
currSet.add(elemOfG.get(i));
for (int m = i + 1; m <= v - (numLeft - 1); m++) {
getNext(m, numLeft - 1, elemOfG, currSet, allSets, numLambdaCovered);
if (allSets.size() > 0)
return;
}
currSet.remove(elemOfG.get(i));
}
for (String diff: differences) {
counter.put(diff, counter.get(diff) - 1);
}
//i++;
//}
}
}
/* generate all possible sets of order ADSOrder
public Set<List<int[]>> getADSCandidates(int[][] allGroupElem) {
List<int[]> currSet = new ArrayList<>();
int firstElemInd = 0;
currSet.add(allGroupElem[firstElemInd]);
firstElemInd++;
Set<List<int[]>> allSets = new LinkedHashSet<>();
getNext(firstElemInd, ADSOrder - 1, allGroupElem, currSet, allSets);
//System.out.println(allSets.size());
return allSets;
}
/* go through the candidates, and return a set of candidates that are
* actually ADS
public Set<List<int[]>> getADS(Set<List<int[]>> candidates,
int[][] allGroupElements) {
Set<List<int[]>> adsSet = new LinkedHashSet<>();
for (List<int[]> candidate : candidates) {
if (this.isADS(candidate, allGroupElements)) {
adsSet.add(candidate);
}
}
return adsSet;
}
*/
}