How does networkx work?


In [1]:
# Import networkx and also matplotlib.pyplot for visualization
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline

Undirected Graphs


In [12]:
# Create an empty undirected graph
G = nx.Graph()

In [13]:
# Add some nodes and edges.  Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])

In [14]:
nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()


Directed Graphs


In [15]:
# Create an empty directed graph
G = nx.DiGraph()

In [16]:
# Add some nodes and edges.  Adding edges aslo adds nodes if they don't already exist.
G.add_node('Janos')
G.add_nodes_from(['Sophie', 'Otto'])
G.add_edge('Janos', 'Sophie')
G.add_edges_from([('Janos', 'Otto'), ('Sophie', 'Otto')])

In [17]:
nx.draw_spectral(G, with_labels=True, node_size=3000)
plt.show()


What can nodes be?

Nodes can be any hashable object: strings, integers, floats, functions, files, etc.

Both edges and nodes can store attributes in a dictionary.


In [18]:
import numpy as np
G.add_node(np.mean)
file = open('abc.txt', 'w')
G.add_node(file)
print(G.nodes())


[<_io.TextIOWrapper name='abc.txt' mode='w' encoding='cp1252'>, <function mean at 0x00000000054FFD90>, 'Janos', 'Otto', 'Sophie']

Reading in Data

We don't usually add data by hand.

NetworkX handles the following (and more) very well:

  • Standard files: txt, csv, json
  • Graph formats: GML, gexf
  • Python objects: dicts, Pandas DataFrames, lists of lists

In [23]:
!head addHealth81.txt


1	 463	1.00
1    679	1.00
1    958	1.00
1	1089	1.00
2     95	1.00
2    271	2.00
2    326	1.00
2    332	1.00
2    588	1.00
2    634	1.00

In [24]:
D = nx.read_weighted_edgelist('addHealth81.txt', create_using=nx.DiGraph())

In [25]:
len(D.nodes()), len(D.edges())


Out[25]:
(12, 10)

In [ ]: