Finding the longest path in a general graph is NP-hard. But in a DAG, you can do it in linear time using topological sort. Why? Because topological order guarantees that when you process node , you have already processed all nodes that can reach .
This is dynamic programming. The state is dist[v] (longest path from source to ). The transition is dist[v] = max(dist[u] + 1) for each edge . Topological order ensures all predecessors are computed before the current node.
The same pattern works for many problems: shortest path, counting paths, maximum/minimum cost paths. The core insight is that DAGs have no cycles, so there's a natural order to process nodes. And that order is the topological sort.