Created
March 3, 2014 06:55
-
-
Save SamuraiT/9319725 to your computer and use it in GitHub Desktop.
small world graph implementation
This file contains 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
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