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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

306 CHAPTER 15. BASICS OF NETWORKSCompare <strong>the</strong>se results with <strong>the</strong> comm<strong>and</strong>s we used above <strong>to</strong> check if <strong>the</strong> node/edgeadditions/removals were conducted correctly.Those nodes() <strong>and</strong> edges() are functions that read <strong>the</strong> raw data inside <strong>the</strong> Graphobject g <strong>and</strong> <strong>the</strong>n produce a cleaned-up list <strong>of</strong> nodes or edges. But NetworkX also allowsyou <strong>to</strong> have direct access <strong>to</strong> <strong>the</strong> Graph object’s internal raw data structure. The data aboutnodes are s<strong>to</strong>red in a dictionary called node right under g:Code 15.3:>>> g.node{’Jeff’: {}, ’Josh’: {}, ’Jess’: {}, ’Jill’: {}, ’Jack’: {}, ’Jane’: {}}This is a dictionary whose keys <strong>and</strong> values are <strong>the</strong> nodes’ names <strong>and</strong> <strong>the</strong>ir properties,respectively. The properties <strong>of</strong> each node are also s<strong>to</strong>red in a dictionary (initially <strong>the</strong>y areall empty, as shown above), so you can dynamically add or modify any node property asfollows:Code 15.4:>>> g.node[’Jeff’][’job’] = ’student’>>> g.node[’Jeff’][’age’] = 20>>> g.node[’Jeff’]{’job’: ’student’, ’age’: 20}We will use this method <strong>to</strong> add dynamic states <strong>to</strong> <strong>the</strong> nodes in Section 16.2.Similarly, <strong>the</strong> data about <strong>the</strong> edges are also s<strong>to</strong>red in a dictionary called edge underg:Code 15.5:>>> g.edge{’Jeff’: {’Jane’: {}, ’Jill’: {}}, ’Josh’: {’Jess’: {}}, ’Jess’: {’Jill’:{}, ’Josh’: {}}, ’Jill’: {’Jess’: {}, ’Jeff’: {}}, ’Jack’: {’Jane’: {}},’Jane’: {’Jack’: {}, ’Jeff’: {}}}This is a little hard <strong>to</strong> read, so let me insert line breaks at <strong>the</strong> appropriate places so that<strong>the</strong> structure <strong>of</strong> <strong>the</strong> data is clearer:Code 15.6:{’Jeff’: {’Jane’: {}, ’Jill’: {}},’Josh’: {’Jess’: {}},

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

Saved successfully!

Ooh no, something went wrong!