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.

15.3. CONSTRUCTING NETWORK MODELS WITH NETWORKX 307}’Jess’: {’Jill’: {}, ’Josh’: {}},’Jill’: {’Jess’: {}, ’Jeff’: {}},’Jack’: {’Jane’: {}},’Jane’: {’Jack’: {}, ’Jeff’: {}}Now its structure is much clearer. g.edge is a dictionary that describes edges in <strong>the</strong>network in <strong>the</strong> adjacency list format. Each entry <strong>of</strong> g.edge is a pair <strong>of</strong> <strong>the</strong> node’s name<strong>and</strong> a dictionary that contains its neighbors. For example, <strong>the</strong> first entry says that Jeffis connected <strong>to</strong> Jane <strong>and</strong> Jill. Note that <strong>the</strong> connections are perfectly symmetric; if Jeffis connected <strong>to</strong> Jane, <strong>the</strong>n <strong>the</strong> entry for Jane (in <strong>the</strong> last line) also contains Jeff as herneighbor. Such symmetry is au<strong>to</strong>matically maintained by NetworkX, because this Graphobject is for undirected graphs.Moreover, each neighbor’s name works as a key associated with ano<strong>the</strong>r dictionaryin which we can s<strong>to</strong>re properties <strong>of</strong> <strong>the</strong> connection (initially <strong>the</strong>y are all empty, as shownabove). So you can dynamically add or modify any edge property as follows:Code 15.7:>>> g.edge[’Jeff’][’Jane’][’trust’] = 1.0>>> g.edge[’Josh’][’Jess’][’love’] = True>>> g.edge[’Jess’][’Josh’][’love’]True>>> g.edge{’Jeff’: {’Jane’: {’trust’: 1.0}, ’Jill’: {}},’Josh’: {’Jess’: {’love’: True}},’Jess’: {’Jill’: {}, ’Josh’: {’love’: True}},’Jill’: {’Jess’: {}, ’Jeff’: {}},’Jack’: {’Jane’: {}},’Jane’: {’Jack’: {}, ’Jeff’: {’trust’: 1.0}}}Again, <strong>the</strong> output is reformatted <strong>to</strong> enhance <strong>the</strong> readability. Note that <strong>the</strong> new edge properties(’trust’ between Jeff <strong>and</strong> Jane; ’love’ between Josh <strong>and</strong> Jess) are correctlyinserted in<strong>to</strong> <strong>the</strong> respective dictionaries, always maintaining symmetry between nodes.I just added True ’love’ from Josh <strong>to</strong> Jess, which has been reciprocated from Jess <strong>to</strong>Josh! What a beautiful world. This is all because we are using a Graph object.The DiGraph object behaves differently. It is designed <strong>to</strong> represent directed graphs, so

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

Saved successfully!

Ooh no, something went wrong!