-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijsktra.js
109 lines (107 loc) · 2.77 KB
/
dijsktra.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
let solved = false;
let solvingDijsktra = false;
let showpath = false; //false
let unvisited = [];
let currentonPathAnimation;
let prevdist = 0;
let visualise = false;
function startDijsktra() {
if (solvingDijsktra) {
console.log("already solving");
return;
} else if (solved) clearState();
else {
//solve using dijsktra here
console.log("solving dijsktra");
solvingDijsktra = true;
unvisited = [source];
source.distance = 0;
}
}
function solveDijsktra() {
console.log("solving");
if (unvisited.length == 0) {
solvingDijsktra = false;
solved = true;
startpath();
return;
}
while (unvisited.length > 0 && unvisited[0].distance == prevdist) {
//console.log(unvisited);
let neighbors = getUnvisitedNeighbors(unvisited[0].x, unvisited[0].y);
//console.log(neighbors);
for (let x = 0; x < neighbors.length; x++) {
unvisited.push(neighbors[x]);
neighbors[x].distance = unvisited[0].distance + 1;
neighbors[x].prev = unvisited[0];
neighbors[x].visitedFromSource = true;
}
unvisited[0].draw();
unvisited.shift();
}
if (unvisited.length > 0) prevdist = unvisited[0].distance;
}
function getUnvisitedNeighbors(col, row) {
let neighbors = [];
if (
row + 1 < rows &&
grid.grid[row + 1][col].path &&
grid.grid[row + 1][col].visitedFromSource == false
)
neighbors.push(grid.grid[row + 1][col]);
if (
col + 1 < cols &&
grid.grid[row][col + 1].path &&
grid.grid[row][col + 1].visitedFromSource == false
)
neighbors.push(grid.grid[row][col + 1]);
if (
row - 1 >= 0 &&
grid.grid[row - 1][col].path &&
grid.grid[row - 1][col].visitedFromSource == false
)
neighbors.push(grid.grid[row - 1][col]);
if (
col - 1 >= 0 &&
grid.grid[row][col - 1].path &&
grid.grid[row][col - 1].visitedFromSource == false
)
neighbors.push(grid.grid[row][col - 1]);
return neighbors;
}
function startpath() {
if (showpath && showpath != destination) {
clearpath();
}
if (showpath == false) {
currentonPathAnimation = destination.prev;
showpath = destination;
visualise = true;
return;
}
if (destination.path) {
currentonPathAnimation = destination.prev;
while (currentonPathAnimation != source) {
currentonPathAnimation.drawroute();
currentonPathAnimation = currentonPathAnimation.prev;
}
showpath = destination;
}
}
function visualisePath() {
if (currentonPathAnimation == source) {
console.log("stopped");
visualise = false;
return;
}
currentonPathAnimation.drawroute();
currentonPathAnimation = currentonPathAnimation.prev;
}
function clearpath() {
current = showpath.prev;
while (current != source) {
current.drawpath();
current = current.prev;
}
showpath = destination;
}