Topological Sorting

update Jul 18, 2017 11:11

lintcodearrow-up-right

Given an directed graph, a topological order of the graph nodes is defined as follow:

For each directed edge A -> B in graph, A must before B in the order list. The first node in the order can be any node in the graph with no nodes direct to it. Find any topological order for the given graph.

Notice

You can assume that there is at least one topological order in the graph.

Example
For graph as follow:


The topological order can be:

[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...

Challenge Can you do it in both BFS and DFS?

Basic Idea:

使用Kahn's Algorithm, 因为直接dfs会由于递归栈的关系存在很多隐患,而Kahn算法可以使用BFS。

具体地,说一下大体思路:

  1. 首先统计每个node的indegree,存在map或者dict里面。

  2. 把indegree为0的node加入res和queue,开始bfs

  3. 在bfs的过程中,每次把node的neighbors的indegree都减一(相当于去掉这个node以及其发出的路径),把indegree为0的neighbor加入res和queue,继续bfs。

  4. 类似地,dfs的方法就是在遇到neighbor的indegree减一之后变为0时,马上跟进进行dfs。

Follow up: 如何判断能否进行 topological sort 呢(即是否有环)?

  1. 如果bfs结束之后,仍有indegree > 0的node,则说明至少有一个环。

  2. 如果采用下面python dfs的类似方法也是一样,如果仍存在大于0的indegree的node,则有环。

  3. (待续)

Java Code:

Python Code:

update 2018-05-19 15:36:48

C++ DFS Solution

因为topological sort 顺序中考前的node一定最后结束dfs,所以只要进行一次dfs,将每个结束dfs的node顺序插入list尾部,最后将list反向再返回就好了。

C++ Kahn's Algorithm Solution