Created
June 16, 2020 19:46
-
-
Save okaysidd/0f016b631ed769765ba803df5eea4675 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Topological_sort(adj): | |
result = [] | |
def dfs_visit(node): | |
for neighbor in adj[node]: | |
if neighbor not in seen: | |
seen.add(neighbor) | |
dfs_visit(neighbor) | |
result.insert(0, node) # insert to beginning of list | |
seen = set() | |
values = [] | |
[values.extend(v) for v in adj.values()] | |
for node in adj: | |
if node not in values: | |
if node not in seen: | |
dfs_visit(node) | |
return result | |
adj = {'A':['B','D'], 'B':['C','D'], 'C':[], 'D':['E'], 'E':[]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment