生成没有冗余的对的所有组合

时间:2013-06-15 14:56:16

标签: java algorithm combinations

我必须解决开源冠军软件的问题(在java中)。这个软件可以找到匹配的最佳组合与不同的标准(参与者的排名,已经玩过的比赛,......)。

目前,使用的算法并不是最佳的,因为它比较了所有匹配组合(包括冗余组合)。通过这种方法,sotfware比较“参与者数量的因子”组合。我正在寻找一种能够生成所有匹配组合而无冗余的算法。

当前代码包含4个参与者的示例:

// Example with 4 participants
public static void main (String[] args) {
   String[] participants = {"A","B","C","D"};
   double factorial = factorial(participants.length);
   for(double i=0;i<factorial;i++) {
      String[] combination = permutation(i,participants);
      System.out.println("Combination "+(int)i+" : "+combination[0]+" vs "+combination[1]+", "+combination[2]+" vs "+combination[3]);
   }
}

// Current code
public static <K> K[] permutation (double k, K[] objects) {
   K[] permutation = objects.clone();
   for (int i = 2; i < permutation.length + 1; i++) {
      k = k / (i - 1);
      swap(permutation, (int)(k % i), i - 1);
   }
   return permutation;
}
public static <K> void swap (K[] objects, int indexA, int indexB) {
   K temp = objects[indexA];
   objects[indexA] = objects[indexB];
   objects[indexB] = temp;
}
public static double factorial (int value) {
   double result = value;
   while (value != 2) {
      result *= --value;
   }
   return result;
}

输出:

Combination 0 : D vs A, B vs C
Combination 1 : D vs B, A vs C
Combination 2 : D vs C, A vs B
Combination 3 : D vs C, B vs A
Combination 4 : D vs A, C vs B
Combination 5 : D vs B, C vs A
Combination 6 : C vs D, B vs A
Combination 7 : C vs D, A vs B
Combination 8 : B vs D, A vs C
Combination 9 : A vs D, B vs C
Combination 10 : B vs D, C vs A
Combination 11 : A vs D, C vs B
Combination 12 : C vs A, D vs B
Combination 13 : C vs B, D vs A
Combination 14 : B vs C, D vs A
Combination 15 : A vs C, D vs B
Combination 16 : B vs A, D vs C
Combination 17 : A vs B, D vs C
Combination 18 : C vs A, B vs D
Combination 19 : C vs B, A vs D
Combination 20 : B vs C, A vs D
Combination 21 : A vs C, B vs D
Combination 22 : B vs A, C vs D
Combination 23 : A vs B, C vs D 

正如你所看到的,有很多冗余(“A vs B,C vs D”与“B vs A,C vs D”和“B vs A,D vs C”和...相同。 )。对于10个或更多参与者(10!= 3.628.800 ...),每个组合的比较失去的时间(目的是找到最好的)是相当可观的。

所需输出的示例(无论顺序):

Combination 0 : A vs B, C vs D
Combination 1 : A vs C, B vs D
Combination 2 : A vs D, B vs C

欢迎您的帮助。

1 个答案:

答案 0 :(得分:0)

回答我的问题:

public void generate (int n) {
   int[] covered = new int[n];
   int[] list = new int[n];
   for (int i = 0; i < n; i++) {
      covered[i] = 0;
   }
   this.generate(n, covered, 0, list);
}
private void generate (int n, int[] covered, int i, int[] list) {
   int j = 0;
   while ((j < n) && (covered[j] == 1)) {
      j++;
   }
   if (j == n) {
      System.out.println(Arrays.toString(list));
      return;
   }
   covered[j] = 1;
   for (int k = 0; k < n; k++) {
      if (covered[k] == 0) {
         covered[k] = 1;
         list[i++] = j;
         list[i++] = k;
         this.generate(n, covered, i, list);
         i -= 2;
         covered[k] = 0;
      }
   }
   covered[j] = 0;
}

找到here