CSR和BFS始终以0步查找

CSR和BFS始终以0步查找,c,graph,breadth-first-search,C,Graph,Breadth First Search,我正在尝试创建一个程序,该程序使用两个数组创建一个CSR(压缩稀疏行)格式的图形,其中一个数组是每个节点的偏移量,第二个数组是边。数据是从文件中读取的,我使用dictionary/map来保留内存。然后,它请求节点和边,并使用BFS搜索图的方式,如果存在路径,它必须打印。但是,无论我键入什么,程序总是返回它找到的,但不打印它存在,并且步骤为0 该文件如下所示: 737 6340 1740 1199 1738 1199 1738 1811 1738 2085 1739

我正在尝试创建一个程序,该程序使用两个数组创建一个CSR(压缩稀疏行)格式的图形,其中一个数组是每个节点的偏移量,第二个数组是边。数据是从文件中读取的,我使用dictionary/map来保留内存。然后,它请求节点和边,并使用BFS搜索图的方式,如果存在路径,它必须打印。但是,无论我键入什么,程序总是返回它找到的,但不打印它存在,并且步骤为0

该文件如下所示:

737 6340
1740    1199
1738    1199
1738    1811
1738    2085
1739    1199
1741    214
1741    1199
1741    1419
1741    1496
1741    1723
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
  int* num;
  int size;
  int top;

} stack;


int nodesdoublesize(int** array,int n){
  int* new_array=malloc(n*2*sizeof(int));
  if(new_array==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  n*=2;
  for(int i=0;i<n;i++){
    new_array[i]=(*array)[i];
  }
  free(*array);
  *array=new_array;
  return n;
}
void stack_destroy(stack *s){
  free(s->num);
  free(s);
}




int hashcmp(const void *a,const void *b){
  return ( *(int*)a - *(int*)b );
}

int hashdoublesize(int** hash,int nodes){
    int* new_array=malloc(nodes*2*sizeof(int));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<nodes;i++){
        new_array[i]=(*hash)[i];
    }
    nodes*=2;

    free(*hash);
    *hash=new_array;
    return nodes;
}

typedef struct {
    int start;
    int end;   
} path;






stack* stack_create(){  
  stack *s=malloc(sizeof(stack));
  if(s==NULL){
    printf("Error allocating memory for stack\n");
    abort();
  }
  s->top=0;
  s->size=10;
  s->num=malloc(s->size*sizeof(int));
  if(s->num==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  return s;
}





int cmp(const void *a,const void *b){
    int l=((path*)a)->start;
    int r=((path*)b)->start;

    if(l>r)
        return 1;
    if(l<r)
        return -1;
    else
        return 0;
}

int doublesize(path** array,int n){
    path* new_array=malloc(n*2*sizeof(path));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<n;i++){
        new_array[i]=(*array)[i];
    }
    free(*array);
    *array=new_array;
    n*=2;
    return n;

}
int bfs(int* arraynodes,int* arrayedges,int n,int st,int end){
  stack *s=stack_create();
  int color[n];
  for(int i=0;i<n;i++){
    color[i]=-1;
  }
    color[st]=0;
  s->num[s->top]=st;
  while (s->top!=0){
    for (int i = arraynodes[st]; i < arraynodes[st+1];i++){
      if (color[i]==-1){
        color[i]=0;
        s->top++;
        s->num[s->top]=i;
        if(s->top==s->size){
         s->top=nodesdoublesize(&s->num,s->size);
        }
        if(s->num[s->top]==end){
          printf("Exists\n");
          return 0;
        }
      }
      s->top--;
    color[i]=1;

    }
  }
  return 1;
}







int main()
{
    int maxsize=10;
    int test;
    char buff[200];
    int counter=0;
    char c;
    int i;
    path* array=malloc(maxsize*sizeof(path));
    if(array==NULL) {
        printf("Error allocating memory\n");
        abort();
    }


    FILE* fd=fopen("Wiki-Vote.txt","r");
    if(fd==NULL) {
        printf("Error opening file\n");
        abort();
    }


  while(fgets(buff,200,fd)) {

        c=buff[0];
        if(c=='#') {
            continue;
        }
    sscanf(buff,"%d%d",&array[counter].start,&array[counter].end);
        counter++;
        if(counter==maxsize){
           maxsize=doublesize(&array,maxsize); 
    }

    }

  maxsize=counter;
    counter=0;
    qsort(&array[0],maxsize,sizeof(path),cmp);




  counter=1;
  int nodes=10;
  int* hash=malloc(nodes*sizeof(int));
  if(hash==NULL){
    printf("Error allocating memory\n");
    abort();
  }

for(i=0;i<maxsize;i++){
  if(hash[counter-1]==array[i].start)
    continue;
        hash[counter]=array[i].start;
        counter++;
        if(counter==nodes){
          nodes=hashdoublesize(&hash,nodes);
        }
}
int j;
for(i=0;i<maxsize;i++){
  for(j=0;j<counter;j++){
    if(hash[j]==array[i].end)
      break;
  }
  if(j!=counter)
    continue;
  hash[counter]=array[i].end;
  counter++;
  if(counter==nodes)
    nodes=hashdoublesize(&hash,nodes);
}

nodes=counter;
qsort(&hash[0],nodes,sizeof(int),hashcmp);


  int* arraynodes=malloc(nodes*sizeof(int));
  int* arrayedges=malloc(maxsize*sizeof(int));
  if(arraynodes==NULL||arrayedges==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  int edge_count=maxsize;
  int edge_offset=0;
  for(int i=0;i<nodes;i++){
    int current_node=hash[i];
    arraynodes[i]=edge_offset;
    while(edge_offset<edge_count&& array[edge_offset].start == current_node){
      edge_offset++;
    }
  }
  for (int i = 0; i < edge_count; i++){
    arrayedges[i]=array[i].end;
  }








int x;
  printf("give number to search: "); 
  scanf("%d",&x);
  for(i=0;i<nodes;i++){
    if(x==hash[i]){
      printf("found \n");
      break;
    }
  }
  if(i==nodes){
    printf("not found \n");
    abort();
  }

/*  for(j=arraynodes[i];j<arraynodes[i+1];j++){
    printf("%d\n",arrayedges[j]);
  }*/

  int en=hash[i];
  int st;
  printf("From where would you like to start: ");
  scanf("%d",&st);
  printf("\n");
  int found;
  found=bfs(arraynodes,arrayedges,nodes,st,en);
  if(found){
    printf("Found\n");
  }
  else
    printf("Not found\n");







  free(arraynodes);
  free(arrayedges);
  free(hash);
    fclose(fd);
    free(array);
        return 0;
}
// Reallocate a dynamically allocated array, doubling its size
int nodesdoublesize(int **array,int n)
{
    int* new_array = realloc(*array, n * 2 * sizeof *new_array);
    if (new_array == NULL)
    {
        fprintf(stderr, "Out of memory\n");
        abort();
    }

    *array = new_array;
    return n * 2;
}

// Reallocate the data of the stack
void stack_doublesize(stack *s)
{
    s->size = nodesdoublesize(&s->num, s->size);
}
程序如下所示:

737 6340
1740    1199
1738    1199
1738    1811
1738    2085
1739    1199
1741    214
1741    1199
1741    1419
1741    1496
1741    1723
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
  int* num;
  int size;
  int top;

} stack;


int nodesdoublesize(int** array,int n){
  int* new_array=malloc(n*2*sizeof(int));
  if(new_array==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  n*=2;
  for(int i=0;i<n;i++){
    new_array[i]=(*array)[i];
  }
  free(*array);
  *array=new_array;
  return n;
}
void stack_destroy(stack *s){
  free(s->num);
  free(s);
}




int hashcmp(const void *a,const void *b){
  return ( *(int*)a - *(int*)b );
}

int hashdoublesize(int** hash,int nodes){
    int* new_array=malloc(nodes*2*sizeof(int));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<nodes;i++){
        new_array[i]=(*hash)[i];
    }
    nodes*=2;

    free(*hash);
    *hash=new_array;
    return nodes;
}

typedef struct {
    int start;
    int end;   
} path;






stack* stack_create(){  
  stack *s=malloc(sizeof(stack));
  if(s==NULL){
    printf("Error allocating memory for stack\n");
    abort();
  }
  s->top=0;
  s->size=10;
  s->num=malloc(s->size*sizeof(int));
  if(s->num==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  return s;
}





int cmp(const void *a,const void *b){
    int l=((path*)a)->start;
    int r=((path*)b)->start;

    if(l>r)
        return 1;
    if(l<r)
        return -1;
    else
        return 0;
}

int doublesize(path** array,int n){
    path* new_array=malloc(n*2*sizeof(path));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<n;i++){
        new_array[i]=(*array)[i];
    }
    free(*array);
    *array=new_array;
    n*=2;
    return n;

}
int bfs(int* arraynodes,int* arrayedges,int n,int st,int end){
  stack *s=stack_create();
  int color[n];
  for(int i=0;i<n;i++){
    color[i]=-1;
  }
    color[st]=0;
  s->num[s->top]=st;
  while (s->top!=0){
    for (int i = arraynodes[st]; i < arraynodes[st+1];i++){
      if (color[i]==-1){
        color[i]=0;
        s->top++;
        s->num[s->top]=i;
        if(s->top==s->size){
         s->top=nodesdoublesize(&s->num,s->size);
        }
        if(s->num[s->top]==end){
          printf("Exists\n");
          return 0;
        }
      }
      s->top--;
    color[i]=1;

    }
  }
  return 1;
}







int main()
{
    int maxsize=10;
    int test;
    char buff[200];
    int counter=0;
    char c;
    int i;
    path* array=malloc(maxsize*sizeof(path));
    if(array==NULL) {
        printf("Error allocating memory\n");
        abort();
    }


    FILE* fd=fopen("Wiki-Vote.txt","r");
    if(fd==NULL) {
        printf("Error opening file\n");
        abort();
    }


  while(fgets(buff,200,fd)) {

        c=buff[0];
        if(c=='#') {
            continue;
        }
    sscanf(buff,"%d%d",&array[counter].start,&array[counter].end);
        counter++;
        if(counter==maxsize){
           maxsize=doublesize(&array,maxsize); 
    }

    }

  maxsize=counter;
    counter=0;
    qsort(&array[0],maxsize,sizeof(path),cmp);




  counter=1;
  int nodes=10;
  int* hash=malloc(nodes*sizeof(int));
  if(hash==NULL){
    printf("Error allocating memory\n");
    abort();
  }

for(i=0;i<maxsize;i++){
  if(hash[counter-1]==array[i].start)
    continue;
        hash[counter]=array[i].start;
        counter++;
        if(counter==nodes){
          nodes=hashdoublesize(&hash,nodes);
        }
}
int j;
for(i=0;i<maxsize;i++){
  for(j=0;j<counter;j++){
    if(hash[j]==array[i].end)
      break;
  }
  if(j!=counter)
    continue;
  hash[counter]=array[i].end;
  counter++;
  if(counter==nodes)
    nodes=hashdoublesize(&hash,nodes);
}

nodes=counter;
qsort(&hash[0],nodes,sizeof(int),hashcmp);


  int* arraynodes=malloc(nodes*sizeof(int));
  int* arrayedges=malloc(maxsize*sizeof(int));
  if(arraynodes==NULL||arrayedges==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  int edge_count=maxsize;
  int edge_offset=0;
  for(int i=0;i<nodes;i++){
    int current_node=hash[i];
    arraynodes[i]=edge_offset;
    while(edge_offset<edge_count&& array[edge_offset].start == current_node){
      edge_offset++;
    }
  }
  for (int i = 0; i < edge_count; i++){
    arrayedges[i]=array[i].end;
  }








int x;
  printf("give number to search: "); 
  scanf("%d",&x);
  for(i=0;i<nodes;i++){
    if(x==hash[i]){
      printf("found \n");
      break;
    }
  }
  if(i==nodes){
    printf("not found \n");
    abort();
  }

/*  for(j=arraynodes[i];j<arraynodes[i+1];j++){
    printf("%d\n",arrayedges[j]);
  }*/

  int en=hash[i];
  int st;
  printf("From where would you like to start: ");
  scanf("%d",&st);
  printf("\n");
  int found;
  found=bfs(arraynodes,arrayedges,nodes,st,en);
  if(found){
    printf("Found\n");
  }
  else
    printf("Not found\n");







  free(arraynodes);
  free(arrayedges);
  free(hash);
    fclose(fd);
    free(array);
        return 0;
}
// Reallocate a dynamically allocated array, doubling its size
int nodesdoublesize(int **array,int n)
{
    int* new_array = realloc(*array, n * 2 * sizeof *new_array);
    if (new_array == NULL)
    {
        fprintf(stderr, "Out of memory\n");
        abort();
    }

    *array = new_array;
    return n * 2;
}

// Reallocate the data of the stack
void stack_doublesize(stack *s)
{
    s->size = nodesdoublesize(&s->num, s->size);
}
#包括
#包括
#包括
类型定义结构{
int*num;
整数大小;
int top;
}堆叠;
int nodesdoublesize(int**数组,int n){
int*new_数组=malloc(n*2*sizeof(int));
if(新数组==NULL){
printf(“分配内存时出错”);
中止();
}
n*=2;
对于(int i=0;inum);
免费的;
}
int hashcmp(常量无效*a,常量无效*b){
返回(*(int*)a-*(int*)b);
}
int hashdoublesize(int**hash,int节点){
int*new_数组=malloc(nodes*2*sizeof(int));
if(新数组==NULL){
printf(“分配内存时出错”);
中止();
}
对于(int i=0;itop=0;
s->size=10;
s->num=malloc(s->size*sizeof(int));
如果(s->num==NULL){
printf(“分配内存时出错”);
中止();
}
返回s;
}
int cmp(常数无效*a,常数无效*b){
int l=((路径*)a)->开始;
int r=((路径*)b)->开始;
如果(l>r)
返回1;
如果(ltop!=0){
对于(int i=阵列节点[st];i<阵列节点[st+1];i++){
如果(颜色[i]=-1){
颜色[i]=0;
s->top++;
s->num[s->top]=i;
如果(s->top==s->size){
s->top=nodesdoublesize(&s->num,s->size);
}
如果(s->num[s->top]==结束){
printf(“存在”\n);
返回0;
}
}
s->顶部--;
颜色[i]=1;
}
}
返回1;
}
int main()
{
int maxsize=10;
智力测验;
字符buff[200];
int计数器=0;
字符c;
int i;
path*array=malloc(maxsize*sizeof(path));
if(数组==NULL){
printf(“分配内存时出错”);
中止();
}
文件*fd=fopen(“Wiki Vote.txt”、“r”);
如果(fd==NULL){
printf(“打开文件时出错”);
中止();
}
而(fgets(buff,200,fd)){
c=增益[0];
如果(c='#'){
继续;
}
sscanf(buff、%d%d、&array[counter]。开始、&array[counter]。结束);
计数器++;
如果(计数器==maxsize){
maxsize=doublesize(&array,maxsize);
}
}
maxsize=计数器;
计数器=0;
qsort(数组[0]、maxsize、sizeof(路径)、cmp);
计数器=1;
int节点=10;
int*hash=malloc(nodes*sizeof(int));
if(hash==NULL){
printf(“分配内存时出错”);
中止();
}

对于(i=0;i您的
stack\u doublesize
函数是非常错误的,这可能是您的问题背后的原因,因为使用它会导致错误

目前如图所示(当我写这个答案时),它基本上将单个
堆栈
结构重新分配为20个
堆栈
结构的数组。然后,它将单个原始
堆栈
结构视为
10
结构的数组,并将这些
10
结构复制到新数组中。这当然会超出范围,因为您没有
10
结构,只有一个结构

此外,如果不重新分配
堆栈
结构本身指向的内存,则
num
成员仍将是相同的。这意味着您也将超出此内存的范围

作为这些问题的解决方案,我建议您将重新分配功能更改为以下内容:

737 6340
1740    1199
1738    1199
1738    1811
1738    2085
1739    1199
1741    214
1741    1199
1741    1419
1741    1496
1741    1723
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
  int* num;
  int size;
  int top;

} stack;


int nodesdoublesize(int** array,int n){
  int* new_array=malloc(n*2*sizeof(int));
  if(new_array==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  n*=2;
  for(int i=0;i<n;i++){
    new_array[i]=(*array)[i];
  }
  free(*array);
  *array=new_array;
  return n;
}
void stack_destroy(stack *s){
  free(s->num);
  free(s);
}




int hashcmp(const void *a,const void *b){
  return ( *(int*)a - *(int*)b );
}

int hashdoublesize(int** hash,int nodes){
    int* new_array=malloc(nodes*2*sizeof(int));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<nodes;i++){
        new_array[i]=(*hash)[i];
    }
    nodes*=2;

    free(*hash);
    *hash=new_array;
    return nodes;
}

typedef struct {
    int start;
    int end;   
} path;






stack* stack_create(){  
  stack *s=malloc(sizeof(stack));
  if(s==NULL){
    printf("Error allocating memory for stack\n");
    abort();
  }
  s->top=0;
  s->size=10;
  s->num=malloc(s->size*sizeof(int));
  if(s->num==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  return s;
}





int cmp(const void *a,const void *b){
    int l=((path*)a)->start;
    int r=((path*)b)->start;

    if(l>r)
        return 1;
    if(l<r)
        return -1;
    else
        return 0;
}

int doublesize(path** array,int n){
    path* new_array=malloc(n*2*sizeof(path));
    if(new_array==NULL){
        printf("Error allocating memory\n");
        abort();
    }

    for(int i=0;i<n;i++){
        new_array[i]=(*array)[i];
    }
    free(*array);
    *array=new_array;
    n*=2;
    return n;

}
int bfs(int* arraynodes,int* arrayedges,int n,int st,int end){
  stack *s=stack_create();
  int color[n];
  for(int i=0;i<n;i++){
    color[i]=-1;
  }
    color[st]=0;
  s->num[s->top]=st;
  while (s->top!=0){
    for (int i = arraynodes[st]; i < arraynodes[st+1];i++){
      if (color[i]==-1){
        color[i]=0;
        s->top++;
        s->num[s->top]=i;
        if(s->top==s->size){
         s->top=nodesdoublesize(&s->num,s->size);
        }
        if(s->num[s->top]==end){
          printf("Exists\n");
          return 0;
        }
      }
      s->top--;
    color[i]=1;

    }
  }
  return 1;
}







int main()
{
    int maxsize=10;
    int test;
    char buff[200];
    int counter=0;
    char c;
    int i;
    path* array=malloc(maxsize*sizeof(path));
    if(array==NULL) {
        printf("Error allocating memory\n");
        abort();
    }


    FILE* fd=fopen("Wiki-Vote.txt","r");
    if(fd==NULL) {
        printf("Error opening file\n");
        abort();
    }


  while(fgets(buff,200,fd)) {

        c=buff[0];
        if(c=='#') {
            continue;
        }
    sscanf(buff,"%d%d",&array[counter].start,&array[counter].end);
        counter++;
        if(counter==maxsize){
           maxsize=doublesize(&array,maxsize); 
    }

    }

  maxsize=counter;
    counter=0;
    qsort(&array[0],maxsize,sizeof(path),cmp);




  counter=1;
  int nodes=10;
  int* hash=malloc(nodes*sizeof(int));
  if(hash==NULL){
    printf("Error allocating memory\n");
    abort();
  }

for(i=0;i<maxsize;i++){
  if(hash[counter-1]==array[i].start)
    continue;
        hash[counter]=array[i].start;
        counter++;
        if(counter==nodes){
          nodes=hashdoublesize(&hash,nodes);
        }
}
int j;
for(i=0;i<maxsize;i++){
  for(j=0;j<counter;j++){
    if(hash[j]==array[i].end)
      break;
  }
  if(j!=counter)
    continue;
  hash[counter]=array[i].end;
  counter++;
  if(counter==nodes)
    nodes=hashdoublesize(&hash,nodes);
}

nodes=counter;
qsort(&hash[0],nodes,sizeof(int),hashcmp);


  int* arraynodes=malloc(nodes*sizeof(int));
  int* arrayedges=malloc(maxsize*sizeof(int));
  if(arraynodes==NULL||arrayedges==NULL){
    printf("Error allocating memory\n");
    abort();
  }
  int edge_count=maxsize;
  int edge_offset=0;
  for(int i=0;i<nodes;i++){
    int current_node=hash[i];
    arraynodes[i]=edge_offset;
    while(edge_offset<edge_count&& array[edge_offset].start == current_node){
      edge_offset++;
    }
  }
  for (int i = 0; i < edge_count; i++){
    arrayedges[i]=array[i].end;
  }








int x;
  printf("give number to search: "); 
  scanf("%d",&x);
  for(i=0;i<nodes;i++){
    if(x==hash[i]){
      printf("found \n");
      break;
    }
  }
  if(i==nodes){
    printf("not found \n");
    abort();
  }

/*  for(j=arraynodes[i];j<arraynodes[i+1];j++){
    printf("%d\n",arrayedges[j]);
  }*/

  int en=hash[i];
  int st;
  printf("From where would you like to start: ");
  scanf("%d",&st);
  printf("\n");
  int found;
  found=bfs(arraynodes,arrayedges,nodes,st,en);
  if(found){
    printf("Found\n");
  }
  else
    printf("Not found\n");







  free(arraynodes);
  free(arrayedges);
  free(hash);
    fclose(fd);
    free(array);
        return 0;
}
// Reallocate a dynamically allocated array, doubling its size
int nodesdoublesize(int **array,int n)
{
    int* new_array = realloc(*array, n * 2 * sizeof *new_array);
    if (new_array == NULL)
    {
        fprintf(stderr, "Out of memory\n");
        abort();
    }

    *array = new_array;
    return n * 2;
}

// Reallocate the data of the stack
void stack_doublesize(stack *s)
{
    s->size = nodesdoublesize(&s->num, s->size);
}
将调用更改为
stack\u doublesize
以遵循新函数



您的代码中可能有更多我没有发现的错误和问题。我建议您从启用额外警告开始构建(
-Wall-Wextra-Wpedantic
如果使用GCC或Clang,以及
/W4
如果使用MSVC),并将所有警告视为需要修复的错误。

现在似乎是学习如何使用调试器在监视变量及其值的同时逐条检查代码的最佳时机。
stack\u doublesize
函数似乎……错误。为什么突然间希望有两个堆栈而不是一个呢?它会的调用
nodesdoublesize
以增加
num
指向的数组的大小更有意义。事实上,调用
stack\u doublesize
当前将导致,因为您循环超出最初创建的单个
堆栈
对象的边界。您可能还对函数感兴趣。您是对的,我这样做了(并编辑了),但它仍然显示了相同的问题。