UNPKG

soonspacejs

Version:
55 lines (54 loc) 1.59 kB
import { BaseObjectInfo } from '../../Interface'; import { MinHeap } from '../../Shared'; /** * Javascript implementation of Dijkstra's algorithm * Based on: http://en.wikipedia.org/wiki/Dijkstra's_algorithm * Author: James Jackson (www.jamesdavidjackson.com) * Source: https://github.com/nojacko/dijkstras-js * * Useage: * const d = new Dijkstras(); * d.setGraph( * [ * ['A', [['B', 20], ['C', 20]] ], * ['B', [['A', 30], ['C', 100]] ], * ['C', [['D', 10], ['A', 20]] ], * ['D', [['C', 10], ['B', 20]] ] * ] * ); * const path = d.getPath('A', 'D'); * */ export type BaseId = BaseObjectInfo['id']; type Graph = Map<BaseId, Map<BaseId, number>>; type InputGraph = [BaseId, [BaseId, number][]][]; declare class Dijkstras { graph: Graph; queue: MinHeap | null; previous: Map<BaseId, BaseId | null>; constructor(); /** * Creates a graph from array. * Each element in the array should be in the format: * [NODE NAME, [[NODE NAME, COST], ...] ] * * For example: [ * ['A', [['B', 20], ['C', 20]] ], * ['B', [['A', 30], ['C', 100]] ], * ['C', [['D', 10], ['A', 20]] ], * ['D', [['C', 10], ['B', 20]] ] * ] * * @param graphy Array of nodes and vertices. **/ setGraph(graph: InputGraph): void; /** * Find shortest path * * @param source The starting node. * @param target The target node. * @return array Path to target, or empty array if unable to find path. */ getPath(source: BaseId, target: BaseId): BaseId[]; } export { Graph, InputGraph, Dijkstras, };