Skip to content

Instantly share code, notes, and snippets.

@cyberoctopi
Created April 2, 2012 23:07
Show Gist options
  • Select an option

  • Save cyberoctopi/2287857 to your computer and use it in GitHub Desktop.

Select an option

Save cyberoctopi/2287857 to your computer and use it in GitHub Desktop.
python basic graph
class Graph(dict)
def __init__(self, v=[], es=[])
"""create a ew graph. (vs) is a list of vertices, (es) is a list of of edges"""
for v in vs:
self.add_vertex(v)
for e in es:
self.add_edge(e)
def add_vertex(self, v):
"""add (v) to the graph"""
self[v] = {}
def add_edge(self, e):
v,w = e
self[v][w] = e
self[w][v]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment