@thi.ng/adjacency
Version:
Sparse & bitwise adjacency matrices, lists and selected traversal algorithms for directed & undirected graphs
63 lines • 2.15 kB
TypeScript
import type { Maybe } from "@thi.ng/api";
import type { CostFn, IGraph } from "./api.js";
/**
* Implementation of the Floyd-Warshall algorithm for finding _all_ shortest
* paths in a directed graph, optionally with positive or negative edge weights.
* A single execution of the algorithm will find the lengths (summed weights) of
* shortest paths between all pairs of vertices.
*
* @remarks
* The default cost function is topological distance (i.e. every edge has a
* length/cost of 1).
*
* Paths & accumulated distances can be queried via {@link FloydWarshall.path}
* and {@link FloydWarshall.distance}.
*
* This algorithm is quite memory hungry and requires `|V| * |V| * 8 bytes`,
* i.e. ~8MB for a graph with 1000 nodes. If possible, use {@link BFS} to
* perform individual shortest-path queries (rather than this global approach).
*
* References:
*
* - https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
*/
export declare class FloydWarshall {
dist: Float32Array;
next: Int32Array;
numV: number;
/**
* Instantiates and pre-computes all shortest paths in given `graph`. See
* class comments for details.
*
* @param graph
* @param cost
*/
constructor(graph: IGraph<number>, cost?: CostFn);
/**
* Returns shortest distance between vertices `a` and `b`, or `undefined` if
* no connecting path exists. Throws an error if either `a` or `b` are out
* of bounds.
*
* @param a
* @param b
*/
distance(a: number, b: number): Maybe<number>;
/**
* Returns iterator of vertex IDs of path between `a` and `b` (if any).
* Throws an error if either `a` or `b` are out of bounds.
*
* @param a
* @param b
*/
path(a: number, b: number): Generator<number, void, unknown>;
protected ensureIndex(id: number): void;
protected ensurePair(a: number, b: number): void;
}
/**
* Factory function for {@link FloydWarshall}.
*
* @param graph
* @param cost
*/
export declare const floydWarshall: (graph: IGraph<number>, cost?: CostFn) => FloydWarshall;
//# sourceMappingURL=floyd-warshall.d.ts.map