Skip to content

Commit

Permalink
添加top排序
Browse files Browse the repository at this point in the history
  • Loading branch information
ResolveWang committed May 20, 2018
1 parent 5f9901c commit a12646b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions basic_algrithms/sort_algrithms/top.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 拓扑排序
def indegree0(v, e):
if not v:
return None
tmp = v[:]
# 找出入度为0的节点
for i in e:
if i[1] in tmp:
tmp.remove(i[1])

if not tmp:
return -1

# 在关系图中标记出入度为0的节点
for t in tmp:
for i in range(len(e)):
if t in e[i]:
e[i] = 'useless'

if v:
for t in tmp:
v.remove(t)
return tmp


def top_sort(v, e):
result = []
while True:
nodes = indegree0(v, e)
if nodes == -1:
print('there\'s a circle.')
return
elif not nodes:
break
else:
result.extend(nodes)
return result


if __name__ == '__main__':
v = ['a', 'b', 'c', 'd', 'e']
e = [('a', 'b'), ('a', 'd'), ('b', 'c'), ('d', 'c'), ('d', 'e'), ('e', 'c')]
res = top_sort(v, e)
print(res)

0 comments on commit a12646b

Please sign in to comment.