ToolSphere

Graph Algorithm Visualizer

Computer science

Visualize BFS, DFS, Dijkstra, Bellman-Ford, Prim, Kruskal and topological sort. Edit a weighted graph and follow queues, distances and each step.

Dijkstra result

s: 0 · t: 8 · x: 9 · y: 5 · z: 7

∞ means unreachable from the source. BFS ignores weights. The complete result is shown first; inspect any step below.

Separate vertex names with spaces. One edge per line: from to weight. Use 1–16 vertices, up to 80 edges and integer weights from −1000000 to 1000000. No parallel edges. Vertex and neighbor ties follow vertex-list order; equal-weight Kruskal edges follow input order. DFS visits the source first, then the rest of the forest. Source is unused by Kruskal and topological sort.

Step 14 of 14: Complete

10512439276stxyz

Thick edges: current predecessor links or accepted forest edges. Thick vertex: current action. Diagram placement has no effect on traversal.

Priority queue (minimum first): Empty

VertexDistance (weight)Parent
s0—
t8y
x9t
y5s
z7y

Step table

Select a step number to inspect it. State is recorded after the action. Topological values are indegrees; Prim values are connection keys; DFS values are tree depths. They are not weighted path distances.

StepActionPriority queue (minimum first)Distance (weight)
Initialize priority queues:0, t:∞, x:∞, y:∞, z:∞s:0, t:∞, x:∞, y:∞, z:∞
Extract st:∞, x:∞, y:∞, z:∞s:0, t:∞, x:∞, y:∞, z:∞
Decrease key t via st:10, x:∞, y:∞, z:∞s:0, t:10, x:∞, y:∞, z:∞
Decrease key y via sy:5, t:10, x:∞, z:∞s:0, t:10, x:∞, y:5, z:∞
Extract yt:10, x:∞, z:∞s:0, t:10, x:∞, y:5, z:∞
Decrease key t via yt:8, x:∞, z:∞s:0, t:8, x:∞, y:5, z:∞
Decrease key x via yt:8, x:14, z:∞s:0, t:8, x:14, y:5, z:∞
Decrease key z via yz:7, t:8, x:14s:0, t:8, x:14, y:5, z:7
Extract zt:8, x:14s:0, t:8, x:14, y:5, z:7
Decrease key x via zt:8, x:13s:0, t:8, x:13, y:5, z:7
Extract tx:13s:0, t:8, x:13, y:5, z:7
Decrease key x via tx:9s:0, t:8, x:9, y:5, z:7
Extract x—s:0, t:8, x:9, y:5, z:7
Complete—s:0, t:8, x:9, y:5, z:7

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use Graph Algorithm Visualizer

  1. 1Start with the loaded Dijkstra answer, or load the MST or DAG example.
  2. 2Choose an algorithm, graph type and source vertex. List vertices and enter one weighted edge per line.
  3. 3Use First step, Next and Previous, or select a numbered table row, to inspect the graph and its working state.
  4. 4Compare distances, DFS discovery/finish times, spanning-forest edges or the topological ordering. Open Tutorial for a guided walkthrough.

About Graph Algorithm Visualizer

Trace graph algorithms step by step

Use this graph algorithm visualizer to compare the methods taught in an Intro to Algorithms or data structures course. Start with the loaded weighted graph and its Dijkstra result, then inspect the working one action at a time. The diagram shows your vertices and weighted edges, while the tables preserve the state after each action. The goal is to explain why a result follows from a graph, so you can compare your own homework trace with the same inputs and tie-breaking rules.

Enter the graph from your assignment

List all vertex names, including vertices with no incident edges. Separate names with spaces or commas. Enter each edge on a separate line using three fields: the starting vertex, ending vertex and integer weight. In directed mode, an edge goes only from the first vertex to the second. In undirected mode, a single line supplies both directions. Do not repeat the reverse line in undirected mode. Self-loops are accepted, but parallel edges between the same ordered pair are not.

The editor supports up to 16 vertices and 80 edges, with integer weights between −1000000 and 1000000. Names begin with a letter and contain up to eight letters, digits or underscores. These limits keep traces manageable on a phone. Invalid entries clear the diagram and answer until corrected; they never silently disappear from a partially parsed graph. Changing the graph, source or algorithm returns the view to the complete result.

Choose the right algorithm

BFS uses a first-in, first-out queue and measures distance by the number of edges. It ignores weights, even when those weights appear on the drawing. DFS follows a branch before returning to try another neighbor. Its stack displays the active recursive calls, with the top on the right. DFS reports discovery and finish times across the whole forest, visiting the source first and then any unvisited vertices. Its depth column describes the DFS tree, not a shortest path.

Dijkstra finds shortest weighted distances when all edge weights are nonnegative. The priority queue shows unsettled vertices in increasing distance order. Bellman-Ford supports negative edge weights by repeatedly scanning and relaxing edges. It also checks for a negative cycle reachable from the source. Affected vertices and all their downstream vertices receive −∞ because no finite shortest distance exists. An unreachable negative cycle does not invalidate distances from this source.

Prim and Kruskal answer a different question: which undirected edges connect each component with minimum total weight? Their result is a minimum spanning tree for a connected graph or a minimum spanning forest otherwise. Prim grows a tree using connection keys; those keys are individual edge weights, not source-to-vertex distances. Kruskal considers edges in weight order and uses disjoint sets to reject cycles. Topological sort uses a queue of zero-indegree vertices in a directed graph. If a cycle prevents completion, the partial removal sequence is not presented as a valid ordering.

Worked weighted-path example

The loaded example uses vertices s, t, x, y and z, with source s. Running the library gives Dijkstra distances s = 0, t = 8, x = 9, y = 5 and z = 7. The shortest route to x goes through y and t: s → y contributes 5, y → t contributes 3 and t → x contributes 1, for a total of 9. The direct s → t edge costs 10, so discovering an edge does not mean its endpoint already has its final distance.

The extraction order is s, y, z, t, x. Bellman-Ford gives the same distances on this graph. Switching to BFS instead gives t = 1, y = 1, x = 2 and z = 2 because BFS counts edges rather than adding weights. Switching to DFS gives discovery order s, t, x, z, y; s is discovered at time 1 and finishes at time 10. All these results come from the same graph, but they answer different questions.

Read the trace and check your reasoning

Choose First step to inspect initialization, then Next to move forward. Previous revisits a state; Final step restores the completed state. You can also select any numbered row in the step table. The frontier and vertex values in that row are snapshots after the stated action. Infinity means a vertex is unreachable for shortest-path searches. Parents show predecessor links. Thick edges on the diagram show those links or the accepted spanning-forest edges, depending on the algorithm.

Load the MST example to compare Prim and Kruskal: both produce total weight 37 with eight accepted edges, although their selected trees can differ because equal-weight alternatives exist. Vertex and neighbor ties follow the vertex list; Kruskal breaks equal-weight edge ties by input order. The priority queue is displayed as a sorted conceptual queue, not a binary-heap animation. Use the Algorithms Lab for related exercises and the hash table simulator to study another core data structure. Your course may choose a different neighbor order, so compare distances and invariants before treating a different traversal sequence as an error.

Guides and tutorials

Frequently asked questions

Which graph algorithms are included?

BFS, DFS with discovery and finish times, Dijkstra, Bellman-Ford, Prim, Kruskal and Kahn’s topological sort. Each includes a step table and graph diagram.

How do I enter a weighted graph?

List the vertices with spaces, then enter one edge per line as from to weight, such as s t 10. Choose directed or undirected. Include isolated vertices in the vertex list.

What is the difference between BFS and Dijkstra?

BFS minimizes the number of edges and ignores weights. Dijkstra minimizes the sum of nonnegative edge weights. They can choose different routes on the same graph.

Can Dijkstra use negative weights?

No. This tool rejects negative weights for Dijkstra. Use Bellman-Ford, which supports negative edges and detects negative cycles reachable from the source.

What do infinity and negative infinity mean?

For shortest paths, ∞ means unreachable from the source. Bellman-Ford uses −∞ for vertices on or reachable from a reachable negative cycle, where no finite minimum path weight exists.

Does DFS show discovery and finish times?

Yes. DFS increments its clock on discovery and finish, showing both times and the active call stack. It visits the source first, then completes a forest over remaining vertices.

Do Prim and Kruskal work on disconnected graphs?

Yes, in undirected mode they return a minimum spanning forest and its component count. A connected graph produces a minimum spanning tree. Negative edge weights are allowed.

Why can my textbook show a different traversal or spanning tree?

Equal choices can have several correct outcomes. This tool follows vertex-list order for vertex and neighbor ties, and edge-input order for equal-weight Kruskal edges. Distances and minimum total weight should still agree.

What happens when topological sort finds a cycle?

The tool reports that no topological ordering exists. Its trace still shows the zero-indegree queue and any removable prefix. Topological sort requires a directed graph.

What are the input limits and privacy rules?

The visualizer accepts up to 16 vertices, 80 edges and integer weights from −1000000 to 1000000, without parallel edges. Calculations run in your browser; the tool does not upload graph inputs.

Have a suggestion for Graph Algorithm Visualizer?Share your feedback ↗