开发者

Sort by hierarchy algorithm

开发者 https://www.devze.com 2023-03-24 23:52 出处:网络
How can I make this sorting algorithm? modules = { 2:[4],# element 2 must be after el开发者_如何学运维ement 4

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

精彩评论

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