Dijkstra's Algorithm
Shortest paths on a weighted graph — always expand the closest unfinished node.
Dijkstra's algorithm finds the shortest distance from one source to every other node in a graph with non-negative weights. It repeatedly picks the unfinished node with the smallest known distance, marks it done, and relaxes each of its edges — updating a neighbour's distance if going through this node is cheaper.
The greedy choice is safe precisely because weights are non-negative: once a node has the smallest tentative distance, no longer detour can improve it. With negative edges this breaks, and you need Bellman-Ford instead.
The `dist` array is the algorithm's memory, and watching it fall from infinity toward its final value is the whole idea. AlgoLens draws the graph beside `dist` so each relaxation is visible as it happens.
The code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 6;
int edges[7][3] = {{0,1,4},{0,2,1},{2,1,2},{1,3,5},{2,3,8},{3,4,3},{4,5,1}};
vector<vector<int>> adj(n);
vector<vector<int>> cost(n);
for (int i = 0; i < 7; i++) {
int u = edges[i][0], v = edges[i][1], w = edges[i][2];
adj[u].push_back(v); cost[u].push_back(w);
adj[v].push_back(u); cost[v].push_back(w);
}
vector<int> dist(n, 1000000);
vector<int> done(n, 0);
dist[0] = 0;
for (int step = 0; step < n; step++) {
int u = -1;
for (int i = 0; i < n; i++) {
if (!done[i] && (u == -1 || dist[i] < dist[u])) u = i;
}
done[u] = 1;
for (int k = 0; k < (int)adj[u].size(); k++) {
int v = adj[u][k], w = cost[u][k];
if (dist[u] + w < dist[v]) dist[v] = dist[u] + w;
}
}
for (int i = 0; i < n; i++) cout << dist[i] << ' ';
cout << '\n';
return 0;
}
Now run your own Dijkstra's Algorithm
AlgoLens traces your code — arrays, graphs, trees and recursion — from a real execution, so you see how it actually behaves, not a canned animation.
Paste your codeKeep going
Breadth-First Search (BFS)
Explore a graph level by level using a queue — shortest paths on unweighted graphs.
Depth-First Search (DFS)
Go as deep as possible before backtracking — the recursion tree made visible.
Topological Sort
Order tasks so every dependency comes first — Kahn's algorithm with in-degrees.
Bubble Sort
The simplest sort — watch the largest value bubble to the end each pass.