【问题标题】:How to get the Nth arrangement in a Combinatoric sequence and vice-versa?如何在组合序列中获得第 N 个排列,反之亦然?
【发布时间】:2018-08-19 18:37:02
【问题描述】:

我如何从将 4 个无法区分的球排列在 3 个不同的桶中的所有可能组合中得到 Nth 排列。如果 Bl = number of balls 和 Bk = number of buckets 例如对于 Bl = 4, Bk = 3,可能的安排是:

004,013,022,031,040,103,112,121,21135@,202,202,@987654336 ,301,310,400.

第一种排列方式(N=0)是004(即桶1 = 0个球, 桶2 = 0个球, 桶 3 = 4 个球),最后一个(N=14)是400。所以说我有103 N 将等于 5。我希望能够做到

int Bl=4,Bk=3;
getN(004,Bl,Bk);// which should be = 0
getNthTerm(8,Bl,Bk);// which should be = 130

PS: 序列的最大项数是 (Bl+Bk-1)C(Bk-1) 其中 C 是组合/组合运算符。 Obtained from stars and bars

【问题讨论】:

  • 这些被称为Integer Compositions。更具体地说,受限整数组合。我不确定如何获得nth 一个。
  • 谢谢约瑟夫,我会查一下

标签: algorithm math combinations combinatorics sequences


【解决方案1】:

据我所知,没有比组合分解更快的方法了,这大约需要 O(Bl) 时间。

我们只需计算进入所选索引的每个桶的球数,一次工作一个桶。对于桶的每个可能分配,我们计算剩余的球和桶的可能排列的数量。如果索引小于那个数字,我们选择那个排列;否则我们在桶中再放一个球,然后从索引中减去我们刚刚跳过的排列数。

这是一个 C 实现。我没有在下面的实现中包含binom 函数。通常最好在您感兴趣的值范围内预先计算二项式系数,因为通常不会太多。增量计算很容易,但每一步都需要乘法和除法;虽然这不会影响渐近复杂度,但它会使内部循环变得更慢(因为除法)并增加了溢出的风险(因为乘法)。

/* Computes arrangement corresponding to index.
 * Returns 0 if index is out of range.
 */
int get_nth(long index, int buckets, int balls, int result[buckets]) {
  int i = 0;
  memset(result, 0, buckets * sizeof *result);
  --buckets;
  while (balls && buckets) {
    long count = binom(buckets + balls - 1, buckets - 1);
    if (index < count) { --buckets; ++i; }
    else { ++result[i]; --balls; index -= count; }
  }
  if (balls) result[i] = balls;
  return index == 0;
}

【讨论】:

  • @mbo: 或count = binom(... :)。我也错过了index -= count。现在我已经对其进行了一些测试,它似乎像宣传的那样工作。谢谢。
【解决方案2】:

可以进行一些有趣的双射。最后,我们可以对常规的 k-combinations 使用排序和非排序方法,这是更常见的知识。

  1. 从每个桶中的球数到桶的有序多集选择的双射;例如:[3, 1, 0] --&gt; [1, 1, 1, 2](三选一,二选一)。

  2. 通过将{c_0, c_1...c_(k−1)} 映射到{c_0, c_(1+1), c_(2+2)...c_(k−1+k−1)}(参见here),从{1...n} 的k 子集(有重复)到{1...n + k − 1} 的k 子集(无重复)的双射。

这是一些python代码:

from itertools import combinations_with_replacement

def toTokens(C):
  return map(lambda x: int(x), list(C))

def compositionToChoice(tokens):
  result = []
  for i, t in enumerate(tokens):
    result = result + [i + 1] * t
  return result

def bijection(C):
  result = []
  k = 0
  for i, _c in enumerate(C):
    result.append(C[i] + k)
    k = k + 1
  return result

compositions = ['004','013','022','031','040','103','112',
                '121','130','202','211','220','301','310','400']

for c in compositions:
  tokens = toTokens(c)
  choices = compositionToChoice(tokens)
  combination = bijection(choices)
  print "%s  -->  %s  -->  %s" % (tokens, choices, combination)

输出:

"""
[0, 0, 4]  -->  [3, 3, 3, 3]  -->  [3, 4, 5, 6]
[0, 1, 3]  -->  [2, 3, 3, 3]  -->  [2, 4, 5, 6]
[0, 2, 2]  -->  [2, 2, 3, 3]  -->  [2, 3, 5, 6]
[0, 3, 1]  -->  [2, 2, 2, 3]  -->  [2, 3, 4, 6]
[0, 4, 0]  -->  [2, 2, 2, 2]  -->  [2, 3, 4, 5]
[1, 0, 3]  -->  [1, 3, 3, 3]  -->  [1, 4, 5, 6]
[1, 1, 2]  -->  [1, 2, 3, 3]  -->  [1, 3, 5, 6]
[1, 2, 1]  -->  [1, 2, 2, 3]  -->  [1, 3, 4, 6]
[1, 3, 0]  -->  [1, 2, 2, 2]  -->  [1, 3, 4, 5]
[2, 0, 2]  -->  [1, 1, 3, 3]  -->  [1, 2, 5, 6]
[2, 1, 1]  -->  [1, 1, 2, 3]  -->  [1, 2, 4, 6]
[2, 2, 0]  -->  [1, 1, 2, 2]  -->  [1, 2, 4, 5]
[3, 0, 1]  -->  [1, 1, 1, 3]  -->  [1, 2, 3, 6]
[3, 1, 0]  -->  [1, 1, 1, 2]  -->  [1, 2, 3, 5]
[4, 0, 0]  -->  [1, 1, 1, 1]  -->  [1, 2, 3, 4]
"""

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多