Last active
September 16, 2016 16:06
-
-
Save jogonba2/a292a5f2bdef903fbc1683448bcdba10 to your computer and use it in GitHub Desktop.
Watts-Strogatz model for generating small-words random graphs.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from random import random,choice | |
import networkx as nx | |
import matplotlib.pyplot as plt | |
import time | |
def watts(G,B): | |
aux_edges = G.edges() | |
aux_nodes = G.nodes() | |
n_edges = len(aux_edges) | |
for i in xrange(n_edges): | |
edge = list(aux_edges[i]) | |
if random()<=B: | |
dest = edge[0] | |
while dest==edge[0]: dest = choice(aux_nodes) | |
edge[1] = dest | |
aux_edges[i] = tuple(edge) | |
G.clear() | |
G.add_edges_from(aux_edges) | |
return G | |
def initialize_graph(N,K,B): | |
G = nx.Graph() | |
for i in xrange(0,N): | |
for j in xrange(0,N): | |
if abs(i-j)<(K/2): G.add_edge(*(i,j)) | |
return G | |
def main(N = 5, K = 2, B = 0.5): | |
G = initialize_graph(N,K,B) | |
nx.draw(G) | |
plt.show() | |
watts(G,B) | |
nx.draw(G) | |
plt.show() | |
if __name__ == "__main__": | |
main(N = 1000, K = 5 , B = 0.43) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment