mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Update g_topological_sort.py (#1873)
This commit is contained in:
parent
f6ee518ee1
commit
d2e8e6215e
|
@ -9,7 +9,7 @@ clothes = {
|
||||||
5: "socks",
|
5: "socks",
|
||||||
6: "shirt",
|
6: "shirt",
|
||||||
7: "tie",
|
7: "tie",
|
||||||
8: "clock",
|
8: "watch",
|
||||||
}
|
}
|
||||||
|
|
||||||
graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
|
graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
|
||||||
|
@ -21,27 +21,27 @@ stack = []
|
||||||
def print_stack(stack, clothes):
|
def print_stack(stack, clothes):
|
||||||
order = 1
|
order = 1
|
||||||
while stack:
|
while stack:
|
||||||
cur_clothe = stack.pop()
|
current_clothing = stack.pop()
|
||||||
print(order, clothes[cur_clothe])
|
print(order, clothes[current_clothing])
|
||||||
order += 1
|
order += 1
|
||||||
|
|
||||||
|
|
||||||
def dfs(u, visited, graph):
|
def depth_first_search(u, visited, graph):
|
||||||
visited[u] = 1
|
visited[u] = 1
|
||||||
for v in graph[u]:
|
for v in graph[u]:
|
||||||
if not visited[v]:
|
if not visited[v]:
|
||||||
dfs(v, visited, graph)
|
depth_first_search(v, visited, graph)
|
||||||
|
|
||||||
stack.append(u)
|
stack.append(u)
|
||||||
|
|
||||||
|
|
||||||
def top_sort(graph, visited):
|
def topological_sort(graph, visited):
|
||||||
for v in range(len(graph)):
|
for v in range(len(graph)):
|
||||||
if not visited[v]:
|
if not visited[v]:
|
||||||
dfs(v, visited, graph)
|
depth_first_search(v, visited, graph)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
top_sort(graph, visited)
|
topological_sort(graph, visited)
|
||||||
print(stack)
|
print(stack)
|
||||||
print_stack(stack, clothes)
|
print_stack(stack, clothes)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user