Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #include "TSP.h" // Gets the Node data type and the function prototypes
  2. #include <stdlib.h> // Needed to get the NULL constant
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <float.h>
  6.  
  7. /**********************************************************
  8. *Name : Brandon Cox
  9. *Username : bpcox
  10. *Description :This program sorts a set of numbers into a linked list to try and find the most effecient path
  11. Constants that define the method to use when adding points to the tour
  12. Replace the body of all the functions in this file.
  13. *********************************************************/
  14.  
  15. // Print out the point stored at the given node.
  16. // You can assume node is not NULL.
  17. // Round to 4 decimal places and output a line feed (\n), e.g.:
  18. // (1.2345, 6.7890)
  19. void printNode(Node* node)
  20. {
  21. Node * print = node;
  22. while(print !=NULL)
  23. {
  24. printf("(%lf, %lf)",print->x,print->y);
  25. print = print -> next;
  26. }
  27. }
  28.  
  29. // Print out all the points in the tour from first to last.
  30. // Passed a pointer to the first node in the tour.
  31. // If the first is NULL, doesn't print anything.
  32. void printTour(Node* first)
  33. {
  34. Node * print = first;
  35. while(print != NULL)
  36. {
  37. printf("(%.4lf, %.4lf)\n",print->x,print->y);
  38. print = print -> next;
  39. }
  40. }
  41.  
  42. // Get the number of points in the tour.
  43. // Passed a pointer to the first node in the tour.
  44. // If first is NULL, return a size of 0.
  45. int tourSize(Node* first)
  46. {
  47. if (first == NULL)
  48. {
  49. return 0;
  50. }
  51. int count = 0;
  52. Node * node = first;
  53. while(node != NULL)
  54. {
  55. count = count + 1;
  56. node = node -> next;
  57. }
  58.  
  59. return count;
  60. }
  61.  
  62. // Calculate the Euclidean distance between two nodes.
  63. // You can assume both a and b are both not NULL.
  64. double distance(Node* a, Node* b)
  65. {
  66. double xdist = 0;
  67. double ydist = 0;
  68. double dist = 0;
  69. Node * var2 = a;
  70. Node * var1 = b;
  71. xdist = ((var2 -> x) - (var1 -> x));
  72. ydist = ((var2 -> y) - (var1 -> y));
  73. dist = sqrt((xdist * xdist + ydist * ydist));
  74. return dist;
  75. }
  76.  
  77. // Calculate the total distance between all points in the tour.
  78. // Since the tour is circular, this includes the distance from the last point back to the start.
  79. // Passed a pointer to the first node in the tour.
  80. // If first is NULL, return a tour length of 0.0.
  81. double tourDistance(Node* first)
  82. {
  83. double dist = 0;
  84. Node * node = first;
  85. Node * temp = node;
  86. if (node == NULL)
  87. {
  88. return 0;
  89. }
  90. while(node->next != NULL)
  91. {
  92. dist = distance(node->next,node)+ dist;
  93. node = node -> next;
  94. }
  95. dist = distance(node, temp) + dist;
  96.  
  97. return dist;
  98. }
  99.  
  100. // Add a new point after the point that it is closest to.
  101. // If there is a tie, insert it after the first such point you find.
  102. // Passed a pointer to the first node in the tour, NULL if creating a new tour.
  103. // Returns pointer to the first node in the linked list after the addition.
  104. Node* addNearestNeighbor(Node* first, double x, double y)
  105. {
  106. double temp;
  107. int count = 0;
  108. int pos = 0;
  109. Node* node = first;
  110. Node* node2 = first;
  111. double dist = DBL_MAX;
  112. Node* tempn = first;
  113. tempn = malloc(sizeof(first));
  114. tempn->x = x;
  115. tempn->y = y;
  116. while (node != NULL)
  117. {
  118.  
  119. temp = distance(tempn, node);
  120. count = count + 1;
  121. if (temp < dist && temp > 0)
  122. {
  123. dist = temp;
  124. pos = count;
  125. }
  126. node = node->next;
  127. }
  128. node2 = malloc(sizeof(first));
  129. if ((pos == count) || (pos == 0))
  130. {
  131. Node* last = first;
  132. node2->x = x;
  133. node2->y = y;
  134. node2->next = NULL;
  135.  
  136. if (first == NULL)
  137. {
  138. first = node2;
  139. return first;
  140. }
  141.  
  142. while (last->next != NULL)
  143. {
  144. last = last->next;
  145. }
  146. last->next = node2;
  147. return first;
  148. }
  149. else
  150. {
  151. Node* prev = first;
  152. node2->x = x;
  153. node2->y = y;
  154. while (pos != 1)
  155. {
  156. prev = prev->next;
  157. pos = pos - 1;
  158. }
  159. node2->next = prev->next;
  160. prev->next = node2;
  161.  
  162. return first;
  163. }
  164. }
  165.  
  166. // Add a new point after the point where it results in the least increase in tour length.
  167. // If there is a tie, insert it after the first such point you find.
  168. // Passed a pointer to the first node in the tour, NULL if creating a new tour.
  169. // Returns pointer to the first node in the linked list after the addition.
  170. Node* addSmallestIncrease(Node* first, double x, double y)
  171. {
  172. double temp;
  173. int count = 0;
  174. int pos = 0;
  175. Node* node = first;
  176. Node* node2 = first;
  177. double dist = DBL_MAX;
  178. Node* tempn = first;
  179. tempn = malloc(sizeof(first));
  180. tempn->x = x;
  181. tempn->y = y;
  182. while (node != NULL)
  183. {
  184. if (node->next != NULL)
  185. {
  186. temp = distance(tempn, node) + distance(tempn, node->next) - distance(node, node->next);
  187. }
  188. else
  189. {
  190. temp = distance(tempn, first) + distance(tempn, node) - distance(node, first);
  191. }
  192. count = count + 1;
  193. if (temp < dist)
  194. {
  195. dist = temp;
  196. pos = count;
  197. }
  198. node = node->next;
  199. }
  200.  
  201.  
  202. node2 = malloc(sizeof(first));
  203. if ((pos == count) || (pos == 0))
  204. {
  205. Node* last = first;
  206. node2->x = x;
  207. node2->y = y;
  208. node2->next = NULL;
  209.  
  210. if (first == NULL)
  211. {
  212. first = node2;
  213. return first;
  214. }
  215.  
  216. while (last->next != NULL)
  217. {
  218. last = last->next;
  219. }
  220. last->next = node2;
  221. freeTour(node2);
  222. return first;
  223. }
  224. else
  225. {
  226. Node* prev = first;
  227. node2->x = x;
  228. node2->y = y;
  229. while (pos != 1)
  230. {
  231. prev = prev->next;
  232. pos = pos - 1;
  233. }
  234. node2->next = prev->next;
  235. prev->next = node2;
  236.  
  237. return first;
  238. }
  239. }
  240.  
  241. // Deallocate all the memory of the Node structures in the linked list.
  242. // Passed a pointer to the first node in the tour.
  243. // If first is NULL, don't do anything.
  244. void freeTour(Node* first)
  245. {
  246. Node* current = first;
  247. while (current != NULL)
  248. {
  249. Node* after = current->next;
  250. free(current);
  251. current = after;
  252. }
  253. first = NULL;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement