-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation.js
More file actions
380 lines (317 loc) · 9.09 KB
/
Copy pathPermutation.js
File metadata and controls
380 lines (317 loc) · 9.09 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
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
/* This class represents a permutation, and encapsulates
* some useful behaviors for dealing with them, such as
* cloning and toString.
*
* Now, you can compose permutations with .compose(), and
* intialize them from cycle notation with Permutation.fromCycleString().
*
* The cycles of a permutation can be accessed with the .cycles property,
* which is a lazy initializer pattern which returns an array of subcycles,
* each of which is an array.
*
* The .toCycleString() method prints a nice cycle representation of the permutation.
*
* I still need to find a nice way of making these permutations immutable, though,
* since changing the value of any of their indicies would break the class.
*/
//
// Array Utilities required for Permutation.js
//
Array.prototype.hasDuplicates = function() {
var counts = {};
for (var i = 0; i < this.length; ++i) {
if (counts[this[i]] === undefined) {
counts[this[i]] = 0;
}
++counts[this[i]];
}
for (var key in counts) {
if (counts[key] >= 2) {
return true;
}
}
return false;
}
//
// An easy foreach construct with Arrays. Functions
// passed to be executed are passed two arguments,
// func(value, index), for each element in the array.
Array.prototype.forEach = function(func) {
for (var i = 0; i < this.length; ++i) {
func(this[i], i);
}
}
//
// Permutation Helpers
//
// only works if a and b are both >= 0.
Math.gcd = function(a, b) {
while (b != 0) {
var c = a % b;
a = b;
b = c;
}
return a;
}
Array.prototype.gcd = function() {
if (this.length == 0) { return undefined; }
else if (this.length == 1) { return this[0]; }
else {
var final_gcd = Math.gcd(this[0], this[1]);
for (var i = 2; i < this.length; ++i) {
final_gcd = Math.gcd(final_gcd, this[i]);
}
return final_gcd;
}
}
function validatePermutationArray(arr) {
if (arr.hasDuplicates()) {
if (console && console.error) {
console.error("The array passed to initialize the permutation " +
"contained duplicates, thus it does not correspond to a 1-1 function.");
}
return false;
}
else {
for (var i = 0; i < arr.length; ++i) {
if (arr[i] > MAX_ALLOWED_PERMUTATION_INDEX ||
arr[i] < PERMUTATION_INDEX) {
if (console && console.error) {
console.error("The array passed to initialize the permutation " +
"had the element " + arr[i] + " which was either too high or low.");
}
return false;
}
}
}
return true;
}
//
// Permutation Class
//
Permutation = function(arr) {
// if the array passed isn't suitable, just leave
if (!validatePermutationArray(arr)) {
return undefined;
}
var _length = arr.length;
// I'd prefer this to be readonly
this.__defineGetter__("length", function() {
return _length;
});
for (var i = 0; i < arr.length; ++i) {
this[PERMUTATION_INDEX + i] = arr[i];
}
var _cycles = undefined;
var getCycles = function(perm) {
var already_used = {};
var cycles = [];
for (var i = PERMUTATION_INDEX; i < perm.length + PERMUTATION_INDEX; ++i) {
if (perm[i] != i && already_used[i] == undefined) {
var curr_cycle = [i];
already_used[i] = true;
var j = perm[i];
while (j != i) {
curr_cycle.push(j);
already_used[j] = true;
j = perm[j];
}
cycles.push(curr_cycle);
}
}
return cycles;
}
this.__defineGetter__("cycles", function() {
if (_cycles == undefined) {
_cycles = getCycles(this);
}
return _cycles;
});
this.__defineGetter__("order", function() {
var lengths_arr = [0]; // if there are no cycles, there
// are only 1-cycles, thus order 0 is appropriate.
// Also, this won't muck with the computation since
// gcd(0,a) = a;
var cyc = this.cycles;
for (var i = 0; i < cyc.length; ++i) {
lengths_arr.push(cyc[i].length);
}
return lengths_arr.gcd();
});
}
Permutation.fromFunction = function(func, n) {
var arr = [];
for (var i = 0; i < n; ++i) {
arr.push(func(i));
}
return new Permutation(arr);
}
Permutation.getIdentity = function(n) {
return Permutation.fromFunction(function(i) { return i + PERMUTATION_INDEX; }, n);
}
Permutation.prototype.toString = function() {
var total = [];
for (var i = PERMUTATION_INDEX; i < this.length + PERMUTATION_INDEX; ++i) {
total.push(Permutation.getChar(this[i]));
}
return total.join('');
}
Permutation.prototype.toCycleString = function() {
var result = "";
for (var i = 0; i < this.cycles.length; ++i) {
result += '(' + this.cycles[i].join('') + ')';
}
if (result == "") {
result = "()"; // a traditional notation for an identity permutation
}
return result;
}
Permutation.prototype.clone = function() {
var clone_arr = [];
for (var i = PERMUTATION_INDEX; i < this.length + PERMUTATION_INDEX; ++i) {
clone_arr.push(this[i]);
}
return new Permutation(clone_arr);
}
Permutation.prototype.at = function(i) {
return this[i+PERMUTATION_INDEX];
}
// Returns a new Permutation consisting of theta
// composed with this, i.e. this(theta)
Permutation.prototype.compose = function(theta) {
if (theta.length != this.length) {
if (console && console.error) {
console.error("Tried to compose two permutations of unequal length.");
}
return undefined;
}
var arr = [];
for (var i = 0; i < this.length; ++i) {
arr[i] = this[theta[i+PERMUTATION_INDEX]];
}
return new Permutation(arr);
}
Permutation.equals = function(left, right) {
for (var i = 0; i < this.length; ++i) {
if (left.at(i) != right.at(i)) {
return false;
}
}
return true;
}
Permutation.prototype.equals = function(right) {
return Permutation.equals(this, right);
}
var C0 = '0'.charCodeAt(0);
var C9 = '9'.charCodeAt(0);
var CA = 'A'.charCodeAt(0);
var CZ = 'Z'.charCodeAt(0);
// Controls whether permutations
// are indexed with the first element at 0 or 1.
// Also controls the string version of permutations.
var PERMUTATION_INDEX = 0;
// Represents the highest number that can be
// represented in a single character in a permutation.
// Currently 35, which is represented by 'Z'
var MAX_ALLOWED_PERMUTATION_INDEX = 35;
// Gets the character used to represent the given
// number in a string version of a permutation, according
// to the following pattern:
// If i = 0..9, then return '0'..'9'
// If i = 10..35 then return 'A'..'Z'
Permutation.getChar = function(i) {
if (i >= 0 && i <= 9) {
return String.fromCharCode(C0 + i);
} else if (i <= MAX_ALLOWED_PERMUTATION_INDEX) {
return String.fromCharCode(CA + (i - 10));
} else {
if (console && console.error) {
console.error("getChar could not represent the integer " + i +
" in a single character. The max allowable is " +
MAX_ALLOWED_PERMUTATION_INDEX + ".");
}
}
}
Permutation.parseChar = function(c) {
var i = c.charCodeAt(0);
if (i >= C0 && i <= C9) {
return (i - C0);
} else if (i >= CA && i <= CZ) {
return ((i + 10) - CA);
} else {
if (console && console.error) {
console.error("parseChar could not parse the character '" + c + "'.");
}
}
}
Permutation.fromString = function(s) {
if (s.indexOf('(') >= 0) {
// if this is a cycle string, pass it to the cycle string
// parsing function
return Permutation.fromCycleString(s);
} else {
var arr = [];
var max_char = 0; // gets
for (var i = 0; i < s.length; ++i) {
var curr_char = Permutation.parseChar(s[i]);
if (curr_char > max_char) {
max_char = curr_char;
}
}
for (var j = 0; j <= max_char; ++j) {
if (j < s.length) {
arr.push(Permutation.parseChar(s[j]));
} else {
arr.push(j);
}
}
return new Permutation(arr);
}
}
Permutation.fromCycleString = function(s, n) {
function splitCycles(s) {
var result = [];
for (var i = s.indexOf('('); i>=0; i = s.indexOf('(', i+1)) {
var j = s.indexOf(')', i);
if (j > 0 && j > i + 2) {
result.push(s.substring(i+1,j));
}
}
return result;
}
function followCycles(cycles, char) {
var value = char;
for (var i = cycles.length - 1; i >= 0; --i) {
var index = cycles[i].indexOf(value);
if (index >= 0) {
if (index < cycles[i].length - 1) {
value = cycles[i][index+1];
} else {
value = cycles[i][0];
}
}
}
return value;
}
function getHighestChar(cycles) {
var highest = 0;
for (var i = 0; i < cycles.length; ++i) {
for (var j = 0; j < cycles[i].length; ++j) {
var curr_code = Permutation.parseChar(cycles[i][j]);
if (curr_code > highest) {
highest = curr_code;
}
}
}
return highest;
}
var splits = splitCycles(s);
if (n == undefined) {
n = getHighestChar(splits);
}
var arr = [];
for (var i = PERMUTATION_INDEX; i < n + PERMUTATION_INDEX; ++i) {
arr.push(Permutation.parseChar(followCycles(splits, Permutation.getChar(i))));
}
return new Permutation(arr);
}