-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.js
199 lines (163 loc) · 5.54 KB
/
algorithms.js
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
// Algorithm implementations and related utility functions
class PathfindingAlgorithms {
constructor(grid, weights) {
this.grid = grid;
this.weights = weights;
}
// Utility method to get neighbors of a cell
getNeighbors(cell, rows, cols) {
const [row, col] = [parseInt(cell.dataset.row), parseInt(cell.dataset.col)];
const neighbors = [];
if (row > 0) neighbors.push(this.grid[row - 1][col]);
if (row < rows - 1) neighbors.push(this.grid[row + 1][col]);
if (col > 0) neighbors.push(this.grid[row][col - 1]);
if (col < cols - 1) neighbors.push(this.grid[row][col + 1]);
return neighbors;
}
// Heuristic function for A* algorithm
heuristic(cell1, cell2) {
const [row1, col1] = [
parseInt(cell1.dataset.row),
parseInt(cell1.dataset.col),
];
const [row2, col2] = [
parseInt(cell2.dataset.row),
parseInt(cell2.dataset.col),
];
return Math.abs(row1 - row2) + Math.abs(col1 - col2);
}
// Helper method to get node with minimum distance
getMinDistanceNode(queue, distances) {
return queue.reduce((min, node) =>
!min || distances.get(node) < distances.get(min) ? node : min
);
}
// Helper method to get node with minimum f-score
getMinFScoreNode(openSet, fScore) {
return openSet.reduce((min, node) =>
!min || fScore.get(node) < fScore.get(min) ? node : min
);
}
// Dijkstra's Algorithm
async dijkstra(startCell, endCell, rows, cols, updateVisitedCell, delay) {
const queue = [startCell];
const visited = new Set();
const prev = new Map();
const distances = new Map();
distances.set(startCell, 0);
while (queue.length > 0) {
const current = this.getMinDistanceNode(queue, distances);
queue.splice(queue.indexOf(current), 1);
if (visited.has(current)) {
continue; // Skip if already visited
}
visited.add(current);
await updateVisitedCell(current, visited.size);
if (current === endCell) {
return { prev, visited };
}
const neighbors = this.getNeighbors(current, rows, cols);
for (const neighbor of neighbors) {
if (!visited.has(neighbor) && !neighbor.classList.contains("wall")) {
const weight = this.weights.get(neighbor) || 1;
const distance = distances.get(current) + weight;
if (!distances.has(neighbor) || distance < distances.get(neighbor)) {
distances.set(neighbor, distance);
prev.set(neighbor, current);
queue.push(neighbor);
await delay();
}
}
}
}
return null;
}
// A* Algorithm
async astar(startCell, endCell, rows, cols, updateVisitedCell, delay) {
const openSet = [startCell];
const closedSet = new Set();
const gScore = new Map();
const fScore = new Map();
const prev = new Map();
gScore.set(startCell, 0);
fScore.set(startCell, this.heuristic(startCell, endCell));
while (openSet.length > 0) {
const current = this.getMinFScoreNode(openSet, fScore);
if (current === endCell) {
return { prev, visited: closedSet };
}
openSet.splice(openSet.indexOf(current), 1);
closedSet.add(current);
await updateVisitedCell(current, closedSet.size);
const neighbors = this.getNeighbors(current, rows, cols);
for (const neighbor of neighbors) {
if (closedSet.has(neighbor) || neighbor.classList.contains("wall"))
continue;
const weight = this.weights.get(neighbor) || 1;
const tentativeGScore = gScore.get(current) + weight;
if (!openSet.includes(neighbor)) {
openSet.push(neighbor);
} else if (tentativeGScore >= gScore.get(neighbor)) {
continue;
}
prev.set(neighbor, current);
gScore.set(neighbor, tentativeGScore);
fScore.set(
neighbor,
gScore.get(neighbor) + this.heuristic(neighbor, endCell)
);
await delay();
}
}
return null;
}
// Breadth-First Search
async bfs(startCell, endCell, rows, cols, updateVisitedCell, delay) {
const queue = [startCell];
const visited = new Set([startCell]);
const prev = new Map();
while (queue.length > 0) {
const current = queue.shift();
if (current === endCell) {
return { prev, visited };
}
const neighbors = this.getNeighbors(current, rows, cols);
for (const neighbor of neighbors) {
if (!visited.has(neighbor) && !neighbor.classList.contains("wall")) {
visited.add(neighbor);
prev.set(neighbor, current);
queue.push(neighbor);
await updateVisitedCell(neighbor, visited.size);
await delay();
}
}
}
return null;
}
// Depth-First Search
async dfs(startCell, endCell, rows, cols, updateVisitedCell, delay) {
const stack = [startCell];
const visited = new Set();
const prev = new Map();
while (stack.length > 0) {
const current = stack.pop();
if (!visited.has(current)) {
visited.add(current);
await updateVisitedCell(current, visited.size);
if (current === endCell) {
return { prev, visited };
}
const neighbors = this.getNeighbors(current, rows, cols);
for (const neighbor of neighbors) {
if (!visited.has(neighbor) && !neighbor.classList.contains("wall")) {
prev.set(neighbor, current);
stack.push(neighbor);
await delay();
}
}
}
}
return null;
}
}
export default PathfindingAlgorithms;