Browse Source

Nicer graph model, VOPs

master
Pete Shadbolt 8 years ago
parent
commit
d6537cc5d7
5 changed files with 67 additions and 1 deletions
  1. +1
    -0
      .gitignore
  2. +0
    -1
      abp.py
  3. +48
    -0
      graph.py
  4. +15
    -0
      tests/test_graph.py
  5. +3
    -0
      vops.py

+ 1
- 0
.gitignore View File

@@ -1,3 +1,4 @@
*.pdf
# Project-specific
ab/
anders_briegel


+ 0
- 1
abp.py View File

@@ -1 +0,0 @@
nothing

+ 48
- 0
graph.py View File

@@ -0,0 +1,48 @@
import networkx as nx
from matplotlib import pyplot as plt
from vops import *

class Graph(object):

def __init__(self, n):
self.vertices = [set() for i in xrange(n)]
self.vops = [hadamard for i in xrange(n)]

def add_edge(self, v1, v2):
self.vertices[v1].add(v2)
self.vertices[v2].add(v1)

def del_edge(self, v1, v2):
self.vertices[v1].remove(v2)
self.vertices[v2].remove(v1)

def edgelist(self):
edges = frozenset(frozenset((i, n))
for i, v in enumerate(self.vertices)
for n in v)
return [tuple(e) for e in edges]

def draw(self, filename="out.pdf"):
g = nx.from_edgelist(self.edgelist())
pos = nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos, node_color="white", node_size=1000)
nx.draw_networkx_labels(g, pos)
nx.draw_networkx_edges(g, pos)
for i, vop in enumerate(self.vops):
if not i in pos: continue
x, y = pos[i]
plt.text(x, y+0.1, vops[vop], ha="center")

plt.axis('off')
plt.savefig(filename)

if __name__ == '__main__':
g = Graph(10)
g.add_edge(0, 1)
g.add_edge(1, 3)
g.add_edge(3, 2)
g.add_edge(3, 0)
g.add_edge(2, 0)
print g.edgelist()

g.draw()

+ 15
- 0
tests/test_graph.py View File

@@ -0,0 +1,15 @@
from graph import Graph

def test_graph():
g = Graph(3)
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,0)
assert g.vertices[0]==set([1,2])

g.del_edge(0,1)
assert g.vertices[0]==set([2])
el = g.edgelist()
assert (1,2) in el
assert not (0,1) in el
assert len(el)==2

+ 3
- 0
vops.py View File

@@ -0,0 +1,3 @@
vops = ["hadamard"]
globals().update({x:i for i, x in enumerate(vops)})


Loading…
Cancel
Save