Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SET UP VARIABLES FOR LOOPING
- //
- // ALWAYS leave corporateHomeCity for startingCity
- // Add corporateHomeCity to citiesVisited
- citiesVisited.add(corporateHomeCity);
- // Add distance to startingCity
- ArrayList<Integer> aCityArrayList = citiesMap.get(new Integer(corporateHomeCity));
- totalDistanceOfTour += aCityArrayList.get(startingCity);
- // shrink map
- citiesMap.remove(new Integer(corporateHomeCity));
- // sets processThisCity to starting tour stop, the startingCity
- processThisCity = startingCity;
- //
- // THE LOOP
- //
- while( !citiesMap.isEmpty() ) {
- //
- aCityArrayList = citiesMap.get(new Integer(processThisCity));
- // loop set up
- cityClosestDistance = 0;
- //
- for(int i = 0; i < aCityArrayList.size(); i++) {
- if( !citiesVisited.contains(i) ) {
- if( processThisCity!=i ) {
- loopCityDistance = aCityArrayList.get(i);
- if (cityClosestDistance==0) {
- cityClosestDistance = loopCityDistance;
- cityClosest = i;
- }
- if (cityClosestDistance > loopCityDistance) {
- cityClosestDistance = loopCityDistance;
- cityClosest = i;
- }
- }
- }
- }
- //
- // adds closest city to list of cities that have been visited.
- citiesVisited.add(processThisCity);
- // last city get distance to starting city
- if( citiesMap.size()==1 ) {
- // adds the distance between the last city and the starting city
- totalDistanceOfTour += aCityArrayList.get(new Integer(corporateHomeCity));
- } else {
- // adds the distance between the currentCity and the closest city to the total distance traveled.
- totalDistanceOfTour += cityClosestDistance;
- }
- // shrink map
- citiesMap.remove(new Integer(processThisCity));
- // sets processThisCity to next tour stop, the cityClosest
- processThisCity = cityClosest;
- //
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement