# Import the package
import networkx as nx
# 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()))
# 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()))
# 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
# View nodes using:
g.nodes()
# Node attributes are kept in a dictionary associated with node,
g.nodes.data()
# Convert to list or dictionary
list(g.nodes())
dict(g.nodes.data())
# View Edges
g.edges
# Can similarly view edge data if we had it
g.edges.data()
# Also able to view as an adjacency list (object resembles series of nested dictionaries)
g.adj
Nodes: Turtles
Ties: Links
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!