【问题标题】:Combinations from n lists picking one element from each list given an index and the quest for the fastest unranking of combinationsn 个列表中的组合在给定索引的情况下从每个列表中选择一个元素,并寻求最快的组合排序
【发布时间】:2018-06-30 11:17:16
【问题描述】:

我有十个列表,每个列表有七个数字,我想将每个列表的每个数字与其他列表的任何其他数字的数量结合起来,这样就可以得到 7 x 7 x 7 x 7 x 7 x 7 x 7 x 7 x 7 x 7 = 282475249 种组合。

现在,如果我使用下面的代码将它们保存在数组中,原因是我每天需要处理许多它们,我需要大量的内存,而我没有:

i0 = 0;
for(p1=0;p1<=6;p1++)
{
 for(p2=7;p2<=13;p2++)
 {
  for(p3=14;p3<=20;p3++)
  {
   for(p4=21;p4<=27;p4++)
   {
    for(p5=28;p5<=34;p5++)
    {
     for(p6=35;p6<=41;p6++)
     {
      for(p7=42;p7<=48;p7++)
      {  
       for(p8=49;p8<=55;p8++)
       {
        for(p9=56;p9<=62;p9++)
        {
         for(p10=63;p10<=69;p10++)
         {          

            comb[i0 * 10 + 0] = p1;
            comb[i0 * 10 + 1] = p2;
            comb[i0 * 10 + 2] = p3;
            comb[i0 * 10 + 3] = p4;
            comb[i0 * 10 + 4] = p5;
            comb[i0 * 10 + 5] = p6;
            comb[i0 * 10 + 6] = p7; 
            comb[i0 * 10 + 7] = p8;
            comb[i0 * 10 + 8] = p9;
            comb[i0 * 10 + 9] = p10;

           i0++;
         }
        }
       }
      }
     }
    }
   }
  }
 }
}

有没有什么方法可以在给定索引的情况下生成这些组合,以便我可以在第一天执行 1 到 100000,第二天执行 100001 到 200000 等等?

谢谢

【问题讨论】:

  • 这是一个关于 C 的问题吗?顺便说一句:您使用幻数,但不是 same。这里for(p1=0;p1&lt;=6;p1++) 比for(p1=0;p1&lt;7;p1++) 更好,因为嵌套循环是for(p2=7;p2&lt;=13;p2++)。常见的幻数现在是7,而不是两个幻数6 和7。假设您在不同的上下文中使用硬代码8,并且您想要调整代码,因此一个功能使用10 变体而不是8。您将有一项艰巨的维护工作来决定各种8 中的哪些需要更改,因此可维护的计划是将硬编码数字放在一个定义中。
  • 嗨,谢谢,原因是我很容易记住列表中的 7 个数字,所以 0-6、7-13、14-20 等等,是的,问题是关于 C,因为上面的代码在 C 中。

标签: c algorithm parallel-processing combinatorics


【解决方案1】:

考虑以下示例:

#define  SIZE  10
#define  BASE  7

void set_combination(char          destination[SIZE],
                     unsigned int  index,
                     const char    source[SIZE][BASE])
{
    int  i = SIZE;

    /* Extract the 'SIZE' digits from 'index', starting
       with the least significant (rightmost) one. */
    while (i-->0) {
        destination[i] = source[i][ index % BASE ];
        index /= BASE;
    }

    /* Make sure destination[] is a string, by adding
       the string-terminating '\0' char. */
    destination[SIZE] = '\0';
}        

该函数将 index 解释为 base BASE 中的 SIZE 位数字。

索引的有效范围是 0 到 BASESIZE-1,包括端点。范围环绕,因此索引 0 和 BASESIZE 在目标中生成完全相同的字符串。 (假设unsigned int 有足够的范围来表达该数字。在这种特殊情况下,如果 BASE=7 和 SIZE=10,则 710-1 = 282,475,248,恰好适合 unsigned int好的。)

最左边的数字是 source[0][i],其中 i 是 0 到 BASE-1(含)。每个数字都有自己的字符数组。最右边的数字是 source[SIZE-1][k],其中 k 是 0 到 BASE-1,包括 0。


在评论中,OP 询问是否可以将这种方法应用于源集为 59 个数字(1 到 59,包括在内)的情况,其中选择了三个。 (在这种情况下,唯一组合的数量为 59*58*57 = 195054。)

我已经展示了一种方法 construct_word() in this answer。与上面示例的不同之处在于,这一次,source 集合现在是可变的,并且每当我们选择一个字母/数字时,我们都会将其从源集合中删除。 (每个组合都以相同的source 集开始;变化只发生在单个组合的生成期间。)适应 OP 的特殊情况,它类似于以下内容:

void  choose_3of59(char          destination[3],
                   unsigned int  index)
{
    char  source[59] = {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
                         11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                         21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                         31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                         41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                         51, 52, 53, 54, 55, 56, 57, 58, 59 };
    unsigned int  base = 59;
    int           i = 3;

    while (i-->0) {
        /* Remove next 'base'-base digit out of index */
        const unsigned int  k = index % base;
        index /= base;

        /* Add that digit to destination */
        destination[i] = source[k];

        /* Remove this digit from the base set. */
        base--;
        source[k] = source[base];
    }
}

index 从 0 到 195053 (= 59*58*57 - 1) 生成所有三位数组合,其中每个数字都是唯一的,从 1 到 59,包括 1 到 59。

请注意,虽然上面确实产生了所有组合,但这些组合不是按升序排列的(也就是说,destination[0]*59*59 + destination[1]*58 + destination[2] 不是按升序排列的)。


有些组合比其他组合更难生成。

例如,要生成具有 n1、n2 和 n3 整数的三元组 1 ≤ n1 0 和 (57*58*59)/(1*2*3) - 1 之间,包括) 对于每个三元组 (n1, n2, n3):

index(n1, n2, n3) = ((n1 - 174)*n1 + 10091)*n1 / 6 + (117 - n2)*n2 / 2 + n3 - 1771

接下来,我们可以使用二分查找首先找到最高有效数 n1(1 到 57 之间最多 6 步)。然后,当找到它时,我们可以使用另一个二进制搜索首先找到中间数字 n2。最后,我们可以用上面的方程来求解最低有效数n3:

/* Given 0 <= index <= 32508, generate a triplet of integers,
         1 <= num[0] < num[1] < num[2] <= 59
*/
static void set_triplet59(char num[3], unsigned int index)
{
    int  num0, num1, num2;

    /* Make sure 0 <= i <= 32508. */
    index %= 32509;

    /* Note that index 'index' corresponds to triplet 'num0', 'num1', 'num2':
        index(num0, num1, num2) = ( (num0 - 174)*num0 + 10091)*num0/6 + (117 - num1)*num1/2 + num2 - 1771

       Also note that
        index(num0, num0+1, num0+2) = index0(num0)
        index(num0, num1, num1+1) = index01(num0, num1) = index0(num0) + index1(num0, num1)
        index(num0, num1, num2) = index0(num0) + index1(num0, num1) + (num2 - num1 - 1)
       where
        index0(num0) = (num0*(num0 - 176) + 10266)*(num0 - 1)/6
        index1(num0, num1) = (num1 + num0 - 118)*(num1 - num0 - 1)/2
        index01(num0, num1) = index0(num0) + index1(num0, num1)
                            = 1/6*num0^3 - 29*num0^2 - 1/2*num1^2 + 10091/6*num0 + 119/2*num1 - 1770
                            = ((num0 - 174)*num0 + 10091)*num0 / 6 - (num1 - 119)*num1/2 - 1770
    */

    /* Use a binary search to find the largest possible num0, where
        index(num0, num0+1, num0+2) <= index.
       The binary search does at most six iterations.
    */
    {
        const int  limit6 = index*6; /* To avoid division by 6 for index0(num0). */
        int        min0   = 1;       /* Inclusive */
        int        max0   = 58;      /* Exclusive */
        while (max0 - min0 > 1) {
            num0 = (min0 + max0) / 2;
            if ((num0*(num0 - 176) + 10266)*(num0 - 1) > limit6)
                max0 = num0;
            else
                min0 = num0;
        }
        num0 = min0;
    }

    /* Use a binary search to find the largest possible num1, where
        index(num0, num1, num1+1) <= index.
       The search does at most six iterations. Note that
        ((num0 - 174)*num0 + 10091)*num0/6 + (117 - num1)*num1/2 + num1 + 1 - 1771 > index
       is can be simplified to
        (117 - num1)*num1/2 + num1 > index + 1770 - ((num0 - 174)*num0 + 10091)*num0/6
       and further to
        (117 - num1)*num1 + 2*num1 > 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0/3
       and finally to
        (119 - num1)*num1 > 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0/3
       where the right side is 'limit'.
    */
    {
        const int limit = 2*index + 3540 - ((num0 - 174)*num0 + 10091)*num0 / 3;
        int       min1 = num0 + 1;  /* Inclusive */
        int       max1 = 59;        /* Exclusive */
        while (max1 - min1 > 1) {
            num1 = (min1 + max1) / 2;
            if ((119 - num1)*num1 > limit)
                max1 = num1;
            else
                min1 = num1;
        }
        num1 = min1;
    }

    /* Since we know index, num0, and num1, we can calculate num2 directly. */
    num2 = index + 1771 + num1*(num1 - 117)/2 - num0*(10091 - num0*(174 - num0))/6;

    num[0] = num0;
    num[1] = num1;
    num[2] = num2;
}

以上经过验证可以生成与

完全相同的组合
index = 0;
for (num[0] = 1; num[0] <= 57; num[0]++)
    for (num[1] = num[0]+1; num[1] <= 58; num[1]++)
        for (num[2] = num[1]+1; num[2] <= 59; num[2]++, index++) {
            /* index, num[0], num[1], num[2] */
        }

在 Intel Core i5-7200U (x86-64) 上,set_triplet59(triplet, index) 在所有有效三元组中需要 145 个周期的中位数。作为比较,choose_3of59(triplet, index) 在所有组合中需要 115 个周期的中位数。 (换句话说,您可以预期 set_triplet() 在 x86-64 上比 choose_3of59() 慢大约四分之一到三分之一。)

【讨论】:

  • 真的非常感谢,很棒的一段代码。我如何移植它以用于正常组合?例如从 10 N(120 种组合)中选择 7 K 项。
  • @Mark: 由于总共有 COUNT = BASE**SIZE = 282475248 个组合,并且索引范围从 0 到 COUNT-1,包括 0 到 COUNT-1,您可以通过计算将它们分成 K 个大致相等大小的集合@ 987654344@。然后,k'th 集是从 C(k-1) 到 C(k)-1,包括在内。例如,如果K = 47,那么第 35 个(共 47 个)集合是从 (282475248*34)/47 = 204343796 到 (282475248*35)/47-1 = 210353907——这是 6010112 个索引,或大约 282475248 的 1/47。
  • 对不起,我的意思是,如果我有这样的东西怎么办: for(p1=1;p1
  • 看来代码生成的是排列而不是组合,32509应该是59个数字的组合,当时取3个
  • @Mark: 代码生成一个从 1 到 59 的唯一数字的有序三元组;我以为这就是你的意思。显然不是这样......好吧,三重循环 for (p1 = 1; p1 &lt; 58; p1++) for (p2 = p1 + 1; p2 &lt; 59; p2++) for (p3 = p2 + 1; p3 &lt; 60; p3++) 产生三元组 {p1, p2, p3} 其中 1 ≤ p1 p2 p3 ≤ 59。这就是你真正想要的吗? (绝对不是“59个数字的组合当时取3个”。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
相关资源
最近更新 更多