-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Star_Algorithm.js
67 lines (56 loc) · 2.9 KB
/
A_Star_Algorithm.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
const aStar = function (graph, heuristic, start, goal) {
//This contains the distances from the start node to all other nodes
var distances = [];
//Initializing with a distance of "Infinity"
for (var i = 0; i < graph.length; i++) distances[i] = Number.MAX_VALUE;
//The distance from the start node to itself is of course 0
distances[start] = 0;
//This contains the priorities with which to visit the nodes, calculated using the heuristic.
var priorities = [];
//Initializing with a priority of "Infinity"
for (var i = 0; i < graph.length; i++) priorities[i] = Number.MAX_VALUE;
//start node has a priority equal to straight line distance to goal. It will be the first to be expanded.
priorities[start] = heuristic[start][goal];
//This contains whether a node was already visited
var visited = [];
//While there are nodes left to visit...
while (true) {
// ... find the node with the currently lowest priority...
var lowestPriority = Number.MAX_VALUE;
var lowestPriorityIndex = -1;
for (var i = 0; i < priorities.length; i++) {
//... by going through all nodes that haven't been visited yet
if (priorities[i] < lowestPriority && !visited[i]) {
lowestPriority = priorities[i];
lowestPriorityIndex = i;
}
}
if (lowestPriorityIndex === -1) {
// There was no node not yet visited --> Node not found
return -1;
} else if (lowestPriorityIndex === goal) {
// Goal node found
// console.log("Goal node found!");
return distances[lowestPriorityIndex];
}
// console.log("Visiting node " + lowestPriorityIndex + " with currently lowest priority of " + lowestPriority);
//...then, for all neighboring nodes that haven't been visited yet....
for (var i = 0; i < graph[lowestPriorityIndex].length; i++) {
if (graph[lowestPriorityIndex][i] !== 0 && !visited[i]) {
//...if the path over this edge is shorter...
if (distances[lowestPriorityIndex] + graph[lowestPriorityIndex][i] < distances[i]) {
//...save this path as new shortest path
distances[i] = distances[lowestPriorityIndex] + graph[lowestPriorityIndex][i];
//...and set the priority with which we should continue with this node
priorities[i] = distances[i] + heuristic[i][goal];
// console.log("Updating distance of node " + i + " to " + distances[i] + " and priority to " + priorities[i]);
}
}
}
// Lastly, note that we are finished with this node.
visited[lowestPriorityIndex] = true;
//console.log("Visited nodes: " + visited);
//console.log("Currently lowest distances: " + distances);
}
};
module.exports = {aStar};