Introduction: NetworkX
official website
11
http://networkx.github.io/
Getting started: the environment
• Start Python (interactive or script mode) and import NetworkX
• Different classes exist for directed and undirected networks. Let’s create a basic
undirected Graph:
• The graph
g can be grown in several ways. NetworkX provides many generator
functions and facilities to read and write graphs in many formats.
$ python
>>> import networkx as nx
>>> g = nx.Graph() # empty graph
13
Getting started: adding nodes
# One node at a time
>>> g.add_node(1)
# A list of nodes
>>> g.add_nodes_from([2, 3])
# A container of nodes
>>> h = nx.path_graph(5)
>>> g.add_nodes_from(h)
# You can also remove any node of the graph
>>> g.remove_node(2)
14
Getting started: node objects
• A node can be
any hashable object such as a string, a function, a file and more.
Dostları ilə paylaş: