1.networkx的作用
The structure of NetworkX can be seen by the organization of its source code. The package provides classes for graphobjects, generators to create standard graphs, IO routines for reading in existing datasets, algorithms to analyze theresulting networks and some basic drawing tools.
创建图,分析数据,且自带一些已经实现的算法,用于计算节点的一些性质,简单的画图工具。
2、networkx的基础操作
2.1 创建图
import networkx as nx
G = nx.Graph()
class Graph(incoming_graph_data=None,**attr)Base class for undirected graphs.
A Graph stores nodes and edges with optional data, or attributes.
Graphs hold undirected edges. Self loops are allowed but multiple (parallel) edges are not.
Nodes can be arbitrary (hashable) Python objects with optional key/value attributes. By conventionNoneis notused as a node.
Edges are represented as links between nodes with optional key/value attributes.
Parameters
•incoming_graph_data(input graph (optional, default: None)) – Data to initialize graph. IfNone (default) an empty graph is created. The data can be any format that is supported bythe to_networkx_graph() function, currently including edge list, dict of dicts, dict of lists,NetworkX graph, NumPy matrix or 2d ndarray, SciPy sparse matrix, or PyGraphviz graph.7
•attr(keyword arguments, optional (default= no attributes)) – Attributes to add to graph askey=value pairs.
上述创建最基本的图,也就是无向无平行边图。
下图为nx支持的基本图的类,分为四种,分别代指 无向图、有向图、无向多边图、有向多边图
2.2 添加节点
2.2.1 添加单个节点
import networkx as nx
G = nx.Graph()
G.add_node(1)
Graph.add_node(node_for_adding,**attr) # 添加单个节点
Add a single nodenode_for_addingand update node attributes.
Parameters
•node_for_adding(node) – A node can be any hashable Python object except None.
•attr(keyword arguments, optional) – Set or change node attributes using key=value.
添加单个节点,可以为添加的节点在后续的参数中赋予属性
例如 G.add_node(1, color = 'red')
2.2.2 添加多个节点
import networkx as nx
G = nx.Graph()
G.add_nodes_from([(1, dict(size=11)), (2, {'color':'blue'})])
G.add_nodes_from([1,2,3], color='red')
Graph.add_nodes_from(nodes_for_adding,**attr) # 批量添加节点
Add multiple nodes.
Parameters
•nodes_for_adding(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, optional (default= no attributes)) – Update attributes for all nodesin nodes. Node attributes specified in nodes as a tuple take precedence over attributes spec-ified via keyword arguments.
可输入列表、字典、集(set)等可迭代容器,或者元祖,附加参数可以定义node的属性,可单独加入每个节点的属性,也可批量为一批节点加入同个属性,如果想要批量为节点加入不同的属性,需要在第一个参数中用字典的形式定义好节点的属性。
.2.2.3 删除节点和删除多个节点
import networkx as nx
G = nx.Graph({0:{1:{'weight':1}}})
G.remove_node(0)
Graph.remove_node(n) # 删除单个节点
Remove node n.Removes the node n and all adjacent edges. Attempting to remove a non-existent node will raise an exception.
Parameters n(node) – A node in the graphRaisesNetworkXError– If n is not in the graph
import networkx as nx
G = nx.Graph({0:{1:{'weight':1}}})
G.remove_nodes_from([0,1])
Graph.remove_nodes_from(nodes)
Remove multiple nodes.
Parameters nodes(iterable container) – A container of nodes (list, dict, set, etc.). If a node in thecontainer is not in the graph it is silently ignored.
2.3 添加边
2.4.1 添加单个边和多个边
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=1)
Graph.add_edge(u_of_edge,v_of_edge,**attr)Add an edge between u and v.
The nodes u and v will be automatically added if they are not already in the graph.
Edge attributes can be specified with keywords or by directly accessing the edge’s attribute dictionary.
Seeexamples below.Parameters
•u, v(nodes) – Nodes can be, for example, strings or numbers. Nodes must be hashable (andnot None) Python objects.
•attr(keyword arguments, optional) – Edge data (or labels or objects) can be assigned usingkeyword arguments.
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,2),[(2,3)], weight=1)
Graph.add_edges_from(ebunch_to_add,**attr)
Add all the edges in ebunch_to_add.Parameters•ebunch_to_add(container of edges) –
Each edge given in the container will be added tothe graph.
The edges must be given as as 2-tuples (u, v) or 3-tuples (u, v, d) where d is adictionary containing edge data.
•attr(keyword arguments, optional) – Edge data (or labels or objects) can be assigned usingkeyword arguments.
2.4.2 删除单个边和多个边
import networkx as nx
G = nx.Graph({0:{1:{'weight':1}}})
G.remove_edge(0, 1)
Graph.remove_edge(u,v) # 删除单个边
Remove the edge between u and v.Parameters u, v(nodes) – Remove the edge between nodes u and v.RaisesNetworkXError– If there is not an edge between u and v.
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,2),(2,3)], weight=1)
G.remove_edges_from([(1,2),(2,3)])
Graph.remove_edges_from(ebunch) # 删除多个边
Remove all edges specified in ebunch
Parameters ebunch(list or container of edge tuples) – Each edge given in the list or container willbe removed from the graph.
The edges can be:• 2-tuples (u, v) edge between u and v.• 3-tuples (u, v, k) where k is ignored.