python模塊networkx整理(1)

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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章