Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. void createATour()
  2. {
  3. int nextNeighbor, lowestCity, backtrackCount, backtrackedNeighbor;
  4.  
  5. nextNeighbor = totalNumberOfStops[1]->_city;
  6. totalNumberOfStops[1]->_visited = TRUE;
  7. currentVisitedCitiesCount = 1;
  8.  
  9. int tempFlag;
  10. tempFlag = 0;
  11.  
  12. backtrackCount = 0;
  13. while (currentVisitedCitiesCount != totalCities)
  14. {
  15. #ifdef DEBUG_LOG
  16. printf("We are CURRENTLY at city %d. Our neighbor's head is %d. Number of neighbors is %d\n", nextNeighbor, totalNumberOfStops[nextNeighbor]->_neighborsHead, totalNumberOfStops[nextNeighbor]->_numberOfNeighbors);
  17. // if (nextNeighbor == 1)
  18. // {
  19. // int head;
  20. // printf("Backtracked back to city 1. %d iteration of backtracking\n", backtrackCount);
  21. // head = totalNumberOfStops[1]->_neighborsHead;
  22. // printf("City's neighbor's head is: %d\n", totalNumberOfStops[1]->_neighbors[head]);
  23. // backtrackCount++;
  24. // }
  25. #endif
  26.  
  27. int i;
  28.  
  29. //Find an unvisited city, and move on from there.
  30. for (i = totalNumberOfStops[nextNeighbor]->_neighborsHead; i <= totalNumberOfStops[nextNeighbor]->_numberOfNeighbors - 1; i++)
  31. {
  32. lowestCity = totalNumberOfStops[nextNeighbor]->_neighbors[i];
  33.  
  34. totalNumberOfStops[nextNeighbor]->_neighborsHead++;
  35. if (totalNumberOfStops[lowestCity]->_visited == TRUE)
  36. {
  37. continue;
  38. }
  39. else
  40. {
  41. //Set the next neighbor to be visited as our next City
  42. totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[lowestCity];
  43. //Set the previous neighbor of the city to be visited as our current city
  44. totalNumberOfStops[lowestCity]->_previousCity = totalNumberOfStops[nextNeighbor];
  45.  
  46. //We move on to next city
  47. nextNeighbor = lowestCity;
  48. totalNumberOfStops[nextNeighbor]->_visited = TRUE;
  49. currentVisitedCitiesCount++;
  50. //printf("Visited Cities count is: %d\n", currentVisitedCitiesCount);
  51.  
  52. #ifdef DEBUG_LOG
  53. printf("Next going to city: %d | Neighbors Head: %d | Number of Neighbors: %d\n", totalNumberOfStops[nextNeighbor]->_city, totalNumberOfStops[nextNeighbor]->_neighborsHead, totalNumberOfStops[nextNeighbor]->_numberOfNeighbors);
  54. #endif
  55.  
  56. break;
  57. }
  58. }
  59.  
  60. if (currentVisitedCitiesCount == totalCities)
  61. {
  62. int connectedCityIndex, flag;
  63. flag = 0;
  64. for (connectedCityIndex = 0; connectedCityIndex < totalNumberOfStops[nextNeighbor]->_numberOfNeighbors; connectedCityIndex++)
  65. {
  66. if (totalNumberOfStops[nextNeighbor]->_neighbors[connectedCityIndex] == 1)
  67. {
  68. //We can connect to city 1 from the last city in the tour, set flag to 1
  69. #ifdef DEBUG_LOG
  70. printf("We CAN reach city 1 from our last city. At city %d. Let's finish the tour...\n", totalNumberOfStops[nextNeighbor]->_city);
  71. #endif
  72.  
  73. flag = 1;
  74. break;
  75. }
  76. }
  77. //We found city 1, we can end the tour now
  78. if (flag == 1)
  79. {
  80. tempFlag = 0;
  81. break;
  82. }
  83. else
  84. {
  85. //Haven't found city 1, means we cannot end the tour. Backtrack again.
  86. #ifdef DEBUG_LOG
  87. printf("CANNOT reach city 1 from last city. At city %d. Creating edge...\n", nextNeighbor);
  88. #endif
  89.  
  90. tempFlag = 1;
  91. break;
  92.  
  93. }
  94. }
  95. else if (totalNumberOfStops[nextNeighbor]->_neighborsHead == totalNumberOfStops[nextNeighbor]->_numberOfNeighbors)
  96. {
  97. #ifdef DEBUG_LOG
  98. printf("DEAD END. At city %d. Backtracking...\n", nextNeighbor);
  99. #endif
  100.  
  101. backtrackedNeighbor = backtrackToPreviousCity(totalNumberOfStops[nextNeighbor]);
  102. nextNeighbor = backtrackedNeighbor;
  103. }
  104.  
  105. }
  106.  
  107. if (tempFlag != 1)
  108. {
  109. //Set the last city's next city in our makeshift tour to be city 1
  110. totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[1];
  111. }
  112. else
  113. {
  114. totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[1];
  115. totalNumberOfStops[nextNeighbor]->_distances[1] = 1000.000000;
  116. }
  117.  
  118. printf("The final city is: %d\n", nextNeighbor);
  119.  
  120. printf("Done with creating a tour. we hope\n");
  121. }
Add Comment
Please, Sign In to add comment