Skip to content

Has path algorithm ​

Check if a graph has a path between two vertices. A missing vertex throws IsNotFoundException. An unreachable goal returns false.

BELLMAN_FORD and A_STAR use BFS. Reachability does not need weights or a heuristic. A reachable negative cycle still returns true; shortest path is the call that rejects it.

Import ​

ts
import {
  DirectedGraph,
  EnumGraphTraversalType,
  hasPath,
} from "@apexds/core";

API reference ​

API: /api/algorithms/graph/has-path

Example usage ​

ts
import {
  DirectedGraph,
  EnumGraphTraversalType,
  hasPath,
} from "@apexds/core";

const graph = new DirectedGraph<string>();
graph.addVertex("A").addVertex("B").addEdge("A", "B");

const graphHasPath = hasPath({
  graph,
  from: "A",
  to: "B",
  traversalType: EnumGraphTraversalType.BFS,
});
// true

MIT Licensed