Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph.py 1.1KB

8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. Provides an extremely basic graph structure, based on neighbour lists
  3. """
  4. def graph(n):
  5. """ Generate a graph with Hadamards on each qubit """
  6. graph = [set() for i in xrange(n)]
  7. vops = [0 for i in xrange(n)] # TODO: seems ugly
  8. return graph, vops
  9. def add_edge(graph, v1, v2):
  10. """ Add an edge between two vertices in the graph """
  11. graph[v1].add(v2)
  12. graph[v2].add(v1)
  13. def del_edge(graph, v1, v2):
  14. """ Delete an edge between two vertices in the graph """
  15. graph[v1].remove(v2)
  16. graph[v2].remove(v1)
  17. def has_edge(graph, v1, v2):
  18. """ Test existence of an edge between two vertices in the graph """
  19. return v2 in graph[v1]
  20. def toggle_edge(graph, v1, v2):
  21. """ Toggle an edge between two vertices in the graph """
  22. if has_edge(graph, v1, v2):
  23. del_edge(graph, v1, v2)
  24. else:
  25. add_edge(graph, v1, v2)
  26. def edgelist(graph):
  27. """ Describe a graph as an edgelist """
  28. edges = frozenset(frozenset((i, n))
  29. for i, v in enumerate(graph)
  30. for n in v)
  31. return [tuple(e) for e in edges]