AIX系列------安全加固-双身份认证
2015-07-14 14:45
711 查看
When I came to topological order again in this online course (https://class.coursera.org/algs4partII-002) , I was told that it can be calculated as "Reverse Post-Order". What I came up first when I see this term, I was curious why the tutor didn't use "Pre-Order"?
Later , I realized that my understanding of "Post-Order" and "Pre-Order" was still against the root of a tree. It should be true that "Reverse Post-Order" is same as "Pre-Order" in terms of traversing from a root of a tree. (Correct me , if I'm wrong)
However for a DAG , "Reverse Post-Order" is not same as "Pre-order" if you traverse from any vertex (same for tree). For example, for the following DAG :
If we traverse from vertex 1, then the pre-order is : 1 , 3, 4, 2 (or 1, 4, 3, 2). Obviously it's not topological order. And the post-order is : 4, 3, 1, 2 and the reverse post-order is topological order.
Another thing is against the 2 DFS algorithm that calculates the SCC (Strongly Connected Components). The first DFS need to caculate the reverse post-order (not topological order, because the graph is not necessarily a DAG) on the reverse Graph and the second DFS is to traverse in the original graph in order of the first DFS output.
So the natural thought is why can't we calculate a post-order on the original graph in the first DFS phase ?
So let's look at the following example
Obviously there are two SCCs {2,5} and {1, 3, 4}. Let's think from the second round DFS, if we traverse from 2 or 5, we will traverse all the graph and get only 1 SCC. That's not what we want. So in the first found of DFS, we should make sure any of {1,3,4} shoud before both 2 and 5 in the output. However if we output a post-order by DFS tranversing from 2, we may get 5 , 4, 3, 2, 1, which is not what we want.
O.K. now let's analyze this algorithm in general. In the second round of DFS, we want to traverse in post-order of it's meta graph (merging all the vertices in one SCC into one vertex). Using the above example, the meta graph is :
{2, 5} --> {1,3,4}
Let's call {2, 5} is a meta vertex A and {1,3,4} a meta vertex B. So we got :
A-->B
So for the first round of DFS, we can't use post-order, because post-order will output a vertex iff all it's children has already been visited, in that case we may out put any vertex in any SCC first. (i.e. vertex 5 in the above example)
How to resove it ? reverse it ! , if there is a path from meta vertex A to B , but no path from B to A, after reversing, there is a path from B to A but no path from A to B:
B<--A
So in the post-order of reverse graph, if we start from a vertex in B (before we traverse any vertex of A), then all vertices in B will appear before A and if we start from a vertex from A(before we traverse any vertex of B) then this vertex (the starting vertex) is before B.
注,由于原教程全为英文,所以为了避免某些关键term翻译不准确,索性全文用E文撰写,全文原创,谢谢:)
Later , I realized that my understanding of "Post-Order" and "Pre-Order" was still against the root of a tree. It should be true that "Reverse Post-Order" is same as "Pre-Order" in terms of traversing from a root of a tree. (Correct me , if I'm wrong)
However for a DAG , "Reverse Post-Order" is not same as "Pre-order" if you traverse from any vertex (same for tree). For example, for the following DAG :
If we traverse from vertex 1, then the pre-order is : 1 , 3, 4, 2 (or 1, 4, 3, 2). Obviously it's not topological order. And the post-order is : 4, 3, 1, 2 and the reverse post-order is topological order.
Another thing is against the 2 DFS algorithm that calculates the SCC (Strongly Connected Components). The first DFS need to caculate the reverse post-order (not topological order, because the graph is not necessarily a DAG) on the reverse Graph and the second DFS is to traverse in the original graph in order of the first DFS output.
So the natural thought is why can't we calculate a post-order on the original graph in the first DFS phase ?
So let's look at the following example
Obviously there are two SCCs {2,5} and {1, 3, 4}. Let's think from the second round DFS, if we traverse from 2 or 5, we will traverse all the graph and get only 1 SCC. That's not what we want. So in the first found of DFS, we should make sure any of {1,3,4} shoud before both 2 and 5 in the output. However if we output a post-order by DFS tranversing from 2, we may get 5 , 4, 3, 2, 1, which is not what we want.
O.K. now let's analyze this algorithm in general. In the second round of DFS, we want to traverse in post-order of it's meta graph (merging all the vertices in one SCC into one vertex). Using the above example, the meta graph is :
{2, 5} --> {1,3,4}
Let's call {2, 5} is a meta vertex A and {1,3,4} a meta vertex B. So we got :
A-->B
So for the first round of DFS, we can't use post-order, because post-order will output a vertex iff all it's children has already been visited, in that case we may out put any vertex in any SCC first. (i.e. vertex 5 in the above example)
How to resove it ? reverse it ! , if there is a path from meta vertex A to B , but no path from B to A, after reversing, there is a path from B to A but no path from A to B:
B<--A
So in the post-order of reverse graph, if we start from a vertex in B (before we traverse any vertex of A), then all vertices in B will appear before A and if we start from a vertex from A(before we traverse any vertex of B) then this vertex (the starting vertex) is before B.
注,由于原教程全为英文,所以为了避免某些关键term翻译不准确,索性全文用E文撰写,全文原创,谢谢:)
相关文章推荐
- Hadoop:为什么RAID-0不行?
- AIX系列------ODM基础
- AIX系列------ISO挂载
- [Win32SDK基本]ListView Controls(1)Report (details) View 详解
- 解决your project contains error s问题
- 全本于2015年7月14日思考强人工智能
- 利用Thumbnailator生成缩略图
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- 出现( linker command failed with exit code 1)错误总结
- 估值2.5亿的Airbnb当初被投资人拒绝的邮件吧
- 未能找到类型“Microsoft.VisualBasic.PowerPacks.ShapeContainer”和“Microsoft.VisualBasic.PowerPacks.LineShape”
- uva 10986 Sending email (dijkstra)
- UVA - 1326 Jurassic Remains
- Creating Custom Connector Sending Claims with SharePoint 2013
- Grails开发环境的快速搭建
- HDU 1239 Calling Extraterrestrial Intelligence Again
- HDU 2124 Repair the Wall
- 63 change事件监控raido选中效果
- Rails性能分析
- HDU 1021 Fibonacci Again