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.

15.3. CONSTRUCTING NETWORK MODELS WITH NETWORKX 305# adding a bunch <strong>of</strong> edges at onceg.add_edges_from([(’Jess’, ’Josh’), (’John’, ’Jack’), (’Jack’, ’Jane’)])# adding more edges# undefined nodes will be created au<strong>to</strong>maticallyg.add_edges_from([(’Jess’, ’Jill’), (’Jill’, ’Jeff’), (’Jeff’, ’Jane’)])# removing <strong>the</strong> edge between ’John’ <strong>and</strong> ’Jane’g.remove_edge(’John’, ’Jane’)# removing <strong>the</strong> node ’John’# all edges connected <strong>to</strong> that node will be removed <strong>to</strong>og.remove_node(’John’)Here I used strings for <strong>the</strong> names <strong>of</strong> <strong>the</strong> nodes, but a node’s name can be a number,a string, a list, or any “hashable” object in Python. For example, in <strong>the</strong> phase spacevisualization code in Section 5.4, we used a tuple for a name <strong>of</strong> each node.I believe each comm<strong>and</strong> used above <strong>to</strong> add or remove nodes/edges is quite selfexplana<strong>to</strong>ry.If not, you can always look at NetworkX’s online documentation <strong>to</strong> learnmore about how each comm<strong>and</strong> works. There are also several o<strong>the</strong>r ways <strong>to</strong> manipulatea graph object, which are not detailed here.Once this code is executed, you can see <strong>the</strong> results in <strong>the</strong> Python comm<strong>and</strong> line, likethis:Code 15.2:>>> g>>> g.nodes()[’Jeff’, ’Josh’, ’Jess’, ’Jill’, ’Jack’, ’Jane’]>>> g.edges()[(’Jeff’, ’Jane’), (’Jeff’, ’Jill’), (’Josh’, ’Jess’), (’Jess’, ’Jill’),(’Jack’, ’Jane’)]>>>The first output (“”) shows that g is a Graph object. In order <strong>to</strong>see <strong>the</strong> contents <strong>of</strong> <strong>the</strong> data in <strong>the</strong> object, we need <strong>to</strong> use some comm<strong>and</strong>s. g.nodes()returns a list <strong>of</strong> all nodes in <strong>the</strong> network, while g.edges() returns a list <strong>of</strong> all edges.

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

Saved successfully!

Ooh no, something went wrong!