Directed Acyclic Graph
Definition
Edges are directed, and there are no cycles.
The English name is Directed Acyclic Graph, abbreviated as DAG.
Properties
-
Any graph that can be topologically sorted must be a directed acyclic graph;
If there is a cycle, then any two nodes on the cycle cannot satisfy the condition in any sequence.
-
A directed acyclic graph can always be topologically sorted;
(Proof by induction) Assume that all directed acyclic graphs with no more than \(k\) nodes can be topologically sorted. Then for a graph with \(k\) nodes, consider the situation after performing the first step of topological sorting.
Determination
How to determine whether a graph is a directed acyclic graph?
Check whether it can be topologically sorted.
Of course, there are other methods. You can perform a DFS on the graph and check whether there are non-tree edges pointing to ancestors (back edges) in the resulting DFS tree. If such edges exist, then there is a cycle.
Applications
DP for Longest (Shortest) Path
On general graphs, the optimal time complexity for finding single-source longest (shortest) paths is \(O(nm)\) (Bellman–Ford algorithm, suitable for graphs with negative weights) or \(O(m \log m)\) (Dijkstra's algorithm, suitable for graphs without negative weights).
However, on DAGs, we can use DP to find the longest (shortest) path, optimizing the time complexity to \(O(n+m)\). The state transition equations are \(dis_v = \min(dis_v, dis_u + w_{u,v})\) or \(dis_v = \max(dis_v, dis_u + w_{u,v})\).
After topological sorting, traverse each node in topological order and use the current node to update subsequent nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
See also: DP on DAGs.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用