简体   繁体   English

计算分区数量的算法?

[英]Algorithm to count the number of partitionings?

Recently, I ancountered the problem to evenly disperse different width-sized, small containers into huge width-sized containers horizontally.最近,我遇到了将不同宽度尺寸的小容器水平均匀分散到巨大宽度尺寸的容器中的问题。 There are millions of huge containers with billions of small containers.有数百万个巨大的容器,有数十亿个小容器。 I need to come up with an algorithm.我需要想出一个算法。 I simplified the problem into the below question:我将问题简化为以下问题:

Let's use Process(Number,Parts) as an example:我们以Process(Number,Parts)为例:

Number 4 can be split into 2 parts in 3 ways (not including 0).数字4可以通过 3 种方式(不包括 0)分成2部分。 Process(4,2)=3 : Process(4,2)=3

1 + 3
3 + 1
2 + 2

Likewise, number 4 can be split into 3 parts in 2 ways Process(4,3)=2 :同样,数字4可以通过 2 种方式分成3部分Process(4,3)=2

1 + 1 + 2
2 + 1 + 1
1 + 2 + 1

And obviously Process(4,4)=1显然Process(4,4)=1

(not including Process(4,1)=1 , because it is 4+0 , where 0 shouldn't be taken into consideration) (不包括Process(4,1)=1 ,因为它是4+0 ,其中0不应该被考虑)

I wonder whether there is any way to calculate我想知道有没有办法计算

SuperProcess(4)=Process(4,2)+Process(4,3)+Process(4,4)=7

with less time complexity?时间复杂度更小? Or with another word, faster!!或者换句话说,更快!

Especially when the request is to calculate: SuperProcess(1209)尤其是请求计算的时候: SuperProcess(1209)

Is there some mathematical method rather than a crude loop to perform this calculation?是否有一些数学方法而不是粗略的循环来执行此计算?

SuperProcess(n) is known as the number of compositions of an integer rather than the number of partitions in which sums containing the same addends are considered identical independent of ordering. SuperProcess(n)被称为 整数的组合数,而不是分区数,其中包含相同加数的和被认为是相同的,与排序无关。

There are exactly 2**(n-1)-1 compositions for a positive integer n excluding the sum with only one addend.正整数n正好有2**(n-1)-1组合,不包括只有一个加数的总和。

Therefore the best algorithm to calculate SuperProcess(n) is simply to evaluate the expression 2**(n-1)-1 , which can be done in Theta(n) time.因此,计算SuperProcess(n)的最佳算法是简单地计算表达式2**(n-1)-1 ,这可以在Theta(n)时间内完成。

If you want to enumerate all combinations, this can be done with a recursive function taking every value 1...n for the integer m in the current position in the sum and then recursively calling itself with nm for the next position, stopping on 0 argument.如果你想枚举所有的组合,这可以通过递归函数来完成,它对和中当前位置的整数m取每个值1...n ,然后用nm递归调用自身作为下一个位置,停止在0争论。

The enumeration algorithm will take Theta(n 2**n) time and this is optimal, because it is the time required to save/print all the combinations explicitly.枚举算法将花费Theta(n 2**n)时间,这是最佳的,因为这是显式保存/打印所有组合所需的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM