This is a java program to solve TSP using MST.
Here is the source code of the Java Program to Solve TSP Using Minimum Spanning Trees. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | package com.maixuanviet.hardgraph; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; public class TSPUsingMST { // Arrays to keep track of info. related to each city private String[] cityName; private String[] cityState; private int [] cityLat; private int [] cityLong; private int [] cityPop; // 2-D array to keep track of pairwise distances between cities private int [][] distances; // number of cities private static int numCities; public TSPUsingMST( int n) { numCities = n; // Allotting the space for each 1-D array cityName = new String[numCities]; cityState = new String[numCities]; cityLat = new int [numCities]; cityLong = new int [numCities]; cityPop = new int [numCities]; // Allocate space for each 2-D array. These arrays have 0 elements in // row 0, // 1 element in row 1, 2 elements in row 2, etc. distances = new int [numCities][]; for ( int i = 0 ; i < numCities; i++) distances[i] = new int [i]; try { // Construct a buffered reader object and connect it to the files // "miles.dat" BufferedReader in = new BufferedReader( new FileReader( "miles.dat" )); // A counter that keeps track of the index of the current city being // read int cityNumber = 0 ; // While-loop for reading in data from "miles.dat." At the beginning // of the while-loop // the expectation is that we'll be reading a line containing the // city name. Instead, // if we encounter a line that starts with "*" then we skip to the // next line while (cityNumber < numCities) { // Read in a line String line = in.readLine(); // Skip the rest of the loop if line starts with a "*" if (line.charAt( 0 ) == '*' ) continue ; // Otherwise tokenize the line StringTokenizer tokenizedLine = new StringTokenizer(line, ",[]" ); // Putting actual data into correct position in the array cityName[cityNumber] = tokenizedLine.nextToken(); cityState[cityNumber] = (tokenizedLine.nextToken()).trim(); // trim() // gets // rid // of // leading/trailing // blanks cityLat[cityNumber] = Integer.parseInt(tokenizedLine .nextToken()); cityLong[cityNumber] = Integer.parseInt(tokenizedLine .nextToken()); cityPop[cityNumber] = Integer.parseInt(tokenizedLine .nextToken()); // while loop to put distances in the array; this may need to // read several lines int mileNumber = 0 ; while (mileNumber < cityNumber) { // Read a mileage line and tokenize it String mileage = in.readLine(); StringTokenizer tokenizedMileage = new StringTokenizer( mileage, " " ); // Read all the mileage data in this line into row // cityNumber; increment // mileNumber after each read while (tokenizedMileage.hasMoreTokens()) { distances[cityNumber][cityNumber - mileNumber - 1 ] = Integer .parseInt(tokenizedMileage.nextToken()); mileNumber++; } } // end of while reading distances cityNumber++; } // end of while reading cities in.close(); } // end of try catch (IOException e) { System.out.println( "File not found." ); } } // end of TSPTester() constructor // A simple getIndex method to help test the constructor int getIndex(String city, String state) { int location; for (location = 0 ; location < numCities; location++) if ((cityName[location].equals(city)) && (cityState[location].equals(state))) return location; return - 1 ; } // Print information about a city, given a city index void printCityInfo( int index) { System.out .println(cityName[index] + " " + cityState[index] + " " + cityLat[index] + " " + cityLong[index] + " " + cityPop[index]); } // Print distance information between a given pair of cities void printDistanceInfo( int i, int j) { if (i < j) System.out.println(distances[j][i]); else System.out.println(distances[i][j]); } int getDistance( int i, int j) { if (i < j) return distances[j][i]; else if (j < i) return distances[i][j]; else return 0 ; } int [] greedyTSP() { // Find a cheapest triangle // Load triangle 0-1-2 into the the first 3 slots of the greedy array int [] greedy = new int [numCities]; int currentDistance; greedy[ 0 ] = 0 ; greedy[ 1 ] = 1 ; greedy[ 2 ] = 2 ; int currentBestDistance = getDistance( 0 , 1 ) + getDistance( 1 , 2 ) + getDistance( 2 , 0 ); for ( int i = 0 ; i < numCities; i++) for ( int j = 0 ; j < i; j++) for ( int k = 0 ; k < j; k++) if ((currentDistance = getDistance(i, j) + getDistance(j, k) + getDistance(i, k)) < currentBestDistance) { greedy[ 0 ] = i; greedy[ 1 ] = j; greedy[ 2 ] = k; currentBestDistance = currentDistance; } // Try greedily to add a city that yields the smallest increase // in the cost of the tour int partialTourSize = 3 ; boolean [] visited = new boolean [numCities]; for ( int i = 0 ; i < numCities; i++) visited[i] = false ; visited[greedy[ 0 ]] = true ; visited[greedy[ 1 ]] = true ; visited[greedy[ 2 ]] = true ; // Main loop: keep repeating until partial tour covers all cities while (partialTourSize < numCities) { int smallestIncrease = Integer.MAX_VALUE; int increase = 0 ; int bestInsertionPoint = 0 ; int bestCity = 0 ; // Scan through all cities, stopping at unvisited cities for ( int i = 0 ; i < numCities; i++) { if (!visited[i]) { // Consider all possible positions of inserting city i into // the tour // and record the smallest increase for ( int j = 0 ; j < partialTourSize; j++) { increase = getDistance(greedy[j], i) + getDistance(i, greedy[(j + 1 ) % numCities]) - getDistance(greedy[j], greedy[(j + 1 ) % numCities]); if (increase < smallestIncrease) { smallestIncrease = increase; bestCity = i; bestInsertionPoint = j; } // end of if we have found a smaller increase } // end of for-j } // end of if not visited } // end of for-i // Now we are ready to insert the bestCity at the bestInsertionPoint for ( int j = partialTourSize - 1 ; j > bestInsertionPoint; j--) greedy[j + 1 ] = greedy[j]; greedy[bestInsertionPoint + 1 ] = bestCity; visited[bestCity] = true ; partialTourSize++; } // end-while return greedy; } void copy( int [] source, int [] dest) { for ( int i = 0 ; i < dest.length; i++) dest[i] = source[i]; } void TSP( int [] R, int partialTourSize, boolean [] visited, int [] T) { // Base case: we have discovered a tour better than T if ((partialTourSize == numCities) && (cost(R) < cost(T))) { System.out.println( "Base case. Tour cost is " + cost(R)); copy(R, T); return ; } // Another base case: our partial tour is not worth completing if (cost(R, partialTourSize) >= cost(T)) return ; // Recursive case: R is not complete and is currently better than T // and is therefore worth completing for ( int i = 0 ; i < numCities; i++) { if (!visited[i]) { // System.out.println("Appending " + i); visited[i] = true ; R[partialTourSize++] = i; TSP(R, partialTourSize, visited, T); partialTourSize--; visited[i] = false ; // System.out.println("Deleting " + i); } } // end of for-loop } // end of TSP double cost( int [] tour) { return cost(tour, tour.length); } double cost( int [] tour, int tourSize) { double c = 0 ; for ( int i = 0 ; i < tourSize - 1 ; i++) c = c + getDistance(tour[i], tour[i + 1 ]); c = c + getDistance(tour[tourSize - 1 ], tour[ 0 ]); return c; } // Main method public static void main(String[] args) { int n = 15 ; TSPUsingMST T = new TSPUsingMST(n); // Initialize the list of vertices in the tree // Initially, no one except vertex 0 is in the tree boolean [] visited = new boolean [n]; for ( int i = 0 ; i < n; i++) visited[i] = false ; visited[ 0 ] = true ; // Initialize the int[] that maintains the tree to default values // No vertices have parents set, except vertex 0 whose parent is itself int [] tree = new int [n]; for ( int i = 0 ; i < n; i++) tree[i] = - 1 ; tree[ 0 ] = 0 ; for ( int i = 1 ; i <= n - 1 ; i++) { long minWeight = Long.MAX_VALUE; int bestVertex = - 1 ; int bestParent = - 1 ; for ( int j = 0 ; j < n; j++) { for ( int k = 0 ; k < n; k++) { if ((visited[j]) && (!visited[k])) { if (T.getDistance(j, k) < minWeight) { minWeight = T.getDistance(j, k); bestVertex = k; bestParent = j; } // end if better distance is found } // end if an edge between a visited and an unvisited is // found } // end for-k } // end for-j // Update visited and tree visited[bestVertex] = true ; tree[bestVertex] = bestParent; } // end for-i // Printing the MST for ( int i = 1 ; i < n; i++) System.out.println(T.cityName[i] + " " + T.cityState[i] + ", " + T.cityName[tree[i]] + " " + T.cityState[tree[i]]); // Compting the MST cost long cost = 0 ; for ( int i = 0 ; i < n; i++) cost += T.getDistance(i, tree[i]); System.out.println( "The cost of the minimum spanning tree is " + cost); } // end main method } // end class |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ javac TSPUsingMST.java $ java TSPUsingMST Yankton SD, Wisconsin Dells WI Yakima WA, Williston ND Worcester MA, Wilmington DE Wisconsin Dells WI, Youngstown OH Winston-Salem NC, Winchester VA Winnipeg MB, Yankton SD Winchester VA, Wilmington DE Wilmington NC, Winston-Salem NC Wilmington DE, Williamsport PA Williston ND, Winnipeg MB Williamsport PA, Youngstown OH Williamson WV, Winston-Salem NC Wichita Falls TX, Wichita KS Wichita KS, Yankton SD The cost of the minimum spanning tree is 5461 |
Related posts:
Java – Rename or Move a File
Thao tác với tập tin và thư mục trong Java
Đồng bộ hóa các luồng trong Java
Spring Data – CrudRepository save() Method
Spring Boot - Sending Email
Interface trong Java 8 – Default method và Static method
Giới thiệu Aspect Oriented Programming (AOP)
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Checked and Unchecked Exceptions in Java
Java Program to Perform Complex Number Multiplication
Quick Guide to Spring Bean Scopes
Guide to the Java Queue Interface
Introduction to Spring Data REST
Exception Handling in Java
New Stream Collectors in Java 9
Một số ký tự đặc biệt trong Java
How to Get the Last Element of a Stream in Java?
Java Program to Implement Word Wrap Problem
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement JobStateReasons API
Java Program to find the number of occurrences of a given number using Binary Search approach
Spring Boot - Quick Start
Java Program to Implement Max Heap
So sánh ArrayList và LinkedList trong Java
HttpClient Timeout
Java Program to Implement Interpolation Search Algorithm
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Program to Implement HashMap API
Using the Not Operator in If Conditions in Java
Join and Split Arrays and Collections in Java
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Implement ConcurrentHashMap API