枚举笛卡尔积,同时尽量减少重复

时间:2016-12-04 05:44:31

标签: algorithm math enumeration discrete-mathematics cartesian-product

给出两组,例如:

{A B C}, {1 2 3 4 5 6}

我想生成笛卡尔积,其顺序是在相等元素之间放置尽可能多的空间。例如,[A1, A2, A3, A4, A5, A6, B1…]并不好,因为所有A都是彼此相邻的。一个可接受的解决方案是“沿着对角线”,然后每次它包裹偏移一个,例如:

[A1, B2, C3, A4, B5, C6, A2, B3, C4, A5, B6, C1, A3…]

直观地表达:

|   | A | B | C | A | B | C | A | B | C | A | B | C | A | B | C | A | B | C |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 2 |   | 2 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 3 |   |   | 3 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 4 |   |   |   | 4 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 5 |   |   |   |   | 5 |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 6 |   |   |   |   |   | 6 |   |   |   |   |   |   |   |   |   |   |   |   |
| 1 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 2 |   |   |   |   |   |   | 7 |   |   |   |   |   |   |   |   |   |   |   |
| 3 |   |   |   |   |   |   |   | 8 |   |   |   |   |   |   |   |   |   |   |
| 4 |   |   |   |   |   |   |   |   | 9 |   |   |   |   |   |   |   |   |   |
| 5 |   |   |   |   |   |   |   |   |   | 10|   |   |   |   |   |   |   |   |
| 6 |   |   |   |   |   |   |   |   |   |   | 11|   |   |   |   |   |   |   |
| 1 |   |   |   |   |   |   |   |   |   |   |   | 12|   |   |   |   |   |   |
| 2 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| 3 |   |   |   |   |   |   |   |   |   |   |   |   | 13|   |   |   |   |   |
| 4 |   |   |   |   |   |   |   |   |   |   |   |   |   | 14|   |   |   |   |
| 5 |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 15|   |   |   |
| 6 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 16|   |   |
| 1 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 17|   |
| 2 |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | 18| 

或等效但不重复行/列:

|   | A  | B  | C  |
|---|----|----|----|
| 1 | 1  | 17 | 15 |
| 2 | 4  | 2  | 18 |
| 3 | 7  | 5  | 3  |
| 4 | 10 | 8  | 6  |
| 5 | 13 | 11 | 9  |
| 6 | 16 | 14 | 12 |

我想也有其他解决方案,但这是我发现最容易思考的问题。但是我一直在撞墙试图找出如何一般地表达它 - 两组的基数是彼此的倍数是一件很方便的事情,但我希望算法为集合做正确的事情例如,尺寸5和7.或尺寸12和69(这是一个真实的例子!)。

有没有建立的算法?我一直在分心思考理性数字如何映射到自然数字集上(以证明它们是可数的),但是它通过ℕ×path的路径对于这种情况不起作用。

应用程序正在用Ruby编写,但我并不关心语言。 Pseudocode,Ruby,Python,Java,Clojure,Javascript,CL,英文段落 - 选择你喜欢的。

Python中的概念验证解决方案(很快将移植到Ruby并与Rails连接):

import sys

letters = sys.argv[1]
MAX_NUM = 6

letter_pos = 0
for i in xrange(MAX_NUM):
    for j in xrange(len(letters)):
        num = ((i + j) % MAX_NUM) + 1
        symbol = letters[letter_pos % len(letters)]
        print "[%s %s]"%(symbol, num)
        letter_pos += 1

3 个答案:

答案 0 :(得分:2)

String letters = "ABC";
int MAX_NUM = 6;

int letterPos = 0;
for (int i=0; i < MAX_NUM; ++i) {
    for (int j=0; j < MAX_NUM; ++j) {
        int num = ((i + j) % MAX_NUM) + 1;
        char symbol = letters.charAt(letterPos % letters.length);
        String output = symbol + "" + num;
        ++letterPos;
    }
}

答案 1 :(得分:2)

使用分形/递归的东西怎么样?该实现将矩形范围划分为四个象限,然后从每个象限产生点。这意味着序列中的相邻点至少与象限不同。

#python3

import sys
import itertools

def interleave(*iters):
  for elements in itertools.zip_longest(*iters):
    for element in elements:
      if element != None:
        yield element

def scramblerange(begin, end):
  width = end - begin

  if width == 1:
    yield begin

  else:
    first = scramblerange(begin, int(begin + width/2))
    second = scramblerange(int(begin + width/2), end)
    yield from interleave(first, second)

def scramblerectrange(top=0, left=0, bottom=1, right=1, width=None, height=None):
  if width != None and height != None:
    yield from scramblerectrange(bottom=height, right=width)
    raise StopIteration

  if right - left == 1:
    if bottom - top == 1:
      yield (left, top)

    else:
      for y in scramblerange(top, bottom):
        yield (left, y)

  else:
    if bottom - top == 1:
      for x in scramblerange(left, right):
        yield (x, top)

    else:
      halfx = int(left + (right - left)/2)
      halfy = int(top + (bottom - top)/2)

      quadrants = [
        scramblerectrange(top=top, left=left, bottom=halfy, right=halfx),
        reversed(list(scramblerectrange(top=top, left=halfx, bottom=halfy, right=right))),
        scramblerectrange(top=halfy, left=left, bottom=bottom, right=halfx),
        reversed(list(scramblerectrange(top=halfy, left=halfx, bottom=bottom, right=right)))
      ]

      yield from interleave(*quadrants)

if __name__ == '__main__':
  letters = 'abcdefghijklmnopqrstuvwxyz'
  output = []

  indices = dict()
  for i, pt in enumerate(scramblerectrange(width=11, height=5)):
    indices[pt] = i
    x, y = pt
    output.append(letters[x] + str(y))

  table = [[indices[x,y] for x in range(11)] for y in range(5)]

  print(', '.join(output))
  print()
  pad = lambda i: ' ' * (2 - len(str(i))) + str(i)
  header = '  |' + ' '.join(map(pad, letters[:11]))
  print(header)
  print('-' * len(header))
  for y, row in enumerate(table):
    print(pad(y)+'|', ' '.join(map(pad, row)))

输出:

a0, i1, a2, i3, e0, h1, e2, g4, a1, i0, a3, k3, e1,
h0, d4, g3, b0, j1, b2, i4, d0, g1, d2, h4, b1, j0,
b3, k4, d1, g0, d3, f4, c0, k1, c2, i2, c1, f1, a4,
h2, k0, e4, j3, f0, b4, h3, c4, j2, e3, g2, c3, j4,
f3, k2, f2

  | a  b  c  d  e  f  g  h  i  j  k
-----------------------------------
 0|  0 16 32 20  4 43 29 13  9 25 40
 1|  8 24 36 28 12 37 21  5  1 17 33
 2|  2 18 34 22  6 54 49 39 35 47 53
 3| 10 26 50 30 48 52 15 45  3 42 11
 4| 38 44 46 14 41 31  7 23 19 51 27

答案 2 :(得分:1)

如果你的集合X和Y的大小是m和n,而Xi是笛卡尔积中第i个对中X元素的索引(和Y类似),那么

Xi = i mod n;
Yi = (i mod n + i div n) mod m;

你可以填写这样的矩阵,让你的对角线更加分散:

for (int i = 0; i < m*n; i++) {
  int xi = i % n;
  int yi = i % m;
  while (matrix[yi][xi] != 0) {
    yi = (yi+1) % m;
  }
  matrix[yi][xi] = i+1;
}