This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/5_Gold/TopoSort.md

3.1 KiB

id title author prerequisites
toposort Topological Sort Benjamin Qi, Michael Cao
Gold - Breadth First Search

To review, a directed graph consists of edges that can only be traversed in one direction. Additionally, a acyclic graph defines a graph which does not contain cycles, meaning you are unable to traverse across one or more edges and return to the node you started on. Putting these definitions together, a directed acyclic graph, sometimes abbreviated as DAG, is a graph which has edges which can only be traversed in one direction and does not contain cycles.

A topological sort of a directed acyclic graph is a linear ordering of its vertices such that for every directed edge u\to v from vertex u to vertex v, u comes before v in the ordering.

Tutorial

Dynamic Programming

One useful property of directed acyclic graphs is, as the name suggests, that there exists no cycles. If we consider each node in the graph as a state, we can perform dynamic programming on the graph if we process the states in an order that guarantees for every edge, u\to v that u is processed before v. Fortunately, this is the exact definition of a topological sort!

Let's consider a classical problem (see Longest Flight Route) where we must find the longest path in a Directed Acyclic Graph. Let dp[curr] = longest path ending at the node curr. Then, if we process states in topological order, the transition is relatively straightforward: dp[curr] = max of all dp[prev] where prev represents a node with an edge going into the current node (word better?). To reiterate, since the states a processed in topological order, we can guarantee that all possible dp[prev] are computed before we compute dp[curr].

However, not all problems clearly give you directed acyclic graphs (see Cave Paintings). An important step in many problems is to reduce the statement into a directed acyclic graph. See the editorial of the linked problem for more information.

Example Problems

Problems