Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

52 行
1.2KB

  1. from clifford import *
  2. """
  3. Porting Anders and Briegel to Python
  4. """
  5. class Vertex(object):
  6. def __init__(self, index):
  7. self.index = index
  8. self.vertex_operator = lco_h;
  9. self.neighbors = set()
  10. def edgelist(self):
  11. return [set((self.index, n)) for n in self.neighbors]
  12. def __str__(self):
  13. return "{}".format(", ".join(map(str, self.neighbors)))
  14. class GraphRegister(object):
  15. def __init__(self, n):
  16. self.vertices = [Vertex(i) for i in xrange(n)]
  17. def add_edge(self, v1, v2):
  18. self.vertices[v1].neighbors.add(v2)
  19. self.vertices[v2].neighbors.add(v1)
  20. def del_edge(self, v1, v2):
  21. self.vertices[v1].neighbors.remove(v2)
  22. self.vertices[v2].neighbors.remove(v1)
  23. def toggle_edge(self, v1, v2):
  24. if v2 in self.vertices[v1].neighbors:
  25. self.del_edge(v1, v2)
  26. else:
  27. self.add_edge(v1, v2)
  28. def edgelist(self):
  29. return map(tuple, frozenset(frozenset((v.index, n))
  30. for v in self.vertices
  31. for n in v.neighbors))
  32. def __str__(self, ):
  33. return "\n".join(str(v) for v in self.vertices if len(v.neighbors) > 0)
  34. if __name__ == '__main__':
  35. g = GraphRegister(10)
  36. g.toggle_edge(0, 1)
  37. print g