Complex Systems 530 - W2020

NetworkX Basics

In [1]:
# Import the package
import networkx as nx

Building networks

In [7]:
# Make a blank graph object
g = nx.Graph() #can also make a DiGraph

# Add nodes
g.add_node("Michigan")
g.add_node("Ohio")
g.add_node("Wisconsin")
g.add_node("Indiana")
g.add_node("Illinois")

# See what we have
print((g.number_of_nodes(), g.number_of_edges()))
(5, 0)
In [8]:
# Add edges
g.add_edge("Michigan","Ohio")
g.add_edge("Michigan","Indiana")
g.add_edge("Michigan","Wisconsin")
g.add_edge("Indiana","Ohio")
g.add_edge("Indiana","Illinois")
g.add_edge("Illinois","Wisconsin")

# See what we have
print((g.number_of_nodes(), g.number_of_edges()))
(5, 6)
In [12]:
# Add data about some of the nodes
g.node['Michigan']['motto'] = 'Si quaeris peninsulam amoenam circumspice'
g.node['Indiana']['motto'] = 'The Crossroads of America'
g.node['Wisconsin']['motto'] = 'Forward'
g.node['Michigan']['population'] = 9996000

Nodes

In [14]:
# View nodes using:
g.nodes()
Out[14]:
NodeView(('Michigan', 'Ohio', 'Wisconsin', 'Indiana', 'Illinois'))
In [15]:
# Node attributes are kept in a dictionary associated with node, 
g.nodes.data()
Out[15]:
NodeDataView({'Michigan': {'motto': 'Si quaeris peninsulam amoenam circumspice', 'population': 9996000}, 'Ohio': {}, 'Wisconsin': {'motto': 'Forward'}, 'Indiana': {'motto': 'The Crossroads of America'}, 'Illinois': {}})
In [18]:
# Convert to list or dictionary
list(g.nodes())
dict(g.nodes.data()) 
Out[18]:
{'Michigan': {'motto': 'Si quaeris peninsulam amoenam circumspice',
  'population': 9996000},
 'Ohio': {},
 'Wisconsin': {'motto': 'Forward'},
 'Indiana': {'motto': 'The Crossroads of America'},
 'Illinois': {}}

Edges

In [20]:
# View Edges
g.edges 
Out[20]:
EdgeView([('Michigan', 'Ohio'), ('Michigan', 'Indiana'), ('Michigan', 'Wisconsin'), ('Ohio', 'Indiana'), ('Wisconsin', 'Illinois'), ('Indiana', 'Illinois')])
In [21]:
# Can similarly view edge data if we had it
g.edges.data()
Out[21]:
EdgeDataView([('Michigan', 'Ohio', {}), ('Michigan', 'Indiana', {}), ('Michigan', 'Wisconsin', {}), ('Ohio', 'Indiana', {}), ('Wisconsin', 'Illinois', {}), ('Indiana', 'Illinois', {})])
In [22]:
# Also able to view as an adjacency list (object resembles series of nested dictionaries)
g.adj 
Out[22]:
AdjacencyView({'Michigan': {'Ohio': {}, 'Indiana': {}, 'Wisconsin': {}}, 'Ohio': {'Michigan': {}, 'Indiana': {}}, 'Wisconsin': {'Michigan': {}, 'Illinois': {}}, 'Indiana': {'Michigan': {}, 'Ohio': {}, 'Illinois': {}}, 'Illinois': {'Indiana': {}, 'Wisconsin': {}}})




Nodes: Turtles

  • Node attributes are “turtles-own” variables
  • Node sets are the agentsets of turtles

Ties: Links

  • Links treated as their own agents in NetLogo
  • Identified by the “who” id of the turtles they connect
  • Default is undirected, but can be directed
  • Set tie attributes using “links-own”

Neighbor set is identified via “[turtle] link-neighbors”

There is also a NetLogo Network Extension

May also want to look at the Virus on a Network example in the Models Library!

In [ ]: