2013-06-04 62 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

我承認我也不完全明白這個問題。然而,解決這種問題的一般方法是提出一個好的符號和規範的形式,以及將某些東西轉換成規範形式的算法。然後,你只需在規範形式上做重量級的事情,不要重複。