rw.py 539 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. # pip3 install graph-tools
  3. import random
  4. import graph_tools
  5. MAX_TIME = 100
  6. def main():
  7. g = graph_tools.Graph(directed=False)
  8. g.add_edge(1, 2)
  9. g.add_edge(1, 3)
  10. g.add_edge(2, 3)
  11. g.add_edge(3, 4)
  12. g.add_edge(4, 5)
  13. # 2
  14. # / \
  15. # 1 ----- 3 ----- 4 ----- 5
  16. t = 0
  17. v = g.random_vertex()
  18. while t <= MAX_TIME:
  19. print(t, v)
  20. neighbors = g.neighbors(v)
  21. v = random.choice(list(neighbors))
  22. t += 1
  23. if __name__ == "__main__":
  24. main()