简体   繁体   English

计算所有可能的组合

[英]Calculate all possible combinations

Preface 前言

Consider a list, array or string of 12 elements, of an irrelevant value (lets say E). 考虑一个由12个元素组成的列表,数组或字符串,它们是无关的值(比方说E)。 Each element can be linked to, at most, one other adjacent element, or if it is the last element of the list, it can be linked to the first element. 每个元素最多可链接到另一个相邻元素,或者如果它是列表的最后一个元素,则它可以链接到第一个元素。

Examples of valid lists, where dashes indicate links, and "E" represents an element. 有效列表的示例,其中破折号表示链接,“E”表示元素。

E E E E E E E E E E E E 
E E-E E-E E E E-E E-E E
E E E-E E E-E E-E E E E-

An example of an invalid list. 无效列表的示例。

E-E-E E E E E-E E E E E-

Question

I want to calculate the total number of unique lists, and print them. 我想计算唯一列表的总数,然后打印它们。

To approach this problem, what might be the best way to represent the data? 要解决这个问题,什么可能是表示数据的最佳方式?

Would it be best to implement a data structure specific to this problem? 是否最好实现特定于此问题的数据结构?

I am looking to implement this in Java, but if you believe that a different language is better suited, I am open to suggestions. 我希望用Java实现这一点,但如果您认为不同的语言更适合我,我愿意接受建议。

Why 为什么

This is NOT a homework question. 这不是一个功课问题。

The idea is to find every rhythmic pattern in a bar of 12/8 consisting of only single and double groupings of eighth notes, of which can be tied across a barline. 我们的想法是在12/8的条形图中找到每个节奏模式,仅由八分音符的单组和双组组成,其中可以绑在条形线上。

Calculating the number of possibilities here actually has an incredibly neat solution (in my opinion). 在这里计算可能性的数量实际上有一个非常巧妙的解决方案(在我看来)。

Notice that for n notes, the number of possible connections ( C(n) ) if the first note is connected to the second is C(n-2) . 请注意,对于n个音符,如果第一个音符连接到第二个音符,则可能的连接数( C(n) )是C(n-2) Otherwise it is C(n-1) . 否则它是C(n-1) This means that 这意味着

C(n) = C(n-1) + C(n-2)
C(1) = 3 //Either the first and second are connected, 
         //neither are connected, or the end is connected.
C(0) = 2 //Either the end is connected or it isn't

Note: If the last note in a single note example can be connected "to itself" G(0) is 1 Otherwise, it is 0. In addition I am unclear whether EE and E E- are separate, if they aren't then, C(1) is 2 not 3. Note these only apply for sequences of 0 or 1 on their own you'd have to have an if statement outside of the actual function C(n) to return 1 instead of 2. Otherwise it screws up the whole recurrence. 注意:如果单个音符示例中的最后一个音符可以“连接到它自己”G(0)是1,否则,它是0.此外,我不清楚EEE E-是否是分开的,如果它们不是那么, C(1)是2不3.注意这些只适用于对自己的 0或1的序列,你得有一个如果实际的功能之外声明C(n)返回1而不是2。否则它拧紧整个复发。 A bit messy, but that's the nature of real world data in algorithms 有点乱,但这是算法中真实世界数据的本质

This means you've basically got a variant on the fibonacci series! 这意味着你基本上有一个斐波那契系列的变种! Cool right? 好吧?

Data Representation 数据表示

I would have a list of n boolean s. 我会有一个n boolean的列表。 An array would work fine. 数组可以正常工作。 If 2 notes are connected, then that entry in the array should be true . 如果连接了2个音符,则数组中的该条目应为true I would have index 0 be the connection be the first and second notes, and index n-1 be whether or not the last note is connected to anything. 我将索引0作为第一和第二个音符的连接,索引n-1是最后一个音符是否连接到任何东西。

Permutation Generation 排列生成

The way in which we calculate the total number of possibilities lends itself nicely to a generation method ( G(n) ). 我们计算可能性总数的方式非常适合于生成方法( G(n) )。 For n we need to tack on EE to G(n-2) and E to G(n-1) . 对于n,我们需要将EE加到G(n-2)EG(n-1)

At the base of this recurrence we have: 在这种复发的基础上,我们有:

G(0) = {E, E-} 
G(1) = {E-E, E E, E E-}

If you let the inter-element spaces be your main data, then you have 12 "spaces" (the last one is the one after the end that ties to the first one). 如果你让元素间空间成为你的主要数据,那么你就有12个“空格”(最后一个是与第一个空格相关的结尾)。

Each "space" can either be blank or have a link. 每个“空间”可以是空白的或具有链接。 So you could represent it as 0 or 1. So there are at most 2^12 possibilities. 所以你可以把它表示为0或1.所以最多有2 ^ 12种可能性。 That's a fairly small number (4096), so you could just generate all of them, then weed out the ones with adjacent 1s. 这是一个相当小的数字(4096),所以你可以生成所有这些,然后清除那些相邻的1。

I think the total number of variants is 466. 我认为变种的总数是466。

One can compute the number as follows: 可以按如下方式计算数字:

If we assume that a EE link is marked as a Y then for example the total number of arrangement s in which only two E's are linked out of 12 is equal to the number of arrangements with repetitions of 2 items when the first is taken to be repeated 10 times and the second is taken to be repeated only one time. 如果我们假设EE链接被标记为Y,那么例如在12中仅有两个E被连接的排列的总数等于当第一个被认为是重复2个项目时的排列的数量。重复10次,第二次重复一次。 Basically this amounts to the following list: 基本上这相当于以下列表:

Y E E E E E E E E E E 
E Y E E E E E E E E E
E E Y E E E E E E E E
..
E E E E E E E E E E Y

This is basically the same as computing multinomial(10, 1) which is 11 ( http://www.wolframalpha.com/input/?i=multinomial%2810%2C+1%29 ) 这与计算多项式(10,1)基本相同,即11( http://www.wolframalpha.com/input/?i=multinomial%2810%2C+1%29

the total number is the following sum: 总数是以下总和:

multinomial(12) + // there is no E-E link at all 
multinomial(12 - 2, 1) + // only one E-E link 
multinomial(12 - 4, 2) + // two E-E links
...
multinomial(12 - 12, 6)  // 6 E-E links

this is 233 ( http://www.wolframalpha.com/input/?i=multinomial%2812%29+%2B+multinomial%2810%2C+1%29+%2B+multinomial%288%2C+2%29+%2B+multinomial%286%2C+3%29+%2B+multinomial%284%2C+4%29+%2B+multinomial%282%2C+5%29+%2B+multinomial%286%29 ) 这是233( http://www.wolframalpha.com/input/?i=multinomial%2812%29+%2B+multinomial%2810%2C+1%29+%2B+multinomial%288%2C+2%29 +%2B +多项%%286%2C + 3%29 +%2B +多项%%284%2C + 4%29 +%2B +多项%282%2C + 5%29 +%2B +多项%286%29

normally i would just multiply this by 2 to account for the end link possibility but this can actually create a chain of three EEE in some cases which will break your problem. 通常情况下我会将此乘以2以说明结束链接的可能性,但实际上这可能会在某些情况下创建三个EEE链,这将打破您的问题。

On the other side there are available algorithms to generate all the multinomial combinations for some parameters and since all of them are like 200 it's easy to generate all and just check which ones can be extended with the circular link at then end. 另一方面,有一些算法可以为某些参数生成所有多项式组合,并且由于它们都是200,因此很容易生成所有这些,只需检查哪些可以通过当时的循环链接进行扩展。

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

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