Skip to content

Commit aaa13c2

Browse files
feat: add AStar search algorithm
1 parent 5c39e87 commit aaa13c2

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

‎Graphs/AStar.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* A* Search Algorithm
3+
* https://en.wikipedia.org/wiki/A*_search_algorithm
4+
*
5+
* A* finds the shortest path from a start node to a goal node
6+
* using f(n) = g(n) + h(n).
7+
*/
8+
9+
export function aStar(graph, start, goals, heuristic) {
10+
const goalSet =
11+
goals instanceof Set
12+
? goals
13+
: new Set(Array.isArray(goals) ? goals : [goals])
14+
15+
if (!Object.prototype.hasOwnProperty.call(graph, start)) {
16+
throw new RangeError('Start node not found in graph')
17+
}
18+
19+
if (heuristic[start] === undefined) {
20+
throw new RangeError('Heuristic value missing for start node')
21+
}
22+
23+
const costs = { [start]: 0 }
24+
const parent = { [start]: null }
25+
const priorityQueue = [{ f: heuristic[start], g: 0, node: start }]
26+
27+
while (priorityQueue.length > 0) {
28+
let minIndex = 0
29+
for (let i = 1; i < priorityQueue.length; i++) {
30+
if (
31+
priorityQueue[i].f < priorityQueue[minIndex].f ||
32+
(priorityQueue[i].f === priorityQueue[minIndex].f &&
33+
priorityQueue[i].g < priorityQueue[minIndex].g)
34+
) {
35+
minIndex = i
36+
}
37+
}
38+
39+
const { g: currentCost, node: currentNode } = priorityQueue.splice(
40+
minIndex,
41+
1
42+
)[0]
43+
44+
if (currentCost > costs[currentNode]) {
45+
continue
46+
}
47+
48+
if (goalSet.has(currentNode)) {
49+
const path = []
50+
let node = currentNode
51+
while (node !== null) {
52+
path.push(node)
53+
node = parent[node]
54+
}
55+
path.reverse()
56+
return path
57+
}
58+
59+
for (const [neighbor, edgeCost] of graph[currentNode]) {
60+
if (heuristic[neighbor] === undefined) {
61+
throw new RangeError('Heuristic value missing for neighbor node')
62+
}
63+
64+
const newCost = currentCost + edgeCost
65+
66+
if (newCost < (costs[neighbor] ?? Infinity)) {
67+
costs[neighbor] = newCost
68+
parent[neighbor] = currentNode
69+
priorityQueue.push({
70+
f: newCost + heuristic[neighbor],
71+
g: newCost,
72+
node: neighbor
73+
})
74+
}
75+
}
76+
}
77+
78+
return null
79+
}

‎Graphs/test/AStar.test.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { aStar } from '../AStar'
2+
3+
describe('AStar', () => {
4+
const graph = {
5+
S: [
6+
['A', 3],
7+
['B', 1],
8+
['C', 5]
9+
],
10+
A: [
11+
['G1', 10],
12+
['E', 7]
13+
],
14+
B: [
15+
['C', 2],
16+
['F', 2]
17+
],
18+
C: [['G3', 11]],
19+
D: [
20+
['S', 6],
21+
['B', 4],
22+
['G2', 5]
23+
],
24+
E: [['G1', 2]],
25+
F: [['D', 1]],
26+
G1: [],
27+
G2: [],
28+
G3: [['F', 0]]
29+
}
30+
31+
const heuristic = {
32+
S: 8,
33+
A: 9,
34+
B: 1,
35+
C: 3,
36+
D: 4,
37+
E: 1,
38+
F: 5,
39+
G1: 0,
40+
G2: 0,
41+
G3: 0
42+
}
43+
44+
it('should find the shortest path to the nearest goal', () => {
45+
expect(aStar(graph, 'S', new Set(['G1', 'G2', 'G3']), heuristic)).toEqual([
46+
'S',
47+
'B',
48+
'F',
49+
'D',
50+
'G2'
51+
])
52+
})
53+
54+
it('should return null when no path exists', () => {
55+
const isolatedGraph = { S: [], G1: [] }
56+
const isolatedHeuristic = { S: 1, G1: 0 }
57+
expect(
58+
aStar(isolatedGraph, 'S', new Set(['G1']), isolatedHeuristic)
59+
).toBeNull()
60+
})
61+
62+
it('should throw RangeError for unknown start node', () => {
63+
expect(() => aStar(graph, 'X', new Set(['G1']), heuristic)).toThrow(
64+
RangeError
65+
)
66+
})
67+
})

0 commit comments

Comments
 (0)