-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
375 lines (317 loc) · 11.3 KB
/
main.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
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
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct Station {
string name;
map<Station*, int> neighbors;
};
struct HeapNode {
Station* station;
int distance;
bool operator>(const HeapNode& other) const {
return distance > other.distance;
}
};
// Binary Search Tree for organizing stations
class StationBST {
public:
void insert(Station* station) {
root = insertRec(root, station);
}
void inorder() {
inorderRec(root);
}
struct TreeNode {
Station* station;
TreeNode* left;
TreeNode* right;
TreeNode(Station* s) : station(s), left(nullptr), right(nullptr) {}
};
TreeNode* root;
TreeNode* insertRec(TreeNode* root, Station* station) {
if (root == nullptr) {
return new TreeNode(station);
}
if (station->name < root->station->name) {
root->left = insertRec(root->left, station);
} else if (station->name > root->station->name) {
root->right = insertRec(root->right, station);
}
return root;
}
void inorderRec(TreeNode* root) {
if (root != nullptr) {
inorderRec(root->left);
cout << root->station->name << " ";
inorderRec(root->right);
}
}
};
class MetroNetwork {
public:
void addStation(const string& name) {
Station* newStation = new Station{name};
stations.push_back(newStation);
stationHashTable[name] = newStation;
stationBST.insert(newStation);
}
void addConnection(const string& station1, const string& station2, int distance) {
Station* s1 = findStation(station1);
Station* s2 = findStation(station2);
if (s1 != nullptr && s2 != nullptr) {
s1->neighbors[s2] = distance;
s2->neighbors[s1] = distance;
} else {
cout << "Invalid station names. Please make sure both stations exist." << endl;
}
}
int calculateFare(const string& source, const string& destination) {
Station* sourceStation = findStation(source);
Station* destinationStation = findStation(destination);
if (sourceStation == nullptr || destinationStation == nullptr) {
cout << "Invalid source or destination station." << endl;
return -1;
}
map<Station*, int> distances = dijkstra(sourceStation);
int distance = distances[destinationStation];
if (distance == INT_MAX) {
cout << "No valid route between the stations." << endl;
return -1;
}
int fare = distance * 2;
return fare;
}
void displayNetwork() {
for (Station* station : stations) {
cout << "Station " << station->name << " Neighbors: ";
for (auto& neighbor : station->neighbors) {
cout << neighbor.first->name << "(" << neighbor.second << " units) ";
}
cout << endl;
}
}
void displayStations() {
cout << "Stations: ";
stationBST.inorder();
cout << endl;
}
void removeStation(const string& name) {
auto it = find_if(stations.begin(), stations.end(),
[name](Station* station) { return station->name == name; });
if (it != stations.end()) {
Station* stationToRemove = *it;
stations.erase(it);
// Remove connections to the station
for (Station* otherStation : stations) {
auto neighborIt = otherStation->neighbors.find(stationToRemove);
if (neighborIt != otherStation->neighbors.end()) {
otherStation->neighbors.erase(neighborIt);
}
}
// Remove from hash table
stationHashTable.erase(name);
// Remove from BST
stationBST = removeFromBST(stationBST, stationToRemove);
delete stationToRemove;
cout << "Station " << name << " removed from the metro network." << endl;
} else {
cout << "Station " << name << " not found in the metro network." << endl;
}
}
void removeConnection(const string& station1, const string& station2) {
Station* s1 = findStation(station1);
Station* s2 = findStation(station2);
if (s1 != nullptr && s2 != nullptr) {
auto it1 = s1->neighbors.find(s2);
auto it2 = s2->neighbors.find(s1);
if (it1 != s1->neighbors.end() && it2 != s2->neighbors.end()) {
s1->neighbors.erase(it1);
s2->neighbors.erase(it2);
cout << "Connection between " << station1 << " and " << station2 << " removed." << endl;
} else {
cout << "No connection found between " << station1 << " and " << station2 << "." << endl;
}
} else {
cout << "Invalid station names. Please make sure both stations exist." << endl;
}
}
~MetroNetwork() {
for (Station* station : stations) {
delete station;
}
}
Station* findStation(const string& name) {
auto it = stationHashTable.find(name);
if (it != stationHashTable.end()) {
return it->second;
} else {
return nullptr;
}
}
private:
vector<Station*> stations;
unordered_map<string, Station*> stationHashTable;
StationBST stationBST;
map<Station*, int> dijkstra(Station* source) {
map<Station*, int> distances;
priority_queue<HeapNode, vector<HeapNode>, greater<HeapNode>> minHeap;
for (Station* station : stations) {
distances[station] = INT_MAX;
}
distances[source] = 0;
minHeap.push({source, 0});
while (!minHeap.empty()) {
HeapNode current = minHeap.top();
minHeap.pop();
for (auto& neighbor : current.station->neighbors) {
int newDistance = distances[current.station] + neighbor.second;
if (newDistance < distances[neighbor.first]) {
distances[neighbor.first] = newDistance;
minHeap.push({neighbor.first, newDistance});
}
}
}
return distances;
}
// Helper function to remove a node from BST
StationBST removeFromBST(StationBST bst, Station* stationToRemove) {
bst.root = removeNode(bst.root, stationToRemove);
return bst;
}
// Helper function to remove a node from BST
StationBST::TreeNode* removeNode(StationBST::TreeNode* root, Station* stationToRemove) {
if (root == nullptr) {
return root;
}
if (stationToRemove->name < root->station->name) {
root->left = removeNode(root->left, stationToRemove);
} else if (stationToRemove->name > root->station->name) {
root->right = removeNode(root->right, stationToRemove);
} else {
// Node with only one child or no child
if (root->left == nullptr) {
StationBST::TreeNode* temp = root->right;
delete root;
return temp;
} else if (root->right == nullptr) {
StationBST::TreeNode* temp = root->left;
delete root;
return temp;
}
// Node with two children, get the inorder successor
StationBST::TreeNode* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->station = temp->station;
// Delete the inorder successor
root->right = removeNode(root->right, temp->station);
}
return root;
}
// Helper function to find the node with the minimum value
StationBST::TreeNode* minValueNode(StationBST::TreeNode* root) {
StationBST::TreeNode* current = root;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
};
void addNewStation(MetroNetwork& metro) {
string stationName;
cout << "Enter the name of the new station: ";
cin >> stationName;
metro.addStation(stationName);
cout << "Station " << stationName << " added to the metro network." << endl;
}
void addNewConnection(MetroNetwork& metro) {
string station1, station2;
int distance;
metro.displayStations();
cout << "Enter the names of the two stations to connect: ";
cin >> station1 >> station2;
cout << "Enter the distance between " << station1 << " and " << station2 << ": ";
cin >> distance;
metro.addConnection(station1, station2, distance);
cout << "Connection between " << station1 << " and " << station2 << " added to the metro network." << endl;
}
void calculateFare(MetroNetwork& metro) {
string source, destination;
metro.displayStations();
cout << "Enter the source station: ";
cin >> source;
cout << "Enter the destination station: ";
cin >> destination;
int fare = metro.calculateFare(source, destination);
if (fare != -1) {
cout << "The fare between stations " << source << " and " << destination << " is $" << fare << "." << endl;
}
}
void removeStation(MetroNetwork& metro) {
string stationName;
metro.displayStations();
cout << "Enter the name of the station to remove: ";
cin >> stationName;
metro.removeStation(stationName);
}
void removeConnection(MetroNetwork& metro) {
string station1, station2;
metro.displayStations();
cout << "Enter the names of the two stations to remove connection: ";
cin >> station1 >> station2;
metro.removeConnection(station1, station2);
}
int main() {
MetroNetwork metro;
metro.addStation("A");
metro.addStation("B");
metro.addStation("C");
metro.addStation("D");
metro.addConnection("A", "B", 5);
metro.addConnection("B", "C", 3);
metro.addConnection("C", "D", 4);
metro.addConnection("D", "A", 7);
while (true) {
cout << "\nMetro Network Operations:" << endl;
cout << "1. Add new station" << endl;
cout << "2. Add new connection between stations" << endl;
cout << "3. Calculate fare between stations" << endl;
cout << "4. Display metro network" << endl;
cout << "5. Remove station" << endl;
cout << "6. Remove connection between stations" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice (1-7): ";
int choice;
cin >> choice;
switch (choice) {
case 1:
addNewStation(metro);
break;
case 2:
addNewConnection(metro);
break;
case 3:
calculateFare(metro);
break;
case 4:
metro.displayNetwork();
break;
case 5:
removeStation(metro);
break;
case 6:
removeConnection(metro);
break;
case 7:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please enter a number between 1 and 7." << endl;
}
}
return 0;
}