给定大小的非相邻集的数量
如果给定集合L={1,2,3,...,N}
和一个整数k
,是否可以有效地计算“的数量”大小为 k 的非相邻子集?如果对于 S
中的每个 x
,既不是 x-1
也不是 ,则子集
位于 S
是不相邻的x+1S
中。
例如,对于 L={1,2,3,4}
和 k=2
,答案是 3,因为我们有 {1,3},{1,4},{2,4}
。对于k=3
,答案是零。
一种方法是生成所有大小为 2 的非相邻子集,然后尝试所有可能的并集(因为非相邻集具有其所有子集都不相邻的属性),但这让我觉得非常浪费,并且也许有一个甜蜜优雅有效的解决方案。
If you are given the set L={1,2,3,...,N}
and an integer k
, is it possible to efficiently calculate the number of "non-adjacent" subsets of size k
? A subset S
is non-adjacent if for each x
in S
, neither x-1
nor x+1
are in S
.
As an example, for L={1,2,3,4}
and k=2
the answer is 3, because we have{1,3},{1,4},{2,4}
. For k=3
the answer is zero.
One way to go would be to generate all size 2 non-adjacent subsets, then trying all possible unions (since a non-adjacent set has the property that all its subsets are non-adjacent), but that strikes me as very wasteful, and probably there is a sweet elegant efficient solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以这种方式思考:如果您知道集合
L'={1, 2, 3, ..., N - 1}
的答案是什么,您可以使用该信息来构建集合L
的答案?这个想法是,当您将
N
添加到L'
时,新的解决方案由L'
可用的所有子集加上 1 个新子集组成对于L'
中小于或等于N - 2*(k - 1)
的每个元素,因此如果L'
的解> 的大小为V'
,那么V
L
的解为V = V' + (N - 2*(k - 1))
如果您再进一步计算,您会发现该解可以表示为前
N - 2k + 2
个自然整数的和。对于小于或等于
N - 2*(k - 1)
部分,添加的新数字N
只会添加到最终数字小于或等于的子集该表达式的结果,因为正在构建的新子集中必须有k
个元素(包括数字N
本身,因此有k - 1
代码> 需要更多)彼此之间至少用 2 个数字分隔,其中与数字N
的距离为2*(k - 1)
,表达式如此。Think of it in this way: if you knew what the answer is for the set
L'={1, 2, 3, ..., N - 1}
, could you use that information to build the answer for the setL
?The idea is that when you add
N
toL'
, the new solution is made of all the subsets available forL'
plus 1 new subset for each of the elements ofL'
that are less or equal thanN - 2*(k - 1)
, so if solution forL'
had a size ofV'
, thenV
the solution forL
will beV = V' + (N - 2*(k - 1))
If you work it out a bit more, you'll find that the solution can be expressed as the sum of the first
N - 2k + 2
natural integers.About the less or equal than
N - 2*(k - 1)
part, the new numberN
being added will only add to subsets whose final number is less or equal than the result of that expression, because there must bek
elements in the new subset being built (including the numberN
itself, so there arek - 1
more needed) separated each other by a minimum of 2 numbers each, which makes a distance of2*(k - 1)
from the numberN
, and so the expression.这可能与 Win32 的解决方案相同,但我不确定。所以我单独发一下。
取 S(n) 为大小为 n 的连续序列的非相邻子集的数量(即,S(n) 是您正在寻找的解决方案)。
让我们计算 S(n+1),即向序列添加元素时的值。当我们添加元素 k 时,我们会增加该序列的非相邻子集的数量。我们可以将这些新子集分为以下几类。
因此,当我们将 k 添加到序列中时,新的非相邻子集的数量为 1 + S(n-1)。换句话说,1 + S(n-1) = S(n+1) - S(n)。我们可以重新整理这个公式,得到 S(n+1) = 1 + S(n-1) + S(n)。
递归解决方案不是很有帮助,因此我们可以尝试对其进行概括。我不擅长这一步,但是Wolfram|Alpha 为,我们发现通式等于以下。
以下是一些示例数据点。
This may be along the same lines as Win32's solution, but I wasn't sure. So I am posting it separately.
Take S(n) to be the number of non-adjacent subsets of your consecutive sequence of size n (i.e., S(n) is the solution you are looking for).
Let us calculate S(n+1), the value when we add an element to the sequence. When we add an element k, we increase the number of non-adjacent subsets of that sequence. We can break down these new subsets into the following categories.
So the number of new non-adjacent subsets when we add k to our sequence is 1 + S(n-1). In other words, 1 + S(n-1) = S(n+1) - S(n). We can rearrange this formula to get S(n+1) = 1 + S(n-1) + S(n).
Recursive solutions are not very helpful, so we can attempt to generalize it. I am not good at this step, but Wolfram|Alpha is, and we find the general formula is equal to the following.
Here are some sample data points.
用
S(m,n)
表示{1,...,n}
中大小为m
的非相邻子集的数量。那么以下内容成立:因此,可以通过添加边界条件
(其中
I()
是指示函数),在O(Nk)
中通过 DP 来解决它。Denote by
S(m,n)
the number of non-adjacent subsets of sizem
in{1,...,n}
. Then the following holds:So one can solve it by DP in
O(Nk)
, by adding the boundary conditionswhere
I()
is the indicator function.让:
显然,< code>S(n-1,k) 是大小为
k
的不相邻子集的集合,并且它是S(n,k)
的子集>。另外,
S(n-2,k-1)
是一组大小为k-1
的不相邻子集,并且这些子集都不包含n -1
。因此,我们可以安全地将{n}
添加到每个子集中,以获得大小为k
的子集。并且由于它们不包含n-1
(与n
唯一相邻的元素),因此它们也是不相邻的。因此:
使用一些实数,让我们尝试求解
S(6,3)
。这符合你的答案。
现在,计算这个数字:
然后:
我还没有确定封闭形式,但这里有一些计算值:
Let:
Clearly,
S(n-1,k)
is a set of non-adjacent subsets of sizek
and it is a subset ofS(n,k)
.Also,
S(n-2,k-1)
is a set of non-adjacent subsets of sizek-1
, and none of these subsets includen-1
. Thus, we can safely add{n}
to each of these subsets to get subsets of sizek
. And since they do not includen-1
(the only adjacent element ton
), they are also non-adjacent.Thus:
Using some real numbers, lets try to solve for
S(6,3)
.Which matches your answer.
Now, to compute the number:
Then:
I haven't yet determined the closed form, but here are some computed values: