1

I have a program in C which reads definition of graph from file, search for Hamiltonian cycle (only one) and prints it on screen if found. Problem is that program is crashing when I'm trying to find cycle in graphs with 30 and more vertices (for 30 vertices it sometimes shows cycle end get crashed (with different saturations), for more get crashed instantly). When I try to debug it stops at free() function and shows SIGTRAP signal. What can I do to fix that? Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

typedef struct Lista
{
    struct Lista * next;
    int v;
} Lista;

Lista * stos;

bool *visited;
int wierzcholki, krawedzie;
int *S;
int sptr;
Lista **graf;
bool czyZnaleziono = false;

int DFSHamilton(int v)
{
    if (!czyZnaleziono)
    {
        int i;
        bool test;
        Lista *p;

        S[sptr++] = v;
        if(sptr < wierzcholki)
        {
            visited[v] = true;
            for(p = graf[v]; p; p = p->next)
                if(!visited[p->v]) DFSHamilton(p->v);
            visited[v] = false;
        }
        else
        {
            test = false;
            for(p = graf[v]; p; p = p->next)
                if(!(p->v))
                {
                    test = true;
                    break;
                }

            if(test)
            {
                printf("Hamiltonian Cycle : ");
                for(i = 0; i < sptr; i++)
                {
                    printf("%d ",S[i]);
                }
                printf("0\n");
                czyZnaleziono = true;
            }
        }
        sptr--;
    }
}

int main()
{
    FILE *plik;
    Lista *p, *r;
    plik = fopen("rzeczy40-95.txt", "r");
    double start, stop, czas;
    int i, j, w1, w2;
    sptr = 0;
    visited = malloc(krawedzie*sizeof(bool));

    if (plik == NULL)
    {
        printf("Nie mozna odnalezc pliku");
        return 0;
    }
    else
    {

        fscanf(plik, "%d %d", &wierzcholki, &krawedzie);
        S = malloc(wierzcholki*sizeof(int));
        graf = malloc(wierzcholki*sizeof(Lista));
        for(i=0; i<wierzcholki; i++)
        {
            graf[i] = NULL;
            visited[i] = false;
        }

        for(j=0; j<krawedzie; j++)
        {

            fscanf(plik,"%d %d", &w1, &w2);
            p = malloc(sizeof(Lista));
            r = malloc(sizeof(Lista));
            p->v = w2;
            p->next = graf[w1];
            graf[w1] = p;
            r->v = w1;
            r->next = graf[w2];
            graf[w2] = r;
        }

        fclose(plik);

        DFSHamilton(0);

        free(visited);
        free(S);
        for (i = 0; i < wierzcholki; i++)
        {
            free(graf[i]);
        }
        free(graf);


        return 0;
    }
}
LislaV
  • 57
  • 1
  • 5
  • 1
    You very likely are getting memory corruption, possibly by overrunning the memory allocated for `visited`. Examine your code carefully: can you tell me how many bytes are allocated for it? – John Bollinger Jun 09 '16 at 21:29
  • Ohh I'm such a fool! Now I see I try to malloc 'visited' using 'krawedzie' which have no assigned value. I just had to move it into another place in code. Thanks you @JohnBollinger so much. – LislaV Jun 09 '16 at 21:51
  • 1
    It's been a while since I've seen some Polish variable names... – davak Jun 09 '16 at 21:57

0 Answers0