Skip to content

Instantly share code, notes, and snippets.

@SamuraiT
Created March 3, 2014 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuraiT/9319725 to your computer and use it in GitHub Desktop.
Save SamuraiT/9319725 to your computer and use it in GitHub Desktop.
small world graph implementation
from RandomGraph import RandomGraph,show_graph
from Graph import *
import string
from random import random, choice
class SmallWorldGraph(RandomGraph):
def rewire(self, p):
vs = self.vertices()
for i, v in enumerate(vs):
for e in self.out_edges(v):
if random() <= p:
without_v = vs[:i]+vs[i+1:]
w = choice(without_v)
self.remove_edge(e)
e = Edge(v,w)
self.add_edge(e)
def main(script='',n=20,k=4,p=0.01):
n,k,p = int(n),int(k),float(p)
labels = string.lowercase + string.uppercase + string.punctuation
vs = [Vertex(c) for c in labels[:n]]
g = SmallWorldGraph(vs)
g.add_regular_edges(k)
g.rewire(p)
show_graph(g)
if __name__ == '__main__':
import sys
main(*sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment