diff --git a/Graphs/AStar.js b/Graphs/AStar.js new file mode 100644 index 0000000000..5d2c8b245c --- /dev/null +++ b/Graphs/AStar.js @@ -0,0 +1,79 @@ +/** + * A* Search Algorithm + * https://en.wikipedia.org/wiki/A*_search_algorithm + * + * A* finds the shortest path from a start node to a goal node + * using f(n) = g(n) + h(n). + */ + +export function aStar(graph, start, goals, heuristic) { + const goalSet = + goals instanceof Set + ? goals + : new Set(Array.isArray(goals) ? goals : [goals]) + + if (!Object.prototype.hasOwnProperty.call(graph, start)) { + throw new RangeError('Start node not found in graph') + } + + if (heuristic[start] === undefined) { + throw new RangeError('Heuristic value missing for start node') + } + + const costs = { [start]: 0 } + const parent = { [start]: null } + const priorityQueue = [{ f: heuristic[start], g: 0, node: start }] + + while (priorityQueue.length > 0) { + let minIndex = 0 + for (let i = 1; i < priorityQueue.length; i++) { + if ( + priorityQueue[i].f < priorityQueue[minIndex].f || + (priorityQueue[i].f === priorityQueue[minIndex].f && + priorityQueue[i].g < priorityQueue[minIndex].g) + ) { + minIndex = i + } + } + + const { g: currentCost, node: currentNode } = priorityQueue.splice( + minIndex, + 1 + )[0] + + if (currentCost > costs[currentNode]) { + continue + } + + if (goalSet.has(currentNode)) { + const path = [] + let node = currentNode + while (node !== null) { + path.push(node) + node = parent[node] + } + path.reverse() + return path + } + + for (const [neighbor, edgeCost] of graph[currentNode]) { + if (heuristic[neighbor] === undefined) { + throw new RangeError('Heuristic value missing for neighbor node') + } + + const newCost = currentCost + edgeCost + + if (newCost < (costs[neighbor] ?? Infinity)) { + costs[neighbor] = newCost + parent[neighbor] = currentNode + priorityQueue.push({ + f: newCost + heuristic[neighbor], + g: newCost, + node: neighbor + }) + } + } + } + + return null +} diff --git a/Graphs/test/AStar.test.js b/Graphs/test/AStar.test.js new file mode 100644 index 0000000000..f7eced4108 --- /dev/null +++ b/Graphs/test/AStar.test.js @@ -0,0 +1,67 @@ +import { aStar } from '../AStar' + +describe('AStar', () => { + const graph = { + S: [ + ['A', 3], + ['B', 1], + ['C', 5] + ], + A: [ + ['G1', 10], + ['E', 7] + ], + B: [ + ['C', 2], + ['F', 2] + ], + C: [['G3', 11]], + D: [ + ['S', 6], + ['B', 4], + ['G2', 5] + ], + E: [['G1', 2]], + F: [['D', 1]], + G1: [], + G2: [], + G3: [['F', 0]] + } + + const heuristic = { + S: 8, + A: 9, + B: 1, + C: 3, + D: 4, + E: 1, + F: 5, + G1: 0, + G2: 0, + G3: 0 + } + + it('should find the shortest path to the nearest goal', () => { + expect(aStar(graph, 'S', new Set(['G1', 'G2', 'G3']), heuristic)).toEqual([ + 'S', + 'B', + 'F', + 'D', + 'G2' + ]) + }) + + it('should return null when no path exists', () => { + const isolatedGraph = { S: [], G1: [] } + const isolatedHeuristic = { S: 1, G1: 0 } + expect( + aStar(isolatedGraph, 'S', new Set(['G1']), isolatedHeuristic) + ).toBeNull() + }) + + it('should throw RangeError for unknown start node', () => { + expect(() => aStar(graph, 'X', new Set(['G1']), heuristic)).toThrow( + RangeError + ) + }) +})