图——拓扑排序(Graph

来源:互联网 发布:windows 批处理 多进程 编辑:程序博客网 时间:2024/06/07 06:34

图——拓扑排序(Graph - Topological Sort)


简介(Introduction)
We know some problems in real life can be modelled as graph like task planning. Assume a directed edge from a to b means that task a must be done before b can be started. Assume those tasks are assigned to a single staff. Can you give a task order ?

In graph, we’re actually trying to linearise a DAG(directed cycle graph). That’s in the order sequence v1, v2, v2, .. , vk. For each edge(vi, vj) in E we have i < j.

示例(Example)
这里写图片描述

算法一(Algorithm1)

1. Perform DFS and note the order that are posed off the stack.2. List the nodes in the reverse of that order.

算法二(Algorithm2)

Repeatedly select a random source in the graph (that is, a node with no incoming edges), list it, and remove it from the graph.

写在最后的话(PS)
Topological sort can help us decide whether a graph has a cycle. if(vj, vi) and i < j.
Welcome questions always and forever.