-
Notifications
You must be signed in to change notification settings - Fork 1
/
Enumerate.java
324 lines (263 loc) · 7.73 KB
/
Enumerate.java
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
package rsn170330.lp4;
import java.util.Comparator;
/**
* CS 5V81.001: Implementation of Data Structures and Algorithms
* Long Project LP4: PERT, Enumeration of topological orders
*
* Team: LP101
* @author Rahul Nalawade (rsn170330)
* @author Prateek Sarna (pxs180012)
* @author Bhavish Khanna Narayanan (bxn170002)
*/
// Permutations and Combinations of distinct items
public class Enumerate<T> {
T[] arr; // array of elements
int k; // size of permutation
// NOTE: permutation is in arr[0..k-1]
long count; // how many permutations/ enumerations we're visiting
Approver<T> app;
// ----------------------- Constructors ----------------------------------
// reference to the array, k, and caller's own approver
public Enumerate(T[] arr, int k, Approver<T> app) {
this.arr = arr;
this.k = k;
this.count = 0;
this.app = app;
}
// when k = n, taking all permutations of arr
public Enumerate(T[] arr, Approver<T> app) {
this(arr, arr.length, app);
}
// no approver (yes to everything)
public Enumerate(T[] arr, int k) {
this(arr, k, new Approver<T>());
}
public Enumerate(T[] arr) {
this(arr, arr.length, new Approver<T>());
}
// -------------- Methods of Enumerate class: To do ----------------------
/**
* Chooses c more elements from arr[k-c...n-1] elements, n = arr.length
*
* Precondition: arr[0...k-c-1] have been selected
*
* @param c number of elements needed to be chosen
*/
public void permute(int c) {
if (c == 0) {
visit(arr); // visit permutation in arr[0...k-1]
}
else {
int d = k - c; // key index where selected element is placed
for (int i = d; i < arr.length; ++i) {
if (app.select(arr[i])) {
// swap arr[d] with arr[i]
T temp = arr[d];
arr[d] = arr[i];
arr[i] = temp;
// Permutations having arr[i] as the next element
permute(c - 1);
// Restore elements where they were before swap
arr[i] = arr[d];
arr[d] = temp;
app.unselect(arr[i]);
}
}
}
}
/**
* Choose c more elements from arr[i...n-1]
* Precondition: arr[0...k-c-1] are already chosen
*
* @param i the left index of the right sub-array arr[i...n-1]
* @param c the number of elements needed to be chosen
*/
public void combine(int i, int c) {
if (c == 0) {
visit(arr); // visit combination in arr[0...k-1]
}
else {
// swap arr[d] with arr[i], where d = k-c
T temp = arr[k - c];
arr[k - c] = arr[i];
arr[i] = temp;
combine(i + 1, c - 1);
// Restore elements where they were before swapping
arr[i] = arr[k - c];
arr[k - c] = temp;
// When there are enough elements remaining
if (arr.length - i > c) {
combine(i + 1, c); // skip arr[i]
}
}
}
/**
* Generate all n! permutations with just a swap from previous permutation
* Precondition: arr[g...n-1] are frozen/ done.
*
* @param g number of elements to go i.e.
* elements in arr[0...g-1] can only be changed
*/
public void heap(int g) {
if (g == 1) {
visit(arr); // visit permutation in arr[0...n-1]
}
else {
for (int i = 0; i < g - 1; ++i) {
heap(g - 1);
if (g % 2 == 0) {
swap(i, g - 1);
} else {
swap(0, g - 1);
}
}
heap(g - 1);
}
}
/**
* Generates all n! permutations in lexicographic order, even when
* elements are not distinct.
*
* Precondition: array A is sorted in natural order, i.e.
* A[0] <= A[1] <= ... <= A[n-1]
*
* @param c comparator
*/
public void algorithmL(Comparator<T> c) {
int j, k;
visit(arr);
j = findJ(c);
// NOTE: no need to have a decreasing array checker method :)
while (j > -1) {
k = findK(c, j);
swap(j, k);
reverse(j+1, arr.length - 1);
// now A[j+1...n-1] is in ascending order
visit(arr);
j = findJ(c);
}
}
/**
* algorithmL() helper method: finds max index j such that A[j] < A[j+1]
* @param c comparator of the generic type used
* @return the j index for the existing array
*/
private int findJ(Comparator<T> c) {
int j = arr.length - 2;
while (j >= 0 && c.compare(arr[j], arr[j+1]) >= 0) {
j--;
}
return j;
}
/**
* algorithmL() helper method: finds max index k such that A[j] < A[k]
* @param c comparator of the generic type used
* @param j existing j index
* @return the k index for j index for the existing array
*/
private int findK(Comparator<T> c, int j) {
int k = arr.length - 1;
while (j < k && c.compare(arr[j], arr[k]) >= 0) {
k--;
}
return k;
}
/**
* Main visit() method, that stores count in Enumerate class itself
* @param array that is passed to the relevant Approver's visit()
*/
public void visit(T[] array) {
// increments count of this class
count++; // may not need to store count for other Enumeration classes
app.visit(array, k); // and call appropriate visit
}
// -------------------- Nested class: Approver ---------------------------
// Class to decide whether to extend a permutation with a selected item
// Extend this class in algorithms that need to enumerate permutations
// with precedence constraints
public static class Approver<T> {
/* Extend permutation by item? */
public boolean select(T item) {
return true;
}
/* Backtrack selected item */
public void unselect(T item) {
}
/* Visit a permutation or combination */
public void visit(T[] array, int k) {
for (int i = 0; i < k; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
// ---------------------------- UTILITIES --------------------------------
/* Swaps an element at i with element at j */
void swap(int i, int j) {
T tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/* Elements from index low to high are reversed */
void reverse(int low, int high) {
while (low < high) {
swap(low, high);
low++;
high--;
}
}
// -------------------------- STATIC METHODS -----------------------------
// Enumerate permutations of k items out of n = arr.length
public static <T> Enumerate<T> permute(T[] arr, int k) {
Enumerate<T> e = new Enumerate<>(arr, k);
e.permute(k);
return e;
}
// Enumerate combinations of k items out of n = arr.length
public static <T> Enumerate<T> combine(T[] arr, int k) {
Enumerate<T> e = new Enumerate<>(arr, k);
e.combine(0, k);
return e;
}
// Enumerate permutations of n = arr.length item, using Heap's algorithm
public static <T> Enumerate<T> heap(T[] arr) {
Enumerate<T> e = new Enumerate<>(arr, arr.length);
e.heap(arr.length);
return e;
}
// Enumerate permutations of items in array, using Knuth's algorithm L
public static <T> Enumerate<T> algorithmL(T[] arr, Comparator<T> c) {
Enumerate<T> e = new Enumerate<>(arr, arr.length);
e.algorithmL(c);
return e;
}
// --------------------------- MAIN METHOD -------------------------------
public static void main(String args[]) {
int n = 4;
int k = 3;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
k = n;
}
if (args.length > 1) {
k = Integer.parseInt(args[1]);
}
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
System.out.println("Permutations: " + n + " " + k);
Enumerate<Integer> e = permute(arr, k);
System.out.println("Count: " + e.count + "\n_________________________");
System.out.println("Combinations: " + n + " " + k);
e = combine(arr, k);
System.out.println("Count: " + e.count + "\n_________________________");
System.out.println("Heap Permutations: " + n);
e = heap(arr);
System.out.println("Count: " + e.count + "\n_________________________");
Integer[] test = { 1, 2, 2, 3, 3, 4 };
System.out.println("Algorithm L Permutations: ");
e = algorithmL(test, (Integer a, Integer b) -> a.compareTo(b));
System.out.println("Count: " + e.count + "\n_________________________");
}
}