shortest-path
Guide: /guide/algorithms/graph/shortest-path
Signature
ts
shortestPath({ graph, from, to, traversalType, heuristic });DFS here means the same fewest-edge path as BFS. Dijkstra requires every weight in the graph to be finite and non-negative. Bellman–Ford returns a path only after a finished run with no reachable negative cycle. A* uses heuristic and stops when the goal is extracted. heuristic is ignored for the other modes.
Params:
| Name | Type | Required | Description |
|---|---|---|---|
| graph | IGraph<T> | + | Graph to search |
| from | T | + | Start vertex |
| to | T | + | End vertex |
| traversalType | EnumGraphTraversalType | + | BFS, DFS (BFS alias), DIJKSTRA, BELLMAN_FORD, or A_STAR |
| heuristic | (vertex: T, goal: T) => number | Used by A_STAR. Defaults to zero |