Skip to content

craigdallimore/a-star

Repository files navigation

A Star

Build Coverage Downloads Size

npm install @decoy9697/a-star

The library presumes you have some kind of linked structure with a start Node and a goal Node. The Node structure can be whatever you like.

Options

You will need to provide an options object to the aStar function, with the following keys:

Key Type Description
start Node The starting node
goal Node The goal node. The algorithm will attempt to make an array of nodes from the start to the goal.
getNeighbours Node => Array<Node> A function that finds the directly connected nodes for a given node. NOTE. The returned nodes will be used as Map keys, so these ought to be references to the nodes in the graph.
eqNode (Node, Node) => boolean A function for comparing two nodes (true if they are the same)
heuristic (Node, Node) => number A function that determines the cost of travelling from one node to the other.

Return type

Key Type Description
path Array<Node> The path from the start to the goal (if possible)
reachedGoal boolean true, given the algorithm found a path from start to goal

Example

import aStar from "@decoy9697/a-star";

const result = aStar({
  start: node0,
  goal: node43,
  getNeighbours: (node) => { ... },
  eqNode: (nodeA, nodeB) => { ... },
  heuristic: (nodeA, nodeB ) => { ... }
});

if (result.reachedGoal) {
  console.log('Path': result.path);
  } else {
  console.log('Did not reach goal');
}