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文撰写,全文原创,谢谢:)
相关推荐
本文将基于“A-Deeper-Understanding-of-Spark-Internals-Aaron-Davidson”的内容,深入探讨Spark的核心组件及其内部工作原理。 #### 二、Spark内部机制概览 ##### 1. 目标:理解Spark如何运行,关注性能 - **...
SUMS85 Understanding Markov Chains -- Examples and Applications, 2nd Edition, Nicolas Privault (2018).zip
Toward an understanding of higher-order thinking among minority students Psychology in [he Schools Volume 29. July 1992 TOWARD AN UNDERSTANDING OF HIGHER-ORDER THINKING AMONG MINORITY STUDENTS ...
Understanding .NET - A Tutorial and Analysis Understanding .NET - A Tutorial and Analysis Understanding .NET - A Tutorial and Analysis
As you read this essay, you understand each word based on your understanding of previous words. You don’t throw everything away and start thinking from scratch again. Your thoughts have persistence....
由于提供的文件信息中包含了重复的标签和无关内容(WeChatOfficialAccounts:BigData321),我将忽略这些部分,并基于标题和描述中所提及的“Understanding-Memory-Management-In-Spark-For-Fun-And-Profit.pdf”和...
This book explains how to see one's own network through the eyes of an attacker, to understand their techniques and effectively protect against them. Through Python code samples the reader learns to ...
understanding-machine-learning-theory-algorithms
**标题**:“Deeper understanding of non-linear geodetic data inversion using a quantitative sensitivity analysis”(通过定量敏感性分析深入理解非线性大地测量数据反演) 此标题指出了一种方法论上的进步,...
ELF readelf objdump
Understanding Complex Datasets: Data Mining with Matrix Decompositions discusses the most common matrix decompositions and shows how they can be used to analyze large datasets in a broad range of ...
飞机姿态算法。从这篇文章是我尝试的飞行器姿态检测采用四元数方法,然后利用卡尔曼滤波算法,并尝试卡尔曼滤波器耦合的多个状态变量可以是一个复杂的过程,线性系统状态估计进行了简单的解耦,将最优估计的态度和最优...
### U-Boot移植基础知识 #### 一、U-Boot简介 U-Boot(Universal Boot Loader)是一种开源的、通用的Boot Loader,适用于多种处理器架构,包括但不限于ARM、PowerPC、x86等。U-Boot的主要功能是在启动操作系统之前...
understanding-security-osi-model_377
Qualcomm Technologies Inc. - Understanding High-Frequency and Fast-Transient Switched-Mode Power Supplies-Qualcomm Technologies.pdf
对Linux网络协议栈结构的理解.Linux的协议栈基于分层的设计思想,总共分为四层,从下往上依次是 :物理层,链路层,网络层,应用层。
It has no concept of classes, and you don't even need to define any objects in order to write code. But don't be fooled—JavaScript is an incredibly powerful and expressive object-oriented language ...
This book is for any professional who wants to have a basic understanding of the latest developments in and applications of FFT. It provides a good reference for any engineer planning to work in this...