-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-star.js
121 lines (100 loc) · 3.16 KB
/
a-star.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
class PriorityQueue {
constructor() {
this.queue = [];
}
enqueue(element, priority) {
this.queue.push({ element, priority });
this.sort();
}
dequeue() {
if (this.isEmpty()) return null;
return this.queue.shift().element;
}
sort() {
this.queue.sort((a, b) => a.priority - b.priority);
}
isEmpty() {
return this.queue.length === 0;
}
}
class Graph {
constructor() {
this.vertices = {};
}
addVertex(vertex) {
if (!this.vertices[vertex]) {
this.vertices[vertex] = {};
}
}
addEdge(source, destination, weight) {
this.vertices[source][destination] = weight;
// this.vertices[destination][source] = weight; // For undirected graph
}
aStar(startVertex, targetVertex, heuristic) {
let closed = {};
let cameFrom = {};
let distances = {};
let open = new PriorityQueue();
for (let vertex in this.vertices) {
distances[vertex] = Infinity;
}
distances[startVertex] = 0;
open.enqueue(startVertex, 0);
while (!open.isEmpty()) {
let currentVertex = open.dequeue();
if (currentVertex === targetVertex) {
return this.reconstructPath(cameFrom, targetVertex);
}
closed[currentVertex] = true;
for (let neighbor in this.vertices[currentVertex]) {
if (closed[neighbor]) continue;
let tentativeDistance = distances[currentVertex] + this.vertices[currentVertex][neighbor];
if (tentativeDistance < distances[neighbor]) {
cameFrom[neighbor] = currentVertex;
distances[neighbor] = tentativeDistance;
let priority = tentativeDistance + heuristic(neighbor, targetVertex);
open.enqueue(neighbor, priority);
}
}
}
return null;
}
reconstructPath(cameFrom, currentVertex) {
let path = [currentVertex];
while (cameFrom[currentVertex] !== undefined) {
currentVertex = cameFrom[currentVertex];
path.unshift(currentVertex);
}
return path;
}
}
const graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addEdge("A", "B", 4);
graph.addEdge("A", "C", 2);
graph.addEdge("B", "C", 5);
graph.addEdge("B", "D", 10);
graph.addEdge("C", "D", 3);
graph.addEdge("C", "E", 7);
graph.addEdge("D", "E", 8);
// Define heuristic function (Euclidean distance)
function heuristic(current, target) {
const coordinates = {
A: [0, 0],
B: [1, 2],
C: [3, 1],
D: [5, 2],
E: [4, 0]
};
const dx = coordinates[target][0] - coordinates[current][0];
const dy = coordinates[target][1] - coordinates[current][1];
return Math.sqrt(dx * dx + dy * dy);
}
const startVertex = "A";
const targetVertex = "E";
const shortestPath = graph.aStar(startVertex, targetVertex, heuristic);
console.log("Shortest path from", startVertex, "to", targetVertex, ":", shortestPath);