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

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

# 1. Add one edge

## 1.1 Examples

# 2. Add a list of edges or a collection of edges

## 2.1 Examples

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

## 1.1 Examples

# 2. Add multiple nodes and update node attributes

## 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.

# 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，算法列表在这里