#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Place {
int index;
string name;
double x, y;
};
struct Edge {
int from, to;
double weight;
bool operator>(const Edge& other) const {
return weight > other.weight;
}
};
double calculateDistance(const Place& a, const Place& b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
vector split(const string &s, char delimiter) {
vector tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
vector readPlacesFromFile(const string& filename) {
ifstream file(filename);
vector places;
string line;
int index = 0;
while (getline(file, line)) {
vector tokens = split(line, ',');
if (tokens.size() == 3) {
Place place;
place.index = index++;
place.name = tokens[0];
place.x = stod(tokens[1]);
place.y = stod(tokens[2]);
places.push_back(place);
}
}
return places;
}
void printPlaces(const vector& places) {
for (const auto& place : places) {
cout << "Index: " << place.index << ", Name: " << place.name << ", X: " << place.x << ", Y: " << place.y << endl;
}
}
vector tspBruteForce(const vector& places) {
// Your implementation goes here
}
vector tspPrimApproximation(const vector& places) {
// your implementation goes here
}
int main() {
string filename;
vector places;
ifstream file;
cout << "Enter the filename: ";
cin >> filename;
file.open(filename);
if (!file) {
cout << "File not found. Please try again." << endl;
return 1;
}
string line;
int index = 0;
while (getline(file, line)) {
vector tokens = split(line, ',');
if (tokens.size() == 4) {
Place place;
place.index = index++;
place.name = tokens[1];
place.x = stod(tokens[2]);
place.y = stod(tokens[3]);
places.push_back(place);
}
}
file.close();
printPlaces(places);
cin.ignore(numeric_limits::max(), '
');
int startIndex;
cout << "Enter the index of the starting city: ";
cin >> startIndex;
if (startIndex < 0 || startIndex >= places.size()) {
cout << "Invalid index for starting city." << endl;
return 1;
}
cin.ignore(numeric_limits::max(), '
');
string indicesList;
cout << "Enter the indices of the cities you wish to visit, separated by commas (do not include the starting city): ";
getline(cin, indicesList);
vector indicesToVisit = split(indicesList, ',');
unordered_set selectedIndices;
vector selectedPlaces;
selectedIndices.insert(startIndex);
cout << "Starting City: " << places[startIndex].name << endl;
for (const auto& indexStr : indicesToVisit) {
int index = stoi(indexStr);
if (index == startIndex) {
cout << "Note: The starting city (Index: " << startIndex << ") was included in the visit list and will be ignored." << endl;
continue;
}
if (index < 0 || index >= places.size()) {
cout << "Invalid index: " << index << ". This city will be ignored." << endl;
continue;
}
if (selectedIndices.count(index) > 0) {
cout << "Duplicate index: " << index << ". This city will be ignored." << endl;
continue;
}
selectedIndices.insert(index);
selectedPlaces.push_back(places[index]);
}
cout << "Cities to Visit:" << endl;
for (const auto& place : selectedPlaces) {
cout << "Index: " << place.index << ", Name: " << place.name << ", X: " << place.x << ", Y: " << place.y << endl;
}
//Modify main below this point to print out the paths traveled for both TSP brute and approx
return 0;
}
There's text file listing some sample places and their coordinates in a fictional x,y plane. Every city has an available connection to every other city, but you must use the Pythagorean theorem to calculate the distance between cities. The main is setup such that a file is read in the format of index,name,xcoordinate,coordinate. You will then choose to begin your tour at a city by choosing that index. You will then choose to visit other cities (you cannot revisit the starting city,you cannot visit other cities more than once). Once you know where you are going,it's time to run two versions of the traveling salesmen problem
Brute force -you will calculate every possible route between the starting location to all the cities that are to be visited before finally returning home to the starting location. Make sure you are returning home to the starting city, otherwise this doesn't count as solving the traveling salesmen problem(the salesmen wants to return home after all that travel, not stay in the last city)
Realize that you must start at your start location and also end at your start location. If you visit 5 places, you will have 6 total distances that are considered [start->place1...(4 other connections needed)...finalplace->start]
When you have a brute force solution let's say you have your starting city and you choose to visit 3 other locations. You will need to consider the following paths: Start->p1->p2->p3->start Start->p1->p3->p2->start Start->p2->p3->p1->start Start->p2->p1->p3->start Start->p3->p1->p2->start Start->p3->p2->p1->start An approximation algorithm that runs remarkably fast and is no worse than twice the optimal solution will be developed by you using Prim's algorithm combined with a minheap (you may use the C++ priority queue as your minheap) 1) Implement tspBruteForce 2) Implement tspPrimApproximation 3) Modify the main in the area
Contents of text file 0,New York,100,200 1,Los Angeles,50,300 2,Chicago,150,180 3,Houston,120,260 4,Phoenix,80,250 5.Philadelphia,110,190 6,San Antonio,130,220 Z,San Diego,60,310 8,Dallas,140,240 9,San Jose,70,280