How to solve segmentation failure (recorded core image) in recursion?

Asked

Viewed 54 times

0

I’m having trouble with this part of my show. I have to do a function with the Bron-Kerbosch algorithm, but it is giving the following error: segmentation failure (image of the recorded core) and I am not able to solve. The problem seems to be in recursiveness.

void BronKerbosch(int p[], int r[], int x[], Grafo G){
    int tam = G->V, i = 1;
    bool pVazio, xVazio;

    pVazio = Vazio(p, tam);
    xVazio = Vazio(x, tam);

    if (pVazio && xVazio){

      Imprime(r);

    }else{
      while (i <= tam && pVazio != true){

        int v = 0;
        int newP[tam], newX[tam];
        IniP(newP,  tam);
        IniX(newX, tam);

        v = p[i];

        Uniao(v, r, tam);
        Intersecao(newP, v, p, G);
        Intersecao(newX, v, x, G);

        BronKerbosch(newP, r, newX, G);

        Retira_v(v, p, tam);
        Insere(v, x);
        IniR(r, tam);
        i++;
      }
    }
}

void IniP(int *p, int V){

  int i = 1;

  while(i <= V){
    p[i] = i;
    i++;
  }
}

void IniR(int *r, int V){

  int i = 1;
  while(i <= V){
    r[i] = 0;
    i++;
  }
}

void IniX(int *x, int V){

  int i = 1;
  while(i <= V){
    x[i] = 0;
    i++;
  }
}

bool Vazio(int *v, int V){
bool vazio = true;
int i = 1;

  while(i <= V){
    if(v[i] != 0){
      vazio = false;
    }
    i++;
  }
  return vazio;
}

void Uniao(int v, int *r, int tam){

  int i = 1;

  while(r[i] != 0 && i <= tam){
    i++;
  }

  r[i] = v;
}

void Intersecao(int *newV, int v, int *old, Grafo G){

  int tam = G->V, j = 1, w = 0;
  lista a = G->adj[v-1];
  no* c  = a;

  while(c){
    w = c->w;
    int i = 1;
    while(i <= tam){
      if(old[i] == w){
        newV[j] = w;
        j++;
      }
      i++;
    }
    c = c->prox;
  }
}

void Retira_v(int v, int *p, int tam){
  int i = 1;

  while(p[i] != v && i <= tam){
    i++;
  }
  if(p[i] == v){
      p[i] = 0;
  }
}

void Insere(int v, int *x){
  int i;

  while(x[i] != 0 && x[i] != v){
    i++;
  }
  if(x[i] != v){
    x[i] = v;
  }
}

void Imprime(int *r){
  int i = 1;

  printf("Clique maximal:\n");

  while(r[i] != 0){
    printf("%d\t", r[i]);
  }

  printf("\n");
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.