-
Notifications
You must be signed in to change notification settings - Fork 0
/
PATH FINDING.cpp
408 lines (361 loc) · 10.6 KB
/
PATH FINDING.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <queue>
#include <limits>
#include <fstream>
#include <conio.h>
#include <sstream>
#include <stdexcept>
#include <stack>
using namespace std;
// Function to display ASCII art
void displayAsciiArt() {
cout << R"(
__ _ __ _
|_ _| \ \ / / | |
| | _ _ _ __ _ _ \ \ / / _ _| |
| |/ _ \| '/ _ \| '_ \ \ \/ / ` | '| _|
| | () | | | () | | | | \ / (| | | | |
\/\/|| \/|| || \/ \,|| \|
)" << endl;
}
// Function to display the welcome screen
void displayWelcomeScreen() {
displayAsciiArt();
cout << "\n=== Welcome to the Tower Detection System ===\n";
cout << "Manage and analyze tower connectivity efficiently.\n\n";
}
// Function to display the menu header
void displayMenuHeader() {
cout << R"(
==========================================
Tower Detection System
==========================================
)" << endl;
}
// Data structure for the Tower Detection System
struct Graph {
int numNodes;
vector<vector<pair<int, int>>> adjList;
Graph(int nodes = 0) : numNodes(nodes) {
adjList.resize(nodes);
}
void addEdge(int src, int dest, int weight) {
if (src >= numNodes || dest >= numNodes || src < 0 || dest < 0) {
cout << "Invalid node. Please ensure the nodes exist.\n";
return;
}
adjList[src].emplace_back(dest, weight);
adjList[dest].emplace_back(src, weight); // Undirected graph
}
void addNode() {
numNodes++;
adjList.resize(numNodes);
cout << "Node " << numNodes - 1 << " added successfully.\n";
}
void displayGraph() {
cout << "\n--- Tower Graph Connections ---\n";
for (int i = 0; i < numNodes; i++) {
cout << "Node " << i << ": ";
for (auto& [neighbor, weight] : adjList[i]) {
cout << "(" << neighbor << ", " << weight << ") ";
}
cout << endl;
}
}
// Minimum Spanning Tree using Prim's Algorithm
void findMST() {
if (numNodes == 0) {
cout << "No nodes in the graph.\n";
return;
}
cout << "\n--- Minimum Spanning Tree (MST) ---\n";
vector<int> key(numNodes, INT_MAX);
vector<bool> inMST(numNodes, false);
vector<int> parent(numNodes, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
key[0] = 0;
pq.push({ 0, 0 });
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (inMST[u]) continue;
inMST[u] = true;
for (auto& [v, weight] : adjList[u]) {
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
parent[v] = u;
pq.push({ key[v], v });
}
}
}
for (int i = 1; i < numNodes; ++i) {
if (parent[i] != -1) {
cout << parent[i] << " - " << i << " (Weight: " << key[i] << ")\n";
}
}
}
// Shortest Path using Dijkstra's Algorithm
void findShortestPath(int start, int end) {
if (start >= numNodes || end >= numNodes || start < 0 || end < 0) {
cout << "Invalid nodes.\n";
return;
}
cout << "\n--- Shortest Path ---\n";
vector<int> dist(numNodes, INT_MAX);
vector<int> parent(numNodes, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
dist[start] = 0;
pq.push({ 0, start });
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
for (auto& [v, weight] : adjList[u]) {
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({ dist[v], v });
parent[v] = u;
}
}
}
if (dist[end] == INT_MAX) {
cout << "No path exists.\n";
return;
}
vector<int> path;
for (int v = end; v != -1; v = parent[v]) {
path.push_back(v);
}
reverse(path.begin(), path.end());
cout << "Path: ";
for (int v : path) {
cout << v << " ";
}
cout << "\nTotal Cost: " << dist[end] << endl;
}
// Improved Connectivity Check using DFS
bool isConnected() {
if (numNodes == 0) {
cout << "Graph is empty.\n";
return false;
}
vector<bool> visited(numNodes, false); // Keep track of visited nodes
stack<int> s;
s.push(0); // Start DFS from node 0
visited[0] = true;
while (!s.empty()) {
int node = s.top();
s.pop();
for (auto& [neighbor, _] : adjList[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
s.push(neighbor);
}
}
}
// Check if all nodes were visited
for (bool visit : visited) {
if (!visit) {
cout << "The graph is not connected. There are unreachable nodes.\n";
return false;
}
}
cout << "The graph is connected.\n";
return true;
}
// Load graph from file
void loadGraphFromFile() {
ifstream file("graph.txt");
if (!file) {
cout << "No graph data file found, creating a new graph.\n";
return;
}
int nodeCount, edgeCount;
file >> nodeCount >> edgeCount;
numNodes = nodeCount;
adjList.resize(numNodes);
for (int i = 0; i < edgeCount; i++) {
int src, dest, weight;
file >> src >> dest >> weight;
addEdge(src, dest, weight);
}
file.close();
}
// Save graph to file
void saveGraphToFile() {
ofstream file("graph.txt");
file << numNodes << " " << adjList.size() << "\n";
for (int i = 0; i < numNodes; i++) {
for (auto& [neighbor, weight] : adjList[i]) {
if (i < neighbor) { // Avoid duplicating edges
file << i << " " << neighbor << " " << weight << "\n";
}
}
}
file.close();
}
};
// Login system
unordered_map<string, string> users;
string getPasswordInput() {
string password;
char ch;
while ((ch = _getch()) != '\r') {
if (ch == '\b') {
if (!password.empty()) {
cout << "\b \b";
password.pop_back();
}
}
else {
password += ch;
cout << '*';
}
}
cout << endl;
return password;
}
void loadUsers() {
ifstream file("users.txt");
if (file.is_open()) {
string username, password;
while (file >> username >> password) {
users[username] = password;
}
file.close();
}
}
void saveUsers() {
ofstream file("users.txt");
for (auto& [username, password] : users) {
file << username << " " << password << endl;
}
}
void registerUser() {
string username, password;
cout << "\n--- New User Registration ---\n";
cout << "Enter username: ";
cin.ignore();
getline(cin, username);
if (users.find(username) != users.end()) {
cout << "Username already exists. Please try again.\n";
return;
}
cout << "Enter password: ";
password = getPasswordInput();
users[username] = password;
saveUsers();
cout << "Registration successful!\n";
}
bool loginUser() {
string username, password;
cout << "\n--- User Login ---\n";
cout << "Enter username: ";
cin.ignore();
getline(cin, username);
if (users.find(username) == users.end()) {
cout << "Username not found. Please register as a new user.\n";
return false;
}
cout << "Enter password: ";
password = getPasswordInput();
if (users[username] == password) {
cout << "Login successful!\n";
return true;
}
else {
cout << "Incorrect password. Please try again.\n";
return false;
}
}
// Menu
void menuPage() {
Graph graph;
graph.loadGraphFromFile(); // Load graph from file on startup
int choice;
do {
displayMenuHeader();
cout << "1. Add Node\n";
cout << "2. Add Edge\n";
cout << "3. Display Graph\n";
cout << "4. Find MST\n";
cout << "5. Find Shortest Path\n";
cout << "6. Check Connectivity\n";
cout << "7. Save Graph\n";
cout << "8. Logout\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
graph.addNode();
break;
case 2: {
int src, dest, weight;
cout << "Enter source, destination, and weight: ";
cin >> src >> dest >> weight;
graph.addEdge(src, dest, weight);
break;
}
case 3:
graph.displayGraph();
break;
case 4:
graph.findMST();
break;
case 5: {
int start, end;
cout << "Enter start and end nodes: ";
cin >> start >> end;
graph.findShortestPath(start, end);
break;
}
case 6:
if (graph.isConnected()) {
cout << "The graph is connected.\n";
}
else {
cout << "The graph is not connected.\n";
}
break;
case 7:
graph.saveGraphToFile();
cout << "Graph saved successfully!\n";
break;
case 8:
cout << "Logging out...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 8);
}
int main() {
loadUsers();
displayWelcomeScreen();
int choice;
do {
cout << "\n1. Register\n";
cout << "2. Login\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
registerUser();
break;
case 2:
if (loginUser()) {
menuPage();
}
break;
case 3:
cout << "Exiting the program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 3);
return 0;
}