Topological Sort
Order tasks so every dependency comes first — Kahn's algorithm with in-degrees.
A topological sort linearises a directed acyclic graph so that every edge points forward. Kahn's algorithm counts each node's in-degree, starts a queue with the zero-in-degree nodes, and after removing a node decrements its neighbours — pushing any that reach zero.
This is course prerequisites, build systems and task scheduling. It also detects cycles for free: if the output is shorter than the node count, the leftover nodes sit in a cycle and no valid order exists.
The `indeg` array is the state that drives everything. AlgoLens shows it next to the graph, so you can watch counts drop and see exactly which node becomes available next.
The code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 6;
int edges[6][2] = {{5,2},{5,0},{4,0},{4,1},{2,3},{3,1}};
vector<vector<int>> adj(n);
vector<int> indeg(n, 0);
for (int i = 0; i < 6; i++) {
int u = edges[i][0], v = edges[i][1];
adj[u].push_back(v);
indeg[v]++;
}
queue<int> q;
for (int i = 0; i < n; i++) {
if (indeg[i] == 0) q.push(i);
}
vector<int> order;
while (!q.empty()) {
int u = q.front();
q.pop();
order.push_back(u);
for (int k = 0; k < (int)adj[u].size(); k++) {
int v = adj[u][k];
indeg[v]--;
if (indeg[v] == 0) q.push(v);
}
}
for (int i = 0; i < (int)order.size(); i++) cout << order[i] << ' ';
cout << '\n';
return 0;
}
Now run your own Topological Sort
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.
Dijkstra's Algorithm
Shortest paths on a weighted graph — always expand the closest unfinished node.
Bubble Sort
The simplest sort — watch the largest value bubble to the end each pass.