Video Presentation: Friday, December 3 by 2 pm
Code submission: Sunday, December 5 by 23:59 pm
This project focuses on using data structures in C++ and implementing various graph algorithms to build a map application.
- Please clone the repository, look through README.md and fill up functions to finish in the project.
- Please make sure that your code can run
bazel run/test
. - In this project, you will need to fill up trojanmap.cc and add unit tests in the
tests
directory.
Each point on the map is represented by the class Node shown below and defined in trojanmap.h.
class Node {
public:
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.
};
For visualization, we use OpenCV library. You will use this library as a black box and don't need to worry about the graphic details.
Use the following commands to install OpenCV.
$ cd 2021Fall_TrojanMap
$ git clone https://github.com/opencv/opencv.git
For Ubuntu:
$ sudo apt-get install cmake libgtk2.0-dev pkg-config
$ sudo apt install libcanberra-gtk-module libcanberra-gtk3-module
$ sudo apt-get install libncurses5-dev libncursesw5-dev
$ cp ubuntu/* ./
For MacOS:
$ brew install cmake
$ brew install ncurses
Next, type the following, but make sure that you set the path_to_install_folder to be the absolute path to the install folder under opencv.
$ cd opencv/
$ mkdir build install
$ cd build
$ cmake -D CMAKE_INSTALL_PREFIX=**path_to_install_folder**\
-D BUILD_LIST=core,highgui,imgcodecs,imgproc,videoio\
-D WITH_TBB=ON -D WITH_OPENMP=ON -D WITH_IPP=ON\
-D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=OFF\
-D WITH_NVCUVID=ON\
-D WITH_CUDA=ON\
-D BUILD_DOCS=OFF\
-D BUILD_PERF_TESTS=OFF\
-D BUILD_TESTS=OFF\
-D WITH_CSTRIPES=ON\
-D WITH_OPENCL=ON ..
$ make install
For example, if cloned this repo under "/Users/ari/github/TrojanMap", you should type:
$ cd opencv/
$ mkdir build install
$ cd build
$ cmake -D CMAKE_INSTALL_PREFIX=/Users/ari/github/TrojanMap/opencv/install\
-D BUILD_LIST=core,highgui,imgcodecs,imgproc,videoio\
-D WITH_TBB=ON -D WITH_OPENMP=ON -D WITH_IPP=ON\
-D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=OFF\
-D WITH_NVCUVID=ON\
-D WITH_CUDA=ON\
-D BUILD_DOCS=OFF\
-D BUILD_PERF_TESTS=OFF\
-D BUILD_TESTS=OFF\
-D WITH_CSTRIPES=ON\
-D WITH_OPENCL=ON ..
$ make install
Please run:
$ bazel run src/main:main
If everything is correct, this menu will show up.
Torjan Map
**************************************************************
* Select the function you want to execute.
* 1. Autocomplete
* 2. Find the position
* 3. CalculateShortestPath
* 4. Travelling salesman problem
* 5. Cycle Detection
* 6. Topological Sort
* 7. Find K Closest Points
* 8. Exit
**************************************************************
Please select 1 - 8:
We created some tests for you to test your program, please run
$ bazel test tests:trojanmap_test
Please add you test in the trojanmap_test_student.cc and run
$ bazel test tests:trojanmap_test_student
std::vector<std::string> Autocomplete(std::string name);
We consider the names of nodes as the locations. Implement a method to type the partial name of the location and return a list of possible locations with partial name as prefix. Please treat uppercase and lower case as the same character.
Example:
Input: "ch"
Output: ["ChickfilA", "Chipotle Mexican Grill"]
Input: "ta"
Output: ["Target", "Tap Two Blue"]
1
**************************************************************
* 1. Autocomplete
**************************************************************
Please input a partial location:ch
*************************Results******************************
ChickfilA
Chipotle Mexican Grill
**************************************************************
Time taken by function: 1904 microseconds
std::pair<double, double> GetPosition(std::string name);
Given a location name, return the latitude and longitude. There are no duplicated location names. You should mark the given locations on the map. If the location does not exist, return (-1, -1).
Example:
Input: "ChickfilA"
Output: (34.0167334, -118.2825307)
Input: "Ralphs"
Output: (34.0317653, -118.2908339)
Input: "Target"
Output: (34.0257016, -118.2843512)
2
**************************************************************
* 2. Find the position
**************************************************************
Please input a location:Target
*************************Results******************************
Latitude: 34.0257 Longitude: -118.284
**************************************************************
Time taken by function: 1215 microseconds
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 2 locations A and B, find the best route from A to B. The distance between 2 points is the euclidean distance using latitude and longitude. You should use both Dijkstra algorithm and Bellman-Ford algorithm. Compare the time for the different methods. Show the routes on the map. If there is no path, please return empty vector.
Please report and compare the time spent by these 2 algorithms.
Example:
Input: "Ralphs", "ChickfilA"
Output: ["2578244375", "5559640911", "6787470571", "6808093910", "6808093913", "6808093919", "6816831441",
"6813405269", "6816193784", "6389467806", "6816193783", "123178876", "2613117895", "122719259",
"2613117861", "6817230316", "3642819026", "6817230310", "7811699597", "5565967545", "123318572",
"6813405206", "6813379482", "544672028", "21306059", "6813379476", "6818390140", "63068610",
"6818390143", "7434941012", "4015423966", "5690152766", "6813379440", "6813379466", "21306060",
"6813379469", "6813379427", "123005255", "6807200376", "6807200380", "6813379451", "6813379463",
"123327639", "6813379460", "4141790922", "4015423963", "1286136447", "1286136422", "4015423962",
"6813379494", "63068643", "6813379496", "123241977", "4015372479", "4015372477", "1732243576",
"6813379548", "4015372476", "4015372474", "4015372468", "4015372463", "6819179749", "1732243544",
"6813405275", "348121996", "348121864", "6813405280", "1472141024", "6813411590", "216155217",
"6813411589", "1837212103", "1837212101", "6820935911", "4547476733"]
3
**************************************************************
* 3. CalculateShortestPath
**************************************************************
Please input the start location:Ralphs
Please input the destination:ChickfilA
*************************Results******************************
The distance of the path is:1.53852 miles
**************************************************************
Time taken by function: 45149 microseconds
In this section, we assume that a complete graph is given to you. That means each node is a neighbor of all other nodes. Given a vector of location ids, assume every location can reach all other locations in the vector (i.e. assume that the vector of location ids is a complete graph). Find the shortest route that covers all the locations exactly once and goes back to the start point.
You will need to return the progress to get the shortest route which will then be converted to an animation.
We will use the following algorithms:
- Brute-force (i.e. generating all permutations, and returning the minimum)
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_Brute_force(
std::vector<std::string> &location_ids);
- Brute-force enhanced with early backtracking
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan(
std::vector<std::string> &location_ids);
- 2-opt Heuristic. Also see this paper
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_2opt(
std::vector<std::string> &location_ids);
Brute-force and backtracking where we use early backtracking when the current cost is higher than current minimum.
Please report and compare the time spent by these 3 algorithms. 2-opt algorithm may not get the optimal solution. Please show how far your solution is from the optimal solution.
Show the routes on the map. For each intermediate solution, create a new plot. Your final video presentation should include the changes to your solution.
We will randomly select N points in the map and run your program.
4
**************************************************************
* 4. Travelling salesman problem
**************************************************************
In this task, we will select N random points on the map and you need to find the path to travel these points and back to the start point.
Please input the number of the places:10
Calculating ...
*************************Results******************************
The distance of the path is:4.70299 miles
**************************************************************
You could find your animation at src/lib/output.avi.
Time taken by function: 152517394 microseconds
bool CycleDetection(std::vector<double> &square);
In this section, we use a square-shaped subgraph of the original graph by using four coordinates stored in std::vector<double> square
, which follows the order of left, right, upper, and lower bounds.
Then try to determine if there is a cycle path in the that subgraph. If it does, return true and report that path on the map. Otherwise return false.
Example 1:
Input: square = {-118.299, -118.264, 34.032, 34.011}
Output: true
Here we use the whole original graph as our subgraph.
Example 2:
Input: square = {-118.290919, -118.282911, 34.02235, 34.019675}
Output: false
Here we use a square area inside USC campus as our subgraph
Note: You could use the function below to visualize the subgraph.
/**
* PlotPoints: Given a vector of location ids draws the points on the map (no path).
*
* @param {std::vector<std::string>} location_ids : points inside square
* @param {std::vector<double>} square : boundary
*/
void TrojanMap::PlotPointsandEdges(std::vector<std::string> &location_ids, std::vector<double> &square)
5
**************************************************************
* 5. Cycle Detection
**************************************************************
Please input the left bound longitude(between -118.299 and -118.264):-118.299
Please input the right bound longitude(between -118.299 and -118.264):-118.264
Please input the upper bound latitude(between 34.011 and 34.032):34.032
Please input the lower bound latitude(between 34.011 and 34.032):34.011
*************************Results******************************
there exists cycle in the subgraph
**************************************************************
Time taken by function: 273734 microseconds
5
**************************************************************
* 5. Cycle Detection
**************************************************************
Please input the left bound longitude(between -118.299 and -118.264):-118.290919
Please input the right bound longitude(between -118.299 and -118.264):-118.282911
Please input the upper bound latitude(between 34.011 and 34.032):34.02235
Please input the lower bound latitude(between 34.011 and 34.032):34.019675
*************************Results******************************
there exist no cycle in the subgraph
**************************************************************
Time taken by function: 290371 microseconds
std::vector<std::string> DeliveringTrojan(std::vector<std::string> &location_names,
std::vector<std::vector<std::string>> &dependencies);
Tommy Trojan got a part-time job from TrojanEats, for which he needs to pick up and deliver food from local restaurants to various location near the campus. Tommy needs to visit a few different location near the campus with certain order, since there are some constraints. For example, he must first get the food from the restaurant before arriving at the delivery point.
The TrojanEats app will have some instructions about these constraints. So, Tommy asks you to help him figure out the feasible route!
Here we will give you a vector of location names that Tommy needs to visit, and also some dependencies between those locations.
For example,
Input:
location_names = {"Cardinal Gardens", "Coffee Bean1", "CVS"}
dependencies = {{"Cardinal Gardens","Coffee Bean1"}, {"Cardinal Gardens","CVS"}, {"Coffee Bean1","CVS"}}
Here, {"Cardinal Gardens","Coffee Bean1"}
means
that Tommy must go to Cardinal Gardens
prior to Coffee Bean1
.
Your output should be:
Output: Cardinal Gardens -> Coffee Bean1 -> CVS
Also, we provide PlotPointsOrder
function that can visualize the results on the map. It will plot each location name and also some arrowed lines to demonstrate a feasible route.
If no feasible route exists, you could simply return an empty vector.
Hint:
- You also need to finish
ReadLocationsFromCSVFile
andReadDependenciesFromCSVFile
functions, so you could read and parse data from you own CSV files. We also give two sample CSV files underinput
folder, which could be a reference. - When it asks you filenames, you need to give the absolute path.
- If you do not have
ReadLocationsFromCSVFile
andReadDependenciesFromCSVFile
functions ready yet, you can just press enter when it asks you filenames. It will call the default locations and dependencies. - The locations are actually nodes, and the dependencies could be directed edges. You may want to first construct a DAG and then implement topological sort algorithm to get the route.
6
*************************Results******************************
Topological Sorting Results:
Cardinal Gardens
Coffee Bean1
CVS
**************************************************************
Time taken by function: 43 microseconds
In the user interface, we read the locations and dependencies from topologicalsort_dependencies.csv
and topologicalsort_locations.csv
to modify your input there.
Given a location name and a integer k , find the k closest locations with name on the map and return a vector of string ids.
We will use the following algorithms:
- Heap
std::vector<std::string> FindKClosestPoints(std::string name, int k);
Please report and compare the time spent by this algorithm and show the points on the map.
**************************************************************
* 7. Find K Closest Points
**************************************************************
7
**************************************************************
* 7. Find K Closest Points
**************************************************************
Please input the locations:Ralphs
Please input k:5
*************************Results******************************
Find K Closest Points Results:
1 St Agnes Church
2 Saint Agnes Elementary School
3 Warning Skate Shop
4 Menlo AvenueWest Twentyninth Street Historic District
5 Vermont Elementary School
**************************************************************
Time taken by function: 1975 microseconds
For each menu item, your program should show the time it took to finish each task.
Please make sure to provide various examples when you report the runtime. For example for topological sort, show an example with few nodes and another example with 10 or more nodes. The idea is to see how your runtime grows as input size grows.
For shortest path algorithms, you should compare solving the same problem with different algorithms (Dijkstra and Bellman-Ford). Please show the results on at least 10 different examples.
Similarly for TSP problem, please provide various examples that show the runtime comparison. In particular, you should show at what point using the exhaustive search is not practical and compare the same input with the heuristic implementation.
Your final project should be checked into Github. The README of your project is your report.
Your README file should include four sections:
- High-level overview of your design (Use diagrams and pictures for your data structures).
- Detailed description of each function and its time complexity.
- Time spent for each function.
- Discussion, conclusion, and lessons learned.
-
Implementation of auto complete: 5 points.
-
Implementation of GetPosition: 5 points.
-
Implementation of shortest path: 15 points.
- Bellman-Ford implementation
- Dijkstra implementation
- Plot two paths, and measure and report time spent by two algorithms.
-
Implementation of Travelling Trojan:
- Brute-force: 5 points.
- Brute-force enhanced with early backtracking: 5 points.
- 2-opt: 10 points.
- Animated plot: 5 points.
-
Implement of Cycle detection: 10 points.
- Boolean value and draw the cycle if there exists one.
-
Topological Sort: 10 points.
- Check whether there exist a topological sort or not
- Return the correct order and plot those point on the map
-
Creating reasonable unit tests: 10 points.
- Three different unit tests for each item.
-
Find K closest points: 10 points.
- Return the correct ids and draw the points.
-
Video presentation and report: 10 points.
-
Extra credit items: Maximum of 20 points:
- 3-opt: 10 points.
- Genetic algorithm implementation for Travelling Trojan: 10 points
- Create dynamic and animated UI using ncurses: 10 points
- You could check https://github.com/ourarash/ncurses_bazel
- Please develope your own UI.
- Example
- Accurate measurement of your algorithm runtime using Google Benchmark while sweeping the input size and providing a diagram of how the runtime grows based on the input size.
$ bazel run --cxxopt='-std=c++17' src/main:main