NetworkX使用笔记:基本操作

用NetworkX有一段时间了,本文从构建一张图要素出发,整理NetworkX基本操作,包括声明图类型、添加边、添加顶点。

1. 概述

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

NetworkX最吸引我的两个地方是:

  • 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)

再加上Python强大的字典功能,这意味着可以用NetworkX构建一张内容丰富的图。官方网站和使用文档如下:

2. 构建一张图

2.1 定义图的类型

NetworkX按边是否有向是否允许重边将图分成4种:Graph、DiGraph、MultiGraph、MultiDiGraph。这4种图皆请允许自环。根据实际需要,用下面语句之一创建一张图:

# 使用举例
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和属性attr默认都是空,详细描述如下:

  • 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 添加边

在NetworkX添加一条边,如果顶点不存在,会自动添加顶点(而在python-igraph,则先手动添加)。NetworkX归结起来,有两种方式添加边,即添加一条边和添加一批边,相应函数如下:

# 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_edge(*e) 

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

上述函数主要涉及到3个参数,如下:

  • 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`

值得注意的是,MultiGraph/MultiDiGraph的add_edge函数多了一个参数key,函数如下:

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

还有一点需要注意的是,对于MultiGraph/MultiDiGraph,添加一条边两次,相当于是两条边

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

2.3 添加节点

跟添加边一样,NetworkX有两种添加顶点的方法:添加单个顶点和添加批量顶点。相关函数如下:

# 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
G.add_node(3,weight=0.4,UTM=('13S',382871,3972649))

# 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

上述函数主要涉及到3个参数,如下:

  • 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 内建属性

比如像权重weight。

2.5 读入外部文件创建图

有了上述基础,就很容易将一个外部文件读入构建一个图,详情见之前博文《NetworkX使用笔记:读入外部文件并转换成各种格式》。

3. 方法

3.1 基本操作

NetworkX提供了对图的一些基本操作,方法列表在这里或者如下:

# 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 一些算法

NetworkX实现了一些算法,比如求最短路径,社区划分的K-Clique,算法列表在这里

发表评论

电子邮件地址不会被公开。 必填项已用*标注