export type PathEntry = { /** * The sum of the weights from `source` to `v` * along the shortest path or `Number.POSITIVE_INFINITY` if there is no path * from `source`. */ distance: number; /** * Can be used to walk the individual * elements of the path from `source` to `v` in reverse order. */ predecessor?: NodeID; }; /** * @typedef {Object} PathEntry * @property {number} distance The sum of the weights from `source` to `v` * along the shortest path or `Number.POSITIVE_INFINITY` if there is no path * from `source`. * @property {NodeID} [predecessor] Can be used to walk the individual * elements of the path from `source` to `v` in reverse order. */ /** * This function is an implementation of [Dijkstra's algorithm][] which finds * the shortest path from `source` to all other nodes in `g`. This * function returns * * [Dijkstra's algorithm]: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm * * @example * * ![](https://github.com/dagrejs/graphlib/wiki/images/dijkstra-source.png) * * * ```js * function weight(e) { return g.edge(e); } * * graphlib.alg.dijkstra(g, "A", weight); * // => { A: { distance: 0 }, * // B: { distance: 6, predecessor: 'C' }, * // C: { distance: 4, predecessor: 'A' }, * // D: { distance: 2, predecessor: 'A' }, * // E: { distance: 8, predecessor: 'F' }, * // F: { distance: 4, predecessor: 'D' } } * ``` * * @remarks It takes `O((|E| + |V|) * log |V|)` time. * * @param {Graph} g - Input graph. * @param {NodeID | number} source - The source node id. Converted to a string. * @param {(e: EdgeObj) => number} [weightFn] - Optional function that returns * the weight for edge `e`. If no `weightFn` is supplied then each edge is * assumed to have a weight of 1. * @param {(v: NodeID) => EdgeObj[]} [edgeFn] - Optional function that returns * the ids of all edges incident to the node `v` for the purposes of shortest * path traversal. * By default this function uses the {@link Graph.outEdges} function on the * supplied graph. * @returns {Record} a map of `v -> { distance, predecessor }`. * @throws {Error} If any of the traversed edges has a negative edge weight. */ export function dijkstra(g: Graph, source: NodeID | number, weightFn?: (e: EdgeObj) => number, edgeFn?: (v: NodeID) => EdgeObj[]): Record; import type { NodeID } from '../graph.js'; import type { Graph } from '../graph.js'; import type { EdgeObj } from '../graph.js';