Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def top_sort_it():
- for n in nodes:
- if n is not in visited:
- stack.push(n)
- while stack is not empty:
- candidate = stack.top
- last = True
- for x in candidate.nodes:
- if x is not in visited:
- visited.insert(x)
- last = False
- stack.push(x)
- if last is True:
- save(candidate)
- stack.pop()
- 1 -> 2
- 3 -> 2 -> 4
- n = 2 stack: 2
- candidate = 2 last = True
- stack: 2 4 last = False
- candidate = 4 last = True
- save(4) stack: 2
- candidate = 2 last = True
- save(2) stack:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement