Skip to content

Commit

Permalink
Update g_topological_sort.py (TheAlgorithms#1873)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandersLin authored Apr 16, 2020
1 parent f6ee518 commit d2e8e62
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions graphs/g_topological_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
5: "socks",
6: "shirt",
7: "tie",
8: "clock",
8: "watch",
}

graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
Expand All @@ -21,27 +21,27 @@
def print_stack(stack, clothes):
order = 1
while stack:
cur_clothe = stack.pop()
print(order, clothes[cur_clothe])
current_clothing = stack.pop()
print(order, clothes[current_clothing])
order += 1


def dfs(u, visited, graph):
def depth_first_search(u, visited, graph):
visited[u] = 1
for v in graph[u]:
if not visited[v]:
dfs(v, visited, graph)
depth_first_search(v, visited, graph)

stack.append(u)


def top_sort(graph, visited):
def topological_sort(graph, visited):
for v in range(len(graph)):
if not visited[v]:
dfs(v, visited, graph)
depth_first_search(v, visited, graph)


if __name__ == "__main__":
top_sort(graph, visited)
topological_sort(graph, visited)
print(stack)
print_stack(stack, clothes)

0 comments on commit d2e8e62

Please sign in to comment.