快速的Python算法,找出一个数字列表的所有可能分区,并且这些子集总和等于给定的比率。

5

假设我有一个0到9之间随机整数的列表。 我想将该列表划分为N个子集,使得子集之和的比率等于给定的值,并且我想找出所有可能的划分。 我编写了以下代码,并使其在N = 2的情况下工作。

import random
import itertools

#lst = [random.randrange(10) for _ in range(20)]
lst = [2, 0, 1, 7, 2, 4, 9, 7, 6, 0, 5, 4, 7, 4, 5, 0, 4, 5, 2, 3]

def partition_sum_with_ratio(numbers, ratios):
    target1 = round(int(sum(numbers) * ratios[0] / (ratios[0] + ratios[1])))
    target2 = sum(numbers) - target1
    p1 = [seq for i in range(len(numbers), 0, -1) for seq in
          itertools.combinations(numbers, i) if sum(seq) == target1
          and sum([s for s in numbers if s not in seq]) == target2]

    p2 = [tuple(n for n in numbers if n not in seq) for seq in p1]

    return list(zip(p1, p2))

partitions = partition_sum_with_ratios(lst, ratios=[4, 3])
print(partitions[0])

输出:

((2, 0, 1, 2, 4, 6, 0, 5, 4, 4, 5, 0, 4, 5, 2), (7, 9, 7, 7, 3))

如果您计算每个子集的和,您会发现比例是44:33 = 4:3,正好是输入值。然而,我希望该函数适用于任意数量的子集。例如,我希望...
partition_sum_with_ratio(lst, ratios=[4, 3, 3])

返回类似于以下内容的结果

((2, 0, 1, 2, 4, 6, 0, 5, 4, 4, 3), (5, 0, 4, 5, 2, 7), (9, 7, 7))

我已经思考这个问题一个月了,发现这个问题非常难。我的结论是这个问题只能通过递归来解决。我想知道是否有任何相对快速的算法可用于此。有什么建议吗?


什么是比率?您能详细解释一下定义吗? - Rafael Valero
ratio = [4, 3] 表示两个子集的和的比例为 4:3。 - Shaun Han
三维空间是如何工作的? - Rafael Valero
你能重复元素吗? - Rafael Valero
1个回答

3

是的,需要使用递归。基本逻辑是将一部分和其余部分进行二分,并在所有可能的情况下对其余部分进行递归划分。我遵循了您提供的思路,假设一切都是可区分的,这会产生很多可能性,可能太多了无法枚举。尽管如此:

import itertools


def totals_from_ratios(sum_numbers, ratios):
    sum_ratios = sum(ratios)
    totals = [(sum_numbers * ratio) // sum_ratios for ratio in ratios]
    residues = [(sum_numbers * ratio) % sum_ratios for ratio in ratios]
    for i in sorted(
        range(len(ratios)), key=lambda i: residues[i] * ratios[i], reverse=True
    )[: sum_numbers - sum(totals)]:
        totals[i] += 1
    return totals


def bipartitions(numbers, total):
    n = len(numbers)
    for k in range(n + 1):
        for combo in itertools.combinations(range(n), k):
            if sum(numbers[i] for i in combo) == total:
                set_combo = set(combo)
                yield sorted(numbers[i] for i in combo), sorted(
                    numbers[i] for i in range(n) if i not in set_combo
                )


def partitions_into_totals(numbers, totals):
    assert totals
    if len(totals) == 1:
        yield [numbers]
    else:
        for first, remaining_numbers in bipartitions(numbers, totals[0]):
            for rest in partitions_into_totals(remaining_numbers, totals[1:]):
                yield [first] + rest


def partitions_into_ratios(numbers, ratios):
    totals = totals_from_ratios(sum(numbers), ratios)
    yield from partitions_into_totals(numbers, totals)


lst = [2, 0, 1, 7, 2, 4, 9, 7, 6, 0, 5, 4, 7, 4, 5, 0, 4, 5, 2, 3]
for part in partitions_into_ratios(lst, [4, 3, 3]):
    print(part)

谢谢。当列表很小时,您的代码完美运行。问题是当我在大型列表上使用代码(len(lst) > 50)时,它需要很长时间。我理解在这种情况下枚举是不可能的。有没有办法随机抽样一定数量的分区,使子集和等于比率?有重复分区也可以,但这种情况非常罕见。 - Shaun Han
1
@ShaunHan 可能值得再提一个问题,因为这段代码不适合随机抽样。我的第一反应是洗牌数字,使用近似比例形成贪婪分区,然后进行局部搜索移动以使比例更接近真实值。 - David Eisenstat
是的,贪婪算法就可以了!我在这里开了另一个问题:https://stackoverflow.com/questions/67939449/fast-python-algorithm-for-random-partitioning-with-subset-sums-equal-or-close-to 如果你有贪婪算法的想法,可以在那里回答。 - Shaun Han

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接