Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

graph.py 1.3KB

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