0

我正在做课堂作业,但遇到了一个我无法弄清楚的问题。我正在使用 BFS 实现 Ford-Fulkerson 算法来查找最大流量。但是在尝试将我的剩余容量矩阵设置为给定容量时,我遇到了分段错误。在我们收到的测试代码中,我可以看到原始容量矩阵是通过其地址按值传递的,但我有一种感觉,在我的代码中我并没有像我想象的那样与它交互?这让我相信我可能会在其他地方再次出现同样的问题。我使用 gdb 并看到我在嵌套 for 循环中的这一行上遇到了分段错误:

    resCap[i][j] = *(capacity + i*n + j);

但是,我尝试过的任何东西都没有为我工作,所以我很难过。

void maximum_flow(int n, int s, int t, int *capacity, int *flow)
{
    int i, j, resCap[n][n], path[n];    // residual capacity and BFS augmenting path
    int min_path = INT_MAX;    // min of the augmenting path

    // Assign residual capacity equal to the given capacity
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {  
            resCap[i][j] = *(capacity + i*n + j);
            *(flow + i*n + j) = 0;  // no initial flow
        }

    // Augment path with BFS from source to sink
    while (bfs(n, s, t, &(resCap[0][0]), path))
    {
        // find min of the augmenting path
        for (j = t; j != s; j = path[j])
        {
            i = path[j];
            min_path = min(min_path, resCap[i][j]);
        }

        // update residual capacities and flows on both directions
        for (j = t; j != s; j = path[j])
        {
            i = path[j];
            if(*(capacity + i*n + j) > 0)
             *(flow + i*n + j) += min_flow_path;
            else
             *(flow + j*n + i) -= min_flow_path;

            resCap[i][j] -= min_flow_path;
            resCap[j][i] += min_flow_path;
        }
    }
}

这是提供给我们的测试代码,以备不时之需:

int main(void)
{  int cap[1000][1000], flow[1000][1000];
   int i,j, flowsum;
   for(i=0; i< 1000; i++)
     for( j =0; j< 1000; j++ )
       cap[i][j] = 0;

   for(i=0; i<499; i++)
     for( j=i+1; j<500; j++) 
       cap[i][j] = 2;
   for(i=1; i<500; i++)
     cap[i][500 + (i/2)] =4;
   for(i=500; i < 750; i++ )
   { cap[i][i-250]=3;
     cap[i][750] = 1;
     cap[i][751] = 1;
     cap[i][752] = 5;
   }
   cap[751][753] = 5;
   cap[752][753] = 5;
   cap[753][750] = 20;
   for( i=754; i< 999; i++)
   {  cap[753][i]=1;
      cap[i][500]=3;
      cap[i][498]=5;
      cap[i][1] = 100;
   }
   cap[900][999] = 1;
   cap[910][999] = 1;
   cap[920][999] = 1;
   cap[930][999] = 1;
   cap[940][999] = 1;
   cap[950][999] = 1;
   cap[960][999] = 1;
   cap[970][999] = 1;
   cap[980][999] = 1;
   cap[990][999] = 1;
   printf("prepared capacity matrix, now executing maxflow code\n");
   maximum_flow(1000,0,999,&(cap[0][0]),&(flow[0][0]));
   for(i=0; i<=999; i++)
     for(j=0; j<=999; j++)
     {  if( flow[i][j] > cap[i][j] )
        {  printf("Capacity violated\n"); exit(0);}
     }    
   flowsum = 0;
   for(i=0; i<=999; i++)
     flowsum += flow[0][i];
   printf("Outflow of  0 is %d, should be 10\n", flowsum);
   flowsum = 0;
   for(i=0; i<=999; i++)
     flowsum += flow[i][999];
   printf("Inflow of 999 is %d, should be 10\n", flowsum);

   printf("End Test\n");
}
4

1 回答 1

0

这条线很可能会出现段错误,它确实使用 Clang。

int i, j, resCap[n][n], path[n]; 

您在堆栈上声明了一个非常大的数组。当您尝试使用 calloc 分配它时,可以看到有多大。试试这个,不要忘记free使用相同类型的循环。

int **resCap2 = calloc(1, n * sizeof(int *));
assert(resCap2);
for (i = 0; i < n; i++) {
  resCap2[i] = calloc(1, n * sizeof(int));
  assert(resCap2[i]);
}

这是一个很大的空间,即

(1000 * sizeof(int*) * (1000 * n * sizeof(int)))
于 2016-05-17T01:51:52.417 回答