Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void createATour()
- {
- int nextNeighbor, lowestCity, backtrackCount, backtrackedNeighbor;
- nextNeighbor = totalNumberOfStops[1]->_city;
- totalNumberOfStops[1]->_visited = TRUE;
- currentVisitedCitiesCount = 1;
- int tempFlag;
- tempFlag = 0;
- backtrackCount = 0;
- while (currentVisitedCitiesCount != totalCities)
- {
- #ifdef DEBUG_LOG
- 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);
- // if (nextNeighbor == 1)
- // {
- // int head;
- // printf("Backtracked back to city 1. %d iteration of backtracking\n", backtrackCount);
- // head = totalNumberOfStops[1]->_neighborsHead;
- // printf("City's neighbor's head is: %d\n", totalNumberOfStops[1]->_neighbors[head]);
- // backtrackCount++;
- // }
- #endif
- int i;
- //Find an unvisited city, and move on from there.
- for (i = totalNumberOfStops[nextNeighbor]->_neighborsHead; i <= totalNumberOfStops[nextNeighbor]->_numberOfNeighbors - 1; i++)
- {
- lowestCity = totalNumberOfStops[nextNeighbor]->_neighbors[i];
- totalNumberOfStops[nextNeighbor]->_neighborsHead++;
- if (totalNumberOfStops[lowestCity]->_visited == TRUE)
- {
- continue;
- }
- else
- {
- //Set the next neighbor to be visited as our next City
- totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[lowestCity];
- //Set the previous neighbor of the city to be visited as our current city
- totalNumberOfStops[lowestCity]->_previousCity = totalNumberOfStops[nextNeighbor];
- //We move on to next city
- nextNeighbor = lowestCity;
- totalNumberOfStops[nextNeighbor]->_visited = TRUE;
- currentVisitedCitiesCount++;
- //printf("Visited Cities count is: %d\n", currentVisitedCitiesCount);
- #ifdef DEBUG_LOG
- printf("Next going to city: %d | Neighbors Head: %d | Number of Neighbors: %d\n", totalNumberOfStops[nextNeighbor]->_city, totalNumberOfStops[nextNeighbor]->_neighborsHead, totalNumberOfStops[nextNeighbor]->_numberOfNeighbors);
- #endif
- break;
- }
- }
- if (currentVisitedCitiesCount == totalCities)
- {
- int connectedCityIndex, flag;
- flag = 0;
- for (connectedCityIndex = 0; connectedCityIndex < totalNumberOfStops[nextNeighbor]->_numberOfNeighbors; connectedCityIndex++)
- {
- if (totalNumberOfStops[nextNeighbor]->_neighbors[connectedCityIndex] == 1)
- {
- //We can connect to city 1 from the last city in the tour, set flag to 1
- #ifdef DEBUG_LOG
- printf("We CAN reach city 1 from our last city. At city %d. Let's finish the tour...\n", totalNumberOfStops[nextNeighbor]->_city);
- #endif
- flag = 1;
- break;
- }
- }
- //We found city 1, we can end the tour now
- if (flag == 1)
- {
- tempFlag = 0;
- break;
- }
- else
- {
- //Haven't found city 1, means we cannot end the tour. Backtrack again.
- #ifdef DEBUG_LOG
- printf("CANNOT reach city 1 from last city. At city %d. Creating edge...\n", nextNeighbor);
- #endif
- tempFlag = 1;
- break;
- }
- }
- else if (totalNumberOfStops[nextNeighbor]->_neighborsHead == totalNumberOfStops[nextNeighbor]->_numberOfNeighbors)
- {
- #ifdef DEBUG_LOG
- printf("DEAD END. At city %d. Backtracking...\n", nextNeighbor);
- #endif
- backtrackedNeighbor = backtrackToPreviousCity(totalNumberOfStops[nextNeighbor]);
- nextNeighbor = backtrackedNeighbor;
- }
- }
- if (tempFlag != 1)
- {
- //Set the last city's next city in our makeshift tour to be city 1
- totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[1];
- }
- else
- {
- totalNumberOfStops[nextNeighbor]->_nextCity = totalNumberOfStops[1];
- totalNumberOfStops[nextNeighbor]->_distances[1] = 1000.000000;
- }
- printf("The final city is: %d\n", nextNeighbor);
- printf("Done with creating a tour. we hope\n");
- }
Add Comment
Please, Sign In to add comment