我正在寻找一种简单的算法来“序列化”有向图。特别是,我有一组文件,它们的执行顺序相互依赖,我想在编译时找到正确的顺序。我知道这一定是很常见的事情-编译器一直都在做-但是我的google-fu今天很薄弱。什么是“去”算法?
最佳答案
Topological Sort(来自Wikipedia):
伪代码:
L ← Empty list where we put the sorted elements
Q ← Set of all nodes with no incoming edges
while Q is non-empty do
remove a node n from Q
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into Q
if graph has edges then
output error message (graph has a cycle)
else
output message (proposed topologically sorted order: L)
关于algorithm - 图序列化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4168/