独特的排列,没有镜像或循环重复
一些背景:我正在编写一个或多或少的强力搜索算法来解决我遇到的问题。 为了做到这一点,我需要生成并评估所有可能性,以找出最好的。 由于评估实际上需要一些时间,我宁愿生成尽可能少的完全覆盖我的搜索空间的解决方案。 此外,我可以执行此操作的元素越多越好。 对于任何数字 K,通常都有 K! 排列,对于大于 10 的数字来说,生成所有排列将很困难。
真正的问题:搜索空间应该包含两个元素的所有排列(N 乘以 el1 和 M 乘以 el2,其中 K=M+N),但有这些限制:
- 它们必须是唯一的(即我只想要 [aabbb] 一次
- )不需要任何排列的相反(即如果我有 [aab],我也不需要 [baa])
- 我认为排列是循环的,所以 [aab] = [aba] = [baa]
如果我如果能够做到这一点,可能性的数量就会大大减少。 由于 K 理想情况下会很大,因此首先生成所有排列然后根据这些标准过滤它们是不可行的。 我已经完成了第一个限制(见下文),它将 Matlab 正常排列函数(perms)的数量从 2^K 减少到 K!/N!M!,这是一个巨大的胜利。 第二个限制只会将可能性的数量减少一半(在最好的情况下),但我认为第三个限制也应该能够真正减少可能性的数量。
如果有人知道如何做到这一点,最好还知道如何计算有多少种可能性,那将对我有很大帮助! 我想要一个解释,但代码也很好(我可以阅读类 C 语言、Java(Script)、Python、Ruby、Lisp/Scheme)。
对于感兴趣的人:这是我迄今为止仅获取唯一排列的算法:
function genPossibilities(n, m, e1, e2)
if n == 0
return array of m e2's
else
possibilities = genPossibilities(n-1, m, e1, e2)
for every possibility:
gain = number of new possibilities we'll get for this smaller possibility*
for i in max(0,(m+n-gain))
if possibility(i) is not e1
add possiblity with e1 inserted in position i
return new possibilities
- 如果您拥有 N-1 和 M 的所有排列,那么您可以通过将 e1 插入其中来使用它们来查找 N 和 M 的排列。 但你不能随处插入,因为那样你会得到重复项。 我不知道为什么会这样,但你可以计算从旧可能性中产生的新可能性的数量(我称之为“增益”)。 对于第一个旧排列,该数字从 M+1 开始,对于每个旧排列减少 1,直到它变为零,此时它返回到 M,依此类推(仅当 M>=N 时才有效)。 因此,如果您想计算 N=3 和 M=3 的排列,并且您有 N=2 和 M=3 的 10 个排列,那么它们的增益将为 [4 3 2 1 3 2 1 2 1 1]。 从排列的长度中减去这个增益,您就可以得到可以开始插入新元素而无需重复的索引。
Some background: I'm writing a more or less brute force search algorithm for solving a problem that I have. In order to do this, I need to generate and evaluate all possibilities to find out which is best. Since the evaluation actually takes some time I would prefer to generate as little as possible solutions that completely cover my search space. Furthermore, the more elements I can do this for, the better. For any number K there are normally K! permutations, and generating them all will be hard for numbers higher than ~10.
Real problem: The search space should contain all permutations of two elements (N times el1 and M times el2, where K=M+N), with these restrictions:
- they have to be unique (i.e. I only want [a a b b b] once)
- I don't need the reverse of any permutation (i.e. if I have [a a b], I don't also need [b a a])
- I consider the permutations to be circular, so [a a b] = [a b a] = [b a a]
If I would be able to do this, the number of possibilities would be decreased drastically. Since K will ideally be large, it is not feasible to first generate all permutations and then filter them according to these criteria. I have already done the first restriction (see below) and it cut back the number from 2^K for Matlab's normal permutations function (perms) to K!/N!M!, which is a huge win. The second restriction will only cut the number of possiblities in half (in the best case), but I think the third should also be able to really cut down the number of possibilities.
If anyone knows how to do it, and preferably also how to calculate how many possibilities there will be, that would help me a lot! I would prefer an explanation, but code is also fine (I can read C-like languages, Java(Script), Python, Ruby, Lisp/Scheme).
For the interested: Here is the algorithm for getting only unique permutations that I have so far:
function genPossibilities(n, m, e1, e2)
if n == 0
return array of m e2's
else
possibilities = genPossibilities(n-1, m, e1, e2)
for every possibility:
gain = number of new possibilities we'll get for this smaller possibility*
for i in max(0,(m+n-gain))
if possibility(i) is not e1
add possiblity with e1 inserted in position i
return new possibilities
- If you have all permutations for N-1 and M, then you can use them to find the permutations for N and M by inserting e1 into them. You can't just insert everywhere though, because then you'll get duplicates. I don't know why this works, but you can calculate the number of new possibilities that you'll generate from an old one (I call this 'gain'). This number starts at M+1 for the first old permutation and decreases by one for each old permutation until it would become zero, at which point it goes back to M, etc. (only works if M>=N). So if you want to calculate the permutations for N=3 and M=3 and you have the 10 permutations for N=2 and M=3, their gains will be [4 3 2 1 3 2 1 2 1 1]. Subtract this gain from the length of the permutation and you get the index at which you can start inserting new elements without making duplicates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您所追求的是 2 元手链的子集(该子集由字符 A 的 n 和字符 B 的 m 定义)。 这套所有手镯允许 A 和 B 的数量发生变化。
以下代码打印出您要查找的序列,并按词汇顺序和恒定的摊销时间执行此操作。 它基于 Sawada 的这篇论文中的通用算法 -有关其工作原理的说明,请参阅该论文。
What you are after is a subset of 2-ary bracelets (the subset is defined by exactly n of character A and m of character B). The set of all bracelets allows for the number of A's and B's to vary.
The following code prints out the sequences you are after, and does so in lexical order and in constant amortised time. It is based on the general algorithm in this paper by Sawada - for an explanation of how it works, see that paper.
我想你想生成 2 元免费项链。 请参阅此问题获取链接、论文和一些代码。
I think you want to generate 2-ary free necklaces. See this question for link, papers, and some code.
您正在寻找与顺序无关的组合。 Matlab 使用 K!/N!M! 正确计算了这一点 这正是计算组合数的公式。
You are looking for combinations - which are order independent. Matlab calculated this correctly with K!/N!M! which is precisely the formula for calculating the number of combinations.
假设您有一个包含所有排列的数组,您可以将数组的内容放入哈希中。 然后这就会起作用(有点暴力,但它是一个开始):
Assuming you have an array of all permutations, you can put the contents of the array into a hash. Then this will work (a little brute force, but its a start):
如果只有两个元素,则空间要小得多:2^k 而不是 k!。
尝试这样的方法:
如果您有 j 个可能的符号,而不仅仅是两个,请执行相同的操作,但使用基数 j 而不是基数 2。
If you have only two elements, your space is much smaller: 2^k rather than k!.
Try an approach like this:
If you have j possible symbols, rather than just two, do the same thing but use base j rather than base 2.
我对 k 进制情况有一个想法,如下:
参考文献:
输出应该是
I got an idea for the k-ary case as follows:
References:
The output should be