-
Notifications
You must be signed in to change notification settings - Fork 2
/
GreedyAlgo.java
74 lines (53 loc) · 2.04 KB
/
GreedyAlgo.java
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
import java.util.*;
// The Greedy Algorithm
public class GreedyAlgo implements Answer {
// Vars: MapInfo for the Map, Tour for the current path, And a list of visited Integers
private MapInfo map;
private Tour path;
private List<Integer> visited = new ArrayList<Integer>();
// Constructor for using the Greedy Algorithm, taking a Map and creating a new Tour
public GreedyAlgo(MapInfo map) {
this.map = map;
path = new Tour();
}
// This chooses the Closest City to the given City
public int closestCity(int city) {
// Using a huge number to start with min
double min = 99999999;
int closestCity = city;
// Cycling through the Map's distances from a City to all other cities
for(int i = 0; i < map.distances[city].length; i++) {
// If i isn't the city, if i isn't in visited, if the distance is less than the min
if(i != city && !visited.contains(i) && map.distances[city][i] < min) {
min = map.distances[city][i];
closestCity = i;
}
}
// Retruning the Closest City to "city"
return closestCity;
}
// The Greedy Algorithm
@Override
public Tour ComputePath() {
// Tour Starts at the First City "0"
int CurrentCity = 0;
// Add the first city to visited
visited.add(CurrentCity);
// This loops through every City on the map until they all have been evaluated
while (visited.size() < map.n) {
// Get the closest city to the current city
int nextCity = closestCity(CurrentCity);
// Adding a Edge to the Tour from current city to the closest city, and giving the distance
path.addEdgeToTour(new Edges(map.Cities.get(CurrentCity), map.Cities.get(nextCity), map.distances[CurrentCity][nextCity]));
// Add that city to the visited array
visited.add(nextCity);
// Start again from the next City
CurrentCity = nextCity;
}
// Returning to the Starting City
path.addEdgeToTour(new Edges(map.Cities.get(CurrentCity), map.Cities.get(0), map.distances[CurrentCity][0]));
System.out.println(visited);
// Return the entire Tour
return path;
}
}