2019-10-18 06:13:58 +00:00
|
|
|
# Author: Phyllipe Bezerra (https://github.com/pmba)
|
|
|
|
|
|
|
|
clothes = {
|
|
|
|
0: "underwear",
|
|
|
|
1: "pants",
|
|
|
|
2: "belt",
|
|
|
|
3: "suit",
|
|
|
|
4: "shoe",
|
|
|
|
5: "socks",
|
|
|
|
6: "shirt",
|
|
|
|
7: "tie",
|
2020-04-16 10:34:14 +00:00
|
|
|
8: "watch",
|
2019-10-18 06:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []]
|
|
|
|
|
|
|
|
visited = [0 for x in range(len(graph))]
|
|
|
|
stack = []
|
|
|
|
|
|
|
|
|
|
|
|
def print_stack(stack, clothes):
|
|
|
|
order = 1
|
|
|
|
while stack:
|
2020-04-16 10:34:14 +00:00
|
|
|
current_clothing = stack.pop()
|
|
|
|
print(order, clothes[current_clothing])
|
2019-10-18 06:13:58 +00:00
|
|
|
order += 1
|
|
|
|
|
|
|
|
|
2020-04-16 10:34:14 +00:00
|
|
|
def depth_first_search(u, visited, graph):
|
2019-10-18 06:13:58 +00:00
|
|
|
visited[u] = 1
|
|
|
|
for v in graph[u]:
|
|
|
|
if not visited[v]:
|
2020-04-16 10:34:14 +00:00
|
|
|
depth_first_search(v, visited, graph)
|
2019-10-18 06:13:58 +00:00
|
|
|
|
|
|
|
stack.append(u)
|
|
|
|
|
|
|
|
|
2020-04-16 10:34:14 +00:00
|
|
|
def topological_sort(graph, visited):
|
2019-10-18 06:13:58 +00:00
|
|
|
for v in range(len(graph)):
|
|
|
|
if not visited[v]:
|
2020-04-16 10:34:14 +00:00
|
|
|
depth_first_search(v, visited, graph)
|
2019-10-18 06:13:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-04-16 10:34:14 +00:00
|
|
|
topological_sort(graph, visited)
|
2019-10-18 06:13:58 +00:00
|
|
|
print(stack)
|
|
|
|
print_stack(stack, clothes)
|