Graph Shortest Path Lab
Build a graph and replay Dijkstra, A*, or Bellman-Ford step by step in your browser.
Examples
Deterministic shortest path
runShortestPath(graph, "A", "C", "dijkstra", "zero")path: ["A", "B", "C"], distance: 4, last step kind: "complete"A → B (2) → C (2) beats A → D (10) → C (1); all three algorithms agree on this fixture.
Negative edge handling
runShortestPath(negative, "A", "C", "dijkstra")
runShortestPath(negative, "A", "C", "bellman-ford")dijkstra error: "Dijkstra cannot run with negative edge weights."
bellman-ford path: ["A", "D", "C"], distance: -2Dijkstra refuses negative weights while Bellman-Ford relaxes through them (and would flag a reachable negative cycle).
About this tool
Graph Shortest Path Lab runs Dijkstra, A*, and Bellman-Ford through runShortestPath(graph, start, goal, algorithm = "dijkstra", heuristic = "zero" | "euclidean"), aliased as shortestPath. Graphs carry nodes with { id, x, y } coordinates and weighted { from, to, weight } edges; undirected graphs mirror each edge, and adjacency lists are sorted by target id then weight so traversal order — and the visit/relax step log — is fully deterministic across runs.
Algorithm rules are strict: Dijkstra returns an error ("Dijkstra cannot run with negative edge weights.") when any edge weight is negative — check first with hasNegativeEdges — while Bellman-Ford relaxes all directed edges for |V|−1 passes, records each improvement as a relax step, and reports reachable negative cycles with negativeCycle: true instead of a path. A* reuses the Dijkstra loop with a score of distance plus either zero or the Euclidean Math.hypot distance from coordinates; on the bundled fixture both heuristics yield the same A → B → C path of length 4.
Results always include { algorithm, path, distance, visitedOrder, distances, steps, error?, negativeCycle? }. Unreachable goals produce an empty path with distance Infinity (distances stay Infinity for unreached nodes), unknown start/goal ids produce a "Start and goal must be existing nodes." error, and every successful run ends with a complete step whose message distinguishes "Shortest path found" from "Goal is unreachable". createGraph clones node/edge arrays into a fresh graph object for convenient fixture building.
How to use this tool
Build the graph
Create nodes with x/y coordinates and weighted edges via createGraph, setting directed true for one-way edges. Keep weights non-negative for Dijkstra and A*.
Run an algorithm
Call runShortestPath with start, goal, and algorithm. Read path and distance, or error when Dijkstra meets a negative edge or an id is unknown.
Compare heuristics and step logs
Run A* with zero versus euclidean heuristics to compare paths, and replay the visit/relax/complete steps to animate or audit the search. Bellman-Ford logs are byte-identical across runs.
Use cases
Teaching shortest-path algorithms
Contrast Dijkstra, A* heuristics, and Bellman-Ford relaxation on one graph with a replayable deterministic step log.
Validating routing weights
Confirm the cheapest path and distance before encoding weights into navigation, network, or game-movement logic.
Auditing negative-weight data
Use hasNegativeEdges plus the Dijkstra error and Bellman-Ford cycle flag to catch bad weights or arbitrage loops.
Common mistakes
Mistake:Running Dijkstra on negative weights and expecting a path.
Fix:Dijkstra returns an empty path with a negative-weights error. Use Bellman-Ford for negative edges and check negativeCycle before trusting the distance.
Mistake:Treating an empty path as zero distance.
Fix:Unreachable goals report distance Infinity with per-node distances left at Infinity. Test path.length or Number.isFinite(distance) before rendering a route.
Mistake:Assuming euclidean A* is always optimal.
Fix:Optimality needs an admissible heuristic (never overestimating). With arbitrary weights like 2 against 100px gaps, verify the euclidean path against the zero-heuristic run.
Frequently asked questions
What does this tool do?
It runs Dijkstra, A* (zero or euclidean heuristic), and Bellman-Ford via runShortestPath on weighted directed or undirected graphs, returning the path, distance, visit order, per-node distances, and a deterministic visit/relax/complete step log.
Why does Dijkstra fail on my graph while Bellman-Ford works?
Dijkstra returns an empty path with a negative-edge-weights error whenever any edge is negative (check with hasNegativeEdges). Bellman-Ford relaxes through negatives — e.g. A → D → C at distance −2 — and additionally flags reachable negative cycles with negativeCycle: true.
Is my graph uploaded or stored?
No. Adjacency building, relaxation, and heuristic scoring run entirely in your browser — nodes and edges never leave your device.
How do unreachable nodes and A* heuristics behave?
Unreachable goals yield an empty path with distance Infinity. On the bundled fixture A* with euclidean and zero heuristics agree (A → B → C, distance 4), and re-running Bellman-Ford produces a byte-identical step log.
Related guides
References & standards
Related tools
3D Affine Transform Visualizer
Compose 4x4 matrices and inspect 3D affine transformations with a live cube wireframe. Multiply, invert, and apply translate, rotate, scale, and skew transforms in your browser.
ETA Calculator
Estimate finish and arrival times from rate or speed, with any start moment.
Exam & Section Timer
Plan multi-section exams with total duration and seconds-per-question pacing.
Flashcards & Spaced Repetition
Review a flashcard with a transparent SuperMemo SM-2 scheduling algorithm.
GPA & Grade Calculator
Calculate credit-weighted GPA and simulate the semester average needed for a target.
Interactive Periodic Table
Explore all 118 elements, filter them and preview phase changes by temperature.