开发者

Sort by hierarchy algorithm

How can I make this sorting algorithm?

modules = {
    2:[4],   # element 2 must be after el开发者_如何学运维ement 4
    1:[],
    3:[2,4], # element 3 must be after 2 and 3 elements
    4:[1]    # element 4 must be after element 1
}

Result [1, 4, 2, 3] by conditions.


That's called a topological sort. Makefiles do it, and there's a standard UNIX utility called tsort that will do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜