Skip to content

Commit

Permalink
toposort
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jul 27, 2020
1 parent 740c162 commit a27b5dc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions TopologicalSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def topsort(g,vtx):
degree = [0]*vtx
for node in g:
for adjnode in g[node]:
degree[adjnode]+=1


bfs = [i for i in range(vtx) if degree[i] == 0]
for node in bfs:
for adjnode in g[node]:
degree[adjnode]-=1
if degree[adjnode] == 0:
bfs.append(adjnode)
return bfs


from collections import defaultdict
g = defaultdict(list)
vtx,e = map(int,input().split())
for i in range(e):
u,v = map(str,input().split())
u = ord(u) - ord('A')
v = ord(v) - ord('A')
g[u].append(v)
topSort = topsort(g,vtx)
topSort = [chr(i+65) for i in topSort]
print(topSort)

0 comments on commit a27b5dc

Please sign in to comment.