15.08.2015 Views

Introduction to the Modeling and Analysis of Complex Systems

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16.3. SIMULATING DYNAMICS OF NETWORKS 351Let’s simulate this model using Python. When you simulate <strong>the</strong> dynamics <strong>of</strong> networks,one practical challenge is how <strong>to</strong> visualize <strong>to</strong>pological changes <strong>of</strong> <strong>the</strong> network. Unlikenode states that can be easily visualized using colors, network <strong>to</strong>pology is <strong>the</strong> shape <strong>of</strong><strong>the</strong> network itself, <strong>and</strong> if <strong>the</strong> shape <strong>of</strong> <strong>the</strong> network is changing dynamically, we also need<strong>to</strong> simulate <strong>the</strong> physical movement <strong>of</strong> nodes in <strong>the</strong> visualization space as well. This is justfor aes<strong>the</strong>tic visualization only, which has nothing <strong>to</strong> do with <strong>the</strong> real science going on in<strong>the</strong> network. But an effective visualization <strong>of</strong>ten helps us better underst<strong>and</strong> what is goingon in <strong>the</strong> simulation, so let’s try some fancy animation here. Fortunately, NetworkX’sspring_layout function can be used <strong>to</strong> update <strong>the</strong> current node positions slightly. Forexample:Code 16.9:g.pos = nx.spring_layout(g, pos = g.pos, iterations = 5)This code calculates a new set <strong>of</strong> node positions by using g.pos as <strong>the</strong> initial positions<strong>and</strong> by applying <strong>the</strong> Fruchterman-Reingold force-directed algorithm <strong>to</strong> <strong>the</strong>m for just fivesteps. This will effectively simulate a slight movement <strong>of</strong> <strong>the</strong> nodes from <strong>the</strong>ir currentpositions, which can be utilized <strong>to</strong> animate <strong>the</strong> <strong>to</strong>pological changes smoothly.Here is an example <strong>of</strong> <strong>the</strong> completed simulation code for <strong>the</strong> small-world network formationby r<strong>and</strong>om edge rewiring:Code 16.10: small-world.pyimport matplotlibmatplotlib.use(’TkAgg’)from pylab import *import networkx as nximport r<strong>and</strong>om as rdn = 30 # number <strong>of</strong> nodesk = 4 # number <strong>of</strong> neighbors <strong>of</strong> each nodedef initialize():global gg = nx.Graph()for i in xrange(n):for j in range(1, k/2 + 1):g.add_edge(i, (i + j) % n)g.add_edge(i, (i - j) % n)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!