-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing_Dijkstra.java
206 lines (172 loc) · 4.82 KB
/
routing_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
//Planning and optimizing flight routes between cities
//Using Dijkstra's Algorithm to continuously eliminate longer paths
//Part of Final Project - for MOOC on Graph Theory, UCSD on Coursera
//******************************************************************
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Dijkstra {
private ArrayList <Vertex> theList; //The vertices
private ArrayList <Vertex> path; //The optimal path
private File infile;
private Scanner in;
private HashMap <String, Vertex> cityList; //vertex, city-name map
public Dijkstra (File f) throws FileNotFoundException
{
infile = f;
in = new Scanner(infile);
cityList = new HashMap<String, Vertex>();
theList = new ArrayList<Vertex>();
path = new ArrayList<Vertex>();
makeGraph();
}
public String[] getCities() //Array of city name strings
{
String[] cityNames = new String[cityList.size()];
Set<String> c = cityList.keySet();
Iterator<String> cIterator = c.iterator();
for(int i=0;i<c.size();i++)
{
cityNames[i]=cIterator.next();
}
return cityNames;
}
public int size() //Number of cities in question
{
return cityList.size();
}
public HashMap <String, Vertex> getMap() //Hashmap of city names
{
return cityList;
}
public ArrayList <Vertex> getList() //Optimal path
{
return theList;
}
public void makeGraph() //Create graph using data
{
while(in.hasNextLine())
{
String start = " "; //Import first city
if(in.hasNext())
start = in.next();
else
break;
String end = " "; //Import second city
if(in.hasNext())
end = in.next();
else
break;
double distance=0.0; //Import distance
if(in.hasNextDouble())
distance = in.nextDouble();
else
break;
if(cityList.containsKey(start)) //Check if city is already on hashmap
{
Vertex v1 = cityList.get(start);
if(!cityList.containsKey(end)) //If city is not on hashmap, add it
{
Vertex x = new Vertex(end);
cityList.put(end, x);
}
Vertex v2 = cityList.get(end);
v1.addList(v2,distance); //Add path to both adjacency lists
v2.addList(v1,distance);
}
else if(!cityList.containsKey(start)){
Vertex v1 = new Vertex(start);
if(!cityList.containsKey(end)) //If destination not on map, add it
{
Vertex x = new Vertex(end);
cityList.put(end, x);
}
Vertex v2 = cityList.get(end);
v1.addList(v2,distance);
v2.addList(v1,distance);
cityList.put(start, v1); //Add start city to hashmap
}
}
Collection <Vertex> vertices = cityList.values();
Iterator <Vertex> it = vertices.iterator();
for(int i = 0; i<vertices.size();i++)
{
theList.add(it.next()); //Create the list of vertices from the hashmap
}
}
public void findPath(String city) //Dijkstra's algorithm
{
MyMinHeap <Vertex> heap = new MyMinHeap<Vertex>();
for(int i = 0; i < theList.size();i++) //reset vertices
{
Vertex v = theList.get(i);
v.setKnown(false);
v.setDist(Double.POSITIVE_INFINITY);
v.setPath(null);
heap.insert(v);
}
Vertex start = cityList.get(city); //Decrease key of starting vertex
Vertex x = start;
start.setDist(0);
heap.decreaseKey(x, start);
for(;;)
{
Vertex v = heap.deleteMin(); //Get closest vertex
v.setKnown(true);
for(int i=0; i<v.getList().size();i++)
{
Vertex w = v.getList().get(i); //Get adjacent vertices
if(!w.getKnown())
{
double cost = v.getDistances().get(i);
if(cost + v.getDist() < w.getDist())
{
Vertex old = w; //Compare costs
w.setDist(v.getDist() + cost);
w.setPath(v);
heap.decreaseKey(old, w);
}
else if(w.getDist() == Double.POSITIVE_INFINITY)
{
Vertex old = w; //New vertex is found
w.setDist(cost+v.getDist());
w.setPath(v);
heap.decreaseKey(old, w);
}
}
}
if(heap.isEmpty())
break;
}
}
public void setPath(String city) //Use recursion to get shortest path to city
{
path.clear();
Vertex v = cityList.get(city);
setPath(v);
}
private void setPath(Vertex v) //Find path shortest recursively
{
if(v.getPath() != null)
{
setPath(v.getPath());
path.add(v);
}
else
path.add(v);
}
public double getDistance(String city) //Returns the path distance
{
return getDistance(cityList.get(city));
}
public double getDistance(Vertex v)
{
return v.getDist();
}
public ArrayList <Vertex> getPath() //Returns the path as an arrayList of vertices
{
return path;
}
}