簡體   English   中英

具有給定 k 個分區的 Python 整數分區

[英]Python Integer Partitioning with given k partitions

我正在嘗試為 Python 查找或開發整數分區代碼。

僅供參考,整數分區將給定的整數 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/

但是,我真正想要的是限制分區的數量。

比如說,# of partition k = 2,一個程序只需要顯示5 = 4 + 1 = 3 + 2

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

我寫了一個生成器解決方案

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

這將生成長度為kn所有分區,每個分區按最小到最大的順序排列。

只是一個簡短的說明:通過cProfile ,使用生成器方法似乎比使用 falsetru 的直接方法快得多,使用測試函數lambda x,y: list(partitionfunc(x,y)) n=50,k-5的測試運行中,我的代碼運行時間為 0.019 秒,而直接方法的運行時間為 2.612 秒。

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]]

更新

使用記憶:

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)

首先我要感謝大家的貢獻。 我到達這里需要一個算法來生成具有以下詳細信息的整數分區:

將一個數字的分區生成為恰好 k 個部分,但也具有 MINIMUM 和 MAXIMUM 約束。

因此,我修改了“Snakes and Coffee”的代碼以適應這些新需求:

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)]

基於先前具有最大和最小約束的答案,我們可以將其優化得更好一些。 例如,對於 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,)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM