python 拓扑排序正确版
2021/4/22 20:29:02
本文主要是介绍python 拓扑排序正确版,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 拓扑排序正确版的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型