Skip to content

Commit 469690a

Browse files
committed
cleaning code up
1 parent fb703d0 commit 469690a

File tree

3 files changed

+88
-52
lines changed

3 files changed

+88
-52
lines changed

src/components/GraphTraversalVisualiser/GraphTraversalVisualiser.jsx

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import "./GraphTraversalVisualiser.css"
33
import { UCS } from './graphAlgorithms/ucs';
44
import { BFS } from './graphAlgorithms/bfs';
55
import { DFS } from './graphAlgorithms/dfs';
6+
import { conflict } from './graphAlgorithms/helper';
67

78
const MatrixVisualization = () => {
89
const [matrix, setMatrix] = useState([]);
910
const [start, setStart] = useState({ row: 0, col: 0 });
10-
// const [objective, setObjective] = useState({ row: 0, col: 0 });
11-
1211
const [objectives, setObjectives] = useState([]);
12+
// const [weights, setWeights] = useState([]);
1313
const [obstacles, setObstacles] = useState([]);
1414
const [algorithm, setAlgorithm] = useState('Reset');
1515
const [button, setButton] = useState(false);
@@ -19,42 +19,10 @@ const MatrixVisualization = () => {
1919
generateMatrix();
2020
}, []);
2121

22-
// function to check for conflict
23-
const conflict = (i, j, maze) => {
24-
// index checking
25-
if (i < 0 || i >= maze.length || j < 0 || j >= maze[0].length) {
26-
return false;
27-
}
28-
// if its currently in use
29-
if (maze[i][j] === "w") {
30-
return true;
31-
}
32-
// if objective is surronded by obstacles
33-
if (maze[i][j] === "o") {
34-
if (
35-
(i > 0 && maze[i - 1][j] === "w") ||
36-
(i < maze.length - 1 && maze[i + 1][j] === "w") ||
37-
(j > 0 && maze[i][j - 1] === "w") ||
38-
(j < maze[0].length - 1 && maze[i][j + 1] === "w")
39-
) {
40-
return true;
41-
}
42-
}
43-
return false;
44-
}
4522

46-
// clear the old path
47-
const clearPath = () => {
48-
const pathNodes = document.querySelectorAll('.path');
49-
pathNodes.forEach((node) => {
50-
node.classList.remove('path');
51-
});
52-
const expandedNodes = document.querySelectorAll('.expanded');
53-
expandedNodes.forEach((node) => {
54-
node.classList.remove('expanded');
55-
});
56-
};
5723

24+
25+
// generate a new matrix
5826
const generateMatrix = () => {
5927
const n = 30
6028
const newMatrix = Array.from({ length: n }, () => Array(n).fill(" ")); // generate array
@@ -93,9 +61,11 @@ const MatrixVisualization = () => {
9361
// set the approriate obstacles, objectives and new matrix/grid
9462
setObjectives(objectivesArray);
9563
setObstacles(obstaclesArray);
64+
// setWeights(weightsArray);
9665
setMatrix(newMatrix);
9766
};
9867

68+
// reset the matrix
9969
const resetMatrix = () => {
10070
clearPath(); // remove the previously drawn path
10171
generateMatrix(); // generate a new matrix
@@ -123,10 +93,20 @@ const MatrixVisualization = () => {
12393
}
12494
}
12595

96+
// clear the old path
97+
const clearPath = () => {
98+
const pathNodes = document.querySelectorAll('.path');
99+
pathNodes.forEach((node) => {
100+
node.classList.remove('path');
101+
});
102+
const expandedNodes = document.querySelectorAll('.expanded');
103+
expandedNodes.forEach((node) => {
104+
node.classList.remove('expanded');
105+
});
106+
};
107+
126108
const handleSubmit = (event) => {
127109
let method = algorithm;
128-
let time = 0;
129-
disableButtons();
130110
if (method === 'astar') {
131111
//
132112
} else if (method === 'BFS') {
@@ -135,7 +115,6 @@ const MatrixVisualization = () => {
135115
colorNodes(result.path, result.expanded);
136116
} else if (method === 'Reset') {
137117
resetMatrix();
138-
enableButtons(1);
139118
} else if (method === 'DFS') {
140119
const result = DFS(matrix,[start.row, start.col], [objectives[0].row, objectives[0].col]);
141120
colorNodes(result.path, result.expanded);
@@ -145,23 +124,13 @@ const MatrixVisualization = () => {
145124
colorNodes(result.path, result.expanded);
146125
}
147126

148-
enableButtons(time);
149127
event.preventDefault();
150128
};
151129

152130
const handleChange = (event) => {
153131
setAlgorithm(event.target.value);
154132
};
155133

156-
const disableButtons = () => {
157-
setButton(true);
158-
};
159-
160-
const enableButtons = (time) => {
161-
setTimeout(() => {
162-
setButton(false);
163-
}, time);
164-
};
165134

166135
return (
167136
<div className='GraphTraversalVisualiser'>
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
export function Astar(grid){
2-
return 0
3-
}
1+
import { PriorityQueue } from './datastructures/priorityQueue';
2+
import { getNeighbors } from './helper';
43

4+
5+
6+
7+
export function Astar(grid, start, destination) {
8+
let priorityQueue = new PriorityQueue();
9+
priorityQueue.enqueue({ node: start, path: [start], cost: 0 });
10+
11+
let visited = {};
12+
let expanded = [];
13+
14+
while (!priorityQueue.isEmpty()) {
15+
let { node, path, cost } = priorityQueue.dequeue(); // get the node, its path, and cost from the front of the priority queue
16+
let nodeKey = node.toString(); // to string for comparison
17+
18+
if (nodeKey === destination.toString()) { // check if we have arrived at the solution
19+
return { status: "found", path, cost , expanded};
20+
} else {
21+
if (!(visited.hasOwnProperty(nodeKey))) { // if we have not been to this node, add it
22+
visited[nodeKey] = true; // mark as visited
23+
let neighbors = getNeighbors(grid, node); // get neighbors of the current node
24+
for (let i = 0; i < neighbors.length; i++) {
25+
let neighbor = neighbors[i];
26+
let neighborKey = neighbor.toString();
27+
let neighborCost = cost
28+
if (grid[neighbor[0]][neighbor[1]] === "e"){
29+
neighborCost += 5;
30+
}else {
31+
neighborCost +=1;
32+
};
33+
34+
if (!(visited.hasOwnProperty(neighborKey))) { // if the neighbor has not been visited, add to the priority queue
35+
if (!(grid[neighbor[0]][neighbor[1]] === "w")) { // if there is no wall, then we add
36+
priorityQueue.enqueue({ node: neighbor, path: path.concat([neighbor]), cost: neighborCost }, neighborCost);
37+
expanded.push(neighbor); // add the expanded node to the list
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
return { status: "not found", path: [], cost: 0 };
46+
}

src/components/GraphTraversalVisualiser/graphAlgorithms/helper.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ export function getNeighbors(matrix, current_node) {
3232

3333
return neighbors;
3434
}
35+
36+
37+
// function to check for conflict
38+
export function conflict(i, j, maze){
39+
// index checking
40+
if (i < 0 || i >= maze.length || j < 0 || j >= maze[0].length) {
41+
return false;
42+
}
43+
// if its currently in use
44+
if (maze[i][j] === "w") {
45+
return true;
46+
}
47+
// if objective is surronded by obstacles
48+
if (maze[i][j] === "o") {
49+
if (
50+
(i > 0 && maze[i - 1][j] === "w") ||
51+
(i < maze.length - 1 && maze[i + 1][j] === "w") ||
52+
(j > 0 && maze[i][j - 1] === "w") ||
53+
(j < maze[0].length - 1 && maze[i][j + 1] === "w")
54+
) {
55+
return true;
56+
}
57+
}
58+
return false;
59+
}

0 commit comments

Comments
 (0)