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

49 行
1.3KB

  1. import networkx as nx
  2. from matplotlib import pyplot as plt
  3. from vops import *
  4. class Graph(object):
  5. def __init__(self, n):
  6. self.vertices = [set() for i in xrange(n)]
  7. self.vops = [hadamard for i in xrange(n)]
  8. def add_edge(self, v1, v2):
  9. self.vertices[v1].add(v2)
  10. self.vertices[v2].add(v1)
  11. def del_edge(self, v1, v2):
  12. self.vertices[v1].remove(v2)
  13. self.vertices[v2].remove(v1)
  14. def edgelist(self):
  15. edges = frozenset(frozenset((i, n))
  16. for i, v in enumerate(self.vertices)
  17. for n in v)
  18. return [tuple(e) for e in edges]
  19. def draw(self, filename="out.pdf"):
  20. g = nx.from_edgelist(self.edgelist())
  21. pos = nx.spring_layout(g)
  22. nx.draw_networkx_nodes(g, pos, node_color="white", node_size=1000)
  23. nx.draw_networkx_labels(g, pos)
  24. nx.draw_networkx_edges(g, pos)
  25. for i, vop in enumerate(self.vops):
  26. if not i in pos: continue
  27. x, y = pos[i]
  28. plt.text(x, y+0.1, vops[vop], ha="center")
  29. plt.axis('off')
  30. plt.savefig(filename)
  31. if __name__ == '__main__':
  32. g = Graph(10)
  33. g.add_edge(0, 1)
  34. g.add_edge(1, 3)
  35. g.add_edge(3, 2)
  36. g.add_edge(3, 0)
  37. g.add_edge(2, 0)
  38. print g.edgelist()
  39. g.draw()