Graph Algorithm Visualizer

BFS, DFS, Dijkstra, Bellman–Ford, A*, MST, topological sort and SCCs on your edge list.

Formats: "A B 4", "A -> B 4", "A,B,4". A line with one name adds an isolated node. # starts a comment. Max 200 nodes, 2,000 edges.

More options

Default: the first node. Used by BFS, DFS, Dijkstra, Bellman–Ford, A* and Prim.

Optional for searches; required for A*.

A* uses the straight-line distance as heuristic; it is admissible only if no edge is shorter than the distance between its end points.

Try an example: · · · · ·

More about this tool

Part of Proofline Math & science

How it works

Paste an edge list (weighted or not, directed or not) and run a classic graph algorithm: BFS, DFS, Dijkstra, Bellman–Ford (with negative-cycle detection), A* with node coordinates, Prim or Kruskal minimum spanning tree, Kahn topological sort or Tarjan strongly connected components. Shows every step, the distances and the highlighted path or tree.

  • Neighbours are visited in the order nodes first appear in your edge list, so every run is deterministic. Undirected edges are usable in both directions.
  • BFS finds fewest-edge paths; DFS records discovery and finish times. Dijkstra settles nodes in order of distance with a binary heap and refuses negative weights; Bellman–Ford relaxes all edges up to n − 1 times and then checks for a negative cycle.
  • A* is Dijkstra ordered by g + h with h = straight-line distance between coordinates. It returns optimal paths when h never overestimates (admissible) and is consistent; the tool warns when an edge is shorter than its straight-line length.
  • Prim grows a tree from the start node (a forest if the graph is disconnected); Kruskal adds the cheapest edges that do not close a cycle (union–find). Kahn's algorithm outputs nodes without remaining incoming edges and detects cycles; Tarjan's algorithm finds strongly connected components in one DFS.

Sources

  • Cormen, Leiserson, Rivest, Stein: Introduction to Algorithms, 4th ed. (2022), chapters 20–23 (MIT Press)
  • Hart, Nilsson, Raphael (1968): A Formal Basis for the Heuristic Determination of Minimum Cost Paths (IEEE Trans. SSC 4(2))

Use this tool in an app

These free programs for your browser have this tool built in, next to everything else the task needs.

  • Proofline Math notebook

    Solve, graph, test and visualise in one notebook — with data, findings and a report.

    Open Proofline

Related tools

How to use the Graph Algorithm Visualizer

Run BFS, DFS, Dijkstra, Bellman–Ford, A*, Prim, Kruskal, topological sort or Tarjan's SCC on your own edge list and follow every step.

What it is for

Which path does Dijkstra actually pick? Why does BFS give a different route? Is there a cycle in my build dependencies? This tool runs the classic algorithms on an edge list you paste, one edge per line (A B 4, A -> B 4 or A,B,4), and shows the result, the distances and a step-by-step log:

  • BFS (fewest edges) and DFS (discovery/finish times),
  • Dijkstra (non-negative weights) and Bellman–Ford (negative weights, negative-cycle detection),
  • A\* with node coordinates as a straight-line heuristic,
  • Prim and Kruskal minimum spanning trees,
  • Kahn's topological sort and Tarjan's strongly connected components.

It's meant for learning, homework checks, interview prep and small real graphs (up to 200 nodes and 2,000 edges), not for routing on road networks.

Worked example

The default graph has six nodes and seven weighted, undirected edges: A B 4, A C 2, B C 5, B D 10, C E 3, E D 4, D F 11.

Dijkstra from A to F: "Dijkstra shortest paths: A → C → E → D → F (cost 20)." The step log shows the key moment. B is settled at distance 4 and offers D at 14, but E (settled at 5) offers D at 9, so D's distance drops to 9 before D is settled.

BFS from A to F on the same graph: "A → B → D → F (edges: 3)". That's fewer edges, but it would cost 4 + 10 + 11 = 25. BFS ignores weights.

Kruskal: 5 edges with total weight 24: A–C (2), C–E (3), A–B (4), E–D (4), D–F (11). B–C (5) and B–D (10) were skipped because they would close a cycle.

Two edge cases are worth trying. Bellman–Ford on S A 1, A B -2, B A 1, B C 3 reports "found a negative cycle (B → A → B): shortest paths from S are undefined". Dijkstra on a graph with a negative edge refuses to run and points you to Bellman–Ford.

How to read the result

  • Summary: the path and its cost, the MST weight, the topological order or the components.
  • Distances: each node's distance from the start and its predecessor. Follow the predecessors backwards to rebuild any shortest path, not only the one to your target.
  • Steps: the algorithm's decisions in order (settle, relax, take, skip, output). This is the part to compare with your own hand trace.
  • Warnings: for example a heuristic that may overestimate in A*, or distances that aren't final because of a negative cycle.

Tips and common mistakes

  • Tick Directed for dependencies, one-way streets and state machines. Undirected edges can be used both ways, which changes everything for topological sort and SCCs.
  • Ties are broken by the order in which nodes first appear in your list. Reorder the lines to see alternative equal-cost answers.
  • For A, coordinates must be consistent with the weights. If an edge is shorter than the straight-line distance between its ends, the tool warns that A may miss the optimum. In the built-in grid example, A* expanded 4 nodes where Dijkstra needed 5.
  • An MST on a disconnected graph is a forest. The tool reports how many components it has.
  • Topological sort fails on a cycle, which is exactly how you find circular dependencies. Run SCC to see which nodes form the cycle.

FAQ

When should I use Bellman–Ford instead of Dijkstra?

Whenever any edge weight is negative. Dijkstra assumes a settled distance can never improve, and negative edges break that. Bellman–Ford is slower but correct, and it detects negative cycles.

Why does BFS give a different path from Dijkstra?

BFS minimises the number of edges and ignores weights. On weighted graphs, use Dijkstra or A*.

What is the difference between Prim and Kruskal?

Both find a minimum spanning tree with the same total weight. Prim grows one tree from a start node, while Kruskal adds the globally cheapest safe edges. Only the order of the steps differs.

Can I add a node without edges?

Yes. A line with a single name adds an isolated node, and # starts a comment.

Guide last updated:

For scripts and AI agents

  • JSON API: POST https://graphalgorithmvisualizer.usefulweb.site/api/v1/calculate (same calculation as this page)
  • Interface description: OpenAPI 3.1 · AI guide: llms.txt
  • MCP server (Streamable HTTP): https://graphalgorithmvisualizer.usefulweb.site/mcp · tool name graph_algorithm_visualizer
Example request
curl -s -X POST https://graphalgorithmvisualizer.usefulweb.site/api/v1/calculate \
  -H 'content-type: application/json' \
  -d '{"algorithm":"dijkstra","source":"A","target":"F"}'
Feedback · ideas & bug reports welcome