Java(n = 8)
import java.util.*;
import java.util.concurrent.*;
public class HankelCombinatorics {
public static final int NUM_THREADS = 8;
private static final int[] FACT = new int[13];
static {
FACT[0] = 1;
for (int i = 1; i < FACT.length; i++) FACT[i] = i * FACT[i-1];
}
public static void main(String[] args) {
long prevElapsed = 0, start = System.nanoTime();
for (int i = 1; i < 12; i++) {
long count = count(i), elapsed = System.nanoTime() - start;
System.out.format("%d in %dms, total elapsed %dms\n", count, (elapsed - prevElapsed) / 1000000, elapsed / 1000000);
prevElapsed = elapsed;
}
}
@SuppressWarnings("unchecked")
private static long count(int n) {
int[][] perms = new int[FACT[n]][];
genPermsInner(0, 0, new int[n], perms, 0);
// We partition by canonical representation of the row sum multiset, discarding any with a density > 50%.
Map<CanonicalMatrix, Map<CanonicalMatrix, Integer>> part = new HashMap<CanonicalMatrix, Map<CanonicalMatrix, Integer>>();
for (int m = 0; m < 1 << (2*n-1); m++) {
int density = 0;
int[] key = new int[n];
for (int i = 0; i < n; i++) {
key[i] = Integer.bitCount((m >> i) & ((1 << n) - 1));
density += key[i];
}
if (2 * density <= n * n) {
CanonicalMatrix _key = new CanonicalMatrix(key);
Map<CanonicalMatrix, Integer> map = part.get(_key);
if (map == null) part.put(_key, map = new HashMap<CanonicalMatrix, Integer>());
map.put(new CanonicalMatrix(m, perms[0]), m);
}
}
List<Job> jobs = new ArrayList<Job>();
ExecutorService pool = Executors.newFixedThreadPool(NUM_THREADS);
for (Map.Entry<CanonicalMatrix, Map<CanonicalMatrix, Integer>> e : part.entrySet()) {
Job job = new Job(n, perms, e.getKey().sum() << 1 == n * n ? 0 : 1, e.getValue());
jobs.add(job);
pool.execute(job);
}
pool.shutdown();
try {
pool.awaitTermination(1, TimeUnit.DAYS); // i.e. until it's finished - inaccurate results are useless
}
catch (InterruptedException ie) {
throw new IllegalStateException(ie);
}
long total = 0;
for (Job job : jobs) total += job.subtotal;
return total;
}
private static int genPermsInner(int idx, int usedMask, int[] a, int[][] perms, int off) {
if (idx == a.length) perms[off++] = a.clone();
else for (int i = 0; i < a.length; i++) {
int m = 1 << (a[idx] = i);
if ((usedMask & m) == 0) off = genPermsInner(idx+1, usedMask | m, a, perms, off);
}
return off;
}
static class Job implements Runnable {
private volatile long subtotal = 0;
private final int n;
private final int[][] perms;
private final int shift;
private final Map<CanonicalMatrix, Integer> unseen;
public Job(int n, int[][] perms, int shift, Map<CanonicalMatrix, Integer> unseen) {
this.n = n;
this.perms = perms;
this.shift = shift;
this.unseen = unseen;
}
public void run() {
long result = 0;
int[][] perms = this.perms;
Map<CanonicalMatrix, Integer> unseen = this.unseen;
while (!unseen.isEmpty()) {
int m = unseen.values().iterator().next();
Set<CanonicalMatrix> equiv = new HashSet<CanonicalMatrix>();
for (int[] perm : perms) {
CanonicalMatrix canonical = new CanonicalMatrix(m, perm);
if (equiv.add(canonical)) {
result += canonical.weight() << shift;
unseen.remove(canonical);
}
}
}
subtotal = result;
}
}
static class CanonicalMatrix {
private final int[] a;
private final int hash;
public CanonicalMatrix(int m, int[] r) {
this(permuteRows(m, r));
}
public CanonicalMatrix(int[] a) {
this.a = a;
Arrays.sort(a);
int h = 0;
for (int i : a) h = h * 37 + i;
hash = h;
}
private static int[] permuteRows(int m, int[] perm) {
int[] cols = new int[perm.length];
for (int i = 0; i < perm.length; i++) {
for (int j = 0; j < cols.length; j++) cols[j] |= ((m >> (perm[i] + j)) & 1L) << i;
}
return cols;
}
public int sum() {
int sum = 0;
for (int i : a) sum += i;
return sum;
}
public int weight() {
int prev = -1, count = 0, weight = FACT[a.length];
for (int col : a) {
if (col == prev) weight /= ++count;
else {
prev = col;
count = 1;
}
}
return weight;
}
@Override public boolean equals(Object obj) {
// Deliberately unsuitable for general-purpose use, but helps catch bugs faster.
CanonicalMatrix that = (CanonicalMatrix)obj;
for (int i = 0; i < a.length; i++) {
if (a[i] != that.a[i]) return false;
}
return true;
}
@Override public int hashCode() {
return hash;
}
}
}
另存为HankelCombinatorics.java
,编译为javac HankelCombinatorics.java
,运行为java -Xmx2G HankelCombinatorics
。
随着NUM_THREADS = 4
我的四核机器上它得到20420819767436
了n=8
在50至55秒钟以后,运行之间的变化相当数量的; 我希望它可以轻松地在八核计算机上管理相同的内容,但是要花一个小时或更长时间才能获得n=9
。
怎么运行的
给定n
,存在2^(2n-1)
二进制n
x n
Hankel矩阵。行可以按n!
方式排列,列可以按方式排列n!
。我们要做的就是避免重复计算...
如果您计算每行的总和,那么对行进行排列或对列进行排列都不会更改总和的多集。例如
0 1 1 0 1
1 1 0 1 0
1 0 1 0 0
0 1 0 0 1
1 0 0 1 0
具有行和multiset {3, 3, 2, 2, 2}
,从中得出的所有Hankelable矩阵也是如此。这意味着我们可以通过这些行和多集对Hankel矩阵进行分组,然后利用多个处理器核心独立处理每个组。
还有一个可利用的对称性:零多于零的矩阵与具有多于零的矩阵是双射的。
当汉克尔矩阵发生重复计算M_1
与行置换r_1
和列置换c_1
汉克尔矩阵匹配M_2
与行置换r_2
和列置换c_2
(最多两个而不是三个的M_1 = M_2
,r_1 = r_2
,c_1 = c_2
)。该行和列的排列是独立的,所以如果我们应用行排列r_1
于M_1
和行置换r_2
到M_2
,列如多集必须相等。因此,对于每个组,我计算通过将行置换应用于组中的矩阵而获得的所有列多集。获得多集规范表示的简单方法是对列进行排序,这在下一步中也很有用。
获得了不同的列多集后,我们需要找到n!
每个列的多少个排列是唯一的。在这一点上,只有在给定的列多集具有重复的列的情况下,才可以进行重复计数:我们需要做的是计算多集中每个不同列的出现次数,然后计算相应的多项式系数。由于列已排序,因此计数很容易。
最后,我们将它们全部加起来。
渐近复杂度要达到完全精确的计算并非易事,因为我们需要对集合进行一些假设。我们根据2^(2n-2) n!
列多集的顺序进行评估n^2 ln n
,每次花费时间(包括排序);如果分组只花一个ln n
因素,那么我们就有时间复杂性Theta(4^n n! n^2 ln n)
。但是由于指数因子完全控制多项式,所以是Theta(4^n n!) = Theta((4n/e)^n)
。