-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubwayStation.java
More file actions
217 lines (184 loc) · 6.28 KB
/
Copy pathSubwayStation.java
File metadata and controls
217 lines (184 loc) · 6.28 KB
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
/**
* @author Hubert
*/
public final class SubwayStation implements Comparable<SubwayStation> {
public final String stationName;
// The given SubwayStation and the time associated from this station to the
// given SubwayStation
private final java.util.HashMap<SubwayStation, Double> connectedStations = new java.util.HashMap<>();
private SubwayStation() {
stationName = null;
}
/**
* Create a SubwayStation
* @param stationName
*/
public SubwayStation(String stationName) {
this(stationName, null);
}
/**
* Create a new subwayStation and add it apart of a subway system.
*
* @param stationName The name of the station
* @param system The Subway System that the new station is apart of
*/
public SubwayStation(String stationName, SubwaySystem system) {
this.stationName = stationName;
if (system != null) {
system.addStation(this);
}
}
/**
* Connects a given station to the current station.
*
* @param station the station the object is connected to
* @param time The time it takes for the train to travel to the given
* station in minutes
*
* @return Returns weather the station has been added to the connected
* stations list
*/
public boolean connectToStation(SubwayStation station, Double time) {
if (time == 0) {
throw new IllegalArgumentException("Travel time cannot be 0 over a distance.");
}
if (!connectedStations.containsKey(station)) {
connectedStations.put(station, time);
station.connectToStation(this, time); // ASSUMING SAME TIME BOTH WAYS
return true;
}
return false;
}
/**
* Update the time between this station and the given station
*
* @param station The station that this object is connected to. If given
* station is not connected, do nothing
* @param time The new time. If new time = old time, do nothing
*/
public void updateTrainStation(SubwayStation station, Double time) {
if (time == 0) {
throw new IllegalArgumentException("Travel time cannot be 0 over a distance.");
}
if (connectedStations.containsKey(station)) {
if (!java.util.Objects.equals(connectedStations.get(station), time)) {
connectedStations.put(station, time); // Update time
station.updateTrainStation(this, time); // Update foreign station to given time ASSUMING BIDIRECTIONAL
} // If the time is already set ignore
} // Dont set if given station is not connected
}
/**
* Searches a list from the current object (SubwayStation) to the desired
* station in occurring order
*
* @param desiredStation The station to find the shortest path to
*
* @return A list of stations from the current to the destination. Returns
* null if doesn't exist
*/
public java.util.LinkedList<SubwayStation> findStation(SubwayStation desiredStation) {
java.util.HashMap<SubwayStation, LengthLinkedList<SubwayStation>> starting = new java.util.HashMap<>();
LengthLinkedList<SubwayStation> startinStation = new SubwayStation.LengthLinkedList<>();
starting.put(this, startinStation);
java.util.HashMap<SubwayStation, LengthLinkedList<SubwayStation>> map = DijkstraRecursion(starting, new java.util.HashSet<>());
java.util.LinkedList<SubwayStation> returnable = map.get(desiredStation);
if (returnable == null) {
return new java.util.LinkedList<>();
}
returnable.add(desiredStation);
return returnable;
}
// Dijstra's Alg for shortest path
private java.util.HashMap<SubwayStation, LengthLinkedList<SubwayStation>> DijkstraRecursion(java.util.HashMap<SubwayStation, LengthLinkedList<SubwayStation>> mapShortestStations, java.util.HashSet<SubwayStation> visited) {
//Set as visiting
visited.add(this);
//For every non-visited adjacent
for (SubwayStation s : connectedStations.keySet()) {
if (visited.contains(s)) {
continue;
}
LengthLinkedList<SubwayStation> shortestStation = mapShortestStations.get(this).clone();
shortestStation.add(this);
shortestStation.length += connectedStations.get(s);
if (mapShortestStations.get(s) == null || mapShortestStations.get(s).length > shortestStation.length) {
mapShortestStations.put(s, shortestStation);
}
}
//Pick station with smallest length
SubwayStation shortestUnvisitedStation = null;
for (SubwayStation s : mapShortestStations.keySet()) {
if (visited.contains(s)) {
continue;
}
if (shortestUnvisitedStation == null) {
shortestUnvisitedStation = s;
continue;
}
if (mapShortestStations.get(s).length < mapShortestStations.get(shortestUnvisitedStation).length) {
shortestUnvisitedStation = s;
}
}
if (shortestUnvisitedStation == null) {
return mapShortestStations;
}
//Iterate for all unvisited
return shortestUnvisitedStation.DijkstraRecursion(mapShortestStations, visited);
}
/**
* If a station is adjacent to another, return 1, -1 otherwise and 0 iff station is itself.
*
* @param o A station to compare object to
*
* @return 0 If parameter is the same as the object being referenced, 1 if
* it is an adjacent station, -1 otherwise.
*/
@Override
public int compareTo(SubwayStation o) {
if (o.equals(this)) {
return 0;
}
if (connectedStations.containsKey(o)) {
return 1;
}
return -1;
}
/**
* Check if two stations are equal if they have the same station name
* @param obj
* @return
*/
@java.lang.Override
public boolean equals(Object obj) {
if (obj instanceof SubwayStation) {
return (((SubwayStation) obj).stationName).equalsIgnoreCase(this.stationName);
}
return false;
}
@java.lang.Override
public String toString() {
return this.stationName;
}
@java.lang.Override
public int hashCode() {
int hash = 3;
hash = 67 * hash + java.util.Objects.hashCode(stationName.toLowerCase());
return hash;
}
// A linkedList class with a length described
private final class LengthLinkedList<E> extends java.util.LinkedList<E> {
Double length = 0.0;
/**
* Copy all elements into a new LengthLinkedList
* @return Copied list
*/
@Override
public LengthLinkedList<E> clone() {
LengthLinkedList<E> temp = new LengthLinkedList<>();
temp.length = this.length;
this.forEach((e) -> {
temp.add(e);
});
return temp;
}
}
}