Skip to content

Commit

Permalink
Adding Graph.py
Browse files Browse the repository at this point in the history
  • Loading branch information
arksega committed Oct 18, 2012
1 parent 93eec19 commit e6ec8da
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Graph.py
@@ -0,0 +1,90 @@
""" Code example from Complexity and Computation, a book about
exploring complexity science with Python. Available free from
http://greenteapress.com/complexity
Copyright 2011 Allen B. Downey.
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
"""

class Vertex(object):
"""A Vertex is a node in a graph."""

def __init__(self, label=''):
self.label = label

def __repr__(self):
"""Returns a string representation of this object that can
be evaluated as a Python expression."""
return 'Vertex(%s)' % repr(self.label)

__str__ = __repr__
"""The str and repr forms of this object are the same."""


class Edge(tuple):
"""An Edge is a list of two vertices."""

def __new__(cls, *vs):
"""The Edge constructor takes two vertices."""
if len(vs) != 2:
raise ValueError, 'Edges must connect exactly two vertices.'
return tuple.__new__(cls, vs)

def __repr__(self):
"""Return a string representation of this object that can
be evaluated as a Python expression."""
return 'Edge(%s, %s)' % (repr(self[0]), repr(self[1]))

__str__ = __repr__
"""The str and repr forms of this object are the same."""


class Graph(dict):
"""A Graph is a dictionary of dictionaries. The outer
dictionary maps from a vertex to an inner dictionary.
The inner dictionary maps from other vertices to edges.
For vertices a and b, graph[a][b] maps
to the edge that connects a->b, if it exists."""

def __init__(self, vs=[], es=[]):
"""Creates a new graph.
vs: list of vertices;
es: list of edges.
"""
for v in vs:
self.add_vertex(v)

for e in es:
self.add_edge(e)

def add_vertex(self, v):
"""Add a vertex to the graph."""
self[v] = {}

def add_edge(self, e):
"""Adds and edge to the graph by adding an entry in both directions.
If there is already an edge connecting these Vertices, the
new edge replaces it.
"""
v, w = e
self[v][w] = e
self[w][v] = e


def main(script, *args):
v = Vertex('v')
print v
w = Vertex('w')
print w
e = Edge(v, w)
print e
g = Graph([v,w], [e])
print g


if __name__ == '__main__':
import sys
main(*sys.argv)

0 comments on commit e6ec8da

Please sign in to comment.