void hamilton2(int value, int *adjmatrix[])
{
    stackH[stackHcount] = value;
    stackHcount++;
    if(stackHcount < n)
    {
        visitedH[value] = true;
        for(i=0; i<n; i++)
        {
            if((adjmatrix[value][i] == 1) && (visitedH[i] == false))
            {
                hamilton2(i, adjmatrix);
            }
        }
        visitedH[value] = false;
    }
    else
    {
        if((adjmatrix[value][0] == 1) && (test == true))
        {
            printf("Cykl Hamiltona: ");
            for(i=0; i<stackHcount; i++)
            {
                printf("%d ",stackH[i]);
            }
            printf("0");
            test = false;
            //return;

        }
    }
    if(test == false) return;
    stackHcount--;
    i = value;
}
int main()
{
    srand(time(NULL));
    n = 0;
    licznik = 0;
    do
    {
        printf("Podaj liczbe wierzcholkow: (min. 8)\n");
        scanf("%d",&n);
    } while (n < 8);


/*-----INICJALIZACJA TABLICY n x n - macierz sasiedztwa-----*/

    int **adjmatrix30;
    adjmatrix30 = (int**)malloc(n*sizeof(int*));
    for (i=0; i<n; i++)
    {
        adjmatrix30[i] = (int*)malloc(n*sizeof(int));
    }

    int **adjmatrix70;
    adjmatrix70 = (int**)malloc(n*sizeof(int*));
    for (i=0; i<n; i++)
    {
        adjmatrix70[i] = (int*)malloc(n*sizeof(int));
    }

/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.3-----*/

    printf("\n-----0.3-----\n");
    nullify(adjmatrix30,n);
    K = 0.3*n*(n-1)/2;

    //generateHamilton2(n,adjmatrix30);
    //adjmatrix_gen2(n, adjmatrix30);

    generowanie(n,adjmatrix30);

    //adjmatrix_print(n, adjmatrix30);
    //printf("\nliczba krawedzi: %d\n",licznik);

  /*---HAMILTON 0.3---*/

    stackH = (int*)malloc((n+1)*sizeof(int));
    stackHcount = 0;
    visitedH = (bool*)malloc(n*sizeof(bool));
    for(i=0; i<n; i++) visitedH[i] = false;
    printf("\n");
    test = true;
    hamilton2(0, adjmatrix30);
    printf("\n");

  /*---EULER 0.3---*/

    stack = (int*)malloc((K+1)*sizeof(int));
    scount = 0;

    euler(0, adjmatrix30);
    printf("Cykl eulera: ");
    for(i=0; i<scount; i++) printf("%d ",stack[i]);
    //adjmatrix_print(n, adjmatrix30);



/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.7-----*/

    printf("\n-----0.7-----\n");
    licznik = 0;
    K = 0.7*n*(n-1)/2;

    //adjmatrix_gen2(n, adjmatrix70);

    generowanie(n, adjmatrix70);

    //adjmatrix_print(n, adjmatrix70);
    //printf("\nliczba krawedzi: %d\n",licznik);

  /*---HAMILTON 0.7---*/
    stackHcount = 0;
    for(i=0; i<n; i++) visitedH[i] = false;
    printf("\n");
    test = true;
    hamilton2(0, adjmatrix70);
    printf("\n");

  /*---EULER 0.7---*/
    stack = (int*)realloc(stack,(K+1)*sizeof(int));
    scount = 0;

    euler(0, adjmatrix70);
    printf("Cykl eulera: ");
    for(i=0; i<scount; i++) printf("%d ",stack[i]);


/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.5-----*/

    printf("\n-----0.7-----\n");
    licznik = 0;
    K = 0.5*n*(n-1)/2;

    generowanie_niesp(n, adjmatrix70);

  /*---HAMILTON 0.5---*/
    stackHcount = 0;
    for(i=0; i<n; i++) visitedH[i] = false;
    printf("\n");
    test = true;
    hamilton2(0, adjmatrix70);
    printf("\n");


    //ZWALNIANIE PAMIECI
    for (i=0; i<n; i++)
    {
        free(adjmatrix30[i]);
        free(adjmatrix70[i]);
    }
    free(adjmatrix30);
    free(adjmatrix70);
    free(stack);
    free(stackH);
    free(visitedH);
}
Beispiel #3
0
int main()
{
    clock_t stop, start, starte3, stope3, starte7, stope7, starth3, stoph3, starth7, stoph7;
    srand(time(NULL));
    n = 0;
    licznik = 0;

    for(n=10;n<=100;n+=10){
    printf("%d",n);
    /*
    do
    {
        printf("Podaj liczbe wierzcholkow: (min. 8)\n");
        scanf("%d",&n);
    } while (n < 8);
*/

/*-----INICJALIZACJA TABLICY n x n - macierz sasiedztwa-----*/

    int **adjmatrix30;
    adjmatrix30 = (int**)malloc(n*sizeof(int*));
    for (i=0; i<n; i++)
    {
        adjmatrix30[i] = (int*)malloc(n*sizeof(int));
    }

    int **adjmatrix70;
    adjmatrix70 = (int**)malloc(n*sizeof(int*));
    for (i=0; i<n; i++)
    {
        adjmatrix70[i] = (int*)malloc(n*sizeof(int));
    }

/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.3-----*/

    //printf("\n-----0.3-----\n");
    nullify(adjmatrix30,n);
    K = 0.3*n*(n-1)/2;

    //generateHamilton2(n,adjmatrix30);
    //adjmatrix_gen2(n, adjmatrix30);

    generowanie(n,adjmatrix30);

    //adjmatrix_print(n, adjmatrix30);
    //printf("\nliczba krawedzi: %d\n",licznik);

  /*---HAMILTON 0.3---*/

    stackH = (int*)malloc((n+1)*sizeof(int));
    stackHcount = 0;
    visitedH = (bool*)malloc(n*sizeof(bool));
    for(i=0; i<n; i++) visitedH[i] = false;
    //printf("\n");
    test = true;
    starth3=clock();
    hamilton2(0, adjmatrix30);
    stoph3=clock();
    //printf("\n");

  /*---EULER 0.3---*/

    stack = (int*)malloc((K+1)*sizeof(int));
    scount = 0;
    starte3=clock();
    euler(0, adjmatrix30);
    stope3=clock();
    //printf("Cykl eulera: ");
    //for(i=0; i<scount; i++) printf("%d ",stack[i]);
    //adjmatrix_print(n, adjmatrix30);



/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.7-----*/

    //printf("\n-----0.7-----\n");
    licznik = 0;
    K = 0.7*n*(n-1)/2;

    //adjmatrix_gen2(n, adjmatrix70);

    generowanie(n, adjmatrix70);

    //adjmatrix_print(n, adjmatrix70);
    //printf("\nliczba krawedzi: %d\n",licznik);

  /*---HAMILTON 0.7---*/
    stackHcount = 0;
    for(i=0; i<n; i++) visitedH[i] = false;
    //printf("\n");
    test = true;
    starth7=clock();
    hamilton2(0, adjmatrix70);
    stoph7=clock();
    //printf("\n");

  /*---EULER 0.7---*/
    stack = (int*)realloc(stack,(K+1)*sizeof(int));
    scount = 0;
    starte7=clock();
    euler(0, adjmatrix70);
    stope7=clock();
    //printf("Cykl eulera: ");
    //for(i=0; i<scount; i++) printf("%d ",stack[i]);




/*-----GENEROWANIE I WYŒWIETLANIE GRAFU 0.5-----*/
    //printf("%d\n",n);
    //printf("\n-----0.7-----\n");
    licznik = 0;
    K = 0.5*n*(n-1)/2;

    generowanie_niesp(n, adjmatrix70);

  /*---HAMILTON 0.5---*/
    stackHcount = 0;
    for(i=0; i<n; i++) visitedH[i] = false;
    //printf("\n");
    test = true;
    start=clock();
    hamilton2(0, adjmatrix70);
    stop=clock();
   // printf("\n");

printf("      %.5f    %.5f    %.5f    %.5f   %.5f\n", (stope3-starte3)/(double)CLOCKS_PER_SEC,(stope7-starte7)/(double)CLOCKS_PER_SEC,(stoph3-starth3)/(double)CLOCKS_PER_SEC,(stoph7-starth7)/(double)CLOCKS_PER_SEC,(stop-start)/(double)CLOCKS_PER_SEC);

    //ZWALNIANIE PAMIECI
    for (i=0; i<n; i++)
    {
        free(adjmatrix30[i]);
        free(adjmatrix70[i]);
    }
    free(adjmatrix30);
    free(adjmatrix70);
    free(stack);
    free(stackH);
    free(visitedH);
    }
}