生成来自多个列表的所有可能组合

huangapple go评论86阅读模式
英文:

generate ALL possible combinations from a number of lists

问题

public List<List<Seance>> allUniqueCombinations1(List<List<Seance>> dataStructure) throws SQLException {
    int n = dataStructure.size();
    long solutions = 1;
    for (List<Seance> vector : dataStructure) {
        solutions *= vector.size();
        if (solutions > 1000000000) break;
    }
    List<List<Seance>> allCombinations = new LinkedList();
   
    for (int i = 0; i < solutions; i++) {
        List<Seance> combination = new LinkedList<>();
        long j = 1;
        List<List<Seance>> data = new LinkedList<>();
        data = dataStructure;
        int u = 0;
        for (int a = 0; a < data.size(); a++) {
            List<Seance> vec = data.get(a);
            combination.add(vec.get((int) ((i / j) % vec.size())));
            j *= vec.size();
            data = remouve_conflicts(data, combination.get(u), u);
            u++;
        }
        allCombinations.add(combination);
    }
    return allCombinations;
}

Please note that the provided code seems to contain some syntax errors, and the method remouve_conflicts is referenced but not provided in the provided code snippet. Additionally, there is a possible logic error in the use of the data list inside the loop. It seems that you're modifying the data list inside the loop, which might lead to unintended behavior. Please make sure to review and modify the code as needed to ensure it works correctly for your use case.

英文:

Salam,
I want to get list of combinations that represents every unique combination of all values in my list of lists. for example, have a list that containes a list of Sessions and i want a combination from each list of sessions:

My List of lists 
     {   List of Sessions1[S1,S2,S3]
         List of Sessions2[S4,S5]
         List of Sessions3[S6,S7]
     }

the List i get is

   List result
              {
               List combination 1[S1,S4,S6]
              List combination 2[S1,S4,S7]
           List combination 3[S1,S5,S6]
    .
    .
    .
}

I have a method and it works but the problem is with the variable j cause my lists are too big so when it passes 9223372036854775807 (2^63-1) it starts giving negative values and it ruins the progress
here's my code

 public List&lt;List&lt;Seance&gt;&gt; allUniqueCombinations1(List&lt;List&lt;Seance&gt;&gt; dataStructure) throws SQLException {
        int n = dataStructure.size();
        long solutions = 1;
        for (List vector : dataStructure) {
            solutions *= vector.size(); if(solutions&gt;1000000000) break;//this condition is for the same problem
        }
        List&lt;List&lt;Seance&gt;&gt; allCombinations = new LinkedList();     
       
        for (int i = 0; i &lt; solutions; i++) {
         List&lt;List&lt;List&lt;Seance&gt;&gt;&gt; liste = new LinkedList();        
              List&lt;Seance&gt; combination = new LinkedList();
                 long j = 1;
               List&lt;List&lt;Seance&gt;&gt; data = new LinkedList();
                data = dataStructure;                
                int u = 0;
                for (int a=0;a&lt; data.size();a++) {
                   List vec =(List&lt;Seance&gt;) data.get(a);
                   
                    combination.add((Seance) vec.get((i / (int)j) % vec.size()));
                    j *= vec.size();
  data = remouve_conflicts(data, combination.get(u),u);//this removes all the sessions that will make a conflict with the session chosen in order not to appear in my combinition
                    u++;
          
                }  
               
            }
        
       
        return allCombinations;
    }

答案1

得分: 0

Long 的限制是 Long.MAX_VALUE,相当于 9,223,372,036,854,775,807。当传递该值时,它会环绕到 Long.MIN_VALUE,相当于 -9,223,372,036,854,775,808。

也就是说,Integer.MAX_VALUE + 1 == Integer.MIN_VALUE,这就是您所经历的情况。

使用数据类型 BigInt,这应该适合您的目的;理论上它没有上限,并且在实际上受到可用内存的限制(对于变量来说可用内存很多)。

英文:

The limit for Long is Long.MAX_VALUE which equates to 9,223,372,036,854,775,807. On passing that value, it wraps around to Long.MIN_VALUE which equates to -9,223,372,036,854,775,808.

That is, Integer.MAX_VALUE + 1 == Integer.MIN_VALUE, which is what you're experiencing.

Use the datatype BigInt, which should suit your purpose; in theory it has no upper bound, and is practically bound by your available memory(which is a lot for a variable).

答案2

得分: 0

如果我正确理解您的需求,您无需预先计算排列的数量。而是可以使用“里程表”方法,保持一个索引数组,指向您的输入列表中的位置,并在每次迭代时递增这个“里程表”。

List<List<String>> input = new ArrayList<>();
input.add(Arrays.asList(new String[] {"S1", "S2", "S3"}));
input.add(Arrays.asList(new String[] {"S4", "S5"}));
input.add(Arrays.asList(new String[] {"S6", "S7"}));

int[] idx = new int[input.size()];

List<String> base = new ArrayList<>();
for (List<String> sl : input) base.add(sl.get(0));

List<List<String>> allCombinations = new ArrayList<>();

while (true) {
  allCombinations.add(new ArrayList<>(base));

  int k = idx.length - 1;
  for (; k >= 0; k--) {
    idx[k] += 1;
    if (idx[k] < input.get(k).size()) {
      base.set(k, input.get(k).get(idx[k]));
      break;
    }
    idx[k] = 0;
    base.set(k, input.get(k).get(idx[k]));
  }
  if (k < 0) break;
}

for (List<String> combo : allCombinations)
  System.out.println(combo);

输出:

[S1, S4, S6]
[S1, S4, S7]
[S1, S5, S6]
[S1, S5, S7]
[S2, S4, S6]
[S2, S4, S7]
[S2, S5, S6]
[S2, S5, S7]
[S3, S4, S6]
[S3, S4, S7]
[S3, S5, S6]
[S3, S5, S7]
英文:

If I understand your requirements correctly you don't have to pre-calculate the number of permutations. Instead you can use the "odometer" approach, keeping an array of indexes into your list of input lists, and incrementing the "odometer" at each iteration.

List&lt;List&lt;String&gt;&gt; input = new ArrayList&lt;&gt;();
input.add(Arrays.asList(new String[] {&quot;S1&quot;, &quot;S2&quot;, &quot;S3&quot;}));
input.add(Arrays.asList(new String[] {&quot;S4&quot;, &quot;S5&quot;}));
input.add(Arrays.asList(new String[] {&quot;S6&quot;, &quot;S7&quot;}));
int[] idx = new int[input.size()];
List&lt;String&gt; base  = new ArrayList&lt;&gt;();
for(List&lt;String&gt; sl : input) base.add(sl.get(0));
List&lt;List&lt;String&gt;&gt; allCombinations  = new ArrayList&lt;&gt;();
while(true)
{
allCombinations.add(new ArrayList&lt;&gt;(base));
int k=idx.length-1;
for(; k&gt;=0; k--)
{
idx[k] += 1;
if(idx[k] &lt; input.get(k).size()) 
{
base.set(k, input.get(k).get(idx[k]));
break;
}
idx[k] = 0;
base.set(k, input.get(k).get(idx[k]));
}
if(k &lt; 0) break;			
}
for(List&lt;String&gt; combo : allCombinations)
System.out.println(combo);

Output:

[S1, S4, S6]
[S1, S4, S7]
[S1, S5, S6]
[S1, S5, S7]
[S2, S4, S6]
[S2, S4, S7]
[S2, S5, S6]
[S2, S5, S7]
[S3, S4, S6]
[S3, S4, S7]
[S3, S5, S6]
[S3, S5, S7]

huangapple
  • 本文由 发表于 2020年4月5日 01:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定