2017-08-25 53 views
0

我有一個對象列表,這些對象的數量是動態的。我需要找到這些對象的所有可能的組合。查找所有可能的組合,並重新選擇允許的項目

我目前在這裏我採取的對象列表的階段,使用下面的代碼沒有repitition返回所有可能的組合:

static void Main(string[] args) 
    { 
     //Say, inputList contains randomObject1,randomObject2 and randomObject3 
     List<List<RandomObject>> AllCombos = ItemCombinations(inputList); 

    } 

    //maxComboCount denotes the maximum number of elements that can be in the combination 
    public static List<List<T>> ItemCombinations<T>(List<T> inputList, int maxComboCount) 
    { 

     int nonEmptyCombinations = (int)Math.Pow(2, inputList.Count) - 1; 

     List<List<T>> listOfCombinations = new List<List<T>>(); 


     for (int i = 1; i <= nonEmptyCombinations; i++) 
     { 
      List<T> thisCombination = new List<T>(); 
      for (int j = 0; j < inputList.Count; j++) 
      { 
       if ((i >> j) % 2 != 0) 
       { 
        thisCombination.Add(inputList[j]); 
       } 
      } 

      if (thisCombination.Count <= maxComboCount) 
      { 
       listOfCombinations.Add(thisCombination); 
      } 
     }      
     return listOfCombinations; 
    } 

我如何獲得所有其他組合,在那裏重複項目,maxComboCount將永遠在那裏,否則我所需的場景可能會陷入無限循環(如果我錯了,請糾正我)。

例如, InputList:{r1,r2,r3}

當前階段:{r1},{r2},{r3},{r1,r2},{r2,r3},{r3,r1},{r1,r2 ,r3}

通緝階段(給定maxComboCount約束= 4):{r1},{r2},{r3},{r1,r1},{r1,r2},{r1,r3},{r2, {r2,r3},{r1,r1,r1},{r1,r1,r2},{r1,r1,r3}等等。 。

有一件事我試過了,

我重複,直到maxBaseCardCount並在每次迭代到另一個tempList加入inputList,我再傳給這個tempList爲滿足在ItemCombinations參數HOD。

 //The loop will be constrained by the maximum number of objects allowed 
     for (int i = 0; i < maxComboCount; i++) 
     { 
      tempList.AddRange(inputList); 
     } 

     List<List<RandomObject>> AllCombos = ItemCombinations(tempList); 

這應該是一個快速和骯髒的解決辦法,並確實給我需要的輸出(有很多重複的值),但我不是很肯定它多少可以打破之前舉行。因此,任何比我的方法更可靠的方法將非常感謝。

編輯

我加入問題的解釋,請讓我知道是否有任何其他的簡化要求

InputList:這是一個對象的列表,從中可以組合成由

ItemCombinations:該函數返回從給定列表中的所有組合,而無需repitition(不是我想要的)

對於inputList = {1,2},ItemCombination返回:空,{1},{2},{1,2},即從長度爲n的任何給定的列表中查閱

所有2^N唯一組合,我希望能夠將這些項目與允許的重複項以及動態組合的長度結合起來。

實施例:

例如InputList:{r1,r2,r3}

ItemCombination函數最初返回:{r1},{r2},{r3},{r1,r2},{r2,r3},{r3,r1},{r1 ,R2,R3}

現在,我要的是,可以作出,如果有多少次沒有限制每個對象可用於

我想要什麼(給出maxComboCount約束的所有組合= 4):{r1},{r2},{r3},{r1,r1},{r1,r2},{r1,r3},{r2,r2},{r2,r3},{r3,r3} {r1,r1,r1},{r1,r1,r2},{r1,r1,r3},{r1,r2,r3}等等......

maxComboCount約束確保no列表大小> 4返回

基本上,我想從n個對象,其中k的範圍可以從1到x(任何數量)

+0

你是指「沒有重複的可能組合」是什麼意思?你是指排列還是別的什麼? – Arjang

+1

你能用簡單的英文寫出你想要的東西嗎?我不想檢查您的代碼,以瞭解您想要實現的目標。 –

+0

「我需要的場景可能會陷入無限循環」這意味着你做錯了什麼,因爲有或沒有重複的元素,組合的數量如果總是有限的(儘管它可以變得非常快)。 – oerkelens

回答

1

你想找到擬定從m項的組合選擇的k個對象的組合n重複項目池。訂單在物品組中無關緊要,因此{1, 2, 2}{2, 2, 1}是等效的;只能添加其中的一個。 (理想情況下,這是項目按升序排列的項目。)

假設您有三個項目的池,並且想創建最多兩個項目的集合。添加空集給你的結果:從

{} + {1} = {1} 
{} + {2} = {2} 
{} + {3} = {3} 

現在創建的兩個項目組:

{} 

與任何項目和項目池迭代組和添加項目創建套一個項目設置一個項目,但僅添加項目時,他們比在每一組中的最後一個,也是最大的項目等於或大於:

{1} -> {1, 1}, {1, 2}, {1, 3} 
{2} -> {2, 2}, {2, 3} 
{3} -> {3, 3} 

現在你有一組T(1)+ T(2)+ T的(3)= 10項:

{} 
{1}, {2}, {3} 
{1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {3, 3} 

(T(n)是,¹/₂第n個三角形數·N·(N + 1))。

我不知道C#,但在僞代碼,你的算法看起來是這樣的:

var m = 3         // max. items 
var pool = {1, 2, 3}      // item pool 
var res = {{}}        // results, 
              // start with empty list 

var k = 0         // starting index of subarray 
              // with one fewer item 

while (m--) {        // loop m times 
    var kk = res.length()     // current result array length 

    for (var i = k; i < kk; i++) { 
     var j0 = 0 

     if (res[i].length() > 0) {   // find index of largest item 
      j0 = pool.index(res[i].last()) // from the set in pool 
     } 

     for (var j = j0; j p in pool {  // add new set 
      res.add(res[i] + {pool[j]}) 
     } 
    } 

    k = kold 
} 

這也可以遞歸實現,在那裏你跟蹤的最後一項指標在各級別,使您不必搜索它做。