Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. def top_sort_it():
  2.  
  3.  
  4. for n in nodes:
  5.  
  6. if n is not in visited:
  7. stack.push(n)
  8.  
  9. while stack is not empty:
  10. candidate = stack.top
  11. last = True
  12.  
  13. for x in candidate.nodes:
  14. if x is not in visited:
  15. visited.insert(x)
  16. last = False
  17. stack.push(x)
  18.  
  19. if last is True:
  20. save(candidate)
  21. stack.pop()
  22.  
  23.  
  24. 1 -> 2
  25. 3 -> 2 -> 4
  26.  
  27.  
  28. n = 2 stack: 2
  29. candidate = 2 last = True
  30. stack: 2 4 last = False
  31. candidate = 4 last = True
  32. save(4) stack: 2
  33.  
  34. candidate = 2 last = True
  35. save(2) stack:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement