【问题标题】:Finding array combination to cover all elements using least amount of arrays使用最少的数组查找数组组合以覆盖所有元素
【发布时间】:2017-06-02 02:13:57
【问题描述】:

您好,我是学习 C 的新手,一直在尝试解决各种问题。我最近对一个问题的尝试已经解决了一半,最后一步我需要知道的是这个。如果有各种这样的数组:

int A[4] = {1,0,1,0};
int B[4] = {1,0,0,1};
int C[4] = {0,1,1,0};
int D[4] = {0,1,0,1};

并且所有数组的长度都是相同的“L”。我需要找出使用最少数组的组合,当组合时(同一位置的所有元素都添加到最初用零填充的相等长度的数组中),将导致长度为 'L 的数组' 里面没有一个 0。

对于上面的示例,它将是 A & D 或 B & C,因此答案将是两个数组。

它不必用 1 填充,它可以是 2 或 3 或其他。只需要没有零。由于我们只是在寻找最小的数字,因此可能有多种组合,但只有一个答案。 L 会小于 10。不过,数组的数量会从 1 到 500 不等。

我尝试过使用我在网上学习的图算法,但是这个问题让我很困惑。

【问题讨论】:

  • 1) 在您需要的位置按 1 的总数对剩余部分进行排名。 2) 取最高排名。 3) 重复
  • 对不起,我不明白。剩下的是什么?未填充的零?
  • john 提出了一个贪心算法,但它不一定给出最好的结果。示例:{{1, 0, 1, 0, 1, 0}, {1, 1, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 1}]。最好的解决方案是取除第一个数组之外的所有数组,但贪心算法将从第一个数组开始,因为它有 3 个数组,然后还要添加其他数组。
  • @maraca 是的。

标签: c arrays algorithm


【解决方案1】:

我已经使用分而治之的方法解决了这个问题。
逻辑
首先,考虑数组集合data,我们希望从中找到最小的数组组,它们的组合是只有 1 的数组。

简单地说,我们可以一个一个地从集合中取出所有数组,然后从剩余的数组中找到最小的组,使得组和当前数组中的数组组合满足条件。 & 最小组合(数组组 + 当前数组)是我们的解决方案。

我们可以简单地给出问题的递归定义。

解决方案

#include<stdio.h>
#define L 4 //length of the array
#define N 4 //number of arrays
int best[N]; //array to keep track of best combination.
int nbest=N; //number of best arrays in best combination.
int D[N][L]={{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1}}; //data
int func(int track[],int ar[],int d); //get best combination
int * copy(int *a,int len); //copy contant of a to b.
int * combine(int a[],int b[],int len);
int main()
{
    int i,j;
    int empty[L]={0,0,0,0};//initial combination
    int init[N]={-1,-1,-1,-1};//initial track.
    // initialize 'best' array.
    for(i=0;i<N;i++)
        best[i]=-1;
    // initial function call
    func(init,empty,0);
    // print the result.
    printf("best combination..\n");
    for(i=0;i<nbest;i++){
        for(j=0;j<L;j++)
            printf("%d, ",D[best[i]][j]);
        printf("\n");
    }
    return 0;
}

/*
    * this is recursive function work on data array 'D'.
    * array 'track' is used to keep track of the arrays of current combination.
    * array 'ar' is indicate the current combination.
    * int 'd' indicate the number of arrays in current combination.
*/
int func(int track[],int ar[],int d){
    int i,j,*t,*tr;
    if(d>=N) //check if there are arrays remain to combine.
        return 0;   
    if(check(ar,L)){ //check if the combination is valid.
        if(nbest>d){ //check if the current solution is better then the best.
            nbest=d;
            for(i=0;i<d;i++)
                best[i]=track[i];
        }
        return 1;
    }
    tr=copy(track,N); //make local copy of track.
    for(i=0;i<N;i++){ // make combination with all remaining arrays.
        if(isin(tr,i)) // check if current array is already in combination.
            continue;
        t=copy(ar,L); //make local copy of current combination.
        tr[d]=i; // update track array to track current array.
        t=combine(t,D[i],L); // combine the array with current combination.
        if(func(tr,t,d+1)){ // recursive call, brake the loop if the current combination is valid.
            break;
        }
    }
    return 0;
}

int * combine(int a[],int b[],int len){ // return combination of array 'a' & 'b'.
    int *c=(int *)calloc(len,sizeof(int));
    int i;
    for(i=0;i<len;i++){
        c[i]=a[i]||b[i];
    }
    return c;
}

int * copy(int *a,int len){ // this function return copy of array 'a' of length 'len'
    int i;
    int *t=(int *)calloc(len,sizeof(int));
    for(i=0;i<len;i++)
        t[i]=a[i];
    return t;
}

int check(int a[],int len){ // check if the array is valid combination. i.e. all elememnts are 1.
    int i;
    for(i=0;i<len;i++)
        if(a[i]!=1)
            return 0;
    return 1;
}

int isin(int *ar,int index){ // return 1 if int 'index' is exist in array 'ar'.
    int i;
    for(i=0;i<N;i++)
        if(ar[i]==index)
            return 1;
    return 0;
}

这里我使用数组best 来跟踪我们在实例中找到的最佳解决方案。

【讨论】:

  • 我明白了。谢谢你。我将尝试通过理解和使用您的代码来改进。干杯
  • 你可以摆脱数组上的所有循环。 1. 将数组集合变成一组具有l 位的无符号整数。 2. 组合两个整数只是 a|b(不再循环位位置) 3. 您希望通过 ORing 所需的最小子集来实现的目标数是 2^l - 1。l 位设置为 1。
  • 顺便说一句。我认为这是回溯,而不是分而治之。也许可以做一些优化(早点返回,先看有希望的分支),但总的来说这似乎是要走的路,因为问题是NP完全的。
  • @maraca 我也这么认为。但是正如你所看到的,数组的长度可能很大,在这种情况下,我们必须使用多个长变量。因此在这种情况下很难维护整数或长整数,而且我们还需要编写将数组转换为整数的代码。
  • @maraca 它看起来像回溯。因为它是NP完全类型问题。 &我试图通过在没有最佳结果的机会时返回来进行优化,正如您在代码中看到的那样。我们也可以先考虑有前途的分支。我认为在那种情况下我们必须使用人工智能。启发式搜索等算法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
相关资源
最近更新 更多