1

I need to iterate, for some values of k, on all combinations of taking k items out of a vector V containing N items. For example if N = 6 and k = 3 then I have to call F() on items V1, V2 and V[3], then on items V1, V2 and V[4], ending with V[4], V[5] and V[6].

Now, going through all combinations of the index is easy. Say k = 3 and N = 6. Then the first element can go from 1 to N-(k-1), the second from 2 to N-(k-2), the k'th from k to N (i.e. N-(k-k)).

So, for k = 3 the loop would be:

for (uint a = 1, a <=  N-2, a++)
  for (uint b = a + 1, b <= N-1, b++)
    for (uint c = b + 1, c <= N, c++)
     F(V[a], V[b], V[c])

and, for k = 4, the iteration would be:

for (uint a = 1, a <=  N-3, a++)
  for (uint b = a + 1, b <= N-2, b++)
    for (uint c = b + 1, c <= N - 1, c++)
       for (uint d = c + 1, d <= N, d++)
          F(V[a], V[b], V[c], V[d])

The question, however, is: How do you accomplish this k-level nesting for an arbitrary k without hard coding it (or using code generation)?

Edit: For background (a.k.a. THIS IS NOT HOMEWORK :)) please see Hans Engler's answer to my Math Stackexchange quesion: 0-1 knapsack like - the set of all non-contained affordable binary selections

Edit: I'll provide a sample. Suppose V= {1,2,3,4,5}.

For k=2 I want F to be called in the following order: F(1,2), F(1,3), F(1,4), F(1,5), F(2,3), F(2,4), F(2,5), F(3,4), F(3, 5) and F(4,5).

For k=3 I want F to be called in the following order: F(1,2,3), F(1,2,4), F(1,2,5), F(2,3,4,), F(2,3,5), F(3,4,5)

4

2 回答 2

1

您的代码示例正在生成唯一的组合,而不是排列。无论顺序如何,组合都是唯一的。例如,如果您的数据看起来像这样 - 123、231、321,那么您将使用排列。组合属于二项式系数的范围。

我用 C# 编写了一个类来处理处理二项式系数的常用函数。它执行以下任务:

  1. 将任何 N 选择 K 的所有 K 索引以良好的格式输出到文件。K-indexes 可以替换为更具描述性的字符串或字母。

  2. 将 K 索引转换为已排序二项式系数表中条目的正确索引。这种技术比依赖迭代的旧已发布技术快得多。它通过使用帕斯卡三角形固有的数学属性来做到这一点。

  3. 将已排序二项式系数表中的索引转换为相应的 K 索引。我相信它也比旧的迭代解决方案更快。

  4. 使用Mark Dominus方法计算二项式系数,它不太可能溢出并且适用于更大的数字。

  5. 该类是用 .NET C# 编写的,并提供了一种通过使用通用列表来管理与问题相关的对象(如果有)的方法。此类的构造函数采用一个名为 InitTable 的 bool 值,当它为 true 时,将创建一个通用列表来保存要管理的对象。如果此值为 false,则不会创建表。无需创建表即可使用上述 4 种方法。提供了访问器方法来访问表。

  6. 有一个关联的测试类,它显示了如何使用该类及其方法。它已经用 2 个案例进行了广泛的测试,并且没有已知的错误。

要了解此类并下载代码,请参阅制表二项式系数

以下代码将按顺序遍历这组组合:

int N = 6;  // Total number of elements in the set.
int K = 3;  // Total number of elements in each group.
int[] V = new int[N]; // Holds the vector of numbers.
// Create the bin coeff object required to get all
// the combos for this N choose K combination.
BinCoeff<int> BC = new BinCoeff<int>(N, K, false);
int NumCombos = BinCoeff<int>.GetBinCoeff(N, K);
int[] KIndexes = new int[K];
// Loop thru all the combinations for this N choose K case.
for (int Combo = 0; Combo < NumCombos; Combo++)
{
   // Get the k-indexes for this combination.
   BC.GetKIndexes(Loop, KIndexes);
   // Do whatever processing with the V array that needs to be done.
   // Code to print the values out in C#:
   String S = V[KIndexes[0]].ToString() + ", " + V[KIndexes[1]].ToString() + ", " + 
      V[KIndexes[2]].ToString();
   Console.WriteLine(S};
}

你应该能够很容易地将这个类移植到你选择的语言上。您可能不必移植类的通用部分来实现您的目标。根据您使用的组合数量,您可能需要使用比 4 字节整数更大的字长。

于 2012-11-27T16:34:21.763 回答
1

这闻起来像家庭作业,没关系,但你应该指出这是家庭作业。

无论如何,当您看到自己陷入一系列没有明确结束的循环时,递归很可能会有所帮助。考虑下面的课程。我将把函数 F() 的内容留空,但为您指明一条合理的路径(我认为):

class Program
{
    private void F(int[] vector, int k)
    {
        if (vector.Length > 0)
        {
            // TO DO: operate on the first k elements of vector
            // Question: what conditions will throw an exception?
            Console.WriteLine("now operating on vector of length: " + vector.Length);

            // Re-define vector and re-apply F()
            vector = vector.Skip(1).Take(vector.Length - 1).ToArray();
            F(vector, k);
        }

    }


    static void Main(string[] args)
    {
        int[] vector = { 0, 1, 1, 2, 3 };
        int k = 3;

        try
        {
            Program p = new Program();
            p.F(vector, k);
        }
        catch (Exception ex)
        {

            Console.WriteLine("Unhandled exception: " + ex.Message);
            System.Environment.Exit(1);
        }
        finally
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.Write("Any key to continue...");
                Console.ReadKey();
            }
        }
    }
}
于 2012-11-27T14:29:31.103 回答