forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutations.java
128 lines (117 loc) · 3.77 KB
/
Permutations.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
package combinatorics;
import java.util.*;
public class Permutations {
public static boolean nextPermutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
public static int[] permutationByNumber(int n, long number) {
long[] fact = new long[n];
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = i * fact[i - 1];
}
int[] p = new int[n];
int[] free = new int[n];
for (int i = 0; i < n; i++) {
free[i] = i;
}
for (int i = 0; i < n; i++) {
int pos = (int) (number / fact[n - 1 - i]);
p[i] = free[pos];
System.arraycopy(free, pos + 1, free, pos, n - 1 - pos);
number %= fact[n - 1 - i];
}
return p;
}
public static long numberByPermutation(int[] p) {
int n = p.length;
long[] fact = new long[n];
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = i * fact[i - 1];
}
long res = 0;
for (int i = 0; i < n; i++) {
int a = p[i];
for (int j = 0; j < i; j++) {
if (p[j] < p[i]) {
--a;
}
}
res += a * fact[n - 1 - i];
}
return res;
}
public static void generatePermutations(int[] p, int depth) {
int n = p.length;
if (depth == n) {
System.out.println(Arrays.toString(p));
return;
}
for (int i = 0; i < n; i++) {
if (p[i] == 0) {
p[i] = depth;
generatePermutations(p, depth + 1);
p[i] = 0;
}
}
}
public static long nextPermutation(long x) {
long s = x & -x;
long r = x + s;
long ones = x ^ r;
ones = (ones >> 2) / s;
return r | ones;
}
public static List<List<Integer>> decomposeIntoCycles(int[] p) {
int n = p.length;
boolean[] vis = new boolean[n];
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (vis[i])
continue;
int j = i;
List<Integer> cur = new ArrayList<>();
do {
cur.add(j);
vis[j] = true;
j = p[j];
} while (j != i);
res.add(cur);
}
return res;
}
// Usage example
public static void main(String[] args) {
// print all permutations method 1
generatePermutations(new int[2], 1);
// print all permutations method 2
int[] p = {0, 1, 2};
int cnt = 0;
do {
System.out.println(Arrays.toString(p));
if (!Arrays.equals(p, permutationByNumber(p.length, numberByPermutation(p)))
|| cnt != numberByPermutation(permutationByNumber(p.length, cnt)))
throw new RuntimeException();
++cnt;
} while (nextPermutation(p));
System.out.println(5 == numberByPermutation(p));
System.out.println(Arrays.equals(new int[] {1, 0, 2}, permutationByNumber(3, 2)));
System.out.println(0b1101 == nextPermutation(0b1011));
System.out.println(decomposeIntoCycles(new int[] {0, 2, 1, 3}));
}
}