def indegree0(v, e):if v == []:return Nonetmp = v[:]for i in e:if i[1] in tmp:tmp.remove(i[1])if tmp == []:return -1for t in tmp:for i in range(len(e)):if t in e[i]:e[i] = 'toDel' # 占位,之后删掉if e:eset = set(e)eset.remove('toDel')e[:] = List(eset)if v:for t in tmp:v.remove(t)return tmpdef topoSort(v, e):result = []while True:nodes = indegree0(v, e)if nodes == None:breakif nodes == -1:print('there\'s a circle.')return Noneresult.append(nodes)return resultif __name__ == '__main__':v = ['v1', 'v2', 'v3', 'v4', 'v5',"v6","v7"]e = [('v1', 'v2'),('v1', 'v3'),('v1', 'v4'),('v2', 'v5'),('v2', 'v6'),('v3', 'v5'),('v3', 'v6'),('v4', 'v6'),('v5', 'v7'),('v6', 'v7')]res = topoSort(v, e)print(res)总结
以上是内存溢出为你收集整理的python 拓扑排序正确版全部内容,希望文章能够帮你解决python 拓扑排序正确版所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)