1. 概述

NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.


  • Nodes can be arbitrary (hashable) Python objects except None (e.g. text, images, XML records)
  • Edges can hold arbitrary data (e.g. weights, time-series)


2. 构建一张图

2.1 定义图的类型


# 使用举例
G = nx.Graph()        # Undirected graphs with self loops
G = nx.DiGraph()      # DiGraphDirected graphs with self loops
G = nx.MultiGraph()   # Directed graphs with self loops and parallel edges
G = nx.MultiDiGraph() # Undirected graphs with self loops and parallel edges

# Graph types
Graph(data=None, **attr)        # Undirected graphs with self loops
DiGraph(data=None, **attr)      # Directed graphs with self loops

MultiGraph(data=None, **attr)   # Undirected graphs with self loops and parallel edges
MultiDiGraph(data=None, **attr) # Directed graphs with self loops and parallel edges


  • data: can be an edge list, any NetworkX graph object; a NumPy matrix, 2d ndarray, a SciPy sparsekey/value attributes matrix, a PyGraphviz graph
  • attr: key/value attribute pairs in an associated attribute dictionary (the keys must be hashable)

2.2 添加边


# 1. Add one edge
Graph.add_edge(u, v, attr_dict=None, **attr)
DiGraph.add_edge(u, v, attr_dict=None, **attr)
MultiGraph.add_edge(u, v, key=None, attr_dict=None, **attr)
MultiDiGraph.add_edge(u, v, key=None, attr_dict=None, **attr)

## 1.1 Examples
G.add_edge(1, 2)G.add_edge(1, 2, key=0, weight=4)   # update data for key=0

# 2. Add a list of edges or a collection of edges
Graph.add_edges_from(ebunch, attr_dict=None, **attr)
DiGraph.add_edges_from(ebunch, attr_dict=None, **attr)
MultiGraph.add_edges_from(ebunch, attr_dict=None, **attr)
MultiDiGraph.add_edges_from(ebunch, attr_dict=None, **attr)

Graph.add_weighted_edges_from(ebunch, weight='weight', **attr)  # convenient way to add weighted edges
DiGraph.add_weighted_edges_from(ebunch, weight='weight', **attr)
MultiGraph.add_weighted_edges_from(ebunch, weight='weight', **attr)
MultiDiGraph.add_weighted_edges_from(ebunch, weight='weight', **attr)

## 2.1 Examples
G.add_edges_from([(1,2),(2,3)], weight=3)

e = (1,2)

G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)]) # 3-tuples (u,v,w) where w is a number


  • ebunch (container of edges): The edges must be given as as 2-tuples (u,v) or 3-tuples (u,v,d) where d is a dictionary containing edge data, such as (u,v,w) where w is a number
  • attr_dict (dictionary): Dictionary of edge attributes. Key/value pairs will update existing data associated with the edge.
  • attr (keyword arguments): Edge data (or labels or objects) can be assigned using keyword arguments, such as `weight=3`


  • key (hashable identifier, optional (default=lowest unused integer)): Used to distinguish multiedges between a pair of nodes.


Adding the same edge twice for Graph/DiGraph simply updates the edge data. For MultiGraph/MultiDiGraph, duplicate edges are stored.

2.3 添加节点


# 1. Add a single node n and update node attributes
Graph.add_node(n, attr_dict=None, **attr)
DiGraph.add_node(n, attr_dict=None, **attr)
MultiGraph.add_node(n, attr_dict=None, **attr)
MultiDiGraph.add_node(n, attr_dict=None, **attr)

## 1.1 Examples

# 2. Add multiple nodes and update node attributes
Graph.add_nodes_from(nodes, **attr)
DiGraph.add_nodes_from(nodes, **attr)
MultiGraph.add_nodes_from(nodes, **attr)
MultiDiGraph.add_nodes_from(nodes, **attr)

## 2.2 Examples
G.add_nodes_from([1,2], size=10) #Use keywords to update specific node attributes for every node.
G.add_nodes_from([(1,dict(size=11)), (2,{'color':'blue'})]) # Use (node, attrdict) tuples to update attributes for specific nodes


  • n (node): A node can be any hashable Python object except None.
  • attr_dict (dictionary): Dictionary of node attributes. Key/value pairs will update existing data associated with the node.
  • nodes (iterable container): A container of nodes (list, dict, set, etc.). OR A container of (node, attribute dict) tuples. Node attributes are updated using the attribute dict.
  • attr (keyword arguments): Update attributes for all nodes in nodes. Node attributes specified in nodes as a tuple take precedence over attributes specified generally.

2.4 内建属性


2.5 读入外部文件创建图


3. 方法

3.1 基本操作


# 0. Python
node in G     # check if node in graph, return True or False
len(G)  # number of nodes in graph

# 1. Graph
degree(G[, nbunch, weight])    # Return degree of single node or of nbunch of nodes.
degree_histogram(G)    # Return a list of the frequency of each degree value.
density(G)    # Return the density of a graph.
info(G[, n])    # Print short summary of information for the graph G or the node n.
create_empty_copy(G[, with_nodes])    # Return a copy of the graph G with all of the edges removed.
is_directed(G)    # Return True if graph is directed.

# 2. Nodes
nodes(G)    # Return a copy of the graph nodes in a list.
number_of_nodes(G)    # Return the number of nodes in the graph.
nodes_iter(G)    # Return an iterator over the graph nodes.
all_neighbors(graph, node)    # Returns all of the neighbors of a node in the graph.
non_neighbors(graph, node)    # Returns the non-neighbors of the node in the graph.
common_neighbors(G, u, v)    # Return the common neighbors of two nodes in a graph.

# 4. Edges
edges(G[, nbunch])    # Return list of edges incident to nodes in nbunch.
number_of_edges(G)    # Return the number of edges in the graph.
edges_iter(G[, nbunch])    # Return iterator over edges incident to nodes in nbunch.
non_edges(graph)    # Returns the non-existent edges in the graph.

# 5. Attributes
set_node_attributes(G, name, values)    # Set node attributes from dictionary of nodes and values
get_node_attributes(G, name)    # Get node attributes from graph
set_edge_attributes(G, name, values)    # Set edge attributes from dictionary of edge tuples and values.
get_edge_attributes(G, name)    # Get edge attributes from graph

# 6. Freezing graph structure
freeze(G)    # Modify graph to prevent further change by adding or removing nodes or edges.
is_frozen(G)    # Return True if graph is frozen.

3.2 一些算法


Leave a Reply

Your email address will not be published. Required fields are marked *