-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
230 lines (187 loc) · 7.25 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
#include <iostream>
#include <limits>
#include <unordered_map>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <algorithm>
using namespace std;
// Struct to represent a cell in the maze
struct Cell {
int x; //position of cell in maze
int y; //position of cell in maze
int id; // unique identifier for the cell
string type; // type of cell -> enter, exit, path, wall
};
// Priority queue node for pathfinding
struct Node {
int id; //node id
double cost; //cost of traveling node
bool operator>(const Node &other) const {
return cost > other.cost; // prioritize smallest cost first
}
};
unordered_map<int, vector<pair<int, double>>> graphBuild(const vector<Cell> &maze, int rows, int cols) {
unordered_map<int, vector<pair<int, double>>> graph;
for (const auto &cell : maze) {
if (cell.type == "wall") continue;
// checking through the neighbors
vector<pair<int, int>> neighbors = {
{cell.x - 1, cell.y}, // up
{cell.x + 1, cell.y}, // down
{cell.x, cell.y - 1}, // left
{cell.x, cell.y + 1} // right
};
// checks if each neighbor is within bounds (rows and columns)
for (const auto &[nx, ny] : neighbors) {
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols) {
int neighborId = nx * cols + ny; // (x,y) -> unique ID
if (maze[neighborId].type != "wall") {
double cost = 1.0; // edge
graph[cell.id].emplace_back(neighborId, cost);
}
}
}
}
return graph;
}
double euclideanDistance(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
// creatas random maze with given rows and columns
vector<Cell> generateMaze(int rows, int cols) {
vector<Cell> maze(rows * cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
int id = i * cols + j;
maze[id] = {i, j, id, (rand() % 10 < 2) ? "wall" : "path"};
}
}
maze[0].type = "enter";
maze[rows * cols - 1].type = "exit";
return maze;
}
// A* Algorithm
vector<int> aStar(const unordered_map<int, vector<pair<int, double>>> &graph, const vector<Cell> &maze, int start, int end) {
unordered_map<int, double> gScore, fScore;
unordered_map<int, int> predecessors;
priority_queue<Node, vector<Node>, greater<Node>> pq;
for (const auto &[id, _] : graph) {
gScore[id] = numeric_limits<double>::infinity();
fScore[id] = numeric_limits<double>::infinity();
}
gScore[start] = 0.0;
fScore[start] = euclideanDistance(maze[start].x, maze[start].y, maze[end].x, maze[end].y);
pq.push({start, fScore[start]});
while (!pq.empty()) {
int current = pq.top().id;
pq.pop();
if (current == end) break; //stops if at end
for (const auto &[neighbor, weight] : graph.at(current)) {
double tentativeGScore = gScore[current] + weight;
if (tentativeGScore < gScore[neighbor]) {
gScore[neighbor] = tentativeGScore;
fScore[neighbor] = tentativeGScore + euclideanDistance(maze[neighbor].x, maze[neighbor].y, maze[end].x, maze[end].y);
predecessors[neighbor] = current;
pq.push({neighbor, fScore[neighbor]});
}
}
}
//remake path
vector<int> path;
int at = end;
while (at != start) {
path.insert(path.begin(), at); // Insert at the beginning
at = predecessors[at];
}
path.insert(path.begin(), start); // Insert the start node
return path;
}
// Dijkstra's Algorithm
vector<int> dijkstra(const unordered_map<int, vector<pair<int, double>>> &graph, int start, int end) {
unordered_map<int, double> dist; // keeps track of the shortest distance node to node
unordered_map<int, int> original; //for remaking graph
priority_queue<Node, vector<Node>, greater<Node>> pq; //next node with most short distance
for (auto it = graph.begin(); it != graph.end(); ++it) {
dist[it->first] = numeric_limits<double>::infinity();
}
dist[start] = 0.0;
pq.push({start, 0.0});
while (!pq.empty()) {
int current = pq.top().id;//gets node w/ smallest distance
pq.pop();
if (current == end) break; //if done stop
//goes through all neighbors of current node
for (const auto &[neighbor, weight] : graph.at(current)) {
double newDist = dist[current] + weight;
if (newDist < dist[neighbor]) {
dist[neighbor] = newDist;
original[neighbor] = current;
pq.push({neighbor, newDist});
}
}
}
// remake path
vector<int> path;
for (int at = end; at != start; at = original[at]) {
path.push_back(at);
}
path.push_back(start);
reverse(path.begin(), path.end());
return path;
}
// Display maze w/ path
void dispMaze(const vector<Cell> &maze, const vector<int> &path, int rows, int cols) {
vector<string> mazeDisplay(rows * cols, ".");
for (int id : path) {
mazeDisplay[id] = "*";
}
mazeDisplay[0] = "S"; // start
mazeDisplay[rows * cols - 1] = "E"; // end
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << mazeDisplay[i * cols + j] << " ";
}
cout << endl;
}
}
int main() {
//creates random maze
srand(time(0));
int rows = 40, cols = 40; // size
vector<Cell> maze = generateMaze(rows, cols);
auto graph = graphBuild(maze, rows, cols);
int start = 0, end = rows * cols - 1;
cout << "------------------------------------------------------------------" << endl;
cout << "| GRAPH PATHFINDING PERFORMANCE |" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "| Choose Pathfinding Algorithm: |" << endl;
cout << "| 1) Dijkstra's Algorithm |" << endl;
cout << "| 2) A* Algorithm |" << endl;
cout << "| 3) Compare Both |" << endl;
cout << "------------------------------------------------------------------" << endl;
char choice;
cin >> choice;
clock_t startTime;
clock_t endTime;
if (choice == '1' || choice == '3') {
startTime = clock();
vector<int> path = dijkstra(graph, start, end);
endTime = clock();
cout << "Dijkstra's Algorithm Results:" << endl;
cout << "Time Taken: " << fixed << setprecision(5) << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
dispMaze(maze, path, rows, cols);
}
if (choice == '2' || choice == '3') {
startTime = clock();
vector<int> path = aStar(graph, maze, start, end);
endTime = clock();
cout << "A* Algorithm Results:" << endl;
cout << "Time Taken: " << fixed << setprecision(5) << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
dispMaze(maze, path, rows, cols);
}
return 0;
}