home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / 2DLab / functions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-14  |  5.7 KB  |  222 lines

  1. #include <math.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include "TwoDView.h"
  5. #include "tspheaders.h"
  6. #include "prototypes.h"
  7. /********************************************************************/
  8. /* This file contains a number of miscellaneous functions used in all
  9.    of the TSP algorithms.
  10. */
  11. /********************************************************************/
  12.  
  13.  
  14.  
  15. /********************************************************************/
  16. /* Function: kfrom
  17.    given i and j, figure out what the k coordinate is.
  18. */
  19. /********************************************************************/
  20. int kfrom(int l, int m, int n)
  21. int i,j,k;
  22.  
  23. if (l < m) {
  24.   i = l;
  25.   j = m;
  26. }
  27. else {
  28.   i = m;
  29.   j = l;
  30. }
  31.  
  32. k = i*n - i*(i+1)/2+j-i-1;
  33. return(k);
  34. }     
  35.  
  36. /********************************************************************/
  37. /* Function: ClosestCities
  38.    given a list of cities, figure out which two are closest, and
  39.    return the distance between them.  
  40.    From and To will contain the indices of the cities.
  41.    The function returns the distance between them.
  42. */
  43. /********************************************************************/
  44. float ClosestCities(float *Distance, int *From, int *To, int NumberOfCities) {
  45. int i,j,k;
  46. float MinDistance;
  47.  
  48. MinDistance = INT_MAX;
  49. for(i=0;i<NumberOfCities;i++) {
  50.   for(j=(i+1);j<NumberOfCities;j++) {
  51.     k = kfrom(i,j,NumberOfCities);
  52.     if (Distance[k] < MinDistance) {
  53. #ifdef DEBUGMYDIR
  54.       printf("k is %d i is %d j is %d \n",k,i,j);
  55.       printf("changing mindistance from %f to %f \n",MinDistance,Distance[k]);
  56. #endif
  57.       MinDistance = Distance[k];
  58.       *From = i;
  59.       *To = j;
  60.     }
  61.   }
  62. }
  63.  
  64. return(MinDistance);
  65. }
  66.  
  67. /********************************************************************/
  68. /* Function: FarthestCities
  69.    given a list of cities, figure out which two are farthest, and
  70.    return the distance between them.
  71.    From and To will contain the indices of the cities.
  72.    The function returns the distance between them.
  73. */
  74. /********************************************************************/
  75. float FarthestCities(float *Distance, int *From, int *To, int NumberOfCities) {
  76. int i,j,k;
  77. float BigDistance;
  78.  
  79. BigDistance = 0;
  80. for(i=0;i<NumberOfCities;i++) {
  81.   for(j=(i+1);j<NumberOfCities;j++) {
  82.     k = kfrom(i,j,NumberOfCities);
  83.     if (Distance[k] > BigDistance) {
  84.  #ifdef DEBUGMYDIR
  85.       printf("k is %d i is %d j is %d \n",k,i,j);
  86.       printf("changing bigdistance from %f to %f \n",BigDistance,Distance[k]);
  87.  #endif
  88.       BigDistance = Distance[k];
  89.       *From = i;
  90.       *To = j;
  91.     }
  92.   }
  93. }
  94.  
  95. return(BigDistance);
  96. }
  97.  
  98.  
  99. /********************************************************************/
  100. /* Function: ValidTour
  101.    given a tour and 2 edges to exchange, figure out if the tour is
  102.    valid if the two edges are exchanged.
  103. */
  104. /********************************************************************/
  105. int ValidTour(int *Tour, int Edge1, int Edge2, int Reverse, 
  106.               int NumberOfCities) {
  107.  
  108. int i,To1,BeginningCity,CurrentCity,GoingThroughTour,IsATour,
  109.     StillLooking,AtEdge,NextEdge,NumberOfCitiesVisited;
  110.  
  111. int *TestingTour;
  112.  
  113. /* allocate space for TestingTour */
  114. TestingTour = (int *)malloc(2*NumberOfCities*sizeof(int));
  115. if (TestingTour == (int *) 0)
  116.   printf("Malloc for TestingTour failed. \n");
  117.  for (i=0;i<2*NumberOfCities;i+=2) {
  118.    TestingTour[i] = Tour[i];
  119.    TestingTour[i+1] = Tour[i+1];
  120.  }
  121.  
  122. /* exchange the edges in the tour. */
  123. /* if Reverse == 1, then make To From and From To. */
  124. if (Reverse) {
  125.   To1 = TestingTour[2*Edge1+1];
  126.   TestingTour[2*Edge1+1] = TestingTour[2*Edge2+1];
  127.   TestingTour[2*Edge2+1] = To1;
  128. }
  129. else {
  130. /* if Reverse == 0, then take the second edge as is. */
  131.   To1 = TestingTour[2*Edge1+1];
  132.   TestingTour[2*Edge1+1] = TestingTour[2*Edge2];
  133.   TestingTour[2*Edge2] = To1;
  134. }
  135.  
  136. GoingThroughTour = 1;
  137. IsATour = 0;
  138. BeginningCity = TestingTour[0];
  139. CurrentCity = TestingTour[1];
  140. NumberOfCitiesVisited = 0;
  141. AtEdge = 0;
  142.  
  143. while (GoingThroughTour) {
  144.   /* set NextEdge to be the next edge after AtEdge in the tour. */
  145.   if (AtEdge >= (NumberOfCities-1)) {
  146.     NextEdge = 0;
  147.   }
  148.   else {
  149.     NextEdge = AtEdge + 1;
  150.   }
  151.  
  152.   StillLooking = 1;
  153.   while ((StillLooking) && (GoingThroughTour)) {
  154.     
  155.     /* look to see if the To of NextEdge is the city we want. */
  156.     if (TestingTour[2*NextEdge] == CurrentCity) {
  157.       NumberOfCitiesVisited+=1;
  158.       if (CurrentCity == BeginningCity) {
  159.         if (NumberOfCitiesVisited == NumberOfCities) {
  160.           /* then we have found a valid tour. */
  161.           IsATour = 1;
  162.           GoingThroughTour = 0;
  163.         }
  164.         else {
  165.           /* otherwise, this tour has subtours. */
  166.           GoingThroughTour = 0;
  167.         }
  168.       }
  169.       CurrentCity = TestingTour[2*NextEdge+1];
  170.       AtEdge = NextEdge;
  171.       StillLooking = 0;
  172.     }
  173.     else {
  174.       /* look to see if the From of NextEdge is the city we want. */
  175.       if (TestingTour[2*NextEdge+1] == CurrentCity) {
  176.         NumberOfCitiesVisited+=1;
  177.         if (CurrentCity == BeginningCity) {
  178.           if (NumberOfCitiesVisited == NumberOfCities) {
  179.             /* then we have found a valid tour. */
  180.             IsATour = 1;
  181.             GoingThroughTour = 0;
  182.           }
  183.           else {
  184.             /* otherwise, this tour has subtours. */
  185.             GoingThroughTour = 0;
  186.           }
  187.         }
  188.             
  189.         CurrentCity = TestingTour[2*NextEdge];
  190.         AtEdge = NextEdge;
  191.         StillLooking = 0;
  192.       }
  193.     }
  194.  
  195.     /* get ready to look at the next edge. */
  196.     if (AtEdge >= (NumberOfCities-1)) {
  197.       if (NextEdge == (AtEdge-1)) {
  198.         StillLooking = 0;
  199.         GoingThroughTour = 0;
  200.       }
  201.       else {
  202.         NextEdge+=1;
  203.       }
  204.     }
  205.     else {
  206.       if (NextEdge >= (NumberOfCities - 1)) {
  207.         NextEdge = 0;
  208.       }
  209.       else {
  210.         NextEdge+=1;
  211.       }
  212.     }
  213.   }
  214. }
  215. free(TestingTour);
  216. return(IsATour);
  217. }
  218.   
  219.   
  220.   
  221.