Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f563017

Browse files
committed
added TSP
1 parent 937af93 commit f563017

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

‎CCSPiJ/src/chapter9/TSP.java‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// TSP.java
2+
// From Classic Computer Science Problems in Java Chapter 9
3+
// Copyright 2020 David Kopec
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package chapter9;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
public class TSP {
25+
private final Map<String, Map<String, Integer>> distances;
26+
27+
public TSP(Map<String, Map<String, Integer>> distances) {
28+
this.distances = distances;
29+
}
30+
31+
public static <T> void swap(T[] array, int first, int second) {
32+
T temp = array[first];
33+
array[first] = array[second];
34+
array[second] = temp;
35+
}
36+
37+
private static <T> void allPermutationsHelper(T[] permutation, List<T[]> permutations, int n) {
38+
// Base case - we found a new permutation, add it and return
39+
if (n <= 0) {
40+
permutations.add(permutation);
41+
return;
42+
}
43+
// Recursive case - find more permutations by doing swaps
44+
T[] tempPermutation = Arrays.copyOf(permutation, permutation.length);
45+
for (int i = 0; i < n; i++) {
46+
swap(tempPermutation, i, n - 1); // move element at i to the end
47+
// move everything else around, holding the end constant
48+
allPermutationsHelper(tempPermutation, permutations, n - 1);
49+
swap(tempPermutation, i, n - 1); // backtrack
50+
}
51+
}
52+
53+
private static <T> List<T[]> permutations(T[] original) {
54+
List<T[]> permutations = new ArrayList<>();
55+
allPermutationsHelper(original, permutations, original.length);
56+
return permutations;
57+
}
58+
59+
public int pathDistance(String[] path) {
60+
String last = path[0];
61+
int distance = 0;
62+
for (String next : Arrays.copyOfRange(path, 1, path.length)) {
63+
distance += distances.get(last).get(next);
64+
// distance to get back from last city to first city
65+
last = next;
66+
}
67+
return distance;
68+
}
69+
70+
public String[] findShortestPath() {
71+
String[] cities = distances.keySet().toArray(String[]::new);
72+
List<String[]> paths = permutations(cities);
73+
String[] shortestPath = null;
74+
int minDistance = Integer.MAX_VALUE; // arbitrarily high
75+
for (String[] path : paths) {
76+
int distance = pathDistance(path);
77+
// distance from last to first must be added
78+
distance += distances.get(path[path.length - 1]).get(path[0]);
79+
if (distance < minDistance) {
80+
minDistance = distance;
81+
shortestPath = path;
82+
}
83+
}
84+
// add first city on to end and return
85+
shortestPath = Arrays.copyOf(shortestPath, shortestPath.length + 1);
86+
shortestPath[shortestPath.length - 1] = shortestPath[0];
87+
return shortestPath;
88+
}
89+
90+
public static void main(String[] args) {
91+
Map<String, Map<String, Integer>> vtDistances = Map.of(
92+
"Rutland", Map.of(
93+
"Burlington", 67,
94+
"White River Junction", 46,
95+
"Bennington", 55,
96+
"Brattleboro", 75),
97+
"Burlington", Map.of(
98+
"Rutland", 67,
99+
"White River Junction", 91,
100+
"Bennington", 122,
101+
"Brattleboro", 153),
102+
"White River Junction", Map.of(
103+
"Rutland", 46,
104+
"Burlington", 91,
105+
"Bennington", 98,
106+
"Brattleboro", 65),
107+
"Bennington", Map.of(
108+
"Rutland", 55,
109+
"Burlington", 122,
110+
"White River Junction", 98,
111+
"Brattleboro", 40),
112+
"Brattleboro", Map.of(
113+
"Rutland", 75,
114+
"Burlington", 153,
115+
"White River Junction", 65,
116+
"Bennington", 40));
117+
TSP tsp = new TSP(vtDistances);
118+
String[] shortestPath = tsp.findShortestPath();
119+
int distance = tsp.pathDistance(shortestPath);
120+
System.out.println("The shortest path is " + Arrays.toString(shortestPath) + " in " +
121+
distance + " miles.");
122+
}
123+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /