#Topological Sort ''' 1 2 3 4 5 1 -> 2 -> 1 3 -> 1 4 -> 5 -> 3 ''' ''' visited = [1,2,3,4,5] ret = [1,5] ''' def travel(visited,d,ele): if ele in visited: return if ele in d: for child in d[ele]: yield from travel(visited,child) yield ele visited.add(ele) def f(array: list,deps: "list of tuple"): ret = [] visited = set() d = {} for v1,v2 in deps: d[v1] = [v2] for ele in array: for n in travel(visited,ele): ret.append(n) return list(reversed(ret)) result = f ( [1,5],[ (2,1),(3,4),(5,3) ] ) print(result)
[5,1]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。