分区中可能的组合数

发布于 2024-07-14 03:52:30 字数 298 浏览 7 评论 0原文

给定一个大小为 n 的集合 S,它被划分为大小为 n1,...,nk 的类 (s1,..,sk)。 自然,n=n1+...+nk。

我有兴趣找出可以组合此分区的元素的方法数量,以便每个组合恰好包含每个类的一个元素。

由于我可以从 s1 中选择 n1 个元素,从 s2 中选择 n2 个元素,依此类推,我正在寻找任意 n1,..nk 的 max(n1*..*nk) 的解决方案,其中它保持 n1+..+nk =n。

我感觉这是一个线性优化问题,但自从我本科生学习这些东西以来已经太久了。 我希望有人记得如何计算这个。

Given is a set S of size n, which is partitioned into classes (s1,..,sk) of sizes n1,..,nk. Naturally, it holds that n = n1+...+nk.

I am interested in finding out the number of ways in which I can combine elements of this partitioning so that each combination contains exactly one element of each class.

Since I can choose n1 elements from s1, n2 elements from s2 and so on, I am looking for the solution to max(n1*..*nk) for arbitrary n1,..nk for which it holds that n1+..+nk=n.

I have the feeling that this is a linear-optimization problem, but it's been too long since I learned this stuff as an undergrad. I hope that somebody remembers how to compute this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹梦到西洲 2024-07-21 03:52:30

您正在寻找每个分区中一个元素的组合数量?

这只是 n1*n2*...*nk。

编辑:
您似乎还提出了一个单独的问题:

给定 N,我如何分配 n1,n2,...,nk 以使它们的乘积最大化。 这实际上不是一个线性优化问题,因为您的变量相乘。

它可以通过一些微积分来解决,即通过在约束下使用拉格朗日乘子对每个变量进行偏导数。

结果是 n1 .. nk 应尽可能接近相同的大小。

if n is a multiple of k, then n_1 = n_2 = ... = n_k = n/k

otherwise, n_1 = n_2 = ... = n_j = Ceiling[n/k]
      and  n_j+1 = ... = n_k = floor[n/k]

基本上,我们尝试将元素尽可能均匀地分布到分区中。 如果它们均匀分布,那就太好了。 如果没有,我们尽可能均匀地划分,并且对于剩下的任何内容,我们为第一个分区每个提供一个额外的元素。 (不一定是第一个分区,这个选择是相当任意的。)这样,任何两个分区拥有的元素数量最多相差一个。

详细信息

这是我们希望最大化的乘积函数:

P = n1*n2*...nK

我们使用拉格朗日乘子定义一个新函数:

Lambda = P + l(N - n1 - n2 ... -nk)

并在每个 k n_i 变量中取偏导数:

dLambda/dn_i = P/n_i - l

和在 l 中:

dLambda/dl = N - n1 -n2 ... -nk

设置所有偏导数 = 0,我们得到一个 k + 1 个方程组,当我们求解它们时,我们会得到 n1 = n2 = ... = nk

一些有用的链接:

拉格朗日乘数

优化

You're looking for the number of combinations with one element from each partition?

That's simply n1*n2*...*nk.

Edit:
You seem to also be asking a separate question:

Given N, how do I assign n1, n2, ..., nk such that their product is maximized. This is not actually a linear optimization problem, since your variables are multiplied together.

It can be solved by some calculus, i.e. by taking partial dervatives in each of the variables, with the constraint, using Lagrange multipliers.

The result will be that the n1 .. nk should be as close to the same size as possible.

if n is a multiple of k, then n_1 = n_2 = ... = n_k = n/k

otherwise, n_1 = n_2 = ... = n_j = Ceiling[n/k]
      and  n_j+1 = ... = n_k = floor[n/k]

Basically, we try to distribute the elements as evenly as possible into partitions. If they divide evenly, great. If not, we divide as evenly as possible, and with whatever is left over, we give an extra element each to the first partitions. (Doesn't have to be the first partitions, that choice is fairly arbitrary.) In this way, the difference in the number of elements owned by any two partitions will be at most one.

Gory Details:

This is the product function which we wish to maximize:

P = n1*n2*...nK

We define a new function using Lagrange multipliers:

Lambda = P + l(N - n1 - n2 ... -nk)

And take Partial derivatives in each of the k n_i variables:

dLambda/dn_i = P/n_i - l

and in l:

dLambda/dl = N - n1 -n2 ... -nk

setting all of the partial derivatives = 0, we get a system of k + 1 equations, and when we solve them, we'll get that n1 = n2 = ... = nk

Some useful links:

Lagrange Multipliers

Optimization

孤寂小茶 2024-07-21 03:52:30
floor(n/k)^(k - n mod k)*ceil(n/k)^(n mod k)

——马库斯

Q 对于您给出的 S = {1,2,3,4}, n = 4, k = 2 的示例,这给出:

floor(4/2)^(2 - 4 mod 2)*ceil(4/2)^(4 mod 2)
floor(2)^(2 - 0)*ceil(2)^(0)
2^2 * 2^0
4 * 1
4

...如您所愿。

为了澄清起见,该公式给出了通过最大可能排列数的划分生成的排列数。 当然还会有其他不太理想的分区。

对于给定的周长,面积最大的矩形是最接近正方形的矩形(在更高的维度中也是如此),这意味着您希望边的长度尽可能接近相等(例如,所有边都可以)平均长度向上或向下舍入)。 那么公式可以看出:

   (length of short sides)^(number of short sides)
times
   (length of long sides)^(number of long sides)

这就是满足这个约束的超矩形的体积。

请注意,以这种方式查看时,它还告诉您如何构建最大分区。

floor(n/k)^(k - n mod k)*ceil(n/k)^(n mod k)

-- MarkusQ

P.S. For the example you gave of S = {1,2,3,4}, n = 4, k = 2 this gives:

floor(4/2)^(2 - 4 mod 2)*ceil(4/2)^(4 mod 2)
floor(2)^(2 - 0)*ceil(2)^(0)
2^2 * 2^0
4 * 1
4

...as you wanted.

To clarify, this formula gives the number of permutations generated by the partitioning with the maximum possible number of permutations. There will of course be other, less optimal partitionings.

For a given perimeter the rectangle with the largest area is the one that is closest to a square (and the same is true in higher dimensions) which means you want the sides to be as close to equal in length as possible (e.g. all either the average length rounded up or down). The formula can then be seen to be:

   (length of short sides)^(number of short sides)
times
   (length of long sides)^(number of long sides)

which is just the volume of the hyper-rectangle meeting this constraint.

Note that, when viewed this way, it also tells you how to construct a maximal partitioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文