Advertisement
Leticia22

HAMMING SIMPLE

Mar 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define N 5
  5.  
  6. /*
  7.  *
  8.  */
  9. void impMat(int M[N][N],int n){
  10.     int i,j;
  11.     for(i=0;i<n;i++){
  12.         for(j=0;j<n;j++){
  13.             printf("%d", M[i][j]);
  14.         }
  15.         printf("\n");
  16.     }  
  17. }
  18.  
  19. int isSafe(int v, int M[N][N], int A[], int pos)
  20. {
  21.     /* Check if this vertex is an adjacent vertex of the previously
  22.        added vertex. */
  23.     if (M [ A[pos-1] ][ v ] == 0)
  24.         return 0;
  25.  
  26.     /* Check if the vertex has already been included.
  27.       This step can be optimized by creating an array of size V */
  28.     int i;
  29.     for (i = 0; i < pos; i++)
  30.         if (A[i] == v)
  31.             return 0;
  32.  
  33.     return 1;
  34. }
  35.  
  36. int hamCycleUtil(int M[N][N], int A[], int pos)
  37. {
  38.     /* base case: If all vertices are included in Hamiltonian Cycle */
  39.     if (pos == N)
  40.     {
  41.         // And if there is an edge from the last included vertex to the
  42.         // first vertex
  43.         if ( M[ A[pos-1] ][ A[0] ] == 1 )
  44.            return 1;
  45.         else
  46.           return 0;
  47.     }
  48.  
  49.     // Try different vertices as a next candidate in Hamiltonian Cycle.
  50.     // We don't try for 0 as we included 0 as starting point in in hamCycle()
  51.     int v;
  52.     for (v = 1; v < N; v++)
  53.     {
  54.         /* Check if this vertex can be added to Hamiltonian Cycle */
  55.         if (isSafe(v, M, A, pos))
  56.         {
  57.             A[pos] = v;
  58.  
  59.             /* recur to construct rest of the path */
  60.             if (hamCycleUtil (M, A, pos+1) == true)
  61.                 return 1;
  62.  
  63.             /* If adding vertex v doesn't lead to a solution,
  64.                then remove it */
  65.             A[pos] = -1;
  66.         }
  67.     }
  68.  
  69.     /* If no vertex can be added to Hamiltonian Cycle constructed so far,
  70.        then return false */
  71.     return 0;
  72. }
  73.  
  74.  
  75. int hamCycle(int M[N][N])
  76. {
  77.     int A[N]; //Arreglo con el ciclo hamiltoniano
  78.     int i;
  79.     for (i = 0; i < N; i++)
  80.         A[i] = -1;
  81.  
  82.     A[0] = 0; //El ciclo siempre empieza en 0
  83.     if ( hamCycleUtil(M, A, 1) == 0 )
  84.     {
  85.         printf("\nSolution does not exist");
  86.         return 0;
  87.     }
  88.  
  89.     impSol(A);
  90.     return 1;
  91. }
  92.  
  93. void impSol(int A[]){
  94.     printf ("Solution Exists:"
  95.             " Following is one Hamiltonian Cycle \n");
  96.     int i;
  97.     for (i = 0; i < N; i++)
  98.         printf(" %d ", A[i]);
  99. }
  100.  
  101. int main(int argc, char** argv) {
  102.     int n=5;
  103.     int M[N][N]={{0,1,1,0,1},{1,0,1,0,1},{1,1,0,1,1},{0,0,1,0,1},{1,1,1,1,0}};
  104.    
  105.     /*Lectura archivos
  106.      int i,j;
  107.      for(i=0;i<n;i++)
  108.          for(j=0;j<n;j++)
  109.              scanf("%d", &M[i][j]);
  110.      */
  111.     impMat(M,n);
  112.     hamCycle(M);
  113.    
  114.     return (EXIT_SUCCESS);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement