Mercurial > public > think_complexity
view SmallWorldGraph.py @ 42:039249efe42f
Chapter 6, exercise 2, #4. Wrote a program to output the center column of
a rule 30 CA as a stream of bytes. It is very slow though. It has to run a very
long time to produce enough data for dieharder. Committing it now but will have
to let it run overnight or something to generate a large file.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 13 Jan 2013 16:24:00 -0600 |
parents | 305cc03c2750 |
children |
line wrap: on
line source
"""This module contains the SmallWorldGraph class for 4.4, exercise 4. """ import random from Graph import Edge from RandomGraph import RandomGraph class SmallWorldGraph(RandomGraph): """A small world graph for 4.4, exercise 4 in Think Complexity.""" def __init__(self, vs, k, p): """Create a small world graph. Parameters: vs - a list of vertices k - regular graph parameter k: the number of edges between vertices p - probability for rewiring. First a regular graph is created from the list of vertices and parameter k, which specifies how many edges each vertex should have. Then the rewire() method is called with parameter p to rewire the graph according to the algorithm by Watts and Strogatz. """ super(SmallWorldGraph, self).__init__(vs=vs) self.add_regular_edges(k) self.rewire(p) self.set_edge_length(1) def rewire(self, p): """Assuming the graph is initially a regular graph, rewires the graph using the Watts and Strogatz algorithm. """ # We iterate over the edges in a random order: edges = self.edges() random.shuffle(edges) vertices = self.vertices() for e in edges: if random.random() < p: v, w = e # remember the 2 vertices this edge connected self.remove_edge(e) # remove from graph # pick another vertex to connect to v; duplicate edges are # forbidden while True: x = random.choice(vertices) if v is not x and not self.get_edge(v, x): self.add_edge(Edge(v, x)) break