-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterrail_planner.cpp
More file actions
394 lines (326 loc) · 14 KB
/
interrail_planner.cpp
File metadata and controls
394 lines (326 loc) · 14 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// interrail_planner.cpp
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <queue>
#include <limits>
#include <tuple>
#include <stack>
#include <algorithm>
using namespace std;
// Type definitions
using Graph = unordered_map<string, unordered_map<string, double>>;
using Edge = tuple<double, string, string>; // (distance, cityA, cityB)
using EdgeDijkstra = pair<double, string>; // (distance from start, city)
// step 1: parse data.
void initializeGraph(Graph &graph) {
// Manually adding the parsed data into the C++ graph
graph["Rome"] = {{"Florence", 1.5}, {"Venice", 4.0}, {"Bucharest", 10.0}, {"Paris", 13.0}, {"Munich", 12.0}, {"Monaco", 4.5}};
graph["Zurich"] = {{"Paris", 4.05}, {"Munich", 4.0}, {"Lyon", 5.5}};
graph["Sarajevo"] = {{"Budapest", 8.0}};
graph["Florence"] = {{"Rome", 1.5}, {"Venice", 3.0}, {"Bucharest", 18.0}};
graph["Venice"] = {{"Rome", 4.0}, {"Florence", 3.0}};
graph["Bucharest"] = {{"Rome", 10.0}, {"Florence", 18.0}, {"Budapest", 12.0}, {"Istanbul", 8.0}, {"Kiev", 15.0}};
graph["Paris"] = {{"Rome", 13.0}, {"Zurich", 4.05}, {"London", 2.5}, {"Lyon", 2.0}, {"Marseille", 3.5}};
graph["Lisbon"] = {{"Barcelona", 12.0}, {"Madrid", 1.0}};
graph["Barcelona"] = {{"Lisbon", 12.0}, {"Madrid", 3.0}, {"Marseille", 4.0}};
graph["Madrid"] = {{"Lisbon", 1.0}, {"Barcelona", 3.0}};
graph["Budapest"] = {{"Sarajevo", 8.0}, {"Bucharest", 12.0}, {"Warsaw", 10.0}};
graph["Minsk"] = {{"Moscow", 8.0}, {"Warsaw", 7.0}, {"Kiev", 7.0}};
graph["Gothenburg"] = {{"Oslo", 3.0}, {"Stockholm", 3.0}};
graph["Athens"] = {{"Munich", 6.0}, {"Istanbul", 10.0}};
graph["Riga"] = {{"Helsinki", 1.0}, {"Warsaw", 6.0}};
graph["Manchester"] = {{"London", 2.0}};
graph["Oslo"] = {{"Gothenburg", 3.0}};
graph["Malmö"] = {{"Stockholm", 0.5}};
graph["Stockholm"] = {{"Gothenburg", 3.0}, {"Malmö", 0.5}, {"Helsinki", 1.0}};
graph["Cologne"] = {{"Munich", 4.5}, {"Amsterdam", 2.5}};
graph["Helsinki"] = {{"Riga", 1.0}, {"Stockholm", 1.0}, {"Moscow", 3.0}};
graph["Moscow"] = {{"Minsk", 8.0}, {"Helsinki", 3.0}, {"Kiev", 9.0}};
graph["Munich"] = {{"Rome", 12.0}, {"Zurich", 4.0}, {"Athens", 6.0}, {"Cologne", 4.5}};
graph["Warsaw"] = {{"Budapest", 10.0}, {"Minsk", 7.0}, {"Riga", 6.0}, {"Kiev", 9.0}};
graph["Istanbul"] = {{"Bucharest", 8.0}, {"Athens", 10.0}};
graph["London"] = {{"Paris", 2.5}, {"Manchester", 2.0}, {"Amsterdam", 1.0}};
graph["Kiev"] = {{"Bucharest", 15.0}, {"Minsk", 7.0}, {"Moscow", 9.0}, {"Warsaw", 9.0}};
graph["Amsterdam"] = {{"Cologne", 2.5}, {"London", 1.0}};
graph["Lyon"] = {{"Zurich", 5.5}, {"Paris", 2.0}, {"Marseille", 3.0}};
graph["Monaco"] = {{"Rome", 4.5}, {"Marseille", 2.5}};
graph["Marseille"] = {{"Paris", 3.5}, {"Barcelona", 4.0}, {"Lyon", 3.0}, {"Monaco", 2.5}};
}
// Step 2: Extract a subgraph
Graph extractSubgraph(const Graph &graph, const vector<string> &selectedCities) {
Graph subgraph;
// loop through each city in the list of selected cities
for (const string &city : selectedCities){
// If the city exists in the main graph, add it to the subgraph and initialize it empty.
if (graph.find(city) != graph.end()){
subgraph[city] = {};
}
// copy connections to other selected cities
for (const auto &[neighbor, distance] : graph.at(city)){
if (find(selectedCities.begin(), selectedCities.end(), neighbor) != selectedCities.end()){
subgraph[city][neighbor] = distance;
}
}
}
return subgraph;
}
// Step 3: Calculate the Minimum Spanning Tree (MST)
vector<Edge> primMST(Graph &graph, const string &startCity) {
unordered_set<string> visitedCities;
vector<Edge> mstEdges;
priority_queue<Edge, vector<Edge>, greater<Edge>> minHeap; // greater<Edge> tells it prioritize smaller items first, making it a min-heap
visitedCities.insert(startCity);
// Add all edges from the starting city to the priority queue
for (const auto &[neighbor, distance] : graph[startCity]){
minHeap.push(std::make_tuple(distance, startCity, neighbor));
}
// Expand the MST
while (!minHeap.empty()){
auto [distance, cityA, cityB] = minHeap.top();
minHeap.pop();
// skip this edge if cityB is already visited
if (visitedCities.find(cityB) != visitedCities.end()){
continue;
}
// Add an unvisited edge to the MST
mstEdges.push_back({distance, cityA, cityB});
visitedCities.insert(cityB);
// Add all edges from cityB to the priority queue if the neighbor is not visited
for (const auto &[neighbor, distance] : graph[cityB]){
if (visitedCities.find(neighbor) == visitedCities.end()){
minHeap.push({distance, cityB, neighbor});
}
}
// stop looping when MST is complete (edges = nodes-1)
if (mstEdges.size() == graph.size() - 1){
break;
}
}
return mstEdges;
}
// Function to print the MST
void printMST(const vector<Edge> &mstEdges) {
cout << "Minimum Spanning Tree (MST):" << endl;
for (const auto &[distance, cityA, cityB] : mstEdges) {
cout << cityA << " --(" << distance << ")-- " << cityB << endl;
}
}
// Step 4: Find the shortest path using Dijkstra's algorithm
vector<string> dijkstra(const Graph &graph, const string &startCity, const string &endCity) {
unordered_map<string, double> distanceMap;
unordered_map<string, string> previousMap;
// initializing distance map with all nodes being at a distance of infinity
for (const auto &[city, _] : graph){
distanceMap[city] = numeric_limits<double>::infinity();
}
distanceMap[startCity] = 0;
// Min-heap priority queue (distance, city)
priority_queue<EdgeDijkstra, vector<EdgeDijkstra>, greater<EdgeDijkstra>> minHeap;
minHeap.push({0, startCity});
while (!minHeap.empty()){
auto [currDistance, currCity] = minHeap.top();
minHeap.pop();
if (currCity == endCity){ // stopping when end city is reached
break;
}
// Update the distance to neighboring cities
for (const auto &[neighbor, distance] : graph.at(currCity)){
double newDistance = currDistance + distance;
if (newDistance < distanceMap[neighbor]){
distanceMap[neighbor] = newDistance;
minHeap.push({newDistance, neighbor});
previousMap[neighbor] = currCity;
}
}
}
// Reconstruct the path from previous map
vector<string> path;
for (string at = endCity; at != ""; at = previousMap[at]){
path.push_back(at);
if (at == startCity) break;
}
reverse(path.begin(), path.end());
return path;
}
// Function to print the shortest path
void printPath(const vector<string> &path) {
if (path.empty()) {
cout << "No path found." << endl;
return;
}
cout << "Shortest path: ";
for (size_t i = 0; i < path.size(); ++i) {
cout << path[i];
if (i < path.size() - 1) cout << " -> ";
}
cout << endl;
}
// Step 5 rework (Traveling Salesman Problem)
pair<vector<string>, double> nearestNeighborTSP(const Graph &graph, const string &startCity) {
unordered_set<string> visitedCities;
vector<string> tour;
stack<string> cityStack;
string currentCity = startCity;
double totalDistance = 0.0;
tour.push_back(startCity);
visitedCities.insert(startCity);
cityStack.push(startCity);
// Main loop to build the tour
while (visitedCities.size() < graph.size()) {
string nearestNeighbor;
double minDistance = numeric_limits<double>::infinity();
// Find the nearest unvisited neighbor
for (const auto &[neighbor, distance] : graph.at(currentCity)) {
if (visitedCities.find(neighbor) == visitedCities.end() && distance < minDistance) {
minDistance = distance;
nearestNeighbor = neighbor;
}
}
if (!nearestNeighbor.empty()) {
// Found an unvisited neighbor, add it to the tour
tour.push_back(nearestNeighbor);
visitedCities.insert(nearestNeighbor);
cityStack.push(nearestNeighbor);
totalDistance += minDistance;
currentCity = nearestNeighbor;
} else {
// No unvisited neighbors found, backtrack
cout << "No unvisited neighbors found for " << currentCity << ". Backtracking." << endl;
string previousCity = cityStack.top();
cityStack.pop();
// If the stack is empty, we have backtracked to the start and there are no unvisited neighbors left
if (cityStack.empty()) {
cout << "All cities visited or no valid tour found." << endl;
break;
}
// Backtrack to the previous city
currentCity = cityStack.top();
double backtrackDistance = graph.at(previousCity).at(currentCity);
tour.push_back(currentCity);
totalDistance += backtrackDistance;
}
}
// Check if a direct edge exists from the last city to the startCity
const string &lastCity = tour.back();
if (graph.at(lastCity).find(startCity) != graph.at(lastCity).end()) {
// Direct connection exists, add it to the tour
tour.push_back(startCity);
totalDistance += graph.at(lastCity).at(startCity);
} else {
// No direct connection, use dijkstra() to find the shortest path back
vector<string> pathToStart = dijkstra(graph, lastCity, startCity);
if (!pathToStart.empty()) {
// Add the path back to the starting city and update the total distance
for (size_t i = 1; i < pathToStart.size(); ++i) {
const string &from = pathToStart[i - 1];
const string &to = pathToStart[i];
tour.push_back(to);
totalDistance += graph.at(from).at(to);
}
} else {
cout << "Failed to find a path back to the starting city." << endl;
}
}
return {tour, totalDistance};
}
// Function to print the TSP tour
void printTour(const vector<string> &tour) {
cout << "TSP Tour: ";
for (size_t i = 0; i < tour.size(); ++i) {
cout << tour[i];
if (i < tour.size() - 1) cout << " -> ";
}
cout << endl;
}
// Step 6: Suggest the most cost-effective Interrail pass
void suggestInterrailPass(const vector<string> &tour, const double totalDistance) {
// Sample pass options (prices are illustrative, not actual values)
struct PassOption {
string name;
int travelDays;
double price;
};
vector<PassOption> passOptions = {
{"5 travel days in 1 month", 5, 200.0},
{"7 travel days in 1 month", 7, 250.0},
{"10 travel days in 1 month", 10, 300.0},
{"Unlimited travel for 1 month", 30, 500.0},
{"Unlimited travel for 2 months", 60, 900.0}
};
// Count the number of travel days (each transition between cities is a travel day)
int travelDays = tour.size() - 1;
// Suggest a pass based on the number of travel days
PassOption bestOption = passOptions.back(); // Start with the most extensive option
for (const auto &option : passOptions) {
if (travelDays <= option.travelDays) {
bestOption = option;
break;
}
}
cout << "Recommended Interrail Pass: " << bestOption.name << " (Price: $" << bestOption.price << ")" << endl;
cout << "Total travel days: " << travelDays << endl;
cout << "Total distance covered: " << totalDistance << " km" << endl;
}
void makeUndirected(Graph &graph) { // function to make a graph undirected
for (const auto &[city, neighbors] : graph) {
for (const auto &[neighbor, distance] : neighbors) {
if (graph[neighbor].find(city) == graph[neighbor].end()) {
graph[neighbor][city] = distance;
}
}
}
}
// Main function for testing
int main() {
Graph cityGraph;
initializeGraph(cityGraph);
// TEST STEP 1
string city = "Rome";
cout << city << " connects to:\n";
for (const auto &[neighbor, distance] : cityGraph[city]) {
cout << " " << neighbor << " with distance " << distance << "\n";
}
// TEST STEP 2
// Define a list of selected cities
vector<string> selectedCities = {"Rome", "Venice", "Paris"};
// Extract the subgraph
Graph subgraph = extractSubgraph(cityGraph, selectedCities);
// Print the subgraph
cout << "\nSubgraph is: \n\n";
for (const auto &[city, neighbors] : subgraph) {
cout << city << " connects to:\n";
for (const auto &[neighbor, distance] : neighbors) {
cout << " " << neighbor << " with distance " << distance << "\n";
}
}
cout << "\n";
// TEST STEP 3
Graph graph;
graph["Rome"] = {{"Florence", 1.5}, {"Venice", 4.0}, {"Paris", 13.0}};
graph["Florence"] = {{"Rome", 1.5}, {"Venice", 3.0}};
graph["Venice"] = {{"Rome", 4.0}, {"Florence", 3.0}, {"Paris", 3.5}};
graph["Paris"] = {{"Rome", 13.0}, {"Zurich", 4.05}};
graph["Zurich"] = {{"Paris", 4.05}, {"Munich", 4.0}};
graph["Munich"] = {{"Zurich", 4.0}, {"Florence", 18.0}};
// Adding reverse connections to make the graph undirected
makeUndirected(graph);
// vector<Edge> mstEdges = primMST(graph, "Rome");
vector<Edge> mstEdges = primMST(cityGraph, "Rome");
printMST(mstEdges);
// TEST STEP 4
// Run Dijkstra's algorithm from "Rome" to "Munich"
// vector<string> shortestPath = dijkstra(graph, "Rome", "Munich");
vector<string> shortestPath = dijkstra(cityGraph, "Madrid", "Stockholm");
// Print the shortest path
printPath(shortestPath);
// TEST STEP 5 REWORK
auto [tour, totalDistance] = nearestNeighborTSP(cityGraph, "Rome");
printTour(tour);
cout << "Total tour distance: " << totalDistance << "\n" << endl;
// TEST STEP 6
suggestInterrailPass(tour, totalDistance);
return 0;
}