Python中的快速组合生成器

时间:2015-01-22 00:28:43

标签: python performance generator combinatorics

作为python中大型项目的一部分,我需要一个快速生成器函数,它生成所有可能的小于n的非负整数集,这样每个集最多只有s个元素并且集合中最大和最小数字之间的差异小于w

到目前为止,我实现的最快的实现方法是使用itertools

import itertools

def subsample(n, s, w):
    nn = range(w)
    for p in range(s):
        o = list(itertools.combinations(nn, p+1))
        for t in o:
            yield t
        for _ in range(0, n-w):
            pt = o
            o = [tuple([op + 1 for op in list(u)]) for u in pt]
            for t in list((set(o) ^ set(pt)) & set(o)):
                yield t

例如:

In [1]: list(subsample(6,3,3))
Out [1]: [(0,), (1,), (2,), (3,), (4,), (5,), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (2, 4), (4, 5), (3, 5), (0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

但我确信必须有更有效的方法来做到这一点。有没有什么可以让这更快?

1 个答案:

答案 0 :(得分:1)

以下是一些基于归属于Knuth的算法的生成器。

  • subsets4生成 1..n 的所有子集 k 或更少的元素
  • subsets5subsets4生成的子集限制为 w 的最大差异
  • subsets生成 1..n 的所有子集,其长度完全为 k
  • subsets2subsets生成的子集限制为 w 的最大差异

运行megatest()函数,为subsets5生成器测试 n k w 的多个值

# all subsets of k or less elements of 1..n
def subsets4(n,k):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n:
      i -= 1

# all subsets of k or less elements of 1..n with max difference <= w
def subsets5(n,k,w):
  a = [ 0 ] * k
  i = 0
  while i >= 0:
    a[i] += 1
    yield a[0:i+1]
    r = a[i]+1
    i += 1
    while i < k and r <= n and r-a[0] <= w:
      a[i] = r
      yield a[0:i+1]
      i += 1
      r += 1
    i -= 1
    if a[i] >= n or a[i]+1-a[0] > w:
      i -= 1

# all subsets of 1..n having exactly k elements
def subsets(n,k):
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    # find i
    i = k-1
    while i >= 0 and a[i]+k-i >= n+1: i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

# all subsets of 1..n having exactly k elements and whose max
# difference is w
def subsets2(n,k,w):
  if k > w: return
  a = range(1,k+1)
  while a[0] <= n+1-k:
    yield a
    i = k-1
    while i >= 0 and (a[i]+k-i >= n+1 or a[i]+k-i-a[0] > w) : i -= 1
    r = a[i]
    a[i] += 1
    j = 2
    i += 1
    while i < k:
      a[i] = r + j
      i += 1
      j += 1

def test(n,k,w):
  s1 = [ s for s in subsets4(n,k) if s[-1] - s[0] <= w ]
  s2 = [ s for s in subsets5(n,k,w) ]
  if s1 == s2:
    print "OK", n,k,w
    return 0
  else:
    print "NOT OK", n, k, w
    return 1 

# for s in subsets2(10,3,4): print s
# for s in subsets(10,3): print s
def megatest():
  failed = 0
  for n in xrange(10,20):
    for k in xrange(1,n+1):
      for w in xrange(k,n+1):
        failed += test(n,k,w)
  print "failed:", failed