networkx 工具包
时间: 2023-11-20 14:07:57 浏览: 111
networkx是一个用于创建、操作和研究复杂网络的Python库。它提供了许多用于创建、操作和分析图形的函数和类。以下是一些networkx的基本用法:
1.创建一个没有节点和边的空图形。
```python
import networkx as nx
G = nx.Graph()
```
2.添加节点和边。
```python
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3])
G.add_edge(1, 2)
G.add_edges_from([(1, 2), (1, 3)])
```
3.删除节点和边。
```python
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from([(1, 2), (1, 3)])
G.remove_node(1)
G.remove_edge(1, 2)
```
4.遍历图形。
```python
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from([(1, 2), (1, 3)])
for node in G.nodes():
print(node)
for edge in G.edges():
print(edge)
```
5.计算图形的度数中心性。
```python
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from([(1, 2), (1, 3)])
degree_centrality = nx.degree_centrality(G)
print(degree_centrality)
```
阅读全文