-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearches.cpp
314 lines (280 loc) · 11.3 KB
/
searches.cpp
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
313
314
#include "searches.h"
#include "drawer.h"
std::unordered_map<Vertex,double> distance;
//Custom comparator class to use for priority queue
//We figured out how to write a custom comparator using this tutorial https://neutrofoton.github.io/blog/2016/12/29/c-plus-plus-priority-queue-with-comparator/
class Comparator{
public:
/**
* A class that is passed as an argument to the priority queue intialization
* Compares the value of two vertices using the distance map.
* @param v1 - One of the vertices to compare
* @param v2 - The other vertex to compare
* @return If vertex v1 has a greater distance from source
*/
bool operator() (Vertex v1,Vertex v2){
return distance[v1]>distance[v2];
}
};
void Search::animateBFSByEdges(Graph* G, GraphBuilder& builder, Vertex source, int max_edges, string output_file, cs225::HSLAPixel color) {
unordered_map<Vertex, string> nodeStateList;
int given_source = 1;
Animation animation;
Drawer drawer("Maps/bfs.png");
if(given_source){
for (Vertex vertex : G -> getVertices()) {
nodeStateList.insert({vertex, "UNEXPLORED"});
}
for (Edge edge : G -> getEdges()) {
G->setEdgeLabel(edge.source,edge.dest,"UNEXPLORED");
}
}
if (!G->vertexExists(source)) {
std::cout << "Warning Invalid Input, can't doing BFS on this" << std::endl;
} else {
Vertex vert=source;
if(nodeStateList[vert]=="UNEXPLORED"){
std::queue <Vertex> myQueue;
if(nodeStateList[vert]!="VISITED"){
visited_vertices.push_back(vert);
}
nodeStateList[vert] = "VISITED";
int num_flights = 0;
myQueue.push(vert);
while (!myQueue.empty()) {
source=myQueue.front();
myQueue.pop();
for (Vertex vertex : G -> getAdjacent(source)) {
Edge edge = G -> getEdge(source, vertex);
if (nodeStateList[vertex] == "UNEXPLORED") {
if(nodeStateList[vertex]!="VISITED"){
visited_vertices.push_back(vertex);
}
nodeStateList[vertex] = "VISITED";
G->setEdgeLabel(edge.source,edge.dest,"DISCOVERY");
//draw line on map
drawer.drawAirport(builder.get_airport_from_code(edge.source),
{60, 1, .5});
drawer.drawFlight(builder.get_airport_from_code(edge.source),
builder.get_airport_from_code(edge.dest), {.5, .5, .5, .5});
drawer.drawAirport(builder.get_airport_from_code(edge.dest),
{60, 1, .5});
//adds frame for every line added
animation.addFrame(*drawer.getPNG());
num_flights += 1;
if (num_flights >= max_edges) {
animation.write(output_file);
return;
}
myQueue.push(vertex);
} else if (edge.getLabel() == "UNEXPLORED") {
G->setEdgeLabel(edge.source,edge.dest,"CROSS EDGE");
}
}
}
}
}
animation.write(output_file);
}
void Search::animateBFSByLayer(Graph* G, GraphBuilder& builder, Vertex source, string output_file, cs225::HSLAPixel color) {
Vertex start=source;
unordered_map<Vertex, string> nodeStateList;
unordered_map<Vertex, int> nodedepth;
std::vector<string> ends;
int given_source = 1;
// creating two unordered maps which store the state of the node and edge
//unordered_map<Edge, string> edgeStateList;
Animation animation;
Drawer drawer("Maps/bfs.png");
if(given_source){
for (Vertex vertex : G -> getVertices()) {
nodeStateList.insert({vertex, "UNEXPLORED"});
}
for (Edge edge : G -> getEdges()) {
G->setEdgeLabel(edge.source,edge.dest,"UNEXPLORED");
//edgeStateList.insert({edge, "UNUSED"});
}
}
nodedepth[source]=0;
if (!G->vertexExists(source)) {
//return "Invalid Input";
} else {
Vertex vert=source;
if(nodeStateList[vert]=="UNEXPLORED"){
std::queue <Vertex> myQueue;
if(nodeStateList[vert]!="VISITED"){
visited_vertices.push_back(vert);
}
nodeStateList[vert] = "VISITED";
int num_flights = 0;
animation.addFrame(*drawer.getPNG());
myQueue.push(vert);
int depth = 0;
while (!myQueue.empty()) {
source=myQueue.front();
if(source!=start&&!myQueue.empty()){
if(nodedepth[myQueue.front()]==nodedepth[source]+1){
ends.push_back(source);
}
}
myQueue.pop();
for (Vertex vertex : G -> getAdjacent(source)) {
Edge edge = G -> getEdge(source, vertex);
if (nodeStateList[vertex] == "UNEXPLORED") {
if(nodeStateList[vertex]!="VISITED"){
visited_vertices.push_back(vertex);
nodedepth[vertex]=nodedepth[source]+1;
}
if (nodedepth[vertex] == depth + 1) {
animation.addFrame(*drawer.getPNG());
++depth;
}
nodeStateList[vertex] = "VISITED";
G->setEdgeLabel(edge.source,edge.dest,"DISCOVERY");
//draw flights. Note it does not create frame in this method at this instant, rather
//only creates if layer is appended.
drawer.drawAirport(builder.get_airport_from_code(edge.source),
{60, 1, .5});
drawer.drawFlight(builder.get_airport_from_code(edge.source),
builder.get_airport_from_code(edge.dest), {.5, .5, .5, .5});
drawer.drawAirport(builder.get_airport_from_code(edge.dest),
{60, 1, .5});
myQueue.push(vertex);
} else if (edge.getLabel() == "UNEXPLORED") {
G->setEdgeLabel(edge.source,edge.dest,"CROSS EDGE");
}
}
}
}
}
ends.push_back(source);
animation.write(output_file);
}
void Search::BFS(Graph * G){
unordered_map<Vertex, string> nodeStateList;
int given_source=1;
// for loops for initialising states of nodes and edges
for (Vertex vertex : G -> getVertices()) {
nodeStateList.insert({vertex, "UNEXPLORED"});
}
for (Edge edge : G -> getEdges()) {
G->setEdgeLabel(edge.source,edge.dest,"UNEXPLORED");
//edgeStateList.insert({edge, "UNUSED"});
}
given_source=0;
for (Vertex vertex : G -> getVertices()) {
if(nodeStateList[vertex]=="UNEXPLORED"){
BFS(G,vertex);
}
}
}
void Search::BFS(Graph * G, Vertex source){
// creating two unordered maps which store the state of the node and edge
//depth variable helps keep track of path length
Vertex start=source;
unordered_map<Vertex, string> nodeStateList;
int given_source=1;
if(given_source){
for (Vertex vertex : G -> getVertices()) {
nodeStateList.insert({vertex, "UNEXPLORED"});
}
for (Edge edge : G -> getEdges()) {
G->setEdgeLabel(edge.source,edge.dest,"UNEXPLORED");
//edgeStateList.insert({edge, "UNUSED"});
}
}
if (!G->vertexExists(source)) {
//return "Invalid Input";
} else {
Vertex vert=source;
if(nodeStateList[vert]=="UNEXPLORED"){
std::queue <Vertex> myQueue;
if(nodeStateList[vert]!="VISITED"){
visited_vertices.push_back(vert);
}
nodeStateList[vert] = "VISITED";
myQueue.push(vert);
while (!myQueue.empty()) {
source=myQueue.front();
myQueue.pop();
for (Vertex vertex : G -> getAdjacent(source)) {
Edge edge = G -> getEdge(source, vertex);
if (nodeStateList[vertex] == "UNEXPLORED") {
if(nodeStateList[vertex]!="VISITED"){
visited_vertices.push_back(vertex);
}
nodeStateList[vertex] = "VISITED";
G->setEdgeLabel(edge.source,edge.dest,"DISCOVERY");
//edgeStateList[edge] == "PATH EDGE";
myQueue.push(vertex);
} else if (edge.getLabel() == "UNEXPLORED") {
G->setEdgeLabel(edge.source,edge.dest,"CROSS EDGE");
}
}
}
}
}
}
void Search::Dijkstra(Graph G, Vertex s){
// Priority queue of vertexes
std::priority_queue<Vertex, std::vector<Vertex>, Comparator> pq;
//Set distance of each vertex to infinity and the predecessor to empty string
for(auto &v:G.getVertices()){
distance[v]=INT_MAX;
predecessor[v]="";
}
//Set the source to 0
distance[s]=0;
//Push all the vertexes to the priority queue
for(auto &v:G.getVertices()){
pq.push(v);
}
//Graph to keep track of visited vertexes
Graph T(true,true);
while(pq.size()!=0){
//Get the vertex with minimum distance from the source
Vertex u=pq.top();
while(T.vertexExists(u)&&pq.size()!=0){
u=pq.top();
pq.pop();
}
T.insertVertex(u);
//Loop through the adjacent vertices
for (auto &v : G.getAdjacent(u))
{
// If the vertex was not visited
// std::cout<<v<<std::endl;
if(!T.vertexExists(v)){
int cost=G.getEdgeWeight(u,v);
// Set the distance between source and vertex to the smallest distance
if((cost+distance[u])<distance[v]){
distance[v]=(cost+distance[u]);
predecessor[v]=u;
}
//push the vertex after it's weight is set to the priority queue
pq.push(v);
}
}
}
}
std::vector<std::string> Search::find_route_in_vector(Graph& G, Vertex start, Vertex end) {
std::vector<std::string> path;
if(!G.vertexExists(end)||!G.vertexExists(start)){
//std::cout << "Airport Code Incorrect" << std::endl;
return std::vector<std::string>();
}
std::string out="";
Dijkstra(G, start);
Vertex curr=end;
out+=end;
path.insert(path.begin(), 1, curr);
while(curr!=start){
if(predecessor[curr]==""){
std::cout << "No route between two airports" << std::endl;
return std::vector<std::string>();
}
curr=predecessor[curr];
path.insert(path.begin(), 1, curr);
}
return path;
}