Created
April 2, 2012 23:07
-
-
Save cyberoctopi/2287857 to your computer and use it in GitHub Desktop.
python basic graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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