解决方案是计算所有顶点的出度,以查看是否有一个单一的汇顶点(出度为 0 的顶点),这是证明。
陈述:当且仅当存在一个出度为 0 的单个顶点时,DAG 有一个顶点可以从所有其他顶点到达。
这个陈述可以分两步来证明。
必要性证明:如果一个 DAG 包含一个可以从其他所有顶点到达的顶点 u,则它必须是唯一的汇顶点(出度为 0 的顶点)
- 你有 0 出度,否则循环形式,与 DAG 相矛盾。
- 不能有另一个出度为 0 的汇顶点 v,否则 u 不能从 v 到达
证明。
Proof of sufficiency: If a DAG contains one single sink vertex u, then u should be reachable from every other vertex.
Assume there exist vertices in the DAG that can't reach u. Let's divide all vertices in the DAG into the set of vertices that can reach u ($V_y$), and those cannot reach u ($V_n$), then there do not exist any edges from vertices in $V_n$ pointing to vertices in $V_y$.
Lemma: Any DAG must contain at least one sink vertices.
This is easy to prove by contradiction. Assume every vertex in the DAG has non-zero out-degrees, so starting from one vertex, we can keep traversing along the out-going edges of each vertex. Since DAG contains a finite number of vertices, we will return to a previously visited vertex eventually, i.e. cycle is detected, contradicting DAG definition.
Consider the graph formed by vertices in $V_n$ and edges between them, it must also be a DAG, otherwise, the original graph cannot be a DAG. Suppose v is a sink vertex in this sub-DAG, so v doesn't have any outgoing edge pointing to vertices in $V_n$.
As mentioned above, there can't be any edges from v pointing to any vertex in $V_y$ either, otherwise, v can reach u.
So the overall out-degrees of v in the original DAG is also 0, contradicting the assumption that the DAG contains a single sink vertex. Proved.