2

我正在寻找一个示例,说明如何在 Ruby、类似 C 的语言或伪代码中创建可变数量的整数数组的笛卡尔积,每个数组的长度不同,并以特定顺序逐步执行结果:

因此,[1,2,3],[1,2,3],[1,2,3]:

[1, 1, 1]
[2, 1, 1]
[1, 2, 1]
[1, 1, 2]
[2, 2, 1]
[1, 2, 2]
[2, 1, 2]
[2, 2, 2]
[3, 1, 1]
[1, 3, 1]
etc.

而不是我看到的典型结果(包括我在下面给出的示例):

[1, 1, 1]
[2, 1, 1]
[3, 1, 1]
[1, 2, 1]
[2, 2, 1]
[3, 2, 1]
[1, 3, 1]
[2, 3, 1]
etc.

此示例的问题在于,在尝试前两个位置的所有组合之前,根本不会探索第三个位置。在使用它的代码中,这意味着即使正确答案通常是(大得多的)1、1、2,它也会检查几百万个可能性,而不是在找到它之前只检查几千个。

我正在处理一百万到数亿的结果集,因此在这里生成它们然后排序是不可行的,并且会破坏在第一个示例中对它们进行排序的原因,即尽快找到正确的答案并因此中断早于笛卡尔积生成。

以防万一它有助于澄清上述任何内容,这就是我现在如何执行此操作(这具有正确的结果和正确的性能,但不是我想要的顺序,即它会创建上面第二个清单中的结果):

def cartesian(a_of_a)
  a_of_a_len = a_of_a.size
  result = Array.new(a_of_a_len)
  j, k, a2, a2_len = nil, nil, nil, nil
  i = 0
  while 1 do
    j, k = i, 0
    while k < a_of_a_len
      a2 = a_of_a[k]
      a2_len = a2.size
      result[k] = a2[j % a2_len]
      j /= a2_len
      k += 1
    end

    return if j > 0
    yield result

    i += 1
  end

end

更新:我没有说得很清楚,我正在寻求一个解决方案,其中在添加 3 之前检查 1,2 的所有组合,然后是所有 3 和 1,然后是所有 3、2 和 1,然后是所有 3 ,2。换句话说,在“垂直”之前“水平”探索所有早期的组合。探索这些可能性的确切顺序,即 1,1,2 或 2,1,1,并不重要,只是在混合 3 之前探索了所有 2 和 1,依此类推。

4

3 回答 3

2

在问题的准确性之后,这是一个修订版。我保留以前的答案,因为它也很有用并且使用不太复杂的顺序。

# yields the possible cartesian products of [first, *rest], where the total
# of the indices that are "distributed" is exactly +nb+ and each index doesn't
# go beyong +depth+, but at least one of them is exactly +depth+
def distribute(nb, depth, reached, first, *rest)
  from  = [nb - rest.size * depth, 0].max
  to    = [first.size-1, depth, nb].min
  from.upto(to) do |i|
    obj = first[i]
    reached ||= i == depth
    if rest.empty?
      yield [obj] if reached
    else
      distribute(nb - i, depth, reached, *rest) do |comb|
        yield [obj, *comb]
      end
    end
  end
end

def depth_first_cartesian(*arrays)
  return to_enum __method__, *arrays unless block_given?
  lengths = arrays.map(&:length)
  total = lengths.inject(:+)
  lengths.max.times do |depth|
    depth.upto(arrays.size * depth) do |nb|
      distribute(nb, depth, false, *arrays) {|c| yield c}
    end
  end
end

p depth_first_cartesian([1, 2, 3], [1, 2, 3, 4], [1, 2, 3]).to_a
# => [[1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 2, 2], [2, 1, 2], [2, 2, 1], [2, 2, 2],
#     [1, 1, 3], [1, 3, 1], [3, 1, 1], [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2],
#     [3, 2, 1], [1, 3, 3], [2, 2, 3], [2, 3, 2], [3, 1, 3], [3, 2, 2], [3, 3, 1], [2, 3, 3],
#     [3, 2, 3], [3, 3, 2], [3, 3, 3], [1, 4, 1], [1, 4, 2], [2, 4, 1], [1, 4, 3], [2, 4, 2],
#     [3, 4, 1], [2, 4, 3], [3, 4, 2], [3, 4, 3]]
于 2010-09-02T15:57:26.207 回答
1

嘿,Marc-André,笛卡尔宝石完全符合您的要求:

require 'cartesian'
[1,2,3].x([1,2,3]).to_a #=> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]

您还可以使用 ** (幂)运算符来简洁

for a,b,c in [1,2,3]**3 ; p [a,b,c] ; end
# output:
#    [1, 1, 1]
#    [1, 1, 2]
#    [1, 1, 3]
#    [1, 2, 1]
#    ...
#    [3, 3, 3]

该项目托管在 github 上,其主页中有 RDoc 文档的链接。

于 2011-01-03T18:14:47.753 回答
1

目前尚不清楚元素[1, 1, 3]在您想要的输出中的位置。如果我的猜测是正确的,下面的作品(虽然它可能会被优化)

# yields the possible cartesian products of [first, *rest], where the total
# of the indices that are "distributed" is exactly +nb+.
def distribute(nb, first, *rest)
  if rest.empty?                    # single array remaining?
    yield first.fetch(nb) {return}  # yield the right element (if there is one)
  else
    first.each_with_index do |obj, i|
      break if i > nb
      distribute(nb - i, *rest) do |comb|
        yield [obj, *comb]
      end
    end
  end
end

def strange_cartesian(*arrays, &block)
  return to_enum __method__, *arrays unless block_given?
  max = arrays.map(&:length).inject(:+)
  max.times do |nb|
    distribute(nb, *arrays, &block)
  end
end

p strange_cartesian([1, 2, 3], [1, 2, 3], [1, 2, 3]).to_a
#  => [[1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 1, 3], [1, 2, 2], [1, 3, 1], [2, 1, 2], [2, 2, 1], [3, 1, 1], [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 2, 2], [2, 3, 1], [3, 1, 2], [3, 2, 1], [1, 3, 3], [2, 2, 3], [2, 3, 2], [3, 1, 3], [3, 2, 2], [3, 3, 1], [2, 3, 3], [3, 2, 3], [3, 3, 2], [3, 3, 3]]

注意:如果您仍在运行 Ruby 1.8.6,请至少升级到 1.8.7(或require 'backports'

于 2010-09-01T20:17:03.260 回答