UNPKG

@graphty/algorithms

Version:

Graph algorithms library for browser environments implemented in TypeScript

52 lines 1.6 kB
import type { Graph } from "../../core/graph.js"; import type { NodeId, ShortestPathResult } from "../../types/index.js"; /** * Bellman-Ford algorithm implementation for single-source shortest paths * * Finds shortest paths from a source node to all other nodes in a weighted graph. * Unlike Dijkstra's algorithm, it can handle negative edge weights and detect * negative cycles. */ /** * Bellman-Ford algorithm options */ export interface BellmanFordOptions { /** * Target node for early termination (optional) */ target?: NodeId; } /** * Result of Bellman-Ford algorithm */ export interface BellmanFordResult { /** * Distance from source to each reachable node */ distances: Map<NodeId, number>; /** * Predecessor of each node in shortest path tree */ predecessors: Map<NodeId, NodeId | null>; /** * Whether a negative cycle was detected */ hasNegativeCycle: boolean; /** * Nodes involved in negative cycle (if any) */ negativeCycleNodes: NodeId[]; } /** * Find shortest paths from source using Bellman-Ford algorithm */ export declare function bellmanFord(graph: Graph, source: NodeId, options?: BellmanFordOptions): BellmanFordResult; /** * Find shortest path between two specific nodes using Bellman-Ford */ export declare function bellmanFordPath(graph: Graph, source: NodeId, target: NodeId): ShortestPathResult | null; /** * Check if graph has negative cycles using Bellman-Ford */ export declare function hasNegativeCycle(graph: Graph): boolean; //# sourceMappingURL=bellman-ford.d.ts.map