Algorithms · interactive · step by step
Graph Algorithm Visualizer
Build a weighted graph and watch BFS, DFS, Dijkstra’s shortest paths and Prim’s and Kruskal’s minimum spanning trees run step by step, with the queue, the distance table and the pseudocode line.
Order —current visited tree / shortest-path edge checking skipped
Step 0Space play · ←→ step
Reference
Graph algorithms at a glance
| Algorithm | Finds | Data structure | Time |
|---|---|---|---|
| BFS | Visit order; fewest-edges paths | Queue | O(V + E) |
| DFS | Visit order; cycles, components | Stack / recursion | O(V + E) |
| Dijkstra | Shortest paths from one node (no negative weights) | Priority queue | O((V + E) log V) |
| Prim | Minimum spanning tree | Priority queue | O(E log V) |
| Kruskal | Minimum spanning tree | Sorting + union-find | O(E log E) |
Shortest distances from A on the sample graph
| To | Distance | Route |
|---|---|---|
| A | 0 | A |
| B | 4 | A → B |
| C | 2 | A → C |
| D | 5 | A → C → D |
| E | 9 | A → C → D → E |
| F | 11 | A → C → D → F |
| G | 12 | A → C → D → E → G |
| H | 7 | A → C → D → H |
Code
Dijkstra, Kruskal, BFS and Prim in code
Python (Dijkstra)
import heapq
def dijkstra(graph, start):
"""graph: {node: [(neighbour, weight), ...]}"""
dist = {node: float("inf") for node in graph}
dist[start] = 0
queue = [(0, start)]
while queue:
d, u = heapq.heappop(queue)
if d > dist[u]:
continue # stale entry
for v, w in graph[u]:
if d + w < dist[v]:
dist[v] = d + w
heapq.heappush(queue, (dist[v], v))
return distCopy and step through it in Compilers JavaScript (Kruskal)
function kruskal(nodes, edges) {
// edges: [{ a, b, w }]. Union-find keeps track of groups.
const parent = new Map(nodes.map((n) => [n, n]));
const find = (x) => (parent.get(x) === x ? x : (parent.set(x, find(parent.get(x))), parent.get(x)));
const tree = [];
for (const e of [...edges].sort((x, y) => x.w - y.w)) {
const ra = find(e.a), rb = find(e.b);
if (ra !== rb) { parent.set(ra, rb); tree.push(e); }
}
return tree; // n − 1 edges if the graph is connected
}Copy and step through it in Compilers C++ (BFS)
#include <queue>
#include <vector>
std::vector<int> bfs(const std::vector<std::vector<int>>& adj, int start) {
std::vector<bool> seen(adj.size());
std::vector<int> order;
std::queue<int> q;
q.push(start); seen[start] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
order.push_back(u);
for (int v : adj[u]) if (!seen[v]) { seen[v] = true; q.push(v); }
}
return order;
}Copy and step through it in Compilers Java (Prim)
import java.util.*;
static int prim(List<List<int[]>> adj) { // adj.get(u): {v, w}
boolean[] inTree = new boolean[adj.size()];
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> x[1] - y[1]);
pq.add(new int[]{0, 0});
int total = 0;
while (!pq.isEmpty()) {
int[] top = pq.poll();
if (inTree[top[0]]) continue;
inTree[top[0]] = true; total += top[1];
for (int[] e : adj.get(top[0])) if (!inTree[e[0]]) pq.add(new int[]{e[0], e[1]});
}
return total;
}Copy and step through it in Compilers Questions
Graph Algorithm Visualizer FAQ
Free, private and works offline: everything runs in your browser.
How does Dijkstra’s algorithm work?
It keeps a tentative distance for every node (∞ at first, 0 for the start) and repeatedly takes the closest node it hasn’t finalised. Its distance is now final, and it “relaxes” each edge out of it: if going through it is shorter, the neighbour’s distance is updated. On the sample graph from A the shortest distances are A 0, B 4, C 2, D 5, E 9, F 11, G 12, H 7; the route to G is A → C → D → E → G.
What is the difference between BFS and DFS on a graph?
BFS visits nodes level by level using a queue: on the sample graph from A it visits A, B, C, D, H, E, F, G. DFS goes as deep as possible using a stack before backtracking: A, B, C, D, E, F, G, H. BFS finds the fewest-edges path in an unweighted graph; DFS is used for cycle detection, topological order and maze solving.
What is a minimum spanning tree?
A set of edges that connects every node with no cycles and the smallest possible total weight, such as the cheapest way to lay cables between towns. A graph with n nodes has a spanning tree of n − 1 edges. The sample graph’s minimum spanning tree has 7 edges and weighs 19.
Prim or Kruskal: which should I use?
Both find a minimum spanning tree of the same total weight. Prim grows one tree from a start node using a priority queue of edges leaving it, which suits dense graphs. Kruskal sorts all edges and adds the cheapest that doesn’t make a cycle, using union-find, which suits sparse graphs. Run both here on the same graph to compare their steps.
Does Dijkstra work with negative weights?
No. Once Dijkstra finalises a node it never revisits it, so a negative edge found later can make that answer wrong. Use the Bellman–Ford algorithm when weights can be negative. Weights here are whole numbers from 1 to 99.
How do I edit the graph?
Choose Add node and click empty space to add a node, Add edge and click two nodes to connect them (with the weight you set), or Move to drag nodes around. Delete removes a node or edge you click. The link in the address bar keeps your graph so you can share it.