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.mdx

92 lines
3.9 KiB
Text
Raw Normal View History

2020-06-04 01:42:57 +00:00
---
2020-06-15 23:19:07 +00:00
id: toposort
2020-06-04 05:39:49 +00:00
title: "Topological Sort"
author: Benjamin Qi, Michael Cao
2020-06-05 00:21:03 +00:00
prerequisites:
2020-06-22 20:51:12 +00:00
- Gold - Breadth First Search
- Gold - Introduction to Dynamic Programming
2020-06-23 02:17:59 +00:00
description: "?"
2020-06-26 18:00:32 +00:00
frequency: 1
2020-06-04 01:42:57 +00:00
---
2020-06-25 02:09:35 +00:00
2020-06-25 01:38:05 +00:00
import { Problem } from "../models";
export const metadata = {
problems: {
sample: [
new Problem("CSES", "Course Schedule", "1679", "Easy", false, []),
],
2020-06-27 00:33:06 +00:00
dp: [
new Problem("CSES", "Longest Flight Route", "1680", "Easy", false, []),
],
2020-06-25 01:38:05 +00:00
general: [
new Problem("CSES", "Game Routes", "1681", "Easy", false, [], "counting paths on DAG"),
2020-06-25 19:11:38 +00:00
new Problem("Kattis", "Quantum", "https://open.kattis.com/contests/acpc17open/problems/quantumsuperposition", "Easy", false, [], "enumerating paths on DAG"),
2020-06-25 01:38:05 +00:00
new Problem("Gold", "Timeline", "1017", "Easy", false, [], "not explicitly given, but graph is a DAG"),
2020-06-26 18:00:32 +00:00
new Problem("Gold", "Milking Order", "838", "Normal", false, ["TopoSort", "Binary Search"]),
2020-06-25 01:38:05 +00:00
new Problem("CSES", "Course Schedule II", "1681", "Hard", false, [], "equivalent to [Minimal Labels](https://codeforces.com/contest/825/problem/E)"),
],
}
};
2020-06-04 01:42:57 +00:00
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.
2020-06-04 05:39:49 +00:00
2020-06-23 02:17:59 +00:00
## Topological Sort
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.sample} />
2020-06-23 02:17:59 +00:00
A [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) 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.
2020-06-03 21:08:42 +00:00
2020-06-27 03:07:31 +00:00
## Tutorial
<resources>
<resource source="CSA" title="Topological Sorting" url="topological_sorting" starred>both BFS, DFS</resource>
</resources>
### DFS
2020-06-03 20:56:04 +00:00
2020-06-27 00:33:06 +00:00
<resources>
<resource source="CPH" title="16.1, 16.2 - Topological Sorting">DFS</resource>
<resource source="cp-algo" title="Topological Sort" url="graph/topological-sort.html">DFS</resource>
</resources>
2020-06-22 14:26:06 +00:00
2020-06-27 03:07:31 +00:00
(implementation)
### BFS
2020-06-26 18:00:32 +00:00
The BFS version, known as [Kahn's Algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm), makes it obvious how to extract the lexicographically minimum topological sort.
2020-06-25 19:11:38 +00:00
2020-06-27 03:07:31 +00:00
(implementation)
## Dynamic Programming
2020-06-22 14:26:06 +00:00
2020-06-27 00:33:06 +00:00
<resources>
<resource source="PAPS" title="9.1">Best Path in a DAG</resource>
</resources>
One useful property of directed acyclic graphs is, as the name suggests, that no cycles exist. 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!
2020-06-23 01:00:35 +00:00
2020-06-27 00:33:06 +00:00
<problems-list problems={metadata.problems.dp} />
2020-06-27 00:33:06 +00:00
In this task, we must find the longest path in a DAG.
2020-06-23 02:17:59 +00:00
<spoiler title="Solution">
2020-06-26 18:00:32 +00:00
Let $dp[v]$ denote the length of the longest path ending at the node $v$. Clearly
$$
dp[v]=\max_{\text{edge } u\to v \text{ exists}}dp[u]+1,
$$
or zero if $v$ has in-degree $0$. If we process the states in topological order, it is guarangeed that $dp[u]$ will already have been computed before computing $dp[v]$.
2020-06-27 00:33:06 +00:00
(implementation?)
2020-06-23 02:17:59 +00:00
</spoiler>
2020-06-26 18:00:32 +00:00
<!-- However, not all problems clearly give you directed acyclic graphs (ex. [Plat - Cave Paintings](http://usaco.org/index.php?page=viewproblem2&cpid=996)). 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.
2020-06-26 18:00:32 +00:00
(Ben - this last paragraph doesn't seem very helpful.) -->
2020-06-23 02:17:59 +00:00
## Problems
2020-06-25 01:38:05 +00:00
<problems-list problems={metadata.problems.general} />