-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.java
312 lines (276 loc) · 10 KB
/
Dijkstra.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
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
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
public class Dijkstra {
// Keep a fast index to nodes in the map
private Map<String, Vertex> vertexNames;
/**
* Construct an empty Dijkstra with a map. The map's key is the name of a vertex
* and the map's value is the vertex object.
*/
public Dijkstra() {
vertexNames = new HashMap<String, Vertex>();
}
public Map<String, Vertex> getVertexMap(){
return vertexNames;
}
public void setVertexMap(Map<String, Vertex> vertexNames){
this.vertexNames = vertexNames;
}
/**
* Adds a vertex to the dijkstra. Throws IllegalArgumentException if two vertices
* with the same name are added.
*
* @param v
* (Vertex) vertex to be added to the dijkstra
*/
public void addVertex(Vertex v) {
if (vertexNames.containsKey(v.name))
throw new IllegalArgumentException("Cannot create new vertex with existing name.");
vertexNames.put(v.name, v);
}
/**
* Gets a collection of all the vertices in the dijkstra
*
* @return (Collection<Vertex>) collection of all the vertices in the dijkstra
*/
public Collection<Vertex> getVertices() {
return vertexNames.values();
}
/**
* Gets the vertex object with the given name
*
* @param name
* (String) name of the vertex object requested
* @return (Vertex) vertex object associated with the name
*/
public Vertex getVertex(String name) {
return vertexNames.get(name);
}
/**
* Adds a directed edge from vertex u to vertex v
*
* @param nameU
* (String) name of vertex u
* @param nameV
* (String) name of vertex v
* @param cost
* (double) cost of the edge between vertex u and v
*/
public void addEdge(String nameU, String nameV, Double cost) {
if (!vertexNames.containsKey(nameU))
throw new IllegalArgumentException(nameU + " does not exist. Cannot create edge.");
if (!vertexNames.containsKey(nameV))
throw new IllegalArgumentException(nameV + " does not exist. Cannot create edge.");
Vertex sourceVertex = vertexNames.get(nameU);
Vertex targetVertex = vertexNames.get(nameV);
Edge newEdge = new Edge(sourceVertex, targetVertex, cost);
sourceVertex.addEdge(newEdge);
}
/**
* Adds an undirected edge between vertex u and vertex v by adding a directed
* edge from u to v, then a directed edge from v to u
*
* @param nameU
* (String) name of vertex u
* @param nameV
* (String) name of vertex v
* @param cost
* (double) cost of the edge between vertex u and v
*/
public void addUndirectedEdge(String nameU, String nameV, double cost) {
addEdge(nameU, nameV, cost);
addEdge(nameV, nameU, cost);
}
// STUDENT CODE STARTS HERE
/**
* Computes the euclidean distance between two points as described by their
* coordinates
*
* @param ux
* (double) x coordinate of point u
* @param uy
* (double) y coordinate of point u
* @param vx
* (double) x coordinate of point v
* @param vy
* (double) y coordinate of point v
* @return (double) distance between the two points
*/
public double computeEuclideanDistance(double ux, double uy, double vx, double vy) {
// TODO
double euclideanresult = Math.sqrt(Math.pow((ux-vx), 2)+Math.pow((uy-vy), 2)); //distance formula
return euclideanresult;
}
/**
* Calculates the euclidean distance for all edges in the map using the
* computeEuclideanCost method.
*/
public void computeAllEuclideanDistances() {
//collection of verticies to loop through
Collection<Vertex> vertexcollection = getVertices();
//each edge has a vertex and each vertex has edges, nested for loop
//access edges using adjacentEdges
for(Vertex vertexobj: vertexcollection){
for(Edge adjedge: vertexobj.adjacentEdges){
// source -- target
Vertex currvertex= adjedge.source; //first vertex
Vertex nextvertex = adjedge.target; //the next vertex to compute distance with
double cost = computeEuclideanDistance(currvertex.x, currvertex.y, nextvertex.x, nextvertex.y);
adjedge.distance = cost;
}
}
}
/**
* Dijkstra's Algorithm.
*
* @param s
* (String) starting city name
*/
public void doDijkstra(String s) {
// TODO
//first for each vertex initialize the known to null, distance to infinity
//collection of verticies to loop through
Collection<Vertex> vertexcollection = getVertices();
ArrayList<Vertex> unknowns = new ArrayList<>(); //LL of unknowns
for (Vertex ver: vertexcollection){
ver.known= false;
ver.distance= Double.POSITIVE_INFINITY;
ver.prev=null;
unknowns.add(ver); //add each vertex into unknowns
}
LinkedList<Vertex> pathlist = new LinkedList<>(); //store path in LL of verticies
//get starting vertex from string parameter
Vertex sver = vertexNames.get(s);
sver.distance = 0.0;
pathlist.add(sver); //add first vertex
sver.known=true; // we have seen it so set to true
// go through the edge check the adjacentedges, loop through
//once you've visted the edge, set it to known
while(!unknowns.isEmpty()){
Vertex minVertex = null;
double minDistance = Double.POSITIVE_INFINITY;
for(Vertex ver: unknowns){
if(ver.distance< minDistance){
minVertex = ver;
minDistance = ver.distance;
}
}
if(minVertex==null){
break; //there are no more unknown verticies to access
}
minVertex.known=true;
unknowns.remove(minVertex);
for(Edge adjedge : minVertex.adjacentEdges){
//compute euclidian distance
Vertex target = adjedge.target; //the target vertex of the edge of this iteration
if(!target.known){
double distance = minVertex.distance + adjedge.distance;
if(distance<target.distance){
target.distance = distance;
target.prev = minVertex;
}
}
}
}
}
/**
* Returns a list of edges for a path from city s to city t. This will be the
* shortest path from s to t as prescribed by Dijkstra's algorithm
*
* @param s
* (String) starting city name
* @param t
* (String) ending city name
* @return (List<Edge>) list of edges from s to t
*/
public List<Edge> getDijkstraPath(String s, String t) {
LinkedList<Edge> path = new LinkedList<>();
doDijkstra(s);
//work previous starting from endpoint
Vertex end = vertexNames.get(t);
while(end.prev!=null){
//adding the edges to the beginning of the LL as we come across it
double weightresult = computeEuclideanDistance(end.prev.x, end.prev.y, end.x, end.y);
path.addFirst(new Edge(end.prev, end, weightresult)); //add edge to beginning of LL
//reset the end to become the prev vertex, continues to work backwards
end = end.prev;
}
return path;
}
// STUDENT CODE ENDS HERE
/**
* Prints out the adjacency list of the dijkstra for debugging
*/
public void printAdjacencyList() {
for (String u : vertexNames.keySet()) {
StringBuilder sb = new StringBuilder();
sb.append(u);
sb.append(" -> [ ");
for (Edge e : vertexNames.get(u).adjacentEdges) {
sb.append(e.target.name);
sb.append("(");
sb.append(e.distance);
sb.append(") ");
}
sb.append("]");
System.out.println(sb.toString());
}
}
/**
* A main method that illustrates how the GUI uses Dijkstra.java to
* read a map and represent it as a graph.
* You can modify this method to test your code on the command line.
*/
public static void main(String[] argv) throws IOException {
String vertexFile = "cityxy.txt";
String edgeFile = "citypairs.txt";
Dijkstra dijkstra = new Dijkstra();
String line;
// Read in the vertices
BufferedReader vertexFileBr = new BufferedReader(new FileReader(vertexFile));
while ((line = vertexFileBr.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length != 3) {
vertexFileBr.close();
throw new IOException("Invalid line in vertex file " + line);
}
String cityname = parts[0];
int x = Integer.valueOf(parts[1]);
int y = Integer.valueOf(parts[2]);
Vertex vertex = new Vertex(cityname, x, y);
dijkstra.addVertex(vertex);
}
vertexFileBr.close();
BufferedReader edgeFileBr = new BufferedReader(new FileReader(edgeFile));
while ((line = edgeFileBr.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length != 3) {
edgeFileBr.close();
throw new IOException("Invalid line in edge file " + line);
}
dijkstra.addUndirectedEdge(parts[0], parts[1], Double.parseDouble(parts[2]));
}
edgeFileBr.close();
// Compute distances.
// This is what happens when you click on the "Compute All Euclidean Distances" button.
dijkstra.computeAllEuclideanDistances();
// print out an adjacency list representation of the graph
dijkstra.printAdjacencyList();
// This is what happens when you click on the "Draw Dijkstra's Path" button.
// In the GUI, these are set through the drop-down menus.
String startCity = "SanFrancisco";
String endCity = "Boston";
dijkstra.doDijkstra(startCity);
// Get weighted shortest path between start and end city.
List<Edge> path = dijkstra.getDijkstraPath(startCity, endCity);
System.out.print("Shortest path between "+startCity+" and "+endCity+": ");
System.out.println(path);
}
}