1 Star 0 Fork 0

heng2617/learnPython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
top_sort.py 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
heng 提交于 2020-06-07 09:41 +08:00 . add top sort
class Node:
def __init__(self, name = None, color = 'white', successors = []):
self.name = name
self.color = color
self.successors = successors
def top_order(G):
L = []
nameList = []
for ele in G:
if ele.color == 'white':
L.extend(dfs(G,ele))
if -1 not in L:
L.reverse()
## nameList = list(map(lambda x: x.name, L))
for knoten in L:
nameList.append(knoten.name)
return nameList
else:
return [-1]
def dfs(G, r):
besuchtList = []
stack = []
r.color = 'grey'
stack.append(r)
while stack != []:
v = stack[-1]
a = False
for n in v.successors:
if n.color == 'grey':
return [-1]
for n in v.successors:
if n.color == 'white':
n.color = 'grey'
stack.append(n)
a = True
break
if not a:
v.color = 'bleak'
besuchtList.append(stack.pop())
return besuchtList
A = Node("内裤",'white',[])
B = Node("秋裤",'white',[])
C = Node("牛仔裤",'white',[])
D = Node("袜子",'white',[])
E = Node("鞋子",'white',[])
F = Node("秋衣",'white',[])
G = Node("外套",'white',[])
A.successors = [B]
B.successors = [C]
C.successors = [E]
D.successors = [E]
F.successors = [G]
G = [E,C,D,B,A,G,F]
print(top_order(G))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/heng2617/learnPython.git
git@gitee.com:heng2617/learnPython.git
heng2617
learnPython
learnPython
master

搜索帮助