generated from hongshuochen/Fall2022_TrojanMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trojanmap.h
176 lines (136 loc) · 6.26 KB
/
trojanmap.h
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
#ifndef TROJAN_MAP_H
#define TROJAN_MAP_H
#include <math.h>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
// A Node is the location of one point in the map.
class Node {
public:
Node(){};
Node(const Node &n) {
id = n.id;
lat = n.lat;
lon = n.lon;
name = n.name;
neighbors = n.neighbors;
attributes = n.attributes;
};
std::string id; // A unique id assign to each point
double lat; // Latitude
double lon; // Longitude
std::string name; // Name of the location. E.g. "Bank of America".
std::vector<std::string>
neighbors; // List of the ids of all neighbor points.
std::unordered_set<std::string>
attributes; // List of the attributes of the location.
};
class TrojanMap {
public:
// Constructor
TrojanMap() { CreateGraphFromCSVFile(); };
// A map of ids to Nodes.
std::unordered_map<std::string, Node> data;
//-----------------------------------------------------
// Read in the data
void CreateGraphFromCSVFile();
//-----------------------------------------------------
// TODO: Implement these functions and create unit tests for them:
// Get the Latitude of a Node given its id.
double GetLat(const std::string &id);
// Get the Longitude of a Node given its id.
double GetLon(const std::string &id);
// Get the name of a Node given its id.
std::string GetName(const std::string &id);
// Get the id given its name.
std::string GetID(const std::string &name);
// Get the neighbor ids of a Node.
std::vector<std::string> GetNeighborIDs(const std::string &id);
// Returns a vector of names given a partial name.
std::vector<std::string> Autocomplete(std::string name);
// GetAllCategories: Return all the possible unique location categories, i.e.
// there should be no duplicates in the output.
std::vector<std::string> GetAllCategories();
std::vector<std::string> GetAllLocationsFromCategory(std::string category);
std::vector<std::string> GetLocationRegex(std::regex location);
// Returns lat and lon of the given the name.
std::pair<double, double> GetPosition(std::string name);
// Calculate location names' edit distance
int CalculateEditDistance(std::string, std::string);
// Find the closest name
std::string FindClosestName(std::string name);
// Get the distance between 2 nodes.
double CalculateDistance(const std::string &a, const std::string &b);
// Calculates the total path length for the locations inside the vector.
double CalculatePathLength(const std::vector<std::string> &path);
// Given the name of two locations, it should return the **ids** of the nodes
// on the shortest path.
std::vector<std::string> CalculateShortestPath_Dijkstra(
std::string location1_name, std::string location2_name);
std::vector<std::string> CalculateShortestPath_Bellman_Ford(
std::string location1_name, std::string location2_name);
// Given CSV filename, it read and parse locations data from CSV file,
// and return locations vector for topological sort problem.
std::vector<std::string> ReadLocationsFromCSVFile(
std::string locations_filename);
// Given CSV filenames, it read and parse dependencise data from CSV file,
// and return dependencies vector for topological sort problem.
std::vector<std::vector<std::string>> ReadDependenciesFromCSVFile(
std::string dependencies_filename);
// Given a vector of location names, it should return a sorting of nodes
// that satisfies the given dependencies.
std::vector<std::string> DeliveringTrojan(
std::vector<std::string> &location_names,
std::vector<std::vector<std::string>> &dependencies);
//travelling salesman brute force recursion helper
void recursion_tsp_bf(std::vector<std::string> &temp_path_r,
std::set<std::string> &visited_r,std::vector<std::string> &location_ids,
std::pair<double, std::vector<std::vector<std::string>>> &records,
std::vector<std::string> &min_path);
//reverse_2opt_tsp
void do2Opt_reverse(std::vector<std::string> &location_ids,int i,int j);
// Given a vector of location ids, it should reorder them such that the path
// that covers all these points has the minimum length.
// The return value is a pair where the first member is the total_path,
// and the second member is the reordered vector of points.
// (Notice that we don't find the optimal answer. You can return an estimated
// path.)
std::pair<double, std::vector<std::vector<std::string>>>
TravelingTrojan_Brute_force(std::vector<std::string> location_ids);
std::pair<double, std::vector<std::vector<std::string>>>
TravelingTrojan_Backtracking(std::vector<std::string> location_ids);
std::pair<double, std::vector<std::vector<std::string>>> TravelingTrojan_2opt(
std::vector<std::string> location_ids);
double CalculateDistance_item11(std::string &id1,std::string &id2,std::map<std::pair<std::string,std::string>,double> &adj_dis);
void recursion_tsp_item11_backtrack(std::vector<std::string> &temp_path,
std::set<std::string> &visited,std::vector<std::string> &location_ids,
std::vector<std::string> &min_path, double &min_dist,
std::map<std::pair<std::string,std::string>,double> &adj_dis);
std::vector<std::string> TrojanPath(std::vector<std::string> &location_names);
// Check whether the id is in square or not
bool inSquare(std::string id, std::vector<double> &square);
// Get the subgraph based on the input
std::vector<std::string> GetSubgraph(std::vector<double> &square);
//DFS helper function for cycle detection
bool detection_helper(std::string &node,std::set<std::string> &visited,std::string &parent,std::vector<double> &square);
// Given a subgraph specified by a square-shape area, determine whether there
// is a cycle or not in this subgraph.
bool CycleDetection(std::vector<std::string> &subgraph,
std::vector<double> &square);
// Given a location id and k, find the k closest points on the map
std::vector<std::string> FindNearby(std::string, std::string, double, int);
//----------------------------------------------------- User-defined functions
};
#endif