NetworkX is a package for creating, analyzing, visualizing networks or graphs. A graph is a general mathematical concept that can be used to represent nodes and edges for moving from one node to another node. The strength of NetworkX is in its simplicity and its algorithms.
import networkx as nx
Make a graph.
# Make a graph where edges do not have direction.
graph = nx.Graph()
# Make a graph where edges have direction.
directed_graph = nx.DiGraph()
# Make a graph where two nodes can have multiple connecting edges without direction.
multi_graph = nx.MultiGraph()
# Make a graph where two nodes can have multiple connecting edges with direction.
# For example, this kind of graph can represent cities that are connected by more than one highway.
multi_directed_graph = nx.MultiDiGraph()
# Generate a random graph.
degree = 2
node_count = 10
random_graph = nx.random_regular_graph(degree, node_count)
Add nodes, see nodes, draw nodes.
graph = nx.Graph()
# Add single nodes
graph.add_node(100)
graph.add_node('lettuce')
# Add multiple nodes
graph.add_nodes_from(['carrot', 'zucchini'])
# List nodes
graph.nodes()
# Print the first three letters of each node using the nodes_iter() iterator,
# which is more efficient than nodes() because it does not create an itermediate list
[str(node)[:3] for node in graph.nodes_iter()]
% matplotlib inline
nx.draw(graph)
Add edges, see edges, draw edges.
graph = nx.Graph()
# Add single edges
graph.add_edge('City Hall', 'Union Square')
graph.add_edge('Union Square', 'Grand Central')
graph.add_edge('Grand Central', 'Times Square')
# Add multiple edges
graph.add_edges_from([
('Penn Station', 'Times Square'),
('Times Square', '72nd Street and Broadway'),
('72nd Street and Broadway', '96th Street and Broadway'),
])
# List nodes
graph.nodes()
# List edges
graph.edges()
# List edges using the edges_iter() iterator, which is more efficient than edges()
# because it does not create an itermediate list
[(node1, node2) for node1, node2 in graph.edges_iter()]
%matplotlib inline
nx.draw(graph)
Add attributes.
# Add graph attributes
graph = nx.Graph(name='phone records')
graph.graph['start_index'] = 500
graph.graph['end_index'] = 10000
graph.graph
# Add node attributes
graph.add_node('18008662453', name='TMobile customer service')
graph.add_node('18883336651', name='AT&T customer service')
graph.add_node('18009220204', name='Verizon customer service')
graph.add_node('18668667509', name='Sprint customer service')
graph.node['18008662453']['name'] = 'T-Mobile customer service'
graph.node['18008662453']
# Look at nodes and their attributes
graph.nodes(data=True)
# Add attributes to an edge
graph.add_edge('18883336651', '18009220204', weight=0.5, confidence=0.9)
graph.add_edge('18009220204', '18668667509', weight=0.3, confidence=0.4)
graph.edge['18883336651']['18009220204']['weight'] = 1.0
graph.edge['18883336651']['18009220204']
# Look at edges and their attributes
graph.edges(data=True)
%matplotlib inline
nx.draw_circular(graph)
Analyze a graph with an algorithm.
graph = nx.Graph()
graph.add_edge('City Hall', 'Union Square', duration=10)
graph.add_edge('Union Square', 'Grand Central', duration=10)
graph.add_edge('Grand Central', 'Times Square', duration=5)
graph.add_edge('Penn Station', 'Times Square', duration=5)
graph.add_edge('Times Square', '72nd Street and Broadway', duration=10)
graph.add_edge('72nd Street and Broadway', '96th Street and Broadway', duration=10)
graph.add_edge('Grand Central', '86th Street and Lexington', duration=10)
# What is the fastest way to get from City Hall to 86th Street and Lexington?
print(nx.dijkstra_path(graph, 'City Hall', '86th Street and Lexington', weight='duration'))
# How long will it take?
print('%s minutes' % nx.dijkstra_path_length(graph, 'City Hall', '86th Street and Lexington', weight='duration'))
Save and load graphs.
path = '/tmp/graph.pkl'
# Save
nx.write_gpickle(graph, path)
# Load
graph = nx.read_gpickle(path)
graph.edges(data=True)