Python Integer使用给定的k分区进行分区

时间:2013-08-29 05:41:27

标签: python algorithm integer-partition

我正在尝试为Python找到或开发Integer Partitioning代码。

FYI,整数分区将给定的整数n表示为小于n的整数之和。例如,整数5可以表示为4 + 1 = 3 + 2 = 3 + 1 + 1 = 2 + 2 + 1 = 2 + 1 + 1 + 1 = 1 + 1 + 1 + 1 + 1

我已经为此找到了许多解决方案。 http://homepages.ed.ac.uk/jkellehe/partitions.phphttp://code.activestate.com/recipes/218332-generator-for-integer-partitions/

但是,我真正想要的是限制分区的数量。

说,分区#em> k = 2,程序只需显示5 = 4 + 1 = 3 + 2

如果 k = 3,5 = 3 + 1 + 1 = 2 + 2 + 1

4 个答案:

答案 0 :(得分:19)

我写了一个生成器解决方案

def partitionfunc(n,k,l=1):
    '''n is the integer to partition, k is the length of partitions, l is the min partition element size'''
    if k < 1:
        raise StopIteration
    if k == 1:
        if n >= l:
            yield (n,)
        raise StopIteration
    for i in range(l,n+1):
        for result in partitionfunc(n-i,k-1,i):
            yield (i,)+result

这将生成长度为n的{​​{1}}的所有分区,每个分区的顺序最小到最大。

快速说明:通过k,使用生成器方法似乎比使用falsetru的直接方法快得多,使用测试函数cProfile。在lambda x,y: list(partitionfunc(x,y))的测试运行中,我的代码在.019秒内与直接方法的2.612秒相比。

答案 1 :(得分:4)

def part(n, k):
    def _part(n, k, pre):
        if n <= 0:
            return []
        if k == 1:
            if n <= pre:
                return [[n]]
            return []
        ret = []
        for i in range(min(pre, n), 0, -1):
            ret += [[i] + sub for sub in _part(n-i, k-1, i)]
        return ret
    return _part(n, k, n)

示例:

>>> part(5, 1)
[[5]]
>>> part(5, 2)
[[4, 1], [3, 2]]
>>> part(5, 3)
[[3, 1, 1], [2, 2, 1]]
>>> part(5, 4)
[[2, 1, 1, 1]]
>>> part(5, 5)
[[1, 1, 1, 1, 1]]
>>> part(6, 3)
[[4, 1, 1], [3, 2, 1], [2, 2, 2]]

<强>更新

使用memoization:

def part(n, k):
    def memoize(f):
        cache = [[[None] * n for j in xrange(k)] for i in xrange(n)]
        def wrapper(n, k, pre):
            if cache[n-1][k-1][pre-1] is None:
                cache[n-1][k-1][pre-1] = f(n, k, pre)
            return cache[n-1][k-1][pre-1]
        return wrapper

    @memoize
    def _part(n, k, pre):
        if n <= 0:
            return []
        if k == 1:
            if n <= pre:
                return [(n,)]
            return []
        ret = []
        for i in xrange(min(pre, n), 0, -1):
            ret += [(i,) + sub for sub in _part(n-i, k-1, i)]
        return ret
    return _part(n, k, n)

答案 2 :(得分:2)

首先,我要感谢大家的贡献。 我到达这里需要一个生成整数分区的算法,其中包含以下细节:

将数字的分区生成为完全k个部分,但也具有MINIMUM和MAXIMUM约束。

因此,我修改了&#34; Snakes and Coffee&#34;的代码。满足这些新要求:

def partition_min_max(n,k,l, m):
'''n is the integer to partition, k is the length of partitions, 
l is the min partition element size, m is the max partition element size '''
if k < 1:
    raise StopIteration
if k == 1:
    if n <= m and n>=l :
        yield (n,)
    raise StopIteration
for i in range(l,m+1):
    for result in partition_min_max(n-i,k-1,i,m):                
        yield result+(i,)


>>> x = list(partition_min_max(20 ,3, 3, 10 ))
>>> print(x)
>>> [(10, 7, 3), (9, 8, 3), (10, 6, 4), (9, 7, 4), (8, 8, 4), (10, 5, 5), (9, 6, 5), (8, 7, 5), (8, 6, 6), (7, 7, 6)]

答案 3 :(得分:0)

基于先前具有最大和最小约束的答案,我们可以将其优化得更好一些。例如,对于 k = 16 , n = 2048 和 m = 128 ,只有一个这样的分区满足约束(128+128+...+128)。但是代码会搜索不必要的分支以寻找可以修剪的答案。

def partition_min_max(n,k,l,m):
#n is the integer to partition, k is the length of partitions, 
#l is the min partition element size, m is the max partition element size
    if k < 1:
        return
    if k == 1:
        if n <= m and n>=l :
            yield (n,)
        return
    if (k*128) < n: #If the current sum is too small to reach n
        return
    if k*1 > n:#If current sum is too big to reach n
        return
    for i in range(l,m+1):
        for result in partition_min_max(n-i,k-1,i,m):                
            yield result+(i,)