2013-06-04 68 views
2

你好,说我有一些数据A -> ....(n个数据点)。现在我从这些数据中获取给定数量的值(m) - 现在我希望遍历这些值的所有独特组合。如何“遍历”该集合

5个值的例子,你取2个唯一的: 一个独特的组合将是类似于“a + b”或“a + c”的东西 - 但是“c + d”与“b + c ”。和“B + E”是相同的“A + d”

A x x x x 
B x  x x 
C x  x 
D  x  x 
E  x  

那些描述一些几何“线”,并且整个标本可“镜像”围在中间点。因此,对于任意数量的行,是否有一个巧妙的算法来重复考虑这种“镜像能力”的所有事情?

什么是一个公式来计算给定的大小n和项目数m的元素数量?

----“3 out 6”的示例:
它也类似于函数combine(6,3) - 但现在我用 - 代替x标记了重复行。

    1 1 1 1 1 1 1 1 1 1 2 
    1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 
A x x x x x x x x - -      A 
B x x x x    x x - - - -   B 
C x  x x x  x x -  - - - C 
D x  x  x - x  - - - - - D 
E  x  x x - x - - - - - E 
F  x  x - -  - - - - - - - F 

所以可能列表是:

ABC,ABD,ABE,ABF,ACD,ACE,ACF,ADE,BCD,BCE

10出20名忽略symetry的潜在候选人。

+0

为什么B + E与A + D相同? –

+0

@OliCharlesworth在中间翻转B + E(在本例中为“C”,偶数为两行)。 B转换为D和E转换为A. – paul23

+0

哦,我明白了。那么在这种情况下,绘制一个NxN网格,并在所有坐标中放入一个对应于您想要生成的组合的坐标。该模式应该在这一点上显而易见。 –

回答

1

不容易。实际上这很难。但这里是破败:

for_each_unmirrored() 
    mark first element as part of the set. 
    mark last element as definitely NOT part of the set. 
    //we know no matter whats inside, this isnt't semetrical, so all combinations 
    for_each_mirrored() on all elements between these. 

    mark first element as part of the set. 
    mark last element as part of the set. 
    //ends are semetrical, so insides cannot be 
    for_each_unmirrored() on all elements between these 

    mark first element as definitely not part of the set. 
    mark last element as definitely not in the set. 
    //ends are semetrical, so insides cannot be 
    for_each_unmirrored() on all elements between these 

for_each_mirrored() 
    for each element 
     mark it as part of the set. 
     if more elements are needed 
      for_each_mirrored on elements to the right but in range 

是的,即使是abriviated伪代码是复杂的。真正的代码在这里:http://ideone.com/WDEn40(包括显示它适用于6个元素选择3)。链接的代码并不是那么简单,因为我优化了它以避免分配和最小化函数调用。 (作为副作用,for_each_call_helper不是线程安全的,如果需要线程安全,只需从该函数中的变量中删除static关键字。)

0

我承认我也不完全明白这个问题。然而,解决这种问题的一般方法是提出一个好的符号和规范的形式,以及将某些东西转换成规范形式的算法。然后,你只需在规范形式上做重量级的事情,不要重复。