Advertisement
Guest User

P4 ALgo

a guest
Dec 14th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #define TAM_MAX 1600
  8.  
  9. // ------------------------------  X  ----------------------------------
  10. // =====================================================================
  11. // ------------------------------  X  ----------------------------------
  12.  
  13.  typedef int ** matriz;
  14.    
  15.  typedef struct {
  16.     int x, y, peso;
  17.  } arista;
  18.  
  19.  typedef arista tipo_elemento;
  20.  
  21.  typedef struct {
  22.     int cabeza, final, tamano;
  23.     tipo_elemento vector[TAM_MAX];
  24.  } cola;
  25.  
  26. // ------------------------------  X  ----------------------------------
  27. // =====================================================================
  28. // ------------------------------  X  ----------------------------------
  29.  
  30.     double microsegundos() {
  31.         /* obtiene la hora del sistema en microsegundos */
  32.         struct timeval t;
  33.        
  34.         if (gettimeofday(&t, NULL) < 0 )
  35.             return 0.0;
  36.         return (t.tv_usec + t.tv_sec * 1000000.0);
  37.     }
  38.  
  39. // ------------------------------  X  ----------------------------------
  40. // =====================================================================
  41. // ------------------------------  X  ----------------------------------
  42.  
  43.  
  44.  
  45. // ------------------------------  X  ----------------------------------
  46. // =====================================================================
  47. // ------------------------------  X  ----------------------------------
  48.  
  49.  void crear_cola(cola *c){
  50.      
  51.      
  52.      c->tamano = 0;
  53.      c->cabeza = 0;
  54.      c->final = -1;
  55.  }
  56.  
  57. // ------------------------------  X  ----------------------------------
  58. // =====================================================================
  59. // ------------------------------  X  ----------------------------------
  60.  
  61.  int cola_vacia(cola c){
  62.    
  63.    
  64.     return (c.tamano==0);
  65.  }
  66.  
  67. // ------------------------------  X  ----------------------------------
  68. // =====================================================================
  69. // ------------------------------  X  ----------------------------------
  70.  
  71.  void incrementar(int *x){ /*ESTO DEBE SER PRIVADO*/
  72.    
  73.    
  74.     if (++(*x) == TAM_MAX)
  75.         *x = 0;
  76.  }
  77.  
  78. // ------------------------------  X  ----------------------------------
  79. // =====================================================================
  80. // ------------------------------  X  ----------------------------------
  81.  
  82.  void insertar(tipo_elemento x, cola * c){
  83.    
  84.    
  85.     if (c->tamano == TAM_MAX){
  86.         printf("error: cola llena: %d\n", c->tamano);
  87.         exit(EXIT_FAILURE);
  88.     }
  89.     c->tamano++;
  90.     incrementar(&(c->final));
  91.     c->vector[c->final] = x;
  92.  }
  93.  
  94. // ------------------------------  X  ----------------------------------
  95. // =====================================================================
  96. // ------------------------------  X  ----------------------------------
  97.  
  98.  tipo_elemento primero(cola c){
  99.    
  100.    
  101.     if (cola_vacia(c)){
  102.         printf("error: cola vacia\n");
  103.         exit(EXIT_FAILURE);
  104.     }
  105.     return(c.vector[c.cabeza]);
  106.  }
  107.  
  108. // ------------------------------  X  ----------------------------------
  109. // =====================================================================
  110. // ------------------------------  X  ----------------------------------
  111.  
  112.  tipo_elemento quitar_primero(cola *c){
  113.     tipo_elemento x;
  114.    
  115.    
  116.     if (cola_vacia(*c)) {
  117.         printf("error: cola vacia\n");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.     c->tamano--;
  121.     x = c->vector[c->cabeza];
  122.     incrementar(&(c->cabeza));
  123.     return x;
  124.  }
  125.  
  126. // ------------------------------  X  ----------------------------------
  127. // =====================================================================
  128. // ------------------------------  X  ----------------------------------
  129.  
  130. void mostrar_cola(cola c){
  131.     int j = c.tamano;
  132.     int cont = 0;
  133.  
  134.  
  135.     printf("Aristas: \n");
  136.         for(int i = 0; i<j; i++){
  137.             printf("(%i,%i) ",primero(c).x,
  138.             primero(c).y);
  139.             cont += primero(c).peso;
  140.             quitar_primero(&c);
  141.         }
  142.     printf("\nPeso: %i\n\n", cont);
  143. }
  144.  
  145. // ------------------------------  X  ----------------------------------
  146. // =====================================================================
  147. // ------------------------------  X  ----------------------------------
  148.  
  149.  matriz crear_matriz(int n) {
  150.     int i;
  151.     matriz aux;
  152.    
  153.    
  154.     if ((aux = malloc(n*sizeof(int *))) == NULL)
  155.         return NULL;
  156.     for (i=0; i<n; i++)
  157.         if ((aux[i] = malloc(n*sizeof(int))) == NULL)
  158.             return NULL;
  159.     return aux;
  160.  }
  161.  
  162. // ------------------------------  X  ----------------------------------
  163. // =====================================================================
  164. // ------------------------------  X  ----------------------------------
  165.  
  166.  void inicializar_matriz(matriz m, int n) {
  167.     /* Crea un grafo completo no dirigido con valores aleatorios entre 1 y n */
  168.     int i, j;
  169.  
  170.    
  171.     for (i=0; i<n; i++)
  172.         for (j=i+1; j<n; j++)
  173.             m[i][j] = rand() % n + 1;
  174.  
  175.     for (i=0; i<n; i++)
  176.         for (j=0; j<=i; j++)
  177.             if (i==j)
  178.                 m[i][j] = 0;
  179.             else
  180.                 m[i][j] = m[j][i];
  181.  }
  182.  
  183. // ------------------------------  X  ----------------------------------
  184. // =====================================================================
  185. // ------------------------------  X  ----------------------------------
  186.  
  187.  void liberar_matriz(matriz m, int n) {
  188.    
  189.    
  190.     int i;
  191.     for (i=0; i<n; i++)
  192.         free(m[i]);
  193.     free(m);
  194.  }
  195.  
  196. // ------------------------------  X  ----------------------------------
  197. // =====================================================================
  198. // ------------------------------  X  ----------------------------------
  199.  
  200.  void prim(matriz m, int nodos, cola *aristas) {
  201.     int min, i, j, k=0;
  202.     int cont = 0;
  203.     arista a;
  204.     int *masProximo = (int *) malloc(nodos*sizeof(int));
  205.     int *distanciaMinima = (int *) malloc(nodos*sizeof(int));
  206.    
  207.    
  208.     crear_cola(aristas);
  209.     distanciaMinima[0] = -1;
  210.     for(i = 1; i < nodos; i++) {
  211.         masProximo[i] = 0;
  212.         distanciaMinima[i] = m[i][0];
  213.     }
  214.       while (cont<(nodos-1)){
  215.       cont++;
  216.       min = INT_MAX;
  217.         for(j=1; j<nodos; j++){
  218.             if ((0 <=distanciaMinima[j]) && (distanciaMinima[j]<min)){
  219.                 min = distanciaMinima[j];
  220.                 k=j;
  221.             }
  222.         }  
  223.      a.x =  masProximo[k];
  224.      a.y = k;
  225.      a.peso =m[masProximo[k]][k];
  226.      insertar(a, aristas);
  227.      distanciaMinima[k] = -1;
  228.      for(j=1; j<nodos; j++){
  229.         if (m[j][k] < distanciaMinima[j]){
  230.             distanciaMinima[j] = m[j][k];
  231.             masProximo[j] = k;  
  232.          }
  233.      }
  234.      }
  235.     free(masProximo);
  236.     free(distanciaMinima);
  237.  }
  238.  
  239. // ------------------------------  X  ----------------------------------
  240. // =====================================================================
  241. // ------------------------------  X  ----------------------------------
  242.  
  243.      void filaTabla(double time, int n, int flag) {
  244.         double cotasub, cotaaxus, cotasobre;
  245.        
  246.         cotasub= time / (pow(n, 1.8));
  247.         cotaaxus= time / (pow(n,2));
  248.         cotasobre= time / (pow(n,2.2));
  249.         if (flag==0) {
  250.             printf("%13d%17f%14f%14f%14f\n", n, time, cotasub, cotaaxus,
  251.                cotasobre);
  252.         } else {
  253.             printf("%3s%10d%17f%14f%14f%14f\n","(*)", n, time, cotasub, cotaaxus,
  254.                cotasobre);
  255.         }
  256.     }
  257.  
  258. // ------------------------------  X  ----------------------------------
  259. // =====================================================================
  260. // ------------------------------  X  ----------------------------------
  261.  
  262.  double comprobacionTiempo (int nodos) {
  263.         int cont = 0;
  264.         double atime, btime;
  265.         double time;
  266.         matriz M;
  267.         cola queue;
  268.        
  269.        
  270.         atime = microsegundos();
  271.         while (cont <= 10000) {
  272.             M = crear_matriz(nodos);
  273.             inicializar_matriz(M, nodos);
  274.             prim(M, nodos, &queue);
  275.             liberar_matriz(M, nodos);
  276.             cont++;
  277.         }
  278.         btime = microsegundos();
  279.         time = btime - atime;
  280.        
  281.         cont = 0;
  282.         atime = microsegundos();
  283.         while (cont <= 10000) {
  284.             M = crear_matriz(nodos);
  285.             inicializar_matriz(M, nodos);
  286.             liberar_matriz(M, nodos);
  287.             cont++;
  288.         }
  289.         btime = microsegundos();
  290.         time = time - (btime - atime);
  291.         time = time / cont;
  292.        
  293.         return time;
  294.     }
  295.    
  296. // ------------------------------  X  ----------------------------------
  297. // =====================================================================
  298. // ------------------------------  X  ----------------------------------
  299.  
  300.     void test2() {
  301.         int nodos;
  302.         matriz M;
  303.         cola queue;
  304.         double a_time, b_time, t_time;
  305.         int flag;
  306.        
  307.         printf("Cálculo de tiempos del algoritmo de Prim. \n");
  308.         printf("========================================================================\n");
  309.         printf("%13s%17s%14s%14s%14s\n", "n", "t(n)", "t(n)/n^1.8",
  310.                 "t(n)/n^2", "t(n)/n^2.2");
  311.         for (nodos = 25; nodos <= 1600; nodos = nodos * 2) {
  312.             flag = 0;
  313.             a_time = microsegundos();
  314.             M = crear_matriz(nodos);
  315.             inicializar_matriz(M, nodos);
  316.             prim(M, nodos, &queue);
  317.             liberar_matriz(M, nodos);
  318.             b_time = microsegundos();
  319.             t_time = b_time - a_time;
  320.             if (t_time < 500) {
  321.                 t_time = comprobacionTiempo(nodos);
  322.                 flag = 1;
  323.             }
  324.             filaTabla(t_time, nodos, flag);
  325.         }
  326.     }
  327.  
  328. // ------------------------------  X  ----------------------------------
  329. // =====================================================================
  330. // ------------------------------  X  ----------------------------------
  331.  
  332.  void matrizTest(matriz mTest, int flag){
  333.    
  334.     int m1[4][4]= {{0,1,4,7},{1,0,2,8},{4,2,0,3},{7,8,3,0}};
  335.     int m2[5][5]= {{0,1,8,4,7},{1,0,2,6,5},{8,2,0,9,5},
  336.                     {4,6,9,0,3},{7,5,5,3,0}};
  337.     int m3[7][7]= {{0,7,99,5,99,99,99},{7,0,8,9,7,99,99},
  338.                     {99,8,0,99,5,99,99},{5,9,99,0,15,6,99},
  339.                     {99,7,5,15,0,8,9},{99,99,99,6,8,0,11},
  340.                     {99,99,99,99,9,11,0}};
  341.  
  342.     if (flag==1){
  343.         for(int i=0; i<4; i++){
  344.             for(int j=0;j<4;j++){
  345.             mTest[i][j] = m1[i][j];
  346.             }
  347.         }
  348.     } else if(flag==2){
  349.        
  350.         for(int i=0; i<5; i++){
  351.             for(int j=0;j<5;j++){
  352.             mTest[i][j] = m2[i][j];
  353.             }
  354.         }
  355.     } else{
  356.         for(int i=0; i<7; i++){
  357.             for(int j=0;j<7;j++){
  358.             mTest[i][j] = m3[i][j];
  359.             }
  360.         }
  361.     }  
  362.  
  363.     return;
  364. }
  365.  
  366. // ------------------------------  X  ----------------------------------
  367. // =====================================================================
  368. // ------------------------------  X  ----------------------------------
  369.  
  370.  void test1() {
  371.     matriz m1 = crear_matriz(4);
  372.     matriz m2 = crear_matriz(5);
  373.     matriz m3 = crear_matriz(7);
  374.     cola c;
  375.    
  376.    
  377.     matrizTest(m1,1);
  378.     prim(m1, 4, &c);
  379.     mostrar_cola(c);
  380.    
  381.     matrizTest(m2,2);
  382.     prim(m2, 5, &c);
  383.     mostrar_cola(c);
  384.    
  385.     matrizTest(m3,3);
  386.     prim(m3, 7, &c);
  387.     mostrar_cola(c);
  388.    
  389.     liberar_matriz(m1,4);
  390.     liberar_matriz(m2,5);
  391.     liberar_matriz(m3,7);
  392.  
  393.  }
  394.  
  395. // ------------------------------  X  ----------------------------------
  396. // =====================================================================
  397. // ------------------------------  X  ----------------------------------
  398.  
  399.  int main() {
  400.      test1();
  401.      test2();
  402.      return 0;
  403.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement